Files
grafana/public/app/features/alerting/AlertTab.tsx

151 lines
3.8 KiB
TypeScript
Raw Normal View History

2019-01-04 12:38:50 +01:00
// Libraries
import React, { PureComponent } from 'react';
2019-01-04 12:38:50 +01:00
// Services & Utils
2018-12-21 11:57:21 +01:00
import { AngularComponent, getAngularLoader } from 'app/core/services/AngularLoader';
2019-01-04 12:38:50 +01:00
import appEvents from 'app/core/app_events';
// Components
import { EditorTabBody, EditorToolbarView } from '../dashboard/panel_editor/EditorTabBody';
2019-01-02 10:57:12 +01:00
import EmptyListCTA from 'app/core/components/EmptyListCTA/EmptyListCTA';
2019-01-02 15:22:22 +01:00
import StateHistory from './StateHistory';
2018-12-21 14:56:49 +01:00
import 'app/features/alerting/AlertTabCtrl';
2019-01-04 12:38:50 +01:00
// Types
import { DashboardModel } from '../dashboard/state/DashboardModel';
import { PanelModel } from '../dashboard/state/PanelModel';
import { TestRuleResult } from './TestRuleResult';
interface Props {
angularPanel?: AngularComponent;
2019-01-02 15:22:22 +01:00
dashboard: DashboardModel;
2018-12-19 13:09:53 +01:00
panel: PanelModel;
}
export class AlertTab extends PureComponent<Props> {
element: any;
component: AngularComponent;
2018-12-21 14:56:49 +01:00
panelCtrl: any;
componentDidMount() {
if (this.shouldLoadAlertTab()) {
this.loadAlertTab();
}
}
componentDidUpdate(prevProps: Props) {
if (this.shouldLoadAlertTab()) {
this.loadAlertTab();
}
}
shouldLoadAlertTab() {
2019-01-02 10:57:12 +01:00
return this.props.angularPanel && this.element && !this.component;
}
componentWillUnmount() {
if (this.component) {
this.component.destroy();
}
}
loadAlertTab() {
const { angularPanel } = this.props;
const scope = angularPanel.getScope();
// When full page reloading in edit mode the angular panel has on fully compiled & instantiated yet
if (!scope.$$childHead) {
setTimeout(() => {
this.forceUpdate();
});
return;
}
2018-12-21 14:56:49 +01:00
this.panelCtrl = scope.$$childHead.ctrl;
const loader = getAngularLoader();
const template = '<alert-tab />';
2019-01-09 11:34:22 +01:00
const scopeProps = { ctrl: this.panelCtrl };
this.component = loader.load(this.element, scopeProps, template);
}
2018-12-21 14:56:49 +01:00
stateHistory = (): EditorToolbarView => {
return {
2018-12-19 13:09:53 +01:00
title: 'State history',
render: () => {
2019-01-02 16:07:29 +01:00
return (
<StateHistory
dashboard={this.props.dashboard}
panelId={this.props.panel.id}
onRefresh={this.panelCtrl.refresh}
/>
);
2018-12-19 13:09:53 +01:00
},
};
2018-12-21 14:56:49 +01:00
};
deleteAlert = (): EditorToolbarView => {
const { panel } = this.props;
return {
title: 'Delete',
2019-01-04 12:38:50 +01:00
btnType: 'danger',
2018-12-21 14:56:49 +01:00
onClick: () => {
appEvents.emit('confirm-modal', {
title: 'Delete Alert',
text: 'Are you sure you want to delete this alert rule?',
text2: 'You need to save dashboard for the delete to take effect',
icon: 'fa-trash',
yesText: 'Delete',
onConfirm: () => {
delete panel.alert;
panel.thresholds = [];
this.panelCtrl.alertState = null;
this.panelCtrl.render();
2019-01-02 10:57:12 +01:00
this.forceUpdate();
2018-12-21 14:56:49 +01:00
},
});
2018-12-19 13:09:53 +01:00
},
};
2018-12-21 14:56:49 +01:00
};
renderTestRuleResult = () => {
2019-01-09 11:34:22 +01:00
const { panel, dashboard } = this.props;
2019-01-10 22:15:37 +00:00
return <TestRuleResult panelId={panel.id} dashboard={dashboard} />;
2019-01-09 11:34:22 +01:00
};
testRule = (): EditorToolbarView => ({
title: 'Test Rule',
render: () => this.renderTestRuleResult(),
2019-01-09 11:34:22 +01:00
});
2019-01-02 10:57:12 +01:00
onAddAlert = () => {
this.panelCtrl._enableAlert();
this.component.digest();
this.forceUpdate();
};
2018-12-21 14:56:49 +01:00
render() {
const { alert } = this.props.panel;
2018-12-19 13:09:53 +01:00
2019-01-09 11:34:22 +01:00
const toolbarItems = alert ? [this.stateHistory(), this.testRule(), this.deleteAlert()] : [];
2018-12-19 13:09:53 +01:00
2019-01-02 10:57:12 +01:00
const model = {
title: 'Panel has no alert rule defined',
icon: 'icon-gf icon-gf-alert',
onClick: this.onAddAlert,
buttonTitle: 'Create Alert',
};
return (
2018-12-19 13:09:53 +01:00
<EditorTabBody heading="Alert" toolbarItems={toolbarItems}>
2019-01-02 10:57:12 +01:00
<>
<div ref={element => (this.element = element)} />
{!alert && <EmptyListCTA model={model} />}
</>
</EditorTabBody>
);
}
}