From 9bdfb6ee136094e943a2d3232ed7d695403e17a4 Mon Sep 17 00:00:00 2001 From: ismail simsek Date: Tue, 21 May 2024 13:46:41 +0200 Subject: [PATCH] InfluxDB: Introduce custom variable support (#87903) * Introduce custom variable support * Remove comment lines * betterer --- .betterer.results | 6 - .../components/editor/query/QueryEditor.tsx | 2 +- .../editor/query/flux/FluxQueryEditor.tsx | 3 - .../editor/variable/VariableQueryEditor.tsx | 123 +++++++++--------- .../plugins/datasource/influxdb/datasource.ts | 2 + .../app/plugins/datasource/influxdb/module.ts | 2 - .../app/plugins/datasource/influxdb/types.ts | 4 + .../plugins/datasource/influxdb/variables.ts | 37 ++++++ 8 files changed, 109 insertions(+), 70 deletions(-) create mode 100644 public/app/plugins/datasource/influxdb/variables.ts diff --git a/.betterer.results b/.betterer.results index 540406092ed..27389d1d9cb 100644 --- a/.betterer.results +++ b/.betterer.results @@ -5555,12 +5555,6 @@ exports[`no gf-form usage`] = { "public/app/plugins/datasource/influxdb/components/editor/query/influxql/visual/TagsSection.tsx:5381": [ [0, 0, 0, "gf-form usage has been deprecated. Use a component from @grafana/ui or custom CSS instead.", "5381"] ], - "public/app/plugins/datasource/influxdb/components/editor/variable/VariableQueryEditor.tsx:5381": [ - [0, 0, 0, "gf-form usage has been deprecated. Use a component from @grafana/ui or custom CSS instead.", "5381"], - [0, 0, 0, "gf-form usage has been deprecated. Use a component from @grafana/ui or custom CSS instead.", "5381"], - [0, 0, 0, "gf-form usage has been deprecated. Use a component from @grafana/ui or custom CSS instead.", "5381"], - [0, 0, 0, "gf-form usage has been deprecated. Use a component from @grafana/ui or custom CSS instead.", "5381"] - ], "public/app/plugins/datasource/loki/components/LokiQueryField.tsx:5381": [ [0, 0, 0, "gf-form usage has been deprecated. Use a component from @grafana/ui or custom CSS instead.", "5381"], [0, 0, 0, "gf-form usage has been deprecated. Use a component from @grafana/ui or custom CSS instead.", "5381"], diff --git a/public/app/plugins/datasource/influxdb/components/editor/query/QueryEditor.tsx b/public/app/plugins/datasource/influxdb/components/editor/query/QueryEditor.tsx index 8c639067a2c..7976e9edf9c 100644 --- a/public/app/plugins/datasource/influxdb/components/editor/query/QueryEditor.tsx +++ b/public/app/plugins/datasource/influxdb/components/editor/query/QueryEditor.tsx @@ -20,7 +20,7 @@ export const QueryEditor = ({ query, onChange, onRunQuery, datasource }: Props) case InfluxVersion.Flux: return (
- +
); case InfluxVersion.SQL: diff --git a/public/app/plugins/datasource/influxdb/components/editor/query/flux/FluxQueryEditor.tsx b/public/app/plugins/datasource/influxdb/components/editor/query/flux/FluxQueryEditor.tsx index d6bdfd58689..56102fdac15 100644 --- a/public/app/plugins/datasource/influxdb/components/editor/query/flux/FluxQueryEditor.tsx +++ b/public/app/plugins/datasource/influxdb/components/editor/query/flux/FluxQueryEditor.tsx @@ -20,7 +20,6 @@ import { InfluxQuery } from '../../../../types'; interface Props extends Themeable2 { onChange: (query: InfluxQuery) => void; - onRunQuery: () => void; query: InfluxQuery; // `datasource` is not used internally, but this component is used at some places // directly, where the `datasource` prop has to exist. later, when the whole @@ -98,7 +97,6 @@ v1.tagValues( class UnthemedFluxQueryEditor extends PureComponent { onFluxQueryChange = (query: string) => { this.props.onChange({ ...this.props.query, query }); - this.props.onRunQuery(); }; onSampleChange = (val: SelectableValue) => { @@ -109,7 +107,6 @@ class UnthemedFluxQueryEditor extends PureComponent { // Angular HACK: Since the target does not actually change! this.forceUpdate(); - this.props.onRunQuery(); }; getSuggestions = (): CodeEditorSuggestionItem[] => { diff --git a/public/app/plugins/datasource/influxdb/components/editor/variable/VariableQueryEditor.tsx b/public/app/plugins/datasource/influxdb/components/editor/variable/VariableQueryEditor.tsx index e771e54a982..dd1bc42d73a 100644 --- a/public/app/plugins/datasource/influxdb/components/editor/variable/VariableQueryEditor.tsx +++ b/public/app/plugins/datasource/influxdb/components/editor/variable/VariableQueryEditor.tsx @@ -1,68 +1,75 @@ -import React, { PureComponent } from 'react'; +import React from 'react'; -import { Field, FieldSet, InlineFormLabel, TextArea } from '@grafana/ui'; +import { QueryEditorProps } from '@grafana/data'; +import { Field, FieldSet, InlineFieldRow, TextArea } from '@grafana/ui'; +import { InlineField } from '@grafana/ui/'; import InfluxDatasource from '../../../datasource'; -import { InfluxVersion } from '../../../types'; +import { InfluxOptions, InfluxQuery, InfluxVariableQuery, InfluxVersion } from '../../../types'; import { FluxQueryEditor } from '../query/flux/FluxQueryEditor'; -interface Props { - query: string; // before flux, it was always a string - onChange: (query?: string) => void; - datasource: InfluxDatasource; -} +export type Props = QueryEditorProps; -export default class VariableQueryEditor extends PureComponent { - onRefresh = () => { - // noop +const refId = 'InfluxVariableQueryEditor-VariableQuery'; + +const useVariableQuery = (query: InfluxVariableQuery | string): InfluxVariableQuery => { + // in legacy variable support query can be only a string + // in new variable support query can be an object and hold more information + // to be able to support old version we check the query here + if (typeof query === 'string') { + return { + refId, + query, + }; + } else { + return { + refId, + query: query.query ?? '', + }; + } +}; + +export const InfluxVariableEditor = ({ onChange, datasource, query }: Props) => { + const varQuery = useVariableQuery(query); + + const onChangeHandler = (q: InfluxQuery) => { + onChange({ refId, query: q.query || '' }); }; - render() { - let { query, datasource, onChange } = this.props; + const onBlurHandler = (e: React.FocusEvent) => { + onChange({ refId, query: e.currentTarget.value }); + }; - switch (datasource.version) { - case InfluxVersion.Flux: - return ( - onChange(v.query)} - /> - ); - case InfluxVersion.SQL: - return ( -
- -