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-04-21 06:57:17 -05:00
|
|
|
import { DataSourceApi, GrafanaTheme } from '@grafana/data';
|
|
|
|
import { selectors } from '@grafana/e2e-selectors';
|
|
|
|
import { Button, HorizontalGroup, Icon, stylesFactory, Tooltip } from '@grafana/ui';
|
|
|
|
import { config, getDataSourceSrv } from '@grafana/runtime';
|
|
|
|
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-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 {
|
|
|
|
defaultDataSource?: DataSourceApi;
|
2020-12-11 07:49:14 -06:00
|
|
|
}
|
2021-04-21 06:57:17 -05:00
|
|
|
export class AlertingQueryEditor extends PureComponent<Props, State> {
|
|
|
|
constructor(props: Props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {};
|
|
|
|
}
|
2020-12-11 07:49:14 -06:00
|
|
|
|
2021-04-21 06:57:17 -05:00
|
|
|
async componentDidMount() {
|
|
|
|
try {
|
|
|
|
const defaultDataSource = await getDataSourceSrv().get();
|
|
|
|
this.setState({ defaultDataSource });
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
}
|
|
|
|
}
|
2021-02-15 06:56:59 -06:00
|
|
|
|
2021-04-21 06:57:17 -05:00
|
|
|
onRunQueries = () => {};
|
2020-12-11 07:49:14 -06:00
|
|
|
|
2021-04-21 06:57:17 -05:00
|
|
|
onDuplicateQuery = (query: GrafanaQuery) => {
|
|
|
|
const { onChange, value = [] } = this.props;
|
|
|
|
onChange([...value, query]);
|
|
|
|
};
|
2021-02-15 06:56:59 -06:00
|
|
|
|
2021-04-21 06:57:17 -05:00
|
|
|
onNewAlertingQuery = () => {
|
|
|
|
const { onChange, value = [] } = this.props;
|
|
|
|
const { defaultDataSource } = this.state;
|
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>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-12-11 07:49:14 -06:00
|
|
|
render() {
|
2021-04-21 06:57:17 -05:00
|
|
|
const { value = [] } = this.props;
|
2020-12-11 07:49:14 -06:00
|
|
|
const styles = getStyles(config.theme);
|
|
|
|
return (
|
2021-04-21 06:57:17 -05:00
|
|
|
<div className={styles.container}>
|
|
|
|
<AlertingQueryRows
|
|
|
|
queries={value}
|
|
|
|
onQueriesChange={this.props.onChange}
|
|
|
|
onDuplicateQuery={this.onDuplicateQuery}
|
|
|
|
onRunQueries={this.onRunQueries}
|
|
|
|
/>
|
|
|
|
{this.renderAddQueryRow(styles)}
|
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,
|
|
|
|
refId: refId,
|
|
|
|
},
|
|
|
|
relativeTimeRange: {
|
|
|
|
from: 21600,
|
|
|
|
to: 0,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
return [...queries, query];
|
|
|
|
};
|
2020-12-11 07:49:14 -06:00
|
|
|
|
|
|
|
const getStyles = stylesFactory((theme: GrafanaTheme) => {
|
|
|
|
return {
|
|
|
|
container: css`
|
|
|
|
background-color: ${theme.colors.panelBg};
|
2020-12-17 09:11:12 -06:00
|
|
|
height: 100%;
|
2020-12-11 07:49:14 -06:00
|
|
|
`,
|
2021-01-19 07:04:54 -06:00
|
|
|
refreshWrapper: css`
|
|
|
|
display: flex;
|
|
|
|
justify-content: flex-end;
|
|
|
|
`,
|
2020-12-11 07:49:14 -06:00
|
|
|
editorWrapper: css`
|
|
|
|
border: 1px solid ${theme.colors.panelBorder};
|
|
|
|
border-radius: ${theme.border.radius.md};
|
|
|
|
`,
|
2021-04-21 06:57:17 -05:00
|
|
|
expressionButton: css`
|
|
|
|
margin-right: ${theme.spacing.sm};
|
|
|
|
`,
|
2020-12-11 07:49:14 -06:00
|
|
|
};
|
|
|
|
});
|