influxdb: more robust query-has-variables check (#37493)

* influxdb: moved queryUtils file to better place

* influxdb: more robust query-has-variables check
This commit is contained in:
Gábor Farkas
2021-08-04 10:45:37 +02:00
committed by GitHub
parent aba7013ce5
commit 4da398014f
5 changed files with 10 additions and 32 deletions

View File

@@ -7,7 +7,7 @@ import { FluxQueryEditor } from './FluxQueryEditor';
import { RawInfluxQLEditor } from './RawInfluxQLEditor';
import { Editor as VisualInfluxQLEditor } from './VisualInfluxQLEditor/Editor';
import { QueryEditorModeSwitcher } from './QueryEditorModeSwitcher';
import { buildRawQuery } from './queryUtils';
import { buildRawQuery } from '../queryUtils';
type Props = QueryEditorProps<InfluxDatasource, InfluxQuery, InfluxOptions>;

View File

@@ -22,7 +22,7 @@ import {
removeGroupByPart,
changeSelectPart,
changeGroupByPart,
} from '../queryUtils';
} from '../../queryUtils';
import { FormatAsSection } from './FormatAsSection';
import { SectionLabel } from './SectionLabel';
import { SectionFill } from './SectionFill';

View File

@@ -31,6 +31,7 @@ import { getBackendSrv, DataSourceWithBackend, frameToMetricFindValue } from '@g
import { Observable, throwError, of } from 'rxjs';
import { FluxQueryEditor } from './components/FluxQueryEditor';
import { catchError, map } from 'rxjs/operators';
import { buildRawQuery } from './queryUtils';
// we detect the field type based on the value-array
function getFieldType(values: unknown[]): FieldType {
@@ -321,34 +322,11 @@ export default class InfluxDatasource extends DataSourceWithBackend<InfluxQuery,
}
targetContainsTemplate(target: any) {
if (this.isFlux) {
return this.templateSrv.variableExists(target.query);
}
// for flux-mode we just take target.query,
// for influxql-mode we use InfluxQueryModel to create the text-representation
const queryText = this.isFlux ? target.query : buildRawQuery(target);
// now we know it is an InfluxQL query.
// it can be either a raw-query or a not-raw-query
if (target.rawQuery) {
return this.templateSrv.variableExists(target.query);
}
// now we know it is an InfluxQL not-raw-query
for (const group of target.groupBy) {
for (const param of group.params) {
if (this.templateSrv.variableExists(param)) {
return true;
}
}
}
for (const i in target.tags) {
if (this.templateSrv.variableExists(target.tags[i].value)) {
return true;
}
}
return false;
return this.templateSrv.variableExists(queryText);
}
interpolateVariablesInQueries(queries: InfluxQuery[], scopedVars: ScopedVars): InfluxQuery[] {

View File

@@ -1,5 +1,5 @@
import { cloneDeep } from 'lodash';
import { InfluxQuery } from '../types';
import { InfluxQuery } from './types';
import { buildRawQuery, normalizeQuery, changeSelectPart, changeGroupByPart } from './queryUtils';
describe('InfluxDB query utils', () => {

View File

@@ -1,6 +1,6 @@
import { cloneDeep } from 'lodash';
import InfluxQueryModel from '../influx_query_model';
import { InfluxQuery } from '../types';
import InfluxQueryModel from './influx_query_model';
import { InfluxQuery } from './types';
// FIXME: these functions are a beginning of a refactoring of influx_query_model.ts
// into a simpler approach with full typescript types.