StateHistory.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import React, { PureComponent } from 'react';
  2. import alertDef from './state/alertDef';
  3. import { getBackendSrv } from '@grafana/runtime';
  4. import { DashboardModel } from '../dashboard/state/DashboardModel';
  5. import appEvents from '../../core/app_events';
  6. interface Props {
  7. dashboard: DashboardModel;
  8. panelId: number;
  9. onRefresh: () => void;
  10. }
  11. interface State {
  12. stateHistoryItems: any[];
  13. }
  14. class StateHistory extends PureComponent<Props, State> {
  15. state: State = {
  16. stateHistoryItems: [],
  17. };
  18. componentDidMount(): void {
  19. const { dashboard, panelId } = this.props;
  20. getBackendSrv()
  21. .get(`/api/annotations?dashboardId=${dashboard.id}&panelId=${panelId}&limit=50&type=alert`)
  22. .then((res: any[]) => {
  23. const items = res.map((item: any) => {
  24. return {
  25. stateModel: alertDef.getStateDisplayModel(item.newState),
  26. time: dashboard.formatDate(item.time, 'MMM D, YYYY HH:mm:ss'),
  27. info: alertDef.getAlertAnnotationInfo(item),
  28. };
  29. });
  30. this.setState({
  31. stateHistoryItems: items,
  32. });
  33. });
  34. }
  35. clearHistory = () => {
  36. const { dashboard, onRefresh, panelId } = this.props;
  37. appEvents.emit('confirm-modal', {
  38. title: 'Delete Alert History',
  39. text: 'Are you sure you want to remove all history & annotations for this alert?',
  40. icon: 'fa-trash',
  41. yesText: 'Yes',
  42. onConfirm: () => {
  43. getBackendSrv()
  44. .post('/api/annotations/mass-delete', {
  45. dashboardId: dashboard.id,
  46. panelId: panelId,
  47. })
  48. .then(() => {
  49. onRefresh();
  50. });
  51. this.setState({
  52. stateHistoryItems: [],
  53. });
  54. },
  55. });
  56. };
  57. render() {
  58. const { stateHistoryItems } = this.state;
  59. return (
  60. <div>
  61. {stateHistoryItems.length > 0 && (
  62. <div className="p-b-1">
  63. <span className="muted">Last 50 state changes</span>
  64. <button className="btn btn-small btn-danger pull-right" onClick={this.clearHistory}>
  65. <i className="fa fa-trash" /> {` Clear history`}
  66. </button>
  67. </div>
  68. )}
  69. <ol className="alert-rule-list">
  70. {stateHistoryItems.length > 0 ? (
  71. stateHistoryItems.map((item, index) => {
  72. return (
  73. <li className="alert-rule-item" key={`${item.time}-${index}`}>
  74. <div className={`alert-rule-item__icon ${item.stateModel.stateClass}`}>
  75. <i className={item.stateModel.iconClass} />
  76. </div>
  77. <div className="alert-rule-item__body">
  78. <div className="alert-rule-item__header">
  79. <p className="alert-rule-item__name">{item.alertName}</p>
  80. <div className="alert-rule-item__text">
  81. <span className={`${item.stateModel.stateClass}`}>{item.stateModel.text}</span>
  82. </div>
  83. </div>
  84. {item.info}
  85. </div>
  86. <div className="alert-rule-item__time">{item.time}</div>
  87. </li>
  88. );
  89. })
  90. ) : (
  91. <i>No state changes recorded</i>
  92. )}
  93. </ol>
  94. </div>
  95. );
  96. }
  97. }
  98. export default StateHistory;