From fad81e169697b6cb45fc37dccd52dbdb8e4f71b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=A4ggmark?= Date: Tue, 26 Jan 2021 09:04:49 +0100 Subject: [PATCH] Variables: Fixes so queries work for numbers values too (#30602) * Variables: Fixes so queries work for numbers values too * Chore: refactor after PR comments --- .../variables/query/operators.test.ts | 15 +++++++++++ .../app/features/variables/query/operators.ts | 26 ++++++++++++++++--- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/public/app/features/variables/query/operators.test.ts b/public/app/features/variables/query/operators.test.ts index 03bc6b16c9e..c3b81000cc6 100644 --- a/public/app/features/variables/query/operators.test.ts +++ b/public/app/features/variables/query/operators.test.ts @@ -277,10 +277,19 @@ describe('operators', () => { }); describe('areMetricFindValues', () => { + const frame = toDataFrame({ + fields: [{ name: 'text', type: FieldType.number, values: [1] }], + }); + it.each` values | expected ${null} | ${false} ${undefined} | ${false} + ${[frame]} | ${false} + ${[{ text: () => {} }]} | ${false} + ${[{ text: { foo: 1 } }]} | ${false} + ${[{ text: Symbol('foo') }]} | ${false} + ${[{ text: true }]} | ${false} ${[]} | ${true} ${[{ text: '' }]} | ${true} ${[{ Text: '' }]} | ${true} @@ -288,6 +297,12 @@ describe('areMetricFindValues', () => { ${[{ Value: '' }]} | ${true} ${[{ text: '', value: '' }]} | ${true} ${[{ Text: '', Value: '' }]} | ${true} + ${[{ text: 1 }]} | ${true} + ${[{ Text: 1 }]} | ${true} + ${[{ value: 1 }]} | ${true} + ${[{ Value: 1 }]} | ${true} + ${[{ text: 1, value: 1 }]} | ${true} + ${[{ Text: 1, Value: 1 }]} | ${true} `('when called with values:$values', ({ values, expected }) => { expect(areMetricFindValues(values)).toBe(expected); }); diff --git a/public/app/features/variables/query/operators.ts b/public/app/features/variables/query/operators.ts index 798e1786f43..5aa6e75661a 100644 --- a/public/app/features/variables/query/operators.ts +++ b/public/app/features/variables/query/operators.ts @@ -5,13 +5,11 @@ import { QueryVariableModel } from '../types'; import { ThunkDispatch } from '../../../types'; import { toVariableIdentifier, toVariablePayload } from '../state/types'; import { validateVariableSelectionState } from '../state/actions'; -import { DataSourceApi, FieldType, getFieldDisplayName, MetricFindValue, PanelData } from '@grafana/data'; +import { DataSourceApi, FieldType, getFieldDisplayName, isDataFrame, MetricFindValue, PanelData } from '@grafana/data'; import { updateVariableOptions, updateVariableTags } from './reducer'; import { getTimeSrv, TimeSrv } from '../../dashboard/services/TimeSrv'; import { getLegacyQueryOptions, getTemplatedRegex } from '../utils'; -const metricFindValueProps = ['text', 'Text', 'value', 'Value']; - export function toMetricFindValues(): OperatorFunction { return (source) => source.pipe( @@ -183,5 +181,25 @@ export function areMetricFindValues(data: any[]): data is MetricFindValue[] { } const firstValue: any = data[0]; - return metricFindValueProps.some((prop) => firstValue.hasOwnProperty(prop) && typeof firstValue[prop] === 'string'); + if (isDataFrame(firstValue)) { + return false; + } + + for (const firstValueKey in firstValue) { + if (!firstValue.hasOwnProperty(firstValueKey)) { + continue; + } + + if (typeof firstValue[firstValueKey] !== 'string' && typeof firstValue[firstValueKey] !== 'number') { + continue; + } + + const key = firstValueKey.toLowerCase(); + + if (key === 'text' || key === 'value') { + return true; + } + } + + return false; }