grafana/public/app/features/alerting/NextGenAlertingPage.tsx
Peter Holmberg 569fb3f112
Alerting: adding query editor when creating threshold rule. (#33123)
* fix viz

* add datasource picker on query rows in mixed mode

* add timerange, handle add/remove queryrunners

* multiqueryrunner test

* trying things out.

* adding another test to verify running a induvidual query runner will update multirunner.

* cleaned up tests a bit.

* draft version working ok.

* fixing so we base the refId from request targets.

* reenable adding expression

* layout fixes for alerting page

* some cleanup

* cleaning up code that we won't use

* changed so we don't display the time range if params not passed.

* remove unused things in querygroup

* changed button to type button.

* removed timerange from dataQuery and removed multiquery runner.

* minor refactoring.

* renamed callback function to make it more clear what it does.

* renamed droppable area.

* changed so we only display the query editor when selecting threshold.

* removed the refresh picker.

* revert

* wip

* extending with data query.

* timerange fixes

* it is now possible to add grafana queries.

* removed unused type.

* removed expect import.

* added docs.

* moved range converting methods to rangeUtil.

* clean up some typings, remove file

* making sure we don't blow up on component being unmounted.

Co-authored-by: Marcus Andersson <marcus.andersson@grafana.com>
2021-04-21 13:57:17 +02:00

178 lines
5.3 KiB
TypeScript

import React, { FormEvent, PureComponent } from 'react';
import { hot } from 'react-hot-loader';
import { connect, ConnectedProps } from 'react-redux';
import { css } from '@emotion/css';
import { GrafanaTheme, SelectableValue } from '@grafana/data';
import { locationService } from '@grafana/runtime';
import { PageToolbar, stylesFactory, ToolbarButton } from '@grafana/ui';
import { config } from 'app/core/config';
import { SplitPaneWrapper } from 'app/core/components/SplitPaneWrapper/SplitPaneWrapper';
import { AlertingQueryEditor } from './components/AlertingQueryEditor';
import { AlertDefinitionOptions } from './components/AlertDefinitionOptions';
import { AlertingQueryPreview } from './components/AlertingQueryPreview';
import {
cleanUpDefinitionState,
createAlertDefinition,
evaluateAlertDefinition,
evaluateNotSavedAlertDefinition,
getAlertDefinition,
updateAlertDefinition,
updateAlertDefinitionOption,
updateAlertDefinitionUiState,
} from './state/actions';
import { StoreState } from 'app/types';
import { GrafanaRouteComponentProps } from 'app/core/navigation/types';
import { GrafanaQuery } from '../../types/unified-alerting-dto';
function mapStateToProps(state: StoreState, props: RouteProps) {
return {
uiState: state.alertDefinition.uiState,
getInstances: state.alertDefinition.getInstances,
alertDefinition: state.alertDefinition.alertDefinition,
pageId: props.match.params.id as string,
};
}
const mapDispatchToProps = {
updateAlertDefinitionUiState,
updateAlertDefinitionOption,
evaluateAlertDefinition,
updateAlertDefinition,
createAlertDefinition,
getAlertDefinition,
evaluateNotSavedAlertDefinition,
cleanUpDefinitionState,
};
const connector = connect(mapStateToProps, mapDispatchToProps);
interface RouteProps extends GrafanaRouteComponentProps<{ id: string }> {}
interface OwnProps {
saveDefinition: typeof createAlertDefinition | typeof updateAlertDefinition;
}
type Props = OwnProps & ConnectedProps<typeof connector>;
class NextGenAlertingPageUnconnected extends PureComponent<Props> {
componentDidMount() {
const { getAlertDefinition, pageId } = this.props;
if (pageId) {
getAlertDefinition(pageId);
}
}
componentWillUnmount() {
this.props.cleanUpDefinitionState();
}
onChangeAlertOption = (event: FormEvent<HTMLElement>) => {
const formEvent = event as FormEvent<HTMLFormElement>;
this.props.updateAlertDefinitionOption({ [formEvent.currentTarget.name]: formEvent.currentTarget.value });
};
onChangeInterval = (interval: SelectableValue<number>) => {
this.props.updateAlertDefinitionOption({
intervalSeconds: interval.value,
});
};
onConditionChange = (condition: SelectableValue<string>) => {
this.props.updateAlertDefinitionOption({
condition: condition.value,
});
};
onSaveAlert = () => {
const { alertDefinition, createAlertDefinition, updateAlertDefinition } = this.props;
if (alertDefinition.uid) {
updateAlertDefinition();
} else {
createAlertDefinition();
}
};
onDiscard = () => {
locationService.replace(`${config.appSubUrl}/alerting/ng/list`);
};
onTest = () => {
const { alertDefinition, evaluateAlertDefinition, evaluateNotSavedAlertDefinition } = this.props;
if (alertDefinition.uid) {
evaluateAlertDefinition();
} else {
evaluateNotSavedAlertDefinition();
}
};
renderToolbarActions() {
return [
<ToolbarButton variant="destructive" key="discard" onClick={this.onDiscard}>
Discard
</ToolbarButton>,
<ToolbarButton key="test" onClick={this.onTest}>
Test
</ToolbarButton>,
<ToolbarButton variant="primary" key="save" onClick={this.onSaveAlert}>
Save
</ToolbarButton>,
];
}
render() {
const { alertDefinition, uiState, updateAlertDefinitionUiState, getInstances } = this.props;
const styles = getStyles(config.theme);
return (
<div className={styles.wrapper}>
<PageToolbar title="Alert editor" pageIcon="bell">
{this.renderToolbarActions()}
</PageToolbar>
<div className={styles.splitPanesWrapper}>
<SplitPaneWrapper
leftPaneComponents={[
<AlertingQueryPreview key="queryPreview" getInstances={getInstances} queries={[]} onTest={this.onTest} />,
<AlertingQueryEditor key="queryEditor" value={[] as GrafanaQuery[]} onChange={() => {}} />,
]}
uiState={uiState}
updateUiState={updateAlertDefinitionUiState}
rightPaneComponents={
<AlertDefinitionOptions
alertDefinition={alertDefinition}
onChange={this.onChangeAlertOption}
onIntervalChange={this.onChangeInterval}
onConditionChange={this.onConditionChange}
/>
}
/>
</div>
</div>
);
}
}
export default hot(module)(connector(NextGenAlertingPageUnconnected));
const getStyles = stylesFactory((theme: GrafanaTheme) => ({
wrapper: css`
width: calc(100% - 55px);
height: 100%;
position: fixed;
top: 0;
bottom: 0;
background: ${theme.colors.dashboardBg};
display: flex;
flex-direction: column;
`,
splitPanesWrapper: css`
display: flex;
flex-direction: column;
height: 100%;
width: 100%;
position: relative;
`,
}));