TestRuleResult.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import React, { PureComponent } from 'react';
  2. import appEvents from 'app/core/app_events';
  3. import { CopyToClipboard } from 'app/core/components/CopyToClipboard/CopyToClipboard';
  4. import { getBackendSrv } from '@grafana/runtime';
  5. import { DashboardModel } from '../dashboard/state/DashboardModel';
  6. import { LoadingPlaceholder, JSONFormatter } from '@grafana/ui';
  7. export interface Props {
  8. panelId: number;
  9. dashboard: DashboardModel;
  10. }
  11. interface State {
  12. isLoading: boolean;
  13. allNodesExpanded: boolean;
  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 { panelId, dashboard } = this.props;
  29. const payload = { dashboard: dashboard.getSaveModelClone(), panelId };
  30. this.setState({ isLoading: true });
  31. const testRuleResponse = await getBackendSrv().post(`/api/alerts/test`, payload);
  32. this.setState({ isLoading: false, testRuleResponse });
  33. }
  34. setFormattedJson = (formattedJson: any) => {
  35. this.formattedJson = formattedJson;
  36. };
  37. getTextForClipboard = () => {
  38. return JSON.stringify(this.formattedJson, null, 2);
  39. };
  40. onClipboardSuccess = () => {
  41. appEvents.emit('alert-success', ['Content copied to clipboard']);
  42. };
  43. onToggleExpand = () => {
  44. this.setState(prevState => ({
  45. ...prevState,
  46. allNodesExpanded: !this.state.allNodesExpanded,
  47. }));
  48. };
  49. getNrOfOpenNodes = () => {
  50. if (this.state.allNodesExpanded === null) {
  51. return 3; // 3 is default, ie when state is null
  52. } else if (this.state.allNodesExpanded) {
  53. return 20;
  54. }
  55. return 1;
  56. };
  57. renderExpandCollapse = () => {
  58. const { allNodesExpanded } = this.state;
  59. const collapse = (
  60. <>
  61. <i className="fa fa-minus-square-o" /> Collapse All
  62. </>
  63. );
  64. const expand = (
  65. <>
  66. <i className="fa fa-plus-square-o" /> Expand All
  67. </>
  68. );
  69. return allNodesExpanded ? collapse : expand;
  70. };
  71. render() {
  72. const { testRuleResponse, isLoading } = this.state;
  73. if (isLoading === true) {
  74. return <LoadingPlaceholder text="Evaluating rule" />;
  75. }
  76. const openNodes = this.getNrOfOpenNodes();
  77. return (
  78. <>
  79. <div className="pull-right">
  80. <button className="btn btn-transparent btn-p-x-0 m-r-1" onClick={this.onToggleExpand}>
  81. {this.renderExpandCollapse()}
  82. </button>
  83. <CopyToClipboard
  84. className="btn btn-transparent btn-p-x-0"
  85. text={this.getTextForClipboard}
  86. onSuccess={this.onClipboardSuccess}
  87. >
  88. <i className="fa fa-clipboard" /> Copy to Clipboard
  89. </CopyToClipboard>
  90. </div>
  91. <JSONFormatter json={testRuleResponse} open={openNodes} onDidRender={this.setFormattedJson} />
  92. </>
  93. );
  94. }
  95. }