Files
grafana/public/app/features/alerting/unified/components/rules/RuleState.tsx
Nathan Rodman a0dac9c6d9 Alerting: Add alertmanager notifications tab (#35759)
* Add alertmanager notifications tab

* Link to silences page from am alert

* Include summary for alertmanager group

* Fix colors for am state

* Add horizontal dividing line

* PR feedback

* Add basic unit test for alert notificaitons

* Rename Notificaitons component file

* Polling interval to groups

* Add alertmanager notifications tab

* Link to silences page from am alert

* Include summary for alertmanager group

* PR feedback

* Add basic unit test for alert notificaitons

* Rename Notificaitons component file

* Alerting: make alertmanager notifications view responsive (#36067)

* refac DynamicTableWithGuidelines

* more responsiveness fixes

* Add more to tests

* Add loading and alert state for notifications

Co-authored-by: Domas <domas.lapinskas@grafana.com>
2021-07-07 16:17:26 -07:00

86 lines
2.3 KiB
TypeScript

import { css } from '@emotion/css';
import { GrafanaTheme2, intervalToAbbreviatedDurationString } from '@grafana/data';
import { HorizontalGroup, Spinner, useStyles2 } from '@grafana/ui';
import { CombinedRule } from 'app/types/unified-alerting';
import { PromAlertingRuleState } from 'app/types/unified-alerting-dto';
import React, { FC, useMemo } from 'react';
import { isAlertingRule, isRecordingRule, getFirstActiveAt } from '../../utils/rules';
import { AlertStateTag } from './AlertStateTag';
interface Props {
rule: CombinedRule;
isDeleting: boolean;
isCreating: boolean;
}
export const RuleState: FC<Props> = ({ rule, isDeleting, isCreating }) => {
const style = useStyles2(getStyle);
const { promRule } = rule;
// return how long the rule has been in it's firing state, if any
const forTime = useMemo(() => {
if (
promRule &&
isAlertingRule(promRule) &&
promRule.alerts?.length &&
promRule.state !== PromAlertingRuleState.Inactive
) {
// find earliest alert
const firstActiveAt = getFirstActiveAt(promRule);
// calculate time elapsed from earliest alert
if (firstActiveAt) {
return (
<span title={String(firstActiveAt)} className={style.for}>
for{' '}
{intervalToAbbreviatedDurationString(
{
start: firstActiveAt,
end: new Date(),
},
false
)}
</span>
);
}
}
return null;
}, [promRule, style]);
if (isDeleting) {
return (
<HorizontalGroup align="flex-start">
<Spinner />
deleting
</HorizontalGroup>
);
} else if (isCreating) {
return (
<HorizontalGroup align="flex-start">
{' '}
<Spinner />
creating
</HorizontalGroup>
);
} else if (promRule && isAlertingRule(promRule)) {
return (
<HorizontalGroup align="flex-start">
<AlertStateTag state={promRule.state} />
{forTime}
</HorizontalGroup>
);
} else if (promRule && isRecordingRule(promRule)) {
return <>Recording rule</>;
}
return <>n/a</>;
};
const getStyle = (theme: GrafanaTheme2) => ({
for: css`
font-size: ${theme.typography.bodySmall.fontSize};
color: ${theme.colors.text.secondary};
white-space: nowrap;
padding-top: 2px;
`,
});