TestRuleResult.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import React, { PureComponent } from 'react';
  2. import { AppEvents } from '@grafana/data';
  3. import { getBackendSrv } from '@grafana/runtime';
  4. import { LoadingPlaceholder, JSONFormatter, Icon, HorizontalGroup, ClipboardButton } from '@grafana/ui';
  5. import appEvents from 'app/core/app_events';
  6. import { DashboardModel, PanelModel } from '../dashboard/state';
  7. export interface Props {
  8. dashboard: DashboardModel;
  9. panel: PanelModel;
  10. }
  11. interface State {
  12. isLoading: boolean;
  13. allNodesExpanded: boolean | null;
  14. testRuleResponse: {};
  15. }
  16. export class TestRuleResult extends PureComponent<Props, State> {
  17. readonly state: State = {
  18. isLoading: false,
  19. allNodesExpanded: null,
  20. testRuleResponse: {},
  21. };
  22. formattedJson: any;
  23. clipboard: any;
  24. componentDidMount() {
  25. this.testRule();
  26. }
  27. async testRule() {
  28. const { dashboard, panel } = this.props;
  29. // dashboard save model
  30. const model = dashboard.getSaveModelClone();
  31. // now replace panel to get current edits
  32. model.panels = model.panels.map((dashPanel) => {
  33. return dashPanel.id === panel.id ? panel.getSaveModel() : dashPanel;
  34. });
  35. const payload = { dashboard: model, panelId: panel.id };
  36. this.setState({ isLoading: true });
  37. const testRuleResponse = await getBackendSrv().post(`/api/alerts/test`, payload);
  38. this.setState({ isLoading: false, testRuleResponse });
  39. }
  40. setFormattedJson = (formattedJson: any) => {
  41. this.formattedJson = formattedJson;
  42. };
  43. getTextForClipboard = () => {
  44. return JSON.stringify(this.formattedJson, null, 2);
  45. };
  46. onClipboardSuccess = () => {
  47. appEvents.emit(AppEvents.alertSuccess, ['Content copied to clipboard']);
  48. };
  49. onToggleExpand = () => {
  50. this.setState((prevState) => ({
  51. ...prevState,
  52. allNodesExpanded: !this.state.allNodesExpanded,
  53. }));
  54. };
  55. getNrOfOpenNodes = () => {
  56. if (this.state.allNodesExpanded === null) {
  57. return 3; // 3 is default, ie when state is null
  58. } else if (this.state.allNodesExpanded) {
  59. return 20;
  60. }
  61. return 1;
  62. };
  63. renderExpandCollapse = () => {
  64. const { allNodesExpanded } = this.state;
  65. const collapse = (
  66. <>
  67. <Icon name="minus-circle" /> Collapse All
  68. </>
  69. );
  70. const expand = (
  71. <>
  72. <Icon name="plus-circle" /> Expand All
  73. </>
  74. );
  75. return allNodesExpanded ? collapse : expand;
  76. };
  77. render() {
  78. const { testRuleResponse, isLoading } = this.state;
  79. if (isLoading === true) {
  80. return <LoadingPlaceholder text="Evaluating rule" />;
  81. }
  82. const openNodes = this.getNrOfOpenNodes();
  83. return (
  84. <>
  85. <div className="pull-right">
  86. <HorizontalGroup spacing="md">
  87. <div onClick={this.onToggleExpand}>{this.renderExpandCollapse()}</div>
  88. <ClipboardButton getText={this.getTextForClipboard} onClipboardCopy={this.onClipboardSuccess} icon="copy">
  89. Copy to Clipboard
  90. </ClipboardButton>
  91. </HorizontalGroup>
  92. </div>
  93. <JSONFormatter json={testRuleResponse} open={openNodes} onDidRender={this.setFormattedJson} />
  94. </>
  95. );
  96. }
  97. }