StateHistory.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { css } from '@emotion/css';
  2. import React, { PureComponent } from 'react';
  3. import { getBackendSrv } from '@grafana/runtime';
  4. import { Icon, ConfirmButton, Button } from '@grafana/ui';
  5. import { DashboardModel } from '../dashboard/state/DashboardModel';
  6. import alertDef from './state/alertDef';
  7. interface Props {
  8. dashboard: DashboardModel;
  9. panelId: number;
  10. onRefresh: () => void;
  11. }
  12. interface State {
  13. stateHistoryItems: any[];
  14. }
  15. class StateHistory extends PureComponent<Props, State> {
  16. state: State = {
  17. stateHistoryItems: [],
  18. };
  19. componentDidMount(): void {
  20. const { dashboard, panelId } = this.props;
  21. getBackendSrv()
  22. .get(
  23. `/api/annotations?dashboardId=${dashboard.id}&panelId=${panelId}&limit=50&type=alert`,
  24. {},
  25. `state-history-${dashboard.id}-${panelId}`
  26. )
  27. .then((data) => {
  28. const items = data.map((item: any) => {
  29. return {
  30. stateModel: alertDef.getStateDisplayModel(item.newState),
  31. time: dashboard.formatDate(item.time, 'MMM D, YYYY HH:mm:ss'),
  32. info: alertDef.getAlertAnnotationInfo(item),
  33. };
  34. });
  35. this.setState({
  36. stateHistoryItems: items,
  37. });
  38. });
  39. }
  40. clearHistory = async () => {
  41. const { dashboard, panelId, onRefresh } = this.props;
  42. await getBackendSrv().post('/api/annotations/mass-delete', {
  43. dashboardId: dashboard.id,
  44. panelId: panelId,
  45. });
  46. this.setState({ stateHistoryItems: [] });
  47. onRefresh();
  48. };
  49. render() {
  50. const { stateHistoryItems } = this.state;
  51. return (
  52. <div>
  53. {stateHistoryItems.length > 0 && (
  54. <div className="p-b-1">
  55. <span className="muted">Last 50 state changes</span>
  56. <ConfirmButton onConfirm={this.clearHistory} confirmVariant="destructive" confirmText="Clear">
  57. <Button
  58. className={css`
  59. direction: ltr;
  60. `}
  61. variant="destructive"
  62. icon="trash-alt"
  63. >
  64. Clear history
  65. </Button>
  66. </ConfirmButton>
  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. <Icon name={item.stateModel.iconClass} size="xl" />
  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;