Correlations: Show correct number of variables (#66191)

* Show correct number of variables

* Remove duplicated test
This commit is contained in:
Piotr Jamróz 2023-04-11 08:53:00 +02:00 committed by GitHub
parent 759a05083a
commit b302cc2297
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 1 deletions

View File

@ -671,6 +671,23 @@ describe('explore links utils', () => {
const dataLinkRtnVal = getVariableUsageInfo(dataLink, scopedVars).allVariablesDefined;
expect(dataLinkRtnVal).toBe(true);
});
it('returns deduplicated list of variables', () => {
const dataLink = {
url: '',
title: '',
internal: {
datasourceUid: 'uid',
datasourceName: 'dsName',
query: { query: 'test ${test} ${foo} ${test:raw} $test' },
},
};
const scopedVars = {
testVal: { text: '', value: 'val1' },
};
const variables = getVariableUsageInfo(dataLink, scopedVars).variables;
expect(variables).toHaveLength(2);
});
});
});

View File

@ -1,3 +1,4 @@
import { uniqBy } from 'lodash';
import { useCallback } from 'react';
import {
@ -260,9 +261,10 @@ export function getVariableUsageInfo<T extends DataLink>(
query: T,
scopedVars: ScopedVars
): { variables: VariableInterpolation[]; allVariablesDefined: boolean } {
const variables: VariableInterpolation[] = [];
let variables: VariableInterpolation[] = [];
const replaceFn = getTemplateSrv().replace.bind(getTemplateSrv());
replaceFn(getStringsFromObject(query), scopedVars, undefined, variables);
variables = uniqBy(variables, 'variableName');
return {
variables: variables,
allVariablesDefined: variables.every((variable) => variable.found),