import React, { PureComponent } from 'react'; import alertDef from './state/alertDef'; import { getBackendSrv } from '@grafana/runtime'; import { DashboardModel } from '../dashboard/state/DashboardModel'; import appEvents from '../../core/app_events'; import { CoreEvents } from 'app/types'; 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`) .then((res: any[]) => { const items = res.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 = () => { const { dashboard, onRefresh, panelId } = this.props; appEvents.emit(CoreEvents.showConfirmModal, { title: 'Delete Alert History', text: 'Are you sure you want to remove all history & annotations for this alert?', icon: 'fa-trash', yesText: 'Yes', onConfirm: () => { getBackendSrv() .post('/api/annotations/mass-delete', { dashboardId: dashboard.id, panelId: panelId, }) .then(() => { 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;