diff --git a/public/app/features/alerting/AlertTab.tsx b/public/app/features/alerting/AlertTab.tsx index a5afbc198fc..5623fac95c1 100644 --- a/public/app/features/alerting/AlertTab.tsx +++ b/public/app/features/alerting/AlertTab.tsx @@ -1,5 +1,5 @@ // Libraries -import React, { PureComponent } from 'react'; +import React, { PureComponent, SFC } from 'react'; // Services & Utils import { AngularComponent, getAngularLoader } from 'app/core/services/AngularLoader'; @@ -14,6 +14,7 @@ import 'app/features/alerting/AlertTabCtrl'; // Types import { DashboardModel } from '../dashboard/dashboard_model'; import { PanelModel } from '../dashboard/panel_model'; +import { TestRuleButton } from './TestRuleButton'; interface Props { angularPanel?: AngularComponent; @@ -21,6 +22,16 @@ interface Props { panel: PanelModel; } +interface LoadingPlaceholderProps { + text: string; +} + +const LoadingPlaceholder: SFC = ({ text }) => ( +
+ {text} +
+); + export class AlertTab extends PureComponent { element: any; component: AngularComponent; @@ -65,9 +76,7 @@ export class AlertTab extends PureComponent { const loader = getAngularLoader(); const template = ''; - const scopeProps = { - ctrl: this.panelCtrl, - }; + const scopeProps = { ctrl: this.panelCtrl }; this.component = loader.load(this.element, scopeProps, template); } @@ -111,6 +120,16 @@ export class AlertTab extends PureComponent { }; }; + renderTestRuleButton = () => { + const { panel, dashboard } = this.props; + return ; + }; + + testRule = (): EditorToolbarView => ({ + title: 'Test Rule', + render: () => this.renderTestRuleButton(), + }); + onAddAlert = () => { this.panelCtrl._enableAlert(); this.component.digest(); @@ -120,7 +139,7 @@ export class AlertTab extends PureComponent { render() { const { alert } = this.props.panel; - const toolbarItems = alert ? [this.stateHistory(), this.deleteAlert()] : []; + const toolbarItems = alert ? [this.stateHistory(), this.testRule(), this.deleteAlert()] : []; const model = { title: 'Panel has no alert rule defined', diff --git a/public/app/features/alerting/TestRuleButton.tsx b/public/app/features/alerting/TestRuleButton.tsx new file mode 100644 index 00000000000..032d7c7e4f8 --- /dev/null +++ b/public/app/features/alerting/TestRuleButton.tsx @@ -0,0 +1,48 @@ +import React, { PureComponent } from 'react'; +import { JSONFormatter } from 'app/core/components/JSONFormatter/JSONFormatter'; +import { getBackendSrv } from 'app/core/services/backend_srv'; +import { DashboardModel } from '../dashboard/dashboard_model'; + +export interface Props { + panelId: number; + dashboard: DashboardModel; + LoadingPlaceholder: any; +} + +interface State { + isLoading: boolean; + testRuleResponse: {}; +} + +export class TestRuleButton extends PureComponent { + constructor(props) { + super(props); + this.state = { isLoading: false, testRuleResponse: {} }; + } + + componentDidMount() { + this.testRule(); + } + + async testRule() { + const { panelId, dashboard } = this.props; + const payload = { dashboard: dashboard.getSaveModelClone(), panelId }; + const testRuleResponse = await getBackendSrv().post(`/api/alerts/test`, payload); + this.setState(prevState => ({ ...prevState, isLoading: false, testRuleResponse })); + } + + render() { + const { testRuleResponse, isLoading } = this.state; + const { LoadingPlaceholder } = this.props; + + if (isLoading === true) { + return ; + } + + return ( + <> + + + ); + } +}