-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Displaying alert data in alert summary in alert details page (#140339)
* displaying data in alert summary in alert details page * refactoring index to include only exports * fixing path error * removing tooltip as not required * [CI] Auto-commit changed files from 'node scripts/precommit_hook.js --ref HEAD~1..HEAD --fix' * [CI] Auto-commit changed files from 'node scripts/eslint --no-cache --fix' * fixing failing tests * Added unit tests for alert summary, refactoring * fix: hook was called conditionally * fixed path errors * removing dependency on ruleId as not required * removing dependency on ruleId as not required * [CI] Auto-commit changed files from 'node scripts/eslint --no-cache --fix' * fixing route * removing ruleid dependency * fixing error * minor changes * removing ruleId dependency as not required * [CI] Auto-commit changed files from 'node scripts/eslint --no-cache --fix' * changes as per design, bug fix * [CI] Auto-commit changed files from 'node scripts/eslint --no-cache --fix' * minor changes * [CI] Auto-commit changed files from 'node scripts/eslint --no-cache --fix' * changes as per feedback * [CI] Auto-commit changed files from 'node scripts/eslint --no-cache --fix' * making alertId not optional * [CI] Auto-commit changed files from 'node scripts/eslint --no-cache --fix' * removing hardcoded field names * [CI] Auto-commit changed files from 'node scripts/eslint --no-cache --fix' * fixing type error * fixing CI errors * fixing tests * [CI] Auto-commit changed files from 'node scripts/precommit_hook.js --ref HEAD~1..HEAD --fix' Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
- Loading branch information
1 parent
91edd77
commit 1b496c5
Showing
23 changed files
with
495 additions
and
247 deletions.
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
File renamed without changes.
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
83 changes: 83 additions & 0 deletions
83
x-pack/plugins/observability/public/pages/alert_details/components/alert_details.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,83 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import React from 'react'; | ||
import * as useUiSettingHook from '@kbn/kibana-react-plugin/public/ui_settings/use_ui_setting'; | ||
import { render } from '../../../utils/test_helper'; | ||
import { useFetchAlertDetail } from '../../../hooks/use_fetch_alert_detail'; | ||
import { AlertDetails } from './alert_details'; | ||
import { Chance } from 'chance'; | ||
import { useParams } from 'react-router-dom'; | ||
import { useBreadcrumbs } from '../../../hooks/use_breadcrumbs'; | ||
import { ConfigSchema } from '../../../plugin'; | ||
import { alert, alertWithNoData } from '../mock/alert'; | ||
import { waitFor } from '@testing-library/react'; | ||
|
||
jest.mock('../../../hooks/use_fetch_alert_detail'); | ||
jest.mock('../../../hooks/use_breadcrumbs'); | ||
jest.mock('react-router-dom', () => ({ | ||
...jest.requireActual('react-router-dom'), | ||
useParams: jest.fn(), | ||
})); | ||
|
||
const useFetchAlertDetailMock = useFetchAlertDetail as jest.Mock; | ||
const useParamsMock = useParams as jest.Mock; | ||
const useBreadcrumbsMock = useBreadcrumbs as jest.Mock; | ||
|
||
const chance = new Chance(); | ||
|
||
const params = { | ||
alertId: chance.guid(), | ||
}; | ||
|
||
const config = { | ||
unsafe: { | ||
alertDetails: { enabled: true }, | ||
}, | ||
} as ConfigSchema; | ||
|
||
describe('Alert details', () => { | ||
jest | ||
.spyOn(useUiSettingHook, 'useUiSetting') | ||
.mockImplementation(() => 'MMM D, YYYY @ HH:mm:ss.SSS'); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
useParamsMock.mockReturnValue(params); | ||
useBreadcrumbsMock.mockReturnValue([]); | ||
}); | ||
|
||
it('should show alert summary', async () => { | ||
useFetchAlertDetailMock.mockReturnValue([false, alert]); | ||
|
||
const alertDetails = render(<AlertDetails />, config); | ||
|
||
expect(alertDetails.queryByTestId('alertDetails')).toBeTruthy(); | ||
await waitFor(() => expect(alertDetails.queryByTestId('centerJustifiedSpinner')).toBeFalsy()); | ||
expect(alertDetails.queryByTestId('alertDetailsError')).toBeFalsy(); | ||
}); | ||
|
||
it('should show error loading the alert details', async () => { | ||
useFetchAlertDetailMock.mockReturnValue([false, alertWithNoData]); | ||
|
||
const alertDetails = render(<AlertDetails />, config); | ||
|
||
expect(alertDetails.queryByTestId('alertDetailsError')).toBeTruthy(); | ||
expect(alertDetails.queryByTestId('centerJustifiedSpinner')).toBeFalsy(); | ||
expect(alertDetails.queryByTestId('alertDetails')).toBeFalsy(); | ||
}); | ||
|
||
it('should show loading spinner', async () => { | ||
useFetchAlertDetailMock.mockReturnValue([true, alertWithNoData]); | ||
|
||
const alertDetails = render(<AlertDetails />, config); | ||
|
||
expect(alertDetails.queryByTestId('centerJustifiedSpinner')).toBeTruthy(); | ||
expect(alertDetails.queryByTestId('alertDetailsError')).toBeFalsy(); | ||
expect(alertDetails.queryByTestId('alertDetails')).toBeFalsy(); | ||
}); | ||
}); |
76 changes: 76 additions & 0 deletions
76
x-pack/plugins/observability/public/pages/alert_details/components/alert_details.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,76 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { i18n } from '@kbn/i18n'; | ||
import { useParams } from 'react-router-dom'; | ||
import { EuiEmptyPrompt, EuiPanel } from '@elastic/eui'; | ||
import { useKibana } from '../../../utils/kibana_react'; | ||
import { ObservabilityAppServices } from '../../../application/types'; | ||
import { usePluginContext } from '../../../hooks/use_plugin_context'; | ||
import { useBreadcrumbs } from '../../../hooks/use_breadcrumbs'; | ||
import { paths } from '../../../config/paths'; | ||
import { AlertDetailsPathParams } from '../types'; | ||
import { CenterJustifiedSpinner } from '../../rule_details/components/center_justified_spinner'; | ||
import { AlertSummary } from '.'; | ||
import PageNotFound from '../../404'; | ||
import { useFetchAlertDetail } from '../../../hooks/use_fetch_alert_detail'; | ||
|
||
export function AlertDetails() { | ||
const { http } = useKibana<ObservabilityAppServices>().services; | ||
const { ObservabilityPageTemplate, config } = usePluginContext(); | ||
const { alertId } = useParams<AlertDetailsPathParams>(); | ||
const [isLoading, alert] = useFetchAlertDetail(alertId); | ||
|
||
useBreadcrumbs([ | ||
{ | ||
href: http.basePath.prepend(paths.observability.alerts), | ||
text: i18n.translate('xpack.observability.breadcrumbs.alertsLinkText', { | ||
defaultMessage: 'Alerts', | ||
}), | ||
}, | ||
]); | ||
|
||
// Redirect to the the 404 page when the user hit the page url directly in the browser while the feature flag is off. | ||
if (!config.unsafe.alertDetails.enabled) { | ||
return <PageNotFound />; | ||
} | ||
|
||
if (isLoading) { | ||
return <CenterJustifiedSpinner />; | ||
} | ||
|
||
if (!isLoading && !alert) | ||
return ( | ||
<EuiPanel data-test-subj="alertDetailsError"> | ||
<EuiEmptyPrompt | ||
iconType="alert" | ||
color="danger" | ||
title={ | ||
<h2> | ||
{i18n.translate('xpack.observability.alertDetails.errorPromptTitle', { | ||
defaultMessage: 'Unable to load alert details', | ||
})} | ||
</h2> | ||
} | ||
body={ | ||
<p> | ||
{i18n.translate('xpack.observability.alertDetails.errorPromptBody', { | ||
defaultMessage: 'There was an error loading the alert details.', | ||
})} | ||
</p> | ||
} | ||
/> | ||
</EuiPanel> | ||
); | ||
|
||
return ( | ||
<ObservabilityPageTemplate data-test-subj="alertDetails"> | ||
<AlertSummary alert={alert} /> | ||
</ObservabilityPageTemplate> | ||
); | ||
} |
70 changes: 70 additions & 0 deletions
70
x-pack/plugins/observability/public/pages/alert_details/components/alert_summary.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,70 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import React from 'react'; | ||
import * as useUiSettingHook from '@kbn/kibana-react-plugin/public/ui_settings/use_ui_setting'; | ||
import { render } from '../../../utils/test_helper'; | ||
import { AlertSummary } from './alert_summary'; | ||
import { asDuration } from '../../../../common/utils/formatters'; | ||
import { kibanaStartMock } from '../../../utils/kibana_react.mock'; | ||
import { useKibana } from '../../../utils/kibana_react'; | ||
import { triggersActionsUiMock } from '@kbn/triggers-actions-ui-plugin/public/mocks'; | ||
import { waitFor } from '@testing-library/react'; | ||
import { alertWithTags, alertWithNoData, tags } from '../mock/alert'; | ||
|
||
jest.mock('react-router-dom', () => ({ | ||
...jest.requireActual('react-router-dom'), | ||
useParams: jest.fn(), | ||
})); | ||
|
||
jest.mock('../../../utils/kibana_react'); | ||
|
||
const useKibanaMock = useKibana as jest.Mock; | ||
|
||
const mockKibana = () => { | ||
useKibanaMock.mockReturnValue({ | ||
services: { | ||
...kibanaStartMock.startContract(), | ||
triggersActionsUi: triggersActionsUiMock.createStart(), | ||
}, | ||
}); | ||
}; | ||
|
||
describe('Alert summary', () => { | ||
jest | ||
.spyOn(useUiSettingHook, 'useUiSetting') | ||
.mockImplementation(() => 'MMM D, YYYY @ HH:mm:ss.SSS'); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
mockKibana(); | ||
}); | ||
|
||
it('should show alert data', async () => { | ||
const alertSummary = render(<AlertSummary alert={alertWithTags} />); | ||
|
||
expect(alertSummary.queryByText('1957')).toBeInTheDocument(); | ||
expect(alertSummary.queryByText(asDuration(882076000))).toBeInTheDocument(); | ||
expect(alertSummary.queryByText('Active')).toBeInTheDocument(); | ||
expect(alertSummary.queryByText('Sep 2, 2021 @ 08:54:09.674')).toBeInTheDocument(); | ||
expect( | ||
alertSummary.getByText('Sep 2, 2021 @ 09:08:51.750', { exact: false }) | ||
).toBeInTheDocument(); | ||
await waitFor(() => expect(alertSummary.queryByTestId('tagsOutPopover')).toBeInTheDocument()); | ||
expect(alertSummary.queryByText(tags[0])).toBeInTheDocument(); | ||
}); | ||
|
||
it('should show empty "-" for fields when no data available', async () => { | ||
const alertSummary = render(<AlertSummary alert={alertWithNoData} />); | ||
|
||
expect(alertSummary.queryByTestId('noAlertStatus')).toBeInTheDocument(); | ||
expect(alertSummary.queryByTestId('noAlertStatus')).toHaveTextContent('-'); | ||
await waitFor(() => | ||
expect(alertSummary.queryByTestId('tagsOutPopover')).not.toBeInTheDocument() | ||
); | ||
}); | ||
}); |
Oops, something went wrong.