grafana/public/app/features/alerting/AlertTab.tsx
Hugo Häggmark 34b4f7c717
Chore: Reduces strict errors (#33012)
* Chore: reduces strict error in OptionPicker tests

* Chore: reduces strict errors in FormDropdownCtrl

* Chore: reduces has no initializer and is not definitely assigned in the constructor errors

* Chore: reduces has no initializer and is not definitely assigned in the constructor errors

* Chore: lowers strict count limit

* Tests: updates snapshots

* Tests: updates snapshots

* Chore: updates after PR comments

* Refactor: removes throw and changes signature for DashboardSrv.getCurrent
2021-04-15 14:21:06 +02:00

258 lines
7.3 KiB
TypeScript

import React, { PureComponent } from 'react';
import { connect, MapDispatchToProps, MapStateToProps } from 'react-redux';
import { Alert, Button, ConfirmModal, Container, CustomScrollbar, HorizontalGroup, IconName, Modal } from '@grafana/ui';
import { selectors } from '@grafana/e2e-selectors';
import { AngularComponent, getAngularLoader, getDataSourceSrv } from '@grafana/runtime';
import { getAlertingValidationMessage } from './getAlertingValidationMessage';
import EmptyListCTA from 'app/core/components/EmptyListCTA/EmptyListCTA';
import StateHistory from './StateHistory';
import 'app/features/alerting/AlertTabCtrl';
import { DashboardModel } from '../dashboard/state/DashboardModel';
import { PanelModel } from '../dashboard/state/PanelModel';
import { TestRuleResult } from './TestRuleResult';
import { AppNotificationSeverity, StoreState } from 'app/types';
import { PanelNotSupported } from '../dashboard/components/PanelEditor/PanelNotSupported';
import { AlertState } from '../../plugins/datasource/alertmanager/types';
interface AngularPanelController {
_enableAlert: () => void;
alertState: AlertState | null;
render: () => void;
refresh: () => void;
}
interface OwnProps {
dashboard: DashboardModel;
panel: PanelModel;
}
interface ConnectedProps {
angularPanelComponent?: AngularComponent | null;
}
interface DispatchProps {}
export type Props = OwnProps & ConnectedProps & DispatchProps;
interface State {
validationMessage: string;
showStateHistory: boolean;
showDeleteConfirmation: boolean;
showTestRule: boolean;
}
class UnConnectedAlertTab extends PureComponent<Props, State> {
element?: HTMLDivElement | null;
component?: AngularComponent;
panelCtrl?: AngularPanelController;
state: State = {
validationMessage: '',
showStateHistory: false,
showDeleteConfirmation: false,
showTestRule: false,
};
componentDidMount() {
this.loadAlertTab();
}
onAngularPanelUpdated = () => {
this.forceUpdate();
};
componentDidUpdate(prevProps: Props) {
this.loadAlertTab();
}
componentWillUnmount() {
if (this.component) {
this.component.destroy();
}
}
async loadAlertTab() {
const { panel, angularPanelComponent } = this.props;
if (!this.element || !angularPanelComponent || this.component) {
return;
}
const scope = angularPanelComponent.getScope();
// When full page reloading in edit mode the angular panel has on fully compiled & instantiated yet
if (!scope.$$childHead) {
setTimeout(() => {
this.forceUpdate();
});
return;
}
this.panelCtrl = scope.$$childHead.ctrl;
const loader = getAngularLoader();
const template = '<alert-tab />';
const scopeProps = { ctrl: this.panelCtrl };
this.component = loader.load(this.element, scopeProps, template);
const validationMessage = await getAlertingValidationMessage(
panel.transformations,
panel.targets,
getDataSourceSrv(),
panel.datasource
);
if (validationMessage) {
this.setState({ validationMessage });
}
}
onAddAlert = () => {
this.panelCtrl?._enableAlert();
this.component?.digest();
this.forceUpdate();
};
onToggleModal = (prop: keyof Omit<State, 'validationMessage'>) => {
const value = this.state[prop];
this.setState({ ...this.state, [prop]: !value });
};
renderTestRule = () => {
if (!this.state.showTestRule) {
return null;
}
const { panel, dashboard } = this.props;
const onDismiss = () => this.onToggleModal('showTestRule');
return (
<Modal isOpen={true} icon="bug" title="Testing rule" onDismiss={onDismiss} onClickBackdrop={onDismiss}>
<TestRuleResult panel={panel} dashboard={dashboard} />
</Modal>
);
};
renderDeleteConfirmation = () => {
if (!this.state.showDeleteConfirmation) {
return null;
}
const { panel } = this.props;
const onDismiss = () => this.onToggleModal('showDeleteConfirmation');
return (
<ConfirmModal
isOpen={true}
icon="trash-alt"
title="Delete"
body={
<div>
Are you sure you want to delete this alert rule?
<br />
<small>You need to save dashboard for the delete to take effect.</small>
</div>
}
confirmText="Delete alert"
onDismiss={onDismiss}
onConfirm={() => {
delete panel.alert;
panel.thresholds = [];
if (this.panelCtrl) {
this.panelCtrl.alertState = null;
this.panelCtrl.render();
}
this.component?.digest();
onDismiss();
}}
/>
);
};
renderStateHistory = () => {
if (!this.state.showStateHistory) {
return null;
}
const { panel, dashboard } = this.props;
const onDismiss = () => this.onToggleModal('showStateHistory');
return (
<Modal isOpen={true} icon="history" title="State history" onDismiss={onDismiss} onClickBackdrop={onDismiss}>
<StateHistory
dashboard={dashboard}
panelId={panel.editSourceId ?? panel.id}
onRefresh={() => this.panelCtrl?.refresh()}
/>
</Modal>
);
};
render() {
const { alert, transformations } = this.props.panel;
const { validationMessage } = this.state;
const hasTransformations = transformations && transformations.length > 0;
if (!alert && validationMessage) {
return <PanelNotSupported message={validationMessage} />;
}
const model = {
title: 'Panel has no alert rule defined',
buttonIcon: 'bell' as IconName,
onClick: this.onAddAlert,
buttonTitle: 'Create Alert',
};
return (
<>
<CustomScrollbar autoHeightMin="100%">
<Container padding="md">
<div aria-label={selectors.components.AlertTab.content}>
{alert && hasTransformations && (
<Alert
severity={AppNotificationSeverity.Error}
title="Transformations are not supported in alert queries"
/>
)}
<div ref={(element) => (this.element = element)} />
{alert && (
<HorizontalGroup>
<Button onClick={() => this.onToggleModal('showStateHistory')} variant="secondary">
State history
</Button>
<Button onClick={() => this.onToggleModal('showTestRule')} variant="secondary">
Test rule
</Button>
<Button onClick={() => this.onToggleModal('showDeleteConfirmation')} variant="destructive">
Delete
</Button>
</HorizontalGroup>
)}
{!alert && !validationMessage && <EmptyListCTA {...model} />}
</div>
</Container>
</CustomScrollbar>
{this.renderTestRule()}
{this.renderDeleteConfirmation()}
{this.renderStateHistory()}
</>
);
}
}
const mapStateToProps: MapStateToProps<ConnectedProps, OwnProps, StoreState> = (state, props) => {
return {
angularPanelComponent: state.dashboard.panels[props.panel.id].angularComponent,
};
};
const mapDispatchToProps: MapDispatchToProps<DispatchProps, OwnProps> = {};
export const AlertTab = connect(mapStateToProps, mapDispatchToProps)(UnConnectedAlertTab);