2019-01-09 04:34:22 -06:00
|
|
|
import React, { PureComponent } from 'react';
|
2021-04-23 03:06:42 -05:00
|
|
|
import { LoadingPlaceholder, JSONFormatter, Icon, HorizontalGroup } from '@grafana/ui';
|
2019-06-25 01:52:17 -05:00
|
|
|
import appEvents from 'app/core/app_events';
|
|
|
|
import { CopyToClipboard } from 'app/core/components/CopyToClipboard/CopyToClipboard';
|
2020-03-30 06:50:18 -05:00
|
|
|
import { DashboardModel, PanelModel } from '../dashboard/state';
|
2020-01-21 03:08:07 -06:00
|
|
|
import { getBackendSrv } from '@grafana/runtime';
|
2019-10-14 03:27:47 -05:00
|
|
|
import { AppEvents } from '@grafana/data';
|
2019-01-09 04:34:22 -06:00
|
|
|
|
|
|
|
export interface Props {
|
|
|
|
dashboard: DashboardModel;
|
2020-03-30 06:50:18 -05:00
|
|
|
panel: PanelModel;
|
2019-01-09 04:34:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
interface State {
|
|
|
|
isLoading: boolean;
|
Chore: Fix all Typescript strict null errors (#26204)
* Chore: Fix typescript strict null errors
* Added new limit
* Fixed ts issue
* fixed tests
* trying to fix type inference
* Fixing more ts errors
* Revert tsconfig option
* Fix
* Fixed code
* More fixes
* fix tests
* Updated snapshot
* Chore: More ts strict null fixes
* More fixes in some really messed up azure config components
* More fixes, current count: 441
* 419
* More fixes
* Fixed invalid initial state in explore
* Fixing tests
* Fixed tests
* Explore fix
* More fixes
* Progress
* Sub 300
* Now at 218
* Progress
* Update
* Progress
* Updated tests
* at 159
* fixed tests
* Progress
* YAy blow 100! at 94
* 10,9,8,7,6,5,4,3,2,1... lift off
* Fixed tests
* Fixed more type errors
Co-authored-by: Ryan McKinley <ryantxu@gmail.com>
2020-07-10 05:46:59 -05:00
|
|
|
allNodesExpanded: boolean | null;
|
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() {
|
2020-03-30 06:50:18 -05:00
|
|
|
const { dashboard, panel } = this.props;
|
|
|
|
|
|
|
|
// dashboard save model
|
|
|
|
const model = dashboard.getSaveModelClone();
|
|
|
|
|
|
|
|
// now replace panel to get current edits
|
2021-01-20 00:59:48 -06:00
|
|
|
model.panels = model.panels.map((dashPanel) => {
|
2020-03-30 06:50:18 -05:00
|
|
|
return dashPanel.id === panel.editSourceId ? panel.getSaveModel() : dashPanel;
|
|
|
|
});
|
|
|
|
|
|
|
|
const payload = { dashboard: model, panelId: panel.id };
|
2019-01-10 15:47:09 -06:00
|
|
|
|
|
|
|
this.setState({ isLoading: true });
|
2020-01-21 03:08:07 -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-08-01 07:38:34 -05:00
|
|
|
setFormattedJson = (formattedJson: any) => {
|
2019-06-25 01:52:17 -05:00
|
|
|
this.formattedJson = formattedJson;
|
|
|
|
};
|
|
|
|
|
|
|
|
getTextForClipboard = () => {
|
|
|
|
return JSON.stringify(this.formattedJson, null, 2);
|
|
|
|
};
|
|
|
|
|
|
|
|
onClipboardSuccess = () => {
|
2019-10-14 03:27:47 -05:00
|
|
|
appEvents.emit(AppEvents.alertSuccess, ['Content copied to clipboard']);
|
2019-06-25 01:52:17 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
onToggleExpand = () => {
|
2021-01-20 00:59:48 -06:00
|
|
|
this.setState((prevState) => ({
|
2019-06-25 01:52:17 -05:00
|
|
|
...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 = (
|
|
|
|
<>
|
2020-04-12 15:20:02 -05:00
|
|
|
<Icon name="minus-circle" /> Collapse All
|
2019-06-25 01:52:17 -05:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
const expand = (
|
|
|
|
<>
|
2020-04-12 15:20:02 -05:00
|
|
|
<Icon name="plus-circle" /> Expand All
|
2019-06-25 01:52:17 -05:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
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">
|
2021-04-23 03:06:42 -05:00
|
|
|
<HorizontalGroup spacing="md">
|
|
|
|
<div onClick={this.onToggleExpand}>{this.renderExpandCollapse()}</div>
|
|
|
|
<CopyToClipboard elType="div" text={this.getTextForClipboard} onSuccess={this.onClipboardSuccess}>
|
|
|
|
<Icon name="copy" /> Copy to Clipboard
|
|
|
|
</CopyToClipboard>
|
|
|
|
</HorizontalGroup>
|
2019-06-25 01:52:17 -05:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<JSONFormatter json={testRuleResponse} open={openNodes} onDidRender={this.setFormattedJson} />
|
|
|
|
</>
|
|
|
|
);
|
2019-01-09 04:34:22 -06:00
|
|
|
}
|
|
|
|
}
|