TestRuleResult.tsx 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import React, { PureComponent } from 'react';
  2. import { JSONFormatter } from 'app/core/components/JSONFormatter/JSONFormatter';
  3. import { getBackendSrv } from 'app/core/services/backend_srv';
  4. import { DashboardModel } from '../dashboard/state/DashboardModel';
  5. import { LoadingPlaceholder } from '@grafana/ui/src';
  6. export interface Props {
  7. panelId: number;
  8. dashboard: DashboardModel;
  9. }
  10. interface State {
  11. isLoading: boolean;
  12. testRuleResponse: {};
  13. }
  14. export class TestRuleResult extends PureComponent<Props, State> {
  15. readonly state: State = {
  16. isLoading: false,
  17. testRuleResponse: {},
  18. };
  19. componentDidMount() {
  20. this.testRule();
  21. }
  22. async testRule() {
  23. const { panelId, dashboard } = this.props;
  24. const payload = { dashboard: dashboard.getSaveModelClone(), panelId };
  25. this.setState({ isLoading: true });
  26. const testRuleResponse = await getBackendSrv().post(`/api/alerts/test`, payload);
  27. this.setState({ isLoading: false, testRuleResponse });
  28. }
  29. render() {
  30. const { testRuleResponse, isLoading } = this.state;
  31. if (isLoading === true) {
  32. return <LoadingPlaceholder text="Evaluating rule" />;
  33. }
  34. return <JSONFormatter json={testRuleResponse} />;
  35. }
  36. }