2024-06-25 12:43:47 +01:00
|
|
|
import { useCallback, useEffect, useRef } from 'react';
|
2022-04-22 14:33:13 +01:00
|
|
|
|
2021-10-15 08:57:55 +02:00
|
|
|
import { DataSourceApi, QueryEditorProps, SelectableValue } from '@grafana/data';
|
2021-04-09 14:46:24 +02:00
|
|
|
import { InlineField, Select } from '@grafana/ui';
|
2022-04-22 14:33:13 +01:00
|
|
|
|
2021-04-09 14:46:24 +02:00
|
|
|
import { ClassicConditions } from './components/ClassicConditions';
|
2022-04-22 14:33:13 +01:00
|
|
|
import { Math } from './components/Math';
|
|
|
|
|
import { Reduce } from './components/Reduce';
|
|
|
|
|
import { Resample } from './components/Resample';
|
2024-02-27 16:16:00 -05:00
|
|
|
import { SqlExpr } from './components/SqlExpr';
|
2022-09-26 16:05:44 +02:00
|
|
|
import { Threshold } from './components/Threshold';
|
2023-06-27 11:35:56 +02:00
|
|
|
import { ExpressionQuery, ExpressionQueryType, expressionTypes } from './types';
|
2022-04-22 14:33:13 +01:00
|
|
|
import { getDefaults } from './utils/expressionTypes';
|
2019-10-30 11:38:28 -07:00
|
|
|
|
2021-10-15 08:57:55 +02:00
|
|
|
type Props = QueryEditorProps<DataSourceApi<ExpressionQuery>, ExpressionQuery>;
|
2019-10-30 11:38:28 -07:00
|
|
|
|
2023-07-04 18:04:47 +05:30
|
|
|
const labelWidth = 15;
|
2019-10-30 11:38:28 -07:00
|
|
|
|
2022-07-12 11:50:58 +02:00
|
|
|
type NonClassicExpressionType = Exclude<ExpressionQueryType, ExpressionQueryType.classic>;
|
|
|
|
|
type ExpressionTypeConfigStorage = Partial<Record<NonClassicExpressionType, string>>;
|
|
|
|
|
|
|
|
|
|
function useExpressionsCache() {
|
|
|
|
|
const expressionCache = useRef<ExpressionTypeConfigStorage>({});
|
|
|
|
|
|
|
|
|
|
const getCachedExpression = useCallback((queryType: ExpressionQueryType) => {
|
|
|
|
|
switch (queryType) {
|
|
|
|
|
case ExpressionQueryType.math:
|
|
|
|
|
case ExpressionQueryType.reduce:
|
|
|
|
|
case ExpressionQueryType.resample:
|
2022-09-26 16:05:44 +02:00
|
|
|
case ExpressionQueryType.threshold:
|
2024-02-27 16:16:00 -05:00
|
|
|
case ExpressionQueryType.sql:
|
2022-07-12 11:50:58 +02:00
|
|
|
return expressionCache.current[queryType];
|
|
|
|
|
case ExpressionQueryType.classic:
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const setCachedExpression = useCallback((queryType: ExpressionQueryType, value: string | undefined) => {
|
|
|
|
|
switch (queryType) {
|
|
|
|
|
case ExpressionQueryType.math:
|
|
|
|
|
expressionCache.current.math = value;
|
|
|
|
|
break;
|
2019-10-30 11:38:28 -07:00
|
|
|
|
2022-09-26 16:05:44 +02:00
|
|
|
// We want to use the same value for Reduce, Resample and Threshold
|
2022-07-12 11:50:58 +02:00
|
|
|
case ExpressionQueryType.reduce:
|
2022-09-26 16:05:44 +02:00
|
|
|
case ExpressionQueryType.resample:
|
2022-07-12 11:50:58 +02:00
|
|
|
case ExpressionQueryType.resample:
|
|
|
|
|
expressionCache.current.reduce = value;
|
|
|
|
|
expressionCache.current.resample = value;
|
2022-09-26 16:05:44 +02:00
|
|
|
expressionCache.current.threshold = value;
|
2022-07-12 11:50:58 +02:00
|
|
|
break;
|
2024-02-27 16:16:00 -05:00
|
|
|
case ExpressionQueryType.sql:
|
|
|
|
|
expressionCache.current.sql = value;
|
2022-07-12 11:50:58 +02:00
|
|
|
}
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
return { getCachedExpression, setCachedExpression };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function ExpressionQueryEditor(props: Props) {
|
|
|
|
|
const { query, queries, onRunQuery, onChange } = props;
|
|
|
|
|
const { getCachedExpression, setCachedExpression } = useExpressionsCache();
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
setCachedExpression(query.type, query.expression);
|
|
|
|
|
}, [query.expression, query.type, setCachedExpression]);
|
|
|
|
|
|
|
|
|
|
const onSelectExpressionType = useCallback(
|
|
|
|
|
(item: SelectableValue<ExpressionQueryType>) => {
|
|
|
|
|
const cachedExpression = getCachedExpression(item.value!);
|
|
|
|
|
const defaults = getDefaults({ ...query, type: item.value! });
|
|
|
|
|
|
|
|
|
|
onChange({ ...defaults, expression: cachedExpression ?? defaults.expression });
|
|
|
|
|
},
|
|
|
|
|
[query, onChange, getCachedExpression]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const renderExpressionType = () => {
|
2021-04-09 14:46:24 +02:00
|
|
|
const refIds = queries!.filter((q) => query.refId !== q.refId).map((q) => ({ value: q.refId, label: q.refId }));
|
2019-10-30 11:38:28 -07:00
|
|
|
|
2021-04-09 14:46:24 +02:00
|
|
|
switch (query.type) {
|
|
|
|
|
case ExpressionQueryType.math:
|
2022-06-06 11:51:23 +01:00
|
|
|
return <Math onChange={onChange} query={query} labelWidth={labelWidth} onRunQuery={onRunQuery} />;
|
2019-10-30 11:38:28 -07:00
|
|
|
|
2021-04-09 14:46:24 +02:00
|
|
|
case ExpressionQueryType.reduce:
|
|
|
|
|
return <Reduce refIds={refIds} onChange={onChange} labelWidth={labelWidth} query={query} />;
|
2020-12-17 13:56:42 +01:00
|
|
|
|
2021-04-09 14:46:24 +02:00
|
|
|
case ExpressionQueryType.resample:
|
|
|
|
|
return <Resample query={query} labelWidth={labelWidth} onChange={onChange} refIds={refIds} />;
|
2019-10-30 11:38:28 -07:00
|
|
|
|
2021-04-09 14:46:24 +02:00
|
|
|
case ExpressionQueryType.classic:
|
|
|
|
|
return <ClassicConditions onChange={onChange} query={query} refIds={refIds} />;
|
2022-09-26 16:05:44 +02:00
|
|
|
|
|
|
|
|
case ExpressionQueryType.threshold:
|
|
|
|
|
return <Threshold onChange={onChange} query={query} labelWidth={labelWidth} refIds={refIds} />;
|
2024-02-27 16:16:00 -05:00
|
|
|
|
|
|
|
|
case ExpressionQueryType.sql:
|
|
|
|
|
return <SqlExpr onChange={onChange} query={query} refIds={refIds} />;
|
2021-04-09 14:46:24 +02:00
|
|
|
}
|
2022-07-12 11:50:58 +02:00
|
|
|
};
|
|
|
|
|
|
2023-06-27 11:35:56 +02:00
|
|
|
const selected = expressionTypes.find((o) => o.value === query.type);
|
2022-07-12 11:50:58 +02:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div>
|
|
|
|
|
<InlineField label="Operation" labelWidth={labelWidth}>
|
2023-06-27 11:35:56 +02:00
|
|
|
<Select options={expressionTypes} value={selected} onChange={onSelectExpressionType} width={25} />
|
2022-07-12 11:50:58 +02:00
|
|
|
</InlineField>
|
|
|
|
|
{renderExpressionType()}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2019-10-30 11:38:28 -07:00
|
|
|
}
|