DashboardScene: Re-add support for default datasource var (#94806)

re-add support for default datasource var
This commit is contained in:
Victor Marin 2024-10-17 08:25:20 +03:00 committed by GitHub
parent e462974359
commit 96893aef2a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 47 additions and 0 deletions

View File

@ -251,6 +251,7 @@ describe('when creating variables objects', () => {
isMulti: true,
description: null,
hide: 0,
defaultOptionEnabled: false,
});
});
@ -673,6 +674,49 @@ describe('when creating variables objects', () => {
type: 'datasource',
value: '',
isMulti: true,
defaultOptionEnabled: false,
});
});
it('should handle datasource variable with default selected', () => {
// @ts-expect-error
const variable: TypedVariableModel = {
id: 'query1',
current: {
text: 'default',
value: 'default',
selected: true,
},
name: 'query1',
type: 'datasource',
global: false,
regex: '/^gdev/',
options: [],
query: 'prometheus',
multi: true,
includeAll: true,
refresh: 1,
allValue: 'Custom all',
};
const migrated = createSceneVariableFromVariableModel(variable);
const { key, ...rest } = migrated.state;
expect(migrated).toBeInstanceOf(DataSourceVariable);
expect(rest).toEqual({
allValue: 'Custom all',
defaultToAll: true,
includeAll: true,
label: undefined,
name: 'query1',
options: [],
pluginId: 'prometheus',
regex: '/^gdev/',
text: 'default',
type: 'datasource',
value: 'default',
isMulti: true,
defaultOptionEnabled: true,
});
});
});

View File

@ -18,6 +18,8 @@ import { SnapshotVariable } from '../serialization/custom-variables/SnapshotVari
import { getCurrentValueForOldIntervalModel, getIntervalsFromQueryString } from './utils';
const DEFAULT_DATASOURCE = 'default';
export function createVariablesForDashboard(oldModel: DashboardModel) {
const variableObjects = oldModel.templating.list
.map((v) => {
@ -189,6 +191,7 @@ export function createSceneVariableFromVariableModel(variable: TypedVariableMode
skipUrlSync: variable.skipUrlSync,
isMulti: variable.multi,
hide: variable.hide,
defaultOptionEnabled: variable.current?.value === DEFAULT_DATASOURCE && variable.current?.text === 'default',
});
} else if (variable.type === 'interval') {
const intervals = getIntervalsFromQueryString(variable.query);