import { css } from '@emotion/css'; import React, { PureComponent } from 'react'; import { getBackendSrv } from '@grafana/runtime'; import { Icon, ConfirmButton, Button } from '@grafana/ui'; import { DashboardModel } from '../dashboard/state/DashboardModel'; import alertDef from './state/alertDef'; interface Props { dashboard: DashboardModel; panelId: number; onRefresh: () => void; } interface State { stateHistoryItems: any[]; } class StateHistory extends PureComponent { state: State = { stateHistoryItems: [], }; componentDidMount(): void { const { dashboard, panelId } = this.props; getBackendSrv() .get( `/api/annotations?dashboardId=${dashboard.id}&panelId=${panelId}&limit=50&type=alert`, {}, `state-history-${dashboard.id}-${panelId}` ) .then((data) => { const items = data.map((item: any) => { return { stateModel: alertDef.getStateDisplayModel(item.newState), time: dashboard.formatDate(item.time, 'MMM D, YYYY HH:mm:ss'), info: alertDef.getAlertAnnotationInfo(item), }; }); this.setState({ stateHistoryItems: items, }); }); } clearHistory = async () => { const { dashboard, panelId, onRefresh } = this.props; await getBackendSrv().post('/api/annotations/mass-delete', { dashboardId: dashboard.id, panelId: panelId, }); this.setState({ stateHistoryItems: [] }); onRefresh(); }; render() { const { stateHistoryItems } = this.state; return (
{stateHistoryItems.length > 0 && (
Last 50 state changes
)}
    {stateHistoryItems.length > 0 ? ( stateHistoryItems.map((item, index) => { return (
  1. {item.alertName}

    {item.stateModel.text}
    {item.info}
    {item.time}
  2. ); }) ) : ( No state changes recorded )}
); } } export default StateHistory;