AlertTab.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // Libraries
  2. import React, { PureComponent } from 'react';
  3. // Services & Utils
  4. import { AngularComponent, getAngularLoader } from '@grafana/runtime';
  5. import appEvents from 'app/core/app_events';
  6. // Components
  7. import { EditorTabBody, EditorToolbarView } from '../dashboard/panel_editor/EditorTabBody';
  8. import EmptyListCTA from 'app/core/components/EmptyListCTA/EmptyListCTA';
  9. import StateHistory from './StateHistory';
  10. import 'app/features/alerting/AlertTabCtrl';
  11. import { Alert } from '@grafana/ui';
  12. // Types
  13. import { DashboardModel } from '../dashboard/state/DashboardModel';
  14. import { PanelModel } from '../dashboard/state/PanelModel';
  15. import { TestRuleResult } from './TestRuleResult';
  16. import { AppNotificationSeverity } from 'app/types';
  17. interface Props {
  18. angularPanel?: AngularComponent;
  19. dashboard: DashboardModel;
  20. panel: PanelModel;
  21. }
  22. export class AlertTab extends PureComponent<Props> {
  23. element: any;
  24. component: AngularComponent;
  25. panelCtrl: any;
  26. componentDidMount() {
  27. if (this.shouldLoadAlertTab()) {
  28. this.loadAlertTab();
  29. }
  30. }
  31. componentDidUpdate(prevProps: Props) {
  32. if (this.shouldLoadAlertTab()) {
  33. this.loadAlertTab();
  34. }
  35. }
  36. shouldLoadAlertTab() {
  37. return this.props.angularPanel && this.element && !this.component;
  38. }
  39. componentWillUnmount() {
  40. if (this.component) {
  41. this.component.destroy();
  42. }
  43. }
  44. loadAlertTab() {
  45. const { angularPanel } = this.props;
  46. const scope = angularPanel.getScope();
  47. // When full page reloading in edit mode the angular panel has on fully compiled & instantiated yet
  48. if (!scope.$$childHead) {
  49. setTimeout(() => {
  50. this.forceUpdate();
  51. });
  52. return;
  53. }
  54. this.panelCtrl = scope.$$childHead.ctrl;
  55. const loader = getAngularLoader();
  56. const template = '<alert-tab />';
  57. const scopeProps = { ctrl: this.panelCtrl };
  58. this.component = loader.load(this.element, scopeProps, template);
  59. }
  60. stateHistory = (): EditorToolbarView => {
  61. return {
  62. title: 'State history',
  63. render: () => {
  64. return (
  65. <StateHistory
  66. dashboard={this.props.dashboard}
  67. panelId={this.props.panel.id}
  68. onRefresh={this.panelCtrl.refresh}
  69. />
  70. );
  71. },
  72. };
  73. };
  74. deleteAlert = (): EditorToolbarView => {
  75. const { panel } = this.props;
  76. return {
  77. title: 'Delete',
  78. btnType: 'danger',
  79. onClick: () => {
  80. appEvents.emit('confirm-modal', {
  81. title: 'Delete Alert',
  82. text: 'Are you sure you want to delete this alert rule?',
  83. text2: 'You need to save dashboard for the delete to take effect',
  84. icon: 'fa-trash',
  85. yesText: 'Delete',
  86. onConfirm: () => {
  87. delete panel.alert;
  88. panel.thresholds = [];
  89. this.panelCtrl.alertState = null;
  90. this.panelCtrl.render();
  91. this.forceUpdate();
  92. },
  93. });
  94. },
  95. };
  96. };
  97. renderTestRuleResult = () => {
  98. const { panel, dashboard } = this.props;
  99. return <TestRuleResult panelId={panel.id} dashboard={dashboard} />;
  100. };
  101. testRule = (): EditorToolbarView => ({
  102. title: 'Test Rule',
  103. render: () => this.renderTestRuleResult(),
  104. });
  105. onAddAlert = () => {
  106. this.panelCtrl._enableAlert();
  107. this.component.digest();
  108. this.forceUpdate();
  109. };
  110. render() {
  111. const { alert, transformations } = this.props.panel;
  112. const hasTransformations = transformations && transformations.length;
  113. if (!alert && hasTransformations) {
  114. return (
  115. <EditorTabBody heading="Alert">
  116. <Alert
  117. severity={AppNotificationSeverity.Warning}
  118. title="Transformations are not supported in alert queries"
  119. />
  120. </EditorTabBody>
  121. );
  122. }
  123. const toolbarItems = alert ? [this.stateHistory(), this.testRule(), this.deleteAlert()] : [];
  124. const model = {
  125. title: 'Panel has no alert rule defined',
  126. buttonIcon: 'gicon gicon-alert',
  127. onClick: this.onAddAlert,
  128. buttonTitle: 'Create Alert',
  129. };
  130. return (
  131. <EditorTabBody heading="Alert" toolbarItems={toolbarItems}>
  132. <>
  133. {alert && hasTransformations && (
  134. <Alert
  135. severity={AppNotificationSeverity.Error}
  136. title="Transformations are not supported in alert queries"
  137. />
  138. )}
  139. <div ref={element => (this.element = element)} />
  140. {!alert && <EmptyListCTA {...model} />}
  141. </>
  142. </EditorTabBody>
  143. );
  144. }
  145. }