Templating: use dashboard timerange when variables are set to refresh 'On Dashboard Load' (#31721)

* Templating: use dashboard timerange when variables are set to load 'On Dashboard Load'

* Add test
This commit is contained in:
Giordano Ricci 2021-03-09 09:20:21 +00:00 committed by GitHub
parent f951672e90
commit 7e0b1f2619
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 3 deletions

View File

@ -418,7 +418,7 @@ function createMultiVariable(extend?: Partial<QueryVariableModel>): QueryVariabl
tagsQuery: 'tags-query',
tagValuesQuery: '',
useTags: true,
refresh: VariableRefresh.onDashboardLoad,
refresh: VariableRefresh.never,
regex: '',
multi: true,
includeAll: true,

View File

@ -144,7 +144,7 @@ const fetchTagValues = (tagText: string): ThunkResult<Promise<string[]>> => {
};
const getTimeRange = (variable: QueryVariableModel) => {
if (variable.refresh === VariableRefresh.onTimeRangeChanged) {
if (variable.refresh === VariableRefresh.onTimeRangeChanged || variable.refresh === VariableRefresh.onDashboardLoad) {
return getTimeSrv().timeRange();
}
return undefined;

View File

@ -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',

View File

@ -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();
}