TestRuleResult.tsx 3.0 KB

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