2019-01-09 04:34:22 -06:00
|
|
|
import React, { PureComponent } from 'react';
|
|
|
|
import { JSONFormatter } from 'app/core/components/JSONFormatter/JSONFormatter';
|
2019-06-25 01:52:17 -05:00
|
|
|
import appEvents from 'app/core/app_events';
|
|
|
|
import { CopyToClipboard } from 'app/core/components/CopyToClipboard/CopyToClipboard';
|
2019-06-03 10:55:59 -05:00
|
|
|
import { getBackendSrv } from '@grafana/runtime';
|
2019-01-31 01:56:17 -06:00
|
|
|
import { DashboardModel } from '../dashboard/state/DashboardModel';
|
2019-07-06 01:05:53 -05:00
|
|
|
import { LoadingPlaceholder } from '@grafana/ui';
|
2019-01-09 04:34:22 -06:00
|
|
|
|
|
|
|
export interface Props {
|
|
|
|
panelId: number;
|
|
|
|
dashboard: DashboardModel;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface State {
|
|
|
|
isLoading: boolean;
|
2019-06-25 01:52:17 -05:00
|
|
|
allNodesExpanded: boolean;
|
2019-01-09 04:34:22 -06:00
|
|
|
testRuleResponse: {};
|
|
|
|
}
|
|
|
|
|
2019-01-10 15:47:09 -06:00
|
|
|
export class TestRuleResult extends PureComponent<Props, State> {
|
2019-01-09 06:20:50 -06:00
|
|
|
readonly state: State = {
|
|
|
|
isLoading: false,
|
2019-06-25 01:52:17 -05:00
|
|
|
allNodesExpanded: null,
|
2019-01-09 06:20:50 -06:00
|
|
|
testRuleResponse: {},
|
|
|
|
};
|
2019-01-09 04:34:22 -06:00
|
|
|
|
2019-06-25 01:52:17 -05:00
|
|
|
formattedJson: any;
|
|
|
|
clipboard: any;
|
|
|
|
|
2019-01-09 04:34:22 -06:00
|
|
|
componentDidMount() {
|
|
|
|
this.testRule();
|
|
|
|
}
|
|
|
|
|
|
|
|
async testRule() {
|
|
|
|
const { panelId, dashboard } = this.props;
|
|
|
|
const payload = { dashboard: dashboard.getSaveModelClone(), panelId };
|
2019-01-10 15:47:09 -06:00
|
|
|
|
|
|
|
this.setState({ isLoading: true });
|
2019-01-09 04:34:22 -06:00
|
|
|
const testRuleResponse = await getBackendSrv().post(`/api/alerts/test`, payload);
|
2019-01-10 15:47:09 -06:00
|
|
|
this.setState({ isLoading: false, testRuleResponse });
|
2019-01-09 04:34:22 -06:00
|
|
|
}
|
|
|
|
|
2019-06-25 01:52:17 -05:00
|
|
|
setFormattedJson = formattedJson => {
|
|
|
|
this.formattedJson = formattedJson;
|
|
|
|
};
|
|
|
|
|
|
|
|
getTextForClipboard = () => {
|
|
|
|
return JSON.stringify(this.formattedJson, null, 2);
|
|
|
|
};
|
|
|
|
|
|
|
|
onClipboardSuccess = () => {
|
|
|
|
appEvents.emit('alert-success', ['Content copied to clipboard']);
|
|
|
|
};
|
|
|
|
|
|
|
|
onToggleExpand = () => {
|
|
|
|
this.setState(prevState => ({
|
|
|
|
...prevState,
|
|
|
|
allNodesExpanded: !this.state.allNodesExpanded,
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
|
|
|
getNrOfOpenNodes = () => {
|
|
|
|
if (this.state.allNodesExpanded === null) {
|
|
|
|
return 3; // 3 is default, ie when state is null
|
|
|
|
} else if (this.state.allNodesExpanded) {
|
|
|
|
return 20;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
renderExpandCollapse = () => {
|
|
|
|
const { allNodesExpanded } = this.state;
|
|
|
|
|
|
|
|
const collapse = (
|
|
|
|
<>
|
|
|
|
<i className="fa fa-minus-square-o" /> Collapse All
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
const expand = (
|
|
|
|
<>
|
|
|
|
<i className="fa fa-plus-square-o" /> Expand All
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
return allNodesExpanded ? collapse : expand;
|
|
|
|
};
|
|
|
|
|
2019-01-09 04:34:22 -06:00
|
|
|
render() {
|
|
|
|
const { testRuleResponse, isLoading } = this.state;
|
|
|
|
|
|
|
|
if (isLoading === true) {
|
|
|
|
return <LoadingPlaceholder text="Evaluating rule" />;
|
|
|
|
}
|
|
|
|
|
2019-06-25 01:52:17 -05:00
|
|
|
const openNodes = this.getNrOfOpenNodes();
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<div className="pull-right">
|
|
|
|
<button className="btn btn-transparent btn-p-x-0 m-r-1" onClick={this.onToggleExpand}>
|
|
|
|
{this.renderExpandCollapse()}
|
|
|
|
</button>
|
|
|
|
<CopyToClipboard
|
|
|
|
className="btn btn-transparent btn-p-x-0"
|
|
|
|
text={this.getTextForClipboard}
|
|
|
|
onSuccess={this.onClipboardSuccess}
|
|
|
|
>
|
|
|
|
<i className="fa fa-clipboard" /> Copy to Clipboard
|
|
|
|
</CopyToClipboard>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<JSONFormatter json={testRuleResponse} open={openNodes} onDidRender={this.setFormattedJson} />
|
|
|
|
</>
|
|
|
|
);
|
2019-01-09 04:34:22 -06:00
|
|
|
}
|
|
|
|
}
|