import { css, cx } from '@emotion/css'; import React, { FC, Fragment, useState } from 'react'; import { useDispatch } from 'react-redux'; import { dateMath, GrafanaTheme, intervalToAbbreviatedDurationString } from '@grafana/data'; import { useStyles, Link } from '@grafana/ui'; import { contextSrv } from 'app/core/services/context_srv'; import { Silence, AlertmanagerAlert } from 'app/plugins/datasource/alertmanager/types'; import { expireSilenceAction } from '../../state/actions'; import { makeAMLink } from '../../utils/misc'; import { CollapseToggle } from '../CollapseToggle'; import { ActionButton } from '../rules/ActionButton'; import { ActionIcon } from '../rules/ActionIcon'; import { Matchers } from './Matchers'; import { SilenceStateTag } from './SilenceStateTag'; import SilencedAlertsTable from './SilencedAlertsTable'; interface Props { className?: string; silence: Silence; silencedAlerts: AlertmanagerAlert[]; alertManagerSourceName: string; } const SilenceTableRow: FC = ({ silence, className, silencedAlerts, alertManagerSourceName }) => { const [isCollapsed, setIsCollapsed] = useState(true); const dispatch = useDispatch(); const styles = useStyles(getStyles); const { status, matchers = [], startsAt, endsAt, comment, createdBy } = silence; const dateDisplayFormat = 'YYYY-MM-DD HH:mm'; const startsAtDate = dateMath.parse(startsAt); const endsAtDate = dateMath.parse(endsAt); const duration = intervalToAbbreviatedDurationString({ start: new Date(startsAt), end: new Date(endsAt) }); const handleExpireSilenceClick = () => { dispatch(expireSilenceAction(alertManagerSourceName, silence.id)); }; const detailsColspan = contextSrv.isEditor ? 4 : 3; return ( setIsCollapsed(value)} /> {silencedAlerts.length} {startsAtDate?.format(dateDisplayFormat)} {'-'}
{endsAtDate?.format(dateDisplayFormat)} {contextSrv.isEditor && ( {status.state === 'expired' ? ( Recreate ) : ( Unsilence )} {status.state !== 'expired' && ( )} )} {!isCollapsed && ( <> Comment {comment} Schedule {`${startsAtDate?.format(dateDisplayFormat)} - ${endsAtDate?.format( dateDisplayFormat )}`} Duration {duration} Created by {createdBy} {!!silencedAlerts.length && ( Affected alerts )} )}
); }; const getStyles = (theme: GrafanaTheme) => ({ matchersCell: css` & > * + * { margin-left: ${theme.spacing.xs}; } `, actionsCell: css` text-align: right; width: 1%; white-space: nowrap; & > * + * { margin-left: ${theme.spacing.sm}; } `, alertRulesCell: css` vertical-align: top; `, }); export default SilenceTableRow;