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

112 lines
3.3 KiB
TypeScript
Raw Normal View History

2019-01-02 08:22:22 -06:00
import React, { PureComponent } from 'react';
import { getBackendSrv } from '@grafana/runtime';
import { Icon, ConfirmButton, Button } from '@grafana/ui';
import alertDef from './state/alertDef';
import { DashboardModel } from '../dashboard/state/DashboardModel';
import { css } from '@emotion/css';
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> {
2019-05-13 02:38:19 -05:00
state: State = {
2019-01-02 08:22:22 -06:00
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) => {
2019-01-02 09:07:29 -06:00
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,
});
});
}
clearHistory = async () => {
const { dashboard, panelId, onRefresh } = this.props;
2019-01-02 09:07:29 -06:00
await getBackendSrv().post('/api/annotations/mass-delete', {
dashboardId: dashboard.id,
panelId: panelId,
2019-01-02 09:07:29 -06:00
});
this.setState({ stateHistoryItems: [] });
onRefresh();
2019-01-02 09:07:29 -06:00
};
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>
<ConfirmButton onConfirm={this.clearHistory} confirmVariant="destructive" confirmText="Clear">
<Button
className={css`
direction: ltr;
`}
variant="destructive"
icon="trash-alt"
>
Clear history
</Button>
</ConfirmButton>
2019-01-04 05:18:49 -06:00
</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}`}>
<Icon name={item.stateModel.iconClass} size="xl" />
2019-01-04 05:18:49 -06:00
</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;