grafana/public/app/features/alerting/StateHistory.tsx

111 lines
3.3 KiB
TypeScript
Raw Normal View History

2019-01-02 08:22:22 -06:00
import React, { PureComponent } from 'react';
import alertDef from './state/alertDef';
2019-01-02 09:07:29 -06:00
import { getBackendSrv } from 'app/core/services/backend_srv';
import { DashboardModel } from '../dashboard/state/DashboardModel';
import appEvents from '../../core/app_events';
2019-01-02 08:22:22 -06:00
interface Props {
dashboard: DashboardModel;
panelId: number;
2019-01-02 09:07:29 -06:00
onRefresh: () => void;
2019-01-02 08:22:22 -06:00
}
interface State {
stateHistoryItems: any[];
}
class StateHistory extends PureComponent<Props, 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 => {
2019-01-02 09:07:29 -06:00
const items = res.map(item => {
return {
2019-01-02 08:22:22 -06:00
stateModel: alertDef.getStateDisplayModel(item.newState),
time: dashboard.formatDate(item.time, 'MMM D, YYYY HH:mm:ss'),
info: alertDef.getAlertAnnotationInfo(item),
2019-01-02 09:07:29 -06:00
};
2019-01-02 08:22:22 -06:00
});
this.setState({
stateHistoryItems: items,
});
});
}
2019-01-02 09:07:29 -06:00
clearHistory = () => {
const { dashboard, onRefresh, panelId } = this.props;
appEvents.emit('confirm-modal', {
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(() => {
onRefresh();
});
this.setState({
stateHistoryItems: [],
});
},
});
};
2019-01-02 08:22:22 -06:00
render() {
const { stateHistoryItems } = this.state;
return (
<div>
2019-01-04 05:18:49 -06:00
{stateHistoryItems.length > 0 && (
<div className="p-b-1">
<span className="muted">Last 50 state changes</span>
<button className="btn btn-mini btn-danger pull-right" onClick={this.clearHistory}>
<i className="fa fa-trash" /> {` Clear history`}
</button>
</div>
)}
<ol className="alert-rule-list">
{stateHistoryItems.length > 0 ? (
stateHistoryItems.map((item, index) => {
return (
<li className="alert-rule-item" key={`${item.time}-${index}`}>
<div className={`alert-rule-item__icon ${item.stateModel.stateClass}`}>
<i className={item.stateModel.iconClass} />
</div>
<div className="alert-rule-item__body">
<div className="alert-rule-item__header">
<p className="alert-rule-item__name">{item.alertName}</p>
<div className="alert-rule-item__text">
<span className={`${item.stateModel.stateClass}`}>{item.stateModel.text}</span>
2019-01-02 08:22:22 -06:00
</div>
</div>
2019-01-04 05:18:49 -06:00
{item.info}
</div>
<div className="alert-rule-item__time">{item.time}</div>
</li>
);
})
) : (
<i>No state changes recorded</i>
)}
</ol>
2019-01-02 08:22:22 -06:00
</div>
);
}
}
export default StateHistory;