2020-12-11 07:49:14 -06:00
|
|
|
import React, { PureComponent } from 'react';
|
2021-04-01 07:15:23 -05:00
|
|
|
import { css } from '@emotion/css';
|
2021-05-04 09:31:25 -05:00
|
|
|
import {
|
|
|
|
DataQuery,
|
|
|
|
getDefaultRelativeTimeRange,
|
|
|
|
GrafanaTheme2,
|
|
|
|
LoadingState,
|
|
|
|
PanelData,
|
|
|
|
RelativeTimeRange,
|
|
|
|
} from '@grafana/data';
|
2021-04-21 06:57:17 -05:00
|
|
|
import { selectors } from '@grafana/e2e-selectors';
|
|
|
|
import { Button, HorizontalGroup, Icon, stylesFactory, Tooltip } from '@grafana/ui';
|
2021-05-04 09:31:25 -05:00
|
|
|
import { config } from '@grafana/runtime';
|
2021-04-21 06:57:17 -05:00
|
|
|
import { AlertingQueryRows } from './AlertingQueryRows';
|
2021-04-29 08:10:14 -05:00
|
|
|
import { dataSource as expressionDatasource, ExpressionDatasourceUID } from '../../expressions/ExpressionDatasource';
|
2021-04-21 06:57:17 -05:00
|
|
|
import { getNextRefIdChar } from 'app/core/utils/query';
|
|
|
|
import { defaultCondition } from '../../expressions/utils/expressionTypes';
|
|
|
|
import { ExpressionQueryType } from '../../expressions/types';
|
2021-04-29 08:10:14 -05:00
|
|
|
import { GrafanaQuery } from 'app/types/unified-alerting-dto';
|
2021-05-04 09:31:25 -05:00
|
|
|
import { AlertingQueryRunner } from '../state/AlertingQueryRunner';
|
|
|
|
import { getDatasourceSrv } from 'app/features/plugins/datasource_srv';
|
|
|
|
import { isExpressionQuery } from 'app/features/expressions/guards';
|
2021-02-15 06:56:59 -06:00
|
|
|
|
2021-04-21 06:57:17 -05:00
|
|
|
interface Props {
|
|
|
|
value?: GrafanaQuery[];
|
|
|
|
onChange: (queries: GrafanaQuery[]) => void;
|
|
|
|
}
|
2020-12-11 07:49:14 -06:00
|
|
|
|
2021-04-21 06:57:17 -05:00
|
|
|
interface State {
|
2021-05-04 09:31:25 -05:00
|
|
|
panelDataByRefId: Record<string, PanelData>;
|
2020-12-11 07:49:14 -06:00
|
|
|
}
|
2021-04-21 06:57:17 -05:00
|
|
|
export class AlertingQueryEditor extends PureComponent<Props, State> {
|
2021-05-04 09:31:25 -05:00
|
|
|
private runner: AlertingQueryRunner;
|
|
|
|
|
2021-04-21 06:57:17 -05:00
|
|
|
constructor(props: Props) {
|
|
|
|
super(props);
|
2021-05-04 09:31:25 -05:00
|
|
|
this.state = { panelDataByRefId: {} };
|
|
|
|
this.runner = new AlertingQueryRunner();
|
2021-04-21 06:57:17 -05:00
|
|
|
}
|
2020-12-11 07:49:14 -06:00
|
|
|
|
2021-05-04 09:31:25 -05:00
|
|
|
componentDidMount() {
|
|
|
|
this.runner.get().subscribe((data) => {
|
|
|
|
this.setState({ panelDataByRefId: data });
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this.runner.destroy();
|
2021-04-21 06:57:17 -05:00
|
|
|
}
|
2021-02-15 06:56:59 -06:00
|
|
|
|
2021-05-04 09:31:25 -05:00
|
|
|
onRunQueries = () => {
|
|
|
|
const { value = [] } = this.props;
|
|
|
|
this.runner.run(value);
|
|
|
|
};
|
|
|
|
|
|
|
|
onCancelQueries = () => {
|
|
|
|
this.runner.cancel();
|
|
|
|
};
|
2020-12-11 07:49:14 -06:00
|
|
|
|
2021-04-21 06:57:17 -05:00
|
|
|
onDuplicateQuery = (query: GrafanaQuery) => {
|
|
|
|
const { onChange, value = [] } = this.props;
|
2021-05-04 09:31:25 -05:00
|
|
|
onChange(addQuery(value, query));
|
2021-04-21 06:57:17 -05:00
|
|
|
};
|
2021-02-15 06:56:59 -06:00
|
|
|
|
2021-04-21 06:57:17 -05:00
|
|
|
onNewAlertingQuery = () => {
|
|
|
|
const { onChange, value = [] } = this.props;
|
2021-05-04 09:31:25 -05:00
|
|
|
const defaultDataSource = getDatasourceSrv().getInstanceSettings('default');
|
2021-02-15 06:56:59 -06:00
|
|
|
|
2021-04-21 06:57:17 -05:00
|
|
|
if (!defaultDataSource) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-29 08:10:14 -05:00
|
|
|
onChange(
|
|
|
|
addQuery(value, {
|
|
|
|
datasourceUid: defaultDataSource.uid,
|
|
|
|
model: {
|
|
|
|
refId: '',
|
|
|
|
datasource: defaultDataSource.name,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
);
|
2021-01-19 07:04:54 -06:00
|
|
|
};
|
2020-12-11 07:49:14 -06:00
|
|
|
|
2021-04-21 06:57:17 -05:00
|
|
|
onNewExpressionQuery = () => {
|
|
|
|
const { onChange, value = [] } = this.props;
|
2021-04-29 08:10:14 -05:00
|
|
|
|
|
|
|
onChange(
|
|
|
|
addQuery(value, {
|
|
|
|
datasourceUid: ExpressionDatasourceUID,
|
|
|
|
model: expressionDatasource.newQuery({
|
|
|
|
type: ExpressionQueryType.classic,
|
|
|
|
conditions: [defaultCondition],
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
);
|
2020-12-11 07:49:14 -06:00
|
|
|
};
|
|
|
|
|
2021-04-21 06:57:17 -05:00
|
|
|
renderAddQueryRow(styles: ReturnType<typeof getStyles>) {
|
|
|
|
return (
|
|
|
|
<HorizontalGroup spacing="md" align="flex-start">
|
|
|
|
<Button
|
|
|
|
type="button"
|
|
|
|
icon="plus"
|
|
|
|
onClick={this.onNewAlertingQuery}
|
|
|
|
variant="secondary"
|
|
|
|
aria-label={selectors.components.QueryTab.addQuery}
|
|
|
|
>
|
|
|
|
Query
|
|
|
|
</Button>
|
|
|
|
{config.expressionsEnabled && (
|
|
|
|
<Tooltip content="Experimental feature: queries could stop working in next version" placement="right">
|
|
|
|
<Button
|
|
|
|
type="button"
|
|
|
|
icon="plus"
|
|
|
|
onClick={this.onNewExpressionQuery}
|
|
|
|
variant="secondary"
|
|
|
|
className={styles.expressionButton}
|
|
|
|
>
|
|
|
|
<span>Expression </span>
|
|
|
|
<Icon name="exclamation-triangle" className="muted" size="sm" />
|
|
|
|
</Button>
|
|
|
|
</Tooltip>
|
|
|
|
)}
|
|
|
|
</HorizontalGroup>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-05-04 09:31:25 -05:00
|
|
|
isRunning() {
|
|
|
|
const data = Object.values(this.state.panelDataByRefId).find((d) => Boolean(d));
|
|
|
|
return data?.state === LoadingState.Loading;
|
|
|
|
}
|
|
|
|
|
|
|
|
renderRunQueryButton() {
|
|
|
|
const isRunning = this.isRunning();
|
|
|
|
const styles = getStyles(config.theme2);
|
|
|
|
|
|
|
|
if (isRunning) {
|
|
|
|
return (
|
|
|
|
<div className={styles.runWrapper}>
|
|
|
|
<Button icon="fa fa-spinner" type="button" variant="destructive" onClick={this.onCancelQueries}>
|
|
|
|
Cancel
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={styles.runWrapper}>
|
|
|
|
<Button icon="sync" type="button" onClick={this.onRunQueries}>
|
|
|
|
Run queries
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-12-11 07:49:14 -06:00
|
|
|
render() {
|
2021-04-21 06:57:17 -05:00
|
|
|
const { value = [] } = this.props;
|
2021-05-18 09:16:26 -05:00
|
|
|
const { panelDataByRefId } = this.state;
|
2021-05-04 09:31:25 -05:00
|
|
|
const styles = getStyles(config.theme2);
|
|
|
|
|
2020-12-11 07:49:14 -06:00
|
|
|
return (
|
2021-04-21 06:57:17 -05:00
|
|
|
<div className={styles.container}>
|
|
|
|
<AlertingQueryRows
|
2021-05-18 09:16:26 -05:00
|
|
|
data={panelDataByRefId}
|
2021-04-21 06:57:17 -05:00
|
|
|
queries={value}
|
|
|
|
onQueriesChange={this.props.onChange}
|
|
|
|
onDuplicateQuery={this.onDuplicateQuery}
|
|
|
|
onRunQueries={this.onRunQueries}
|
|
|
|
/>
|
|
|
|
{this.renderAddQueryRow(styles)}
|
2021-05-04 09:31:25 -05:00
|
|
|
{this.renderRunQueryButton()}
|
2020-12-11 07:49:14 -06:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-29 08:10:14 -05:00
|
|
|
const addQuery = (
|
|
|
|
queries: GrafanaQuery[],
|
|
|
|
queryToAdd: Pick<GrafanaQuery, 'model' | 'datasourceUid'>
|
|
|
|
): GrafanaQuery[] => {
|
2021-04-21 06:57:17 -05:00
|
|
|
const refId = getNextRefIdChar(queries);
|
|
|
|
|
|
|
|
const query: GrafanaQuery = {
|
2021-04-29 08:10:14 -05:00
|
|
|
...queryToAdd,
|
2021-04-21 06:57:17 -05:00
|
|
|
refId,
|
|
|
|
queryType: '',
|
|
|
|
model: {
|
2021-04-29 08:10:14 -05:00
|
|
|
...queryToAdd.model,
|
2021-04-21 06:57:17 -05:00
|
|
|
hide: false,
|
2021-05-04 09:31:25 -05:00
|
|
|
refId,
|
2021-04-21 06:57:17 -05:00
|
|
|
},
|
2021-05-04 09:31:25 -05:00
|
|
|
relativeTimeRange: defaultTimeRange(queryToAdd.model),
|
2021-04-21 06:57:17 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
return [...queries, query];
|
|
|
|
};
|
2020-12-11 07:49:14 -06:00
|
|
|
|
2021-05-04 09:31:25 -05:00
|
|
|
const defaultTimeRange = (model: DataQuery): RelativeTimeRange | undefined => {
|
|
|
|
if (isExpressionQuery(model)) {
|
|
|
|
return;
|
|
|
|
}
|
2021-05-18 09:16:26 -05:00
|
|
|
|
2021-05-04 09:31:25 -05:00
|
|
|
return getDefaultRelativeTimeRange();
|
|
|
|
};
|
|
|
|
|
|
|
|
const getStyles = stylesFactory((theme: GrafanaTheme2) => {
|
2020-12-11 07:49:14 -06:00
|
|
|
return {
|
|
|
|
container: css`
|
2021-05-04 09:31:25 -05:00
|
|
|
background-color: ${theme.colors.background.primary};
|
2020-12-17 09:11:12 -06:00
|
|
|
height: 100%;
|
2020-12-11 07:49:14 -06:00
|
|
|
`,
|
2021-05-04 09:31:25 -05:00
|
|
|
runWrapper: css`
|
|
|
|
margin-top: ${theme.spacing(1)};
|
2021-01-19 07:04:54 -06:00
|
|
|
`,
|
2020-12-11 07:49:14 -06:00
|
|
|
editorWrapper: css`
|
2021-05-04 09:31:25 -05:00
|
|
|
border: 1px solid ${theme.colors.border.medium};
|
|
|
|
border-radius: ${theme.shape.borderRadius()};
|
2020-12-11 07:49:14 -06:00
|
|
|
`,
|
2021-04-21 06:57:17 -05:00
|
|
|
expressionButton: css`
|
2021-05-04 09:31:25 -05:00
|
|
|
margin-right: ${theme.spacing(0.5)};
|
2021-04-21 06:57:17 -05:00
|
|
|
`,
|
2020-12-11 07:49:14 -06:00
|
|
|
};
|
|
|
|
});
|