kibana/x-pack/examples/alerting_example/public/components/view_alert.tsx
ymao1 a7c9d3f1e0
[Alerting] Update UI to reflect new terminology (#93597)
* Renaming alerts to rules

* Updating formatted messages

* Updating i18n labels

* Completed renaming in UI

* Updating client routes including redirect

* wip docs update

* Reverting title changes for now

* Fixing types check

* Fixing unit tests

* Fixing functional test

* Fixing functional test

* docs wip

* wip docs update

* Finished first run through docs

* docs docs docs

* Fixing bad merge

* Fixing functional test

* Docs cleanup

* Cleaning up i18n labels

* Fixing functional test

* Updating screenshots

* Updating screenshots

* Updating screenshots

* Updating terminology in alerting examples

* Updating terminology in alerting examples

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
2021-03-15 10:03:39 -04:00

105 lines
3.6 KiB
TypeScript

/*
* 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, { useState, useEffect, Fragment } from 'react';
import {
EuiText,
EuiLoadingKibana,
EuiCallOut,
EuiTextColor,
EuiDescriptionList,
EuiDescriptionListTitle,
EuiDescriptionListDescription,
EuiCodeBlock,
EuiSpacer,
} from '@elastic/eui';
import { withRouter, RouteComponentProps } from 'react-router-dom';
import { CoreStart } from 'kibana/public';
import { isEmpty } from 'lodash';
import { Alert, AlertTaskState, BASE_ALERT_API_PATH } from '../../../../plugins/alerting/common';
import { ALERTING_EXAMPLE_APP_ID } from '../../common/constants';
type Props = RouteComponentProps & {
http: CoreStart['http'];
id: string;
};
export const ViewAlertPage = withRouter(({ http, id }: Props) => {
const [alert, setAlert] = useState<Alert | null>(null);
const [alertState, setAlertState] = useState<AlertTaskState | null>(null);
useEffect(() => {
if (!alert) {
http.get(`${BASE_ALERT_API_PATH}/alert/${id}`).then(setAlert);
}
if (!alertState) {
http.get(`${BASE_ALERT_API_PATH}/alert/${id}/state`).then(setAlertState);
}
}, [alert, alertState, http, id]);
return alert && alertState ? (
<Fragment>
<EuiCallOut title={`Rule "${alert.name}"`} iconType="search">
<p>
This is a generic view for all Rules created by the
<EuiTextColor color="accent"> {ALERTING_EXAMPLE_APP_ID} </EuiTextColor>
plugin.
</p>
<p>
You are now viewing the <EuiTextColor color="accent">{`${alert.name}`} </EuiTextColor>
Rule, whose ID is <EuiTextColor color="accent">{`${alert.id}`}</EuiTextColor>.
</p>
<p>
Its RuleType is <EuiTextColor color="accent">{`${alert.alertTypeId}`}</EuiTextColor> and
its scheduled to run at an interval of
<EuiTextColor color="accent"> {`${alert.schedule.interval}`}</EuiTextColor>.
</p>
</EuiCallOut>
<EuiSpacer size="l" />
<EuiText>
<h2>Alerts</h2>
</EuiText>
{isEmpty(alertState.alertInstances) ? (
<EuiCallOut title="No Alerts!" color="warning" iconType="help">
<p>This Rule doesn&apos;t have any active alerts at the moment.</p>
</EuiCallOut>
) : (
<Fragment>
<EuiCallOut title="Active State" color="success" iconType="user">
<p>
Below are the active Alerts which were activated on the rules last run.
<br />
For each alert id you can see its current state in JSON format.
</p>
</EuiCallOut>
<EuiSpacer size="l" />
<EuiDescriptionList compressed>
{Object.entries(alertState.alertInstances ?? {}).map(([instance, { state }]) => (
<Fragment>
<EuiDescriptionListTitle>{instance}</EuiDescriptionListTitle>
<EuiDescriptionListDescription>
<EuiCodeBlock
language="json"
fontSize="m"
paddingSize="m"
color="dark"
overflowHeight={300}
isCopyable
>
{`${JSON.stringify(state)}`}
</EuiCodeBlock>
</EuiDescriptionListDescription>
</Fragment>
))}
</EuiDescriptionList>
</Fragment>
)}
</Fragment>
) : (
<EuiLoadingKibana size="xl" />
);
});