From 1616ea14f0e18025b09e4330bf5008438fafc04d Mon Sep 17 00:00:00 2001 From: "Grot (@grafanabot)" <43478413+grafanabot@users.noreply.github.com> Date: Thu, 11 Mar 2021 09:10:20 +0000 Subject: [PATCH] Templating: use dashboard timerange when variables are set to refresh 'On Dashboard Load' (#31721) (#31801) * Templating: use dashboard timerange when variables are set to load 'On Dashboard Load' * Add test (cherry picked from commit 7e0b1f2619fe5db1f7f70938a036c0f350d75f8a) Co-authored-by: Giordano Ricci --- .../pickers/OptionsPicker/actions.test.ts | 2 +- .../pickers/OptionsPicker/actions.ts | 2 +- .../variables/query/queryRunners.test.ts | 35 +++++++++++++++++++ public/app/features/variables/utils.ts | 2 +- 4 files changed, 38 insertions(+), 3 deletions(-) diff --git a/public/app/features/variables/pickers/OptionsPicker/actions.test.ts b/public/app/features/variables/pickers/OptionsPicker/actions.test.ts index d1f51c6ee23..b4417488024 100644 --- a/public/app/features/variables/pickers/OptionsPicker/actions.test.ts +++ b/public/app/features/variables/pickers/OptionsPicker/actions.test.ts @@ -418,7 +418,7 @@ function createMultiVariable(extend?: Partial): QueryVariabl tagsQuery: 'tags-query', tagValuesQuery: '', useTags: true, - refresh: VariableRefresh.onDashboardLoad, + refresh: VariableRefresh.never, regex: '', multi: true, includeAll: true, diff --git a/public/app/features/variables/pickers/OptionsPicker/actions.ts b/public/app/features/variables/pickers/OptionsPicker/actions.ts index 42c89affba7..585c570ab66 100644 --- a/public/app/features/variables/pickers/OptionsPicker/actions.ts +++ b/public/app/features/variables/pickers/OptionsPicker/actions.ts @@ -144,7 +144,7 @@ const fetchTagValues = (tagText: string): ThunkResult> => { }; const getTimeRange = (variable: QueryVariableModel) => { - if (variable.refresh === VariableRefresh.onTimeRangeChanged) { + if (variable.refresh === VariableRefresh.onTimeRangeChanged || variable.refresh === VariableRefresh.onDashboardLoad) { return getTimeSrv().timeRange(); } return undefined; diff --git a/public/app/features/variables/query/queryRunners.test.ts b/public/app/features/variables/query/queryRunners.test.ts index a31621b151a..bef2104d212 100644 --- a/public/app/features/variables/query/queryRunners.test.ts +++ b/public/app/features/variables/query/queryRunners.test.ts @@ -70,6 +70,41 @@ describe('QueryRunners', () => { }); }); + describe('and calling runRequest with a variable that refreshes on dashboard load', () => { + const { datasource, runner, runnerArgs, request, timeSrv, defaultTimeRange } = getLegacyTestContext({ + query: 'A query', + refresh: VariableRefresh.onDashboardLoad, + }); + const observable = runner.runRequest(runnerArgs, request); + + it('then it should return correct observable', async () => { + await expect(observable).toEmitValuesWith((received) => { + const value = received[0]; + expect(value).toEqual({ + series: [{ text: 'A', value: 'A' }], + state: 'Done', + timeRange: defaultTimeRange, + }); + }); + }); + + it('and it should call timeSrv.timeRange()', () => { + expect(timeSrv.timeRange).toHaveBeenCalledTimes(1); + }); + + it('and it should call metricFindQuery with correct options', () => { + expect(datasource.metricFindQuery).toHaveBeenCalledTimes(1); + expect(datasource.metricFindQuery).toHaveBeenCalledWith('A query', { + range: defaultTimeRange, + searchFilter: 'A searchFilter', + variable: { + query: 'A query', + refresh: VariableRefresh.onDashboardLoad, + }, + }); + }); + }); + describe('and calling runRequest with a variable that does not refresh when time range changes', () => { const { datasource, runner, runnerArgs, request, timeSrv } = getLegacyTestContext({ query: 'A query', diff --git a/public/app/features/variables/utils.ts b/public/app/features/variables/utils.ts index 76dd337073a..25eef231040 100644 --- a/public/app/features/variables/utils.ts +++ b/public/app/features/variables/utils.ts @@ -137,7 +137,7 @@ export function getTemplatedRegex(variable: QueryVariableModel, templateSrv = ge export function getLegacyQueryOptions(variable: QueryVariableModel, searchFilter?: string, timeSrv = getTimeSrv()) { const queryOptions: any = { range: undefined, variable, searchFilter }; - if (variable.refresh === VariableRefresh.onTimeRangeChanged) { + if (variable.refresh === VariableRefresh.onTimeRangeChanged || variable.refresh === VariableRefresh.onDashboardLoad) { queryOptions.range = timeSrv.timeRange(); }