mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Templating: use default datasource when missing (#21495)
This commit is contained in:
parent
ba7d8c1a3d
commit
f6c94b9c40
@ -3,6 +3,7 @@ import { stringToJsRegex } from '@grafana/data';
|
||||
import { VariableSrv } from './variable_srv';
|
||||
import { TemplateSrv } from './template_srv';
|
||||
import { DatasourceSrv } from '../plugins/datasource_srv';
|
||||
import { config } from '@grafana/runtime';
|
||||
|
||||
export class DatasourceVariable implements VariableActions {
|
||||
regex: any;
|
||||
@ -84,7 +85,8 @@ export class DatasourceVariable implements VariableActions {
|
||||
if (this.includeAll) {
|
||||
this.addAllOption();
|
||||
}
|
||||
return this.variableSrv.validateVariableSelectionState(this);
|
||||
const { defaultDatasource } = config.bootData.settings;
|
||||
return this.variableSrv.validateVariableSelectionState(this, defaultDatasource);
|
||||
}
|
||||
|
||||
addAllOption() {
|
||||
|
@ -181,7 +181,7 @@ export class VariableSrv {
|
||||
return selected;
|
||||
}
|
||||
|
||||
validateVariableSelectionState(variable: any) {
|
||||
validateVariableSelectionState(variable: any, defaultValue?: string) {
|
||||
if (!variable.current) {
|
||||
variable.current = {};
|
||||
}
|
||||
@ -205,17 +205,33 @@ export class VariableSrv {
|
||||
|
||||
return variable.setValue(selected);
|
||||
} else {
|
||||
const currentOption: any = _.find(variable.options, {
|
||||
let option: any = undefined;
|
||||
|
||||
// 1. find the current value
|
||||
option = _.find(variable.options, {
|
||||
text: variable.current.text,
|
||||
});
|
||||
if (currentOption) {
|
||||
return variable.setValue(currentOption);
|
||||
} else {
|
||||
if (!variable.options.length) {
|
||||
return Promise.resolve();
|
||||
if (option) {
|
||||
return variable.setValue(option);
|
||||
}
|
||||
|
||||
// 2. find the default value
|
||||
if (defaultValue) {
|
||||
option = _.find(variable.options, {
|
||||
text: defaultValue,
|
||||
});
|
||||
if (option) {
|
||||
return variable.setValue(option);
|
||||
}
|
||||
}
|
||||
|
||||
// 3. use the first value
|
||||
if (variable.options) {
|
||||
return variable.setValue(variable.options[0]);
|
||||
}
|
||||
|
||||
// 4... give up
|
||||
return Promise.resolve();
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user