grafana/public/app/features/alerting/TestRuleResult.tsx

46 lines
1.2 KiB
TypeScript
Raw Normal View History

2019-01-09 04:34:22 -06:00
import React, { PureComponent } from 'react';
import { JSONFormatter } from 'app/core/components/JSONFormatter/JSONFormatter';
import { getBackendSrv } from '@grafana/runtime';
import { DashboardModel } from '../dashboard/state/DashboardModel';
2019-01-10 16:15:37 -06:00
import { LoadingPlaceholder } from '@grafana/ui/src';
2019-01-09 04:34:22 -06:00
export interface Props {
panelId: number;
dashboard: DashboardModel;
}
interface State {
isLoading: boolean;
testRuleResponse: {};
}
export class TestRuleResult extends PureComponent<Props, State> {
2019-01-09 06:20:50 -06:00
readonly state: State = {
isLoading: false,
testRuleResponse: {},
};
2019-01-09 04:34:22 -06:00
componentDidMount() {
this.testRule();
}
async testRule() {
const { panelId, dashboard } = this.props;
const payload = { dashboard: dashboard.getSaveModelClone(), panelId };
this.setState({ isLoading: true });
2019-01-09 04:34:22 -06:00
const testRuleResponse = await getBackendSrv().post(`/api/alerts/test`, payload);
this.setState({ isLoading: false, testRuleResponse });
2019-01-09 04:34:22 -06:00
}
render() {
const { testRuleResponse, isLoading } = this.state;
if (isLoading === true) {
return <LoadingPlaceholder text="Evaluating rule" />;
}
2019-01-09 06:20:50 -06:00
return <JSONFormatter json={testRuleResponse} />;
2019-01-09 04:34:22 -06:00
}
}