mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Variables: Fixes so queries work for numbers values too (#30602)
* Variables: Fixes so queries work for numbers values too * Chore: refactor after PR comments
This commit is contained in:
@@ -277,10 +277,19 @@ describe('operators', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('areMetricFindValues', () => {
|
describe('areMetricFindValues', () => {
|
||||||
|
const frame = toDataFrame({
|
||||||
|
fields: [{ name: 'text', type: FieldType.number, values: [1] }],
|
||||||
|
});
|
||||||
|
|
||||||
it.each`
|
it.each`
|
||||||
values | expected
|
values | expected
|
||||||
${null} | ${false}
|
${null} | ${false}
|
||||||
${undefined} | ${false}
|
${undefined} | ${false}
|
||||||
|
${[frame]} | ${false}
|
||||||
|
${[{ text: () => {} }]} | ${false}
|
||||||
|
${[{ text: { foo: 1 } }]} | ${false}
|
||||||
|
${[{ text: Symbol('foo') }]} | ${false}
|
||||||
|
${[{ text: true }]} | ${false}
|
||||||
${[]} | ${true}
|
${[]} | ${true}
|
||||||
${[{ text: '' }]} | ${true}
|
${[{ text: '' }]} | ${true}
|
||||||
${[{ Text: '' }]} | ${true}
|
${[{ Text: '' }]} | ${true}
|
||||||
@@ -288,6 +297,12 @@ describe('areMetricFindValues', () => {
|
|||||||
${[{ Value: '' }]} | ${true}
|
${[{ Value: '' }]} | ${true}
|
||||||
${[{ text: '', value: '' }]} | ${true}
|
${[{ text: '', 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 }) => {
|
`('when called with values:$values', ({ values, expected }) => {
|
||||||
expect(areMetricFindValues(values)).toBe(expected);
|
expect(areMetricFindValues(values)).toBe(expected);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -5,13 +5,11 @@ import { QueryVariableModel } from '../types';
|
|||||||
import { ThunkDispatch } from '../../../types';
|
import { ThunkDispatch } from '../../../types';
|
||||||
import { toVariableIdentifier, toVariablePayload } from '../state/types';
|
import { toVariableIdentifier, toVariablePayload } from '../state/types';
|
||||||
import { validateVariableSelectionState } from '../state/actions';
|
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 { updateVariableOptions, updateVariableTags } from './reducer';
|
||||||
import { getTimeSrv, TimeSrv } from '../../dashboard/services/TimeSrv';
|
import { getTimeSrv, TimeSrv } from '../../dashboard/services/TimeSrv';
|
||||||
import { getLegacyQueryOptions, getTemplatedRegex } from '../utils';
|
import { getLegacyQueryOptions, getTemplatedRegex } from '../utils';
|
||||||
|
|
||||||
const metricFindValueProps = ['text', 'Text', 'value', 'Value'];
|
|
||||||
|
|
||||||
export function toMetricFindValues(): OperatorFunction<PanelData, MetricFindValue[]> {
|
export function toMetricFindValues(): OperatorFunction<PanelData, MetricFindValue[]> {
|
||||||
return (source) =>
|
return (source) =>
|
||||||
source.pipe(
|
source.pipe(
|
||||||
@@ -183,5 +181,25 @@ export function areMetricFindValues(data: any[]): data is MetricFindValue[] {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const firstValue: any = data[0];
|
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;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user