-
Notifications
You must be signed in to change notification settings - Fork 187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Refactor] Health check #3197
Merged
Merged
[Refactor] Health check #3197
Changes from 11 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
6648078
refactor(healthcheck): Adapt the refactorized health check component …
Desvelao ce831c7
feat(healtcheck): Replace health check initial state to waiting
Desvelao 95c8078
fet(healthcheck): Add can retry to healthcheck checks
Desvelao 31bc700
fix(menu): Fix error in toast from WzMenu and revome unnecessary retu…
Desvelao 3d00e8a
fix(health-check): Fix create index pattern when change the setting in
Desvelao 7d83daa
fix(health-check): renamed files from appConfig Redux actions and red…
Desvelao 86bcaa3
fix(frontend): Replace config singleton saving to Redux
Desvelao 414d0f3
fix(health-check): Fix infinite loop rendering component when a check…
Desvelao 8f2127e
fix(health-check): Rename health checks titles
Desvelao 965771d
fix(health-check): Fix the tests for Health check
Desvelao 6514bcb
changelog: Add PR to changelog
Desvelao b919cd8
refactor(health-check): Request changes, add max buckets check and so…
Desvelao eb4162f
changelog: Fix link of the change
Desvelao f1eabad
Merge branch '4.2-7.10' into refactor/healthcheck-4.2-7.10
frankeros File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* Wazuh app - React hook for app configuration | ||
* Copyright (C) 2015-2021 Wazuh, Inc. | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Find more information about this on the LICENSE file. | ||
*/ | ||
|
||
import { AppRootState } from '../../../redux/types'; | ||
import { useSelector } from 'react-redux'; | ||
|
||
export const useAppConfig = () => { | ||
const appConfig = useSelector((state: AppRootState) => state.appConfig); | ||
return appConfig; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
public/components/health-check/components/__snapshots__/check-result.test.tsx.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Check result component should render a Check result screen 1`] = ` | ||
<CheckResult | ||
awaitFor={Array []} | ||
canRetry={true} | ||
check={true} | ||
checksReady={Object {}} | ||
cleanErrors={ | ||
[MockFunction] { | ||
"calls": Array [ | ||
Array [ | ||
"test", | ||
], | ||
], | ||
"results": Array [ | ||
Object { | ||
"type": "return", | ||
"value": undefined, | ||
}, | ||
], | ||
} | ||
} | ||
handleCheckReady={[MockFunction]} | ||
handleErrors={[MockFunction]} | ||
isLoading={false} | ||
name="test" | ||
title="test chest" | ||
validationService={ | ||
[MockFunction] { | ||
"calls": Array [ | ||
Array [], | ||
], | ||
"results": Array [ | ||
Object { | ||
"type": "return", | ||
"value": Object { | ||
"errors": Array [], | ||
}, | ||
}, | ||
], | ||
} | ||
} | ||
> | ||
<EuiDescriptionListTitle> | ||
<dt | ||
className="euiDescriptionList__title" | ||
> | ||
test chest | ||
</dt> | ||
</EuiDescriptionListTitle> | ||
<EuiDescriptionListDescription> | ||
<dd | ||
className="euiDescriptionList__description" | ||
> | ||
<span> | ||
<EuiLoadingSpinner | ||
size="m" | ||
> | ||
<span | ||
className="euiLoadingSpinner euiLoadingSpinner--medium" | ||
/> | ||
</EuiLoadingSpinner> | ||
Checking... | ||
</span> | ||
</dd> | ||
</EuiDescriptionListDescription> | ||
</CheckResult> | ||
`; |
88 changes: 88 additions & 0 deletions
88
public/components/health-check/components/check-result.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* Wazuh app - Check Result Component - Test | ||
* | ||
* Copyright (C) 2015-2021 Wazuh, Inc. | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Find more information about this on the LICENSE file. | ||
* | ||
*/ | ||
import React from 'react'; | ||
import { mount } from 'enzyme'; | ||
import { CheckResult } from './check-result'; | ||
|
||
describe('Check result component', () => { | ||
const validationService = jest.fn(); | ||
const handleErrors = jest.fn(); | ||
const handleCheckReady = jest.fn(); | ||
const cleanErrors = jest.fn(); | ||
|
||
test('should render a Check result screen', () => { | ||
validationService.mockImplementation(() => ({ errors: [] })); | ||
const component = mount( | ||
<CheckResult | ||
name={'test'} | ||
title={'Check Test'} | ||
awaitFor={[]} | ||
check={true} | ||
validationService={validationService} | ||
handleErrors={handleErrors} | ||
isLoading={false} | ||
handleCheckReady={handleCheckReady} | ||
checksReady={{}} | ||
cleanErrors={cleanErrors} | ||
canRetry={true} | ||
/> | ||
); | ||
|
||
expect(component).toMatchSnapshot(); | ||
}); | ||
|
||
test('should print ready', () => { | ||
validationService.mockImplementation(() => ({ errors: [] })); | ||
const component = mount( | ||
<CheckResult | ||
name={'test'} | ||
title={'Check Test'} | ||
awaitFor={[]} | ||
check={true} | ||
validationService={validationService} | ||
handleErrors={handleErrors} | ||
isLoading={false} | ||
handleCheckReady={handleCheckReady} | ||
checksReady={{}} | ||
cleanErrors={cleanErrors} | ||
canRetry={true} | ||
/> | ||
); | ||
setImmediate(() => { | ||
expect(component.find('EuiDescriptionListDescription').find('span').at(0).text().trim()).toBe('Ready'); | ||
}); | ||
}); | ||
|
||
test('should print error', () => { | ||
validationService.mockImplementation(() => ({ errors: ['error'] })); | ||
const component = mount( | ||
<CheckResult | ||
name={'test'} | ||
title={'Check Test'} | ||
awaitFor={[]} | ||
check={true} | ||
validationService={validationService} | ||
handleErrors={handleErrors} | ||
isLoading={false} | ||
handleCheckReady={handleCheckReady} | ||
checksReady={{}} | ||
cleanErrors={cleanErrors} | ||
canRetry={true} | ||
/> | ||
); | ||
setImmediate(() => { | ||
expect(component.find('EuiDescriptionListDescription').find('span').at(0).text().trim()).toBe('Error'); | ||
}); | ||
}); | ||
}); |
132 changes: 132 additions & 0 deletions
132
public/components/health-check/components/check-result.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/* | ||
* Wazuh app - Check Result Component | ||
* | ||
* Copyright (C) 2015-2021 Wazuh, Inc. | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Find more information about this on the LICENSE file. | ||
* | ||
*/ | ||
import React, { useEffect, useState } from 'react'; | ||
import { | ||
EuiButtonIcon, | ||
EuiDescriptionListDescription, | ||
EuiDescriptionListTitle, | ||
EuiIcon, | ||
EuiLoadingSpinner, | ||
EuiToolTip | ||
} from '@elastic/eui'; | ||
|
||
type Result = 'loading' | 'ready' | 'error' | 'error_retry' | 'disabled' | 'waiting'; | ||
|
||
export function CheckResult(props) { | ||
const [result, setResult] = useState<Result>('waiting'); | ||
const [isCheckStarted, setIsCheckStarted] = useState<boolean>(false); | ||
|
||
useEffect(() => { | ||
if (props.check && !props.isLoading && awaitForIsReady()){ | ||
initCheck(); | ||
} else if (props.check === false && !props.checksReady[props.name]) { | ||
setResult('disabled'); | ||
setAsReady(); | ||
} | ||
}, [props.check, props.isLoading, props.checksReady]); | ||
|
||
const setAsReady = () => { | ||
props.handleCheckReady(props.name, true); | ||
}; | ||
|
||
const handleErrors = (errors, parsed?) => { | ||
props.handleErrors(props.name, errors, parsed); | ||
}; | ||
|
||
/** | ||
* validate if the current check is not started and if the dependentes checks are ready | ||
*/ | ||
const awaitForIsReady = () => { | ||
return !isCheckStarted && (props.awaitFor.length === 0 || props.awaitFor.every((check) => { | ||
return props.checksReady[check]; | ||
})) | ||
} | ||
|
||
const initCheck = async () => { | ||
setIsCheckStarted(true); | ||
setResult('loading'); | ||
props.cleanErrors(props.name); | ||
try { | ||
const { errors } = await props.validationService(); | ||
if (errors.length) { | ||
handleErrors(errors); | ||
setResult('error'); | ||
props.canRetry ? setResult('error_retry') : setResult('error'); | ||
} else { | ||
setResult('ready'); | ||
setAsReady(); | ||
} | ||
} catch (error) { | ||
handleErrors([error], true); | ||
props.canRetry ? setResult('error_retry') : setResult('error'); | ||
} | ||
}; | ||
|
||
const renderResult = () => { | ||
switch (result) { | ||
case 'loading': | ||
return ( | ||
<span> | ||
<EuiLoadingSpinner size="m" /> Checking... | ||
</span> | ||
); | ||
case 'disabled': | ||
return <span>Disabled</span>; | ||
case 'ready': | ||
return ( | ||
<span> | ||
<EuiIcon type="check" color="secondary"></EuiIcon> Ready | ||
</span> | ||
); | ||
case 'error': | ||
return ( | ||
<span> | ||
<EuiIcon type="alert" color="danger"></EuiIcon> Error | ||
</span> | ||
); | ||
case 'error_retry': | ||
return ( | ||
<span> | ||
<EuiIcon type="alert" color="danger"></EuiIcon> Error | ||
<EuiToolTip | ||
position='top' | ||
content='Retry' | ||
> | ||
<EuiButtonIcon | ||
display="base" | ||
iconType="refresh" | ||
iconSize="m" | ||
onClick={initCheck} | ||
size="m" | ||
aria-label="Next" | ||
/> | ||
</EuiToolTip> | ||
</span> | ||
); | ||
case 'waiting': | ||
return ( | ||
<span> | ||
Waiting... | ||
</span> | ||
); | ||
} | ||
}; | ||
|
||
return ( | ||
<> | ||
<EuiDescriptionListTitle>{props.title}</EuiDescriptionListTitle> | ||
<EuiDescriptionListDescription>{renderResult()}</EuiDescriptionListDescription> | ||
</> | ||
); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👏 👏