2020-03-10 08:53:41 +01:00
|
|
|
import {
|
|
|
|
|
assignModelProperties,
|
|
|
|
|
DataSourceVariableModel,
|
|
|
|
|
VariableActions,
|
|
|
|
|
VariableHide,
|
|
|
|
|
VariableOption,
|
|
|
|
|
VariableRefresh,
|
|
|
|
|
variableTypes,
|
2020-03-24 05:46:31 +01:00
|
|
|
} from './types';
|
2020-04-05 21:44:49 -07:00
|
|
|
import { VariableType, stringToJsRegex } from '@grafana/data';
|
2019-07-16 11:35:42 +02:00
|
|
|
import { VariableSrv } from './variable_srv';
|
|
|
|
|
import { TemplateSrv } from './template_srv';
|
|
|
|
|
import { DatasourceSrv } from '../plugins/datasource_srv';
|
2020-01-14 22:51:25 -08:00
|
|
|
import { config } from '@grafana/runtime';
|
2020-03-24 05:46:31 +01:00
|
|
|
import { containsVariable } from './utils';
|
2016-09-16 16:50:30 +02:00
|
|
|
|
2020-03-10 08:53:41 +01:00
|
|
|
export class DatasourceVariable implements DataSourceVariableModel, VariableActions {
|
|
|
|
|
type: VariableType;
|
|
|
|
|
name: string;
|
|
|
|
|
label: string;
|
|
|
|
|
hide: VariableHide;
|
2016-09-16 16:50:30 +02:00
|
|
|
regex: any;
|
|
|
|
|
query: string;
|
2020-03-10 08:53:41 +01:00
|
|
|
options: VariableOption[];
|
|
|
|
|
current: VariableOption;
|
2019-03-05 15:51:16 -06:00
|
|
|
multi: boolean;
|
|
|
|
|
includeAll: boolean;
|
2020-03-10 08:53:41 +01:00
|
|
|
refresh: VariableRefresh;
|
2018-07-11 19:06:36 +02:00
|
|
|
skipUrlSync: boolean;
|
2016-09-16 16:50:30 +02:00
|
|
|
|
2020-03-10 08:53:41 +01:00
|
|
|
defaults: DataSourceVariableModel = {
|
2017-12-20 12:33:33 +01:00
|
|
|
type: 'datasource',
|
|
|
|
|
name: '',
|
2016-09-19 17:03:29 +02:00
|
|
|
hide: 0,
|
2017-12-20 12:33:33 +01:00
|
|
|
label: '',
|
2020-03-10 08:53:41 +01:00
|
|
|
current: {} as VariableOption,
|
2017-12-20 12:33:33 +01:00
|
|
|
regex: '',
|
2016-09-19 17:03:29 +02:00
|
|
|
options: [],
|
2017-12-20 12:33:33 +01:00
|
|
|
query: '',
|
2019-03-05 15:51:16 -06:00
|
|
|
multi: false,
|
|
|
|
|
includeAll: false,
|
2017-12-20 12:33:33 +01:00
|
|
|
refresh: 1,
|
2018-07-11 19:06:36 +02:00
|
|
|
skipUrlSync: false,
|
2016-09-19 17:03:29 +02:00
|
|
|
};
|
|
|
|
|
|
2018-08-31 16:40:43 +02:00
|
|
|
/** @ngInject */
|
2019-07-16 11:35:42 +02:00
|
|
|
constructor(
|
|
|
|
|
private model: any,
|
|
|
|
|
private datasourceSrv: DatasourceSrv,
|
|
|
|
|
private variableSrv: VariableSrv,
|
|
|
|
|
private templateSrv: TemplateSrv
|
|
|
|
|
) {
|
2016-09-19 17:03:29 +02:00
|
|
|
assignModelProperties(this, model, this.defaults);
|
2016-09-28 12:12:38 +02:00
|
|
|
this.refresh = 1;
|
2016-09-19 17:03:29 +02:00
|
|
|
}
|
|
|
|
|
|
2016-11-17 11:28:33 +01:00
|
|
|
getSaveModel() {
|
2016-09-19 17:03:29 +02:00
|
|
|
assignModelProperties(this.model, this, this.defaults);
|
2016-11-17 11:38:06 +01:00
|
|
|
|
2018-04-13 19:48:37 +02:00
|
|
|
// don't persist options
|
2016-11-17 12:32:11 +01:00
|
|
|
this.model.options = [];
|
2016-09-19 17:03:29 +02:00
|
|
|
return this.model;
|
2016-09-16 16:50:30 +02:00
|
|
|
}
|
|
|
|
|
|
2019-07-16 11:35:42 +02:00
|
|
|
setValue(option: any) {
|
2016-09-19 18:06:36 +02:00
|
|
|
return this.variableSrv.setOptionAsCurrent(this, option);
|
2016-09-16 16:50:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateOptions() {
|
2020-03-10 08:53:41 +01:00
|
|
|
const options: VariableOption[] = [];
|
2018-08-26 21:52:57 +02:00
|
|
|
const sources = this.datasourceSrv.getMetricSources({ skipVariables: true });
|
2018-08-30 08:58:43 +02:00
|
|
|
let regex;
|
2016-09-16 16:50:30 +02:00
|
|
|
|
|
|
|
|
if (this.regex) {
|
2020-03-10 08:53:41 +01:00
|
|
|
regex = this.templateSrv.replace(this.regex, undefined, 'regex');
|
2019-03-09 23:50:46 -08:00
|
|
|
regex = stringToJsRegex(regex);
|
2016-09-16 16:50:30 +02:00
|
|
|
}
|
|
|
|
|
|
2018-08-30 08:58:43 +02:00
|
|
|
for (let i = 0; i < sources.length; i++) {
|
2018-08-26 21:52:57 +02:00
|
|
|
const source = sources[i];
|
2016-09-16 16:50:30 +02:00
|
|
|
// must match on type
|
|
|
|
|
if (source.meta.id !== this.query) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (regex && !regex.exec(source.name)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-10 08:53:41 +01:00
|
|
|
options.push({ text: source.name, value: source.name, selected: false });
|
2016-09-16 16:50:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (options.length === 0) {
|
2020-03-10 08:53:41 +01:00
|
|
|
options.push({ text: 'No data sources found', value: '', selected: false });
|
2016-09-16 16:50:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.options = options;
|
2019-03-05 15:51:16 -06:00
|
|
|
if (this.includeAll) {
|
|
|
|
|
this.addAllOption();
|
|
|
|
|
}
|
2020-01-14 22:51:25 -08:00
|
|
|
const { defaultDatasource } = config.bootData.settings;
|
|
|
|
|
return this.variableSrv.validateVariableSelectionState(this, defaultDatasource);
|
2016-09-16 16:50:30 +02:00
|
|
|
}
|
|
|
|
|
|
2019-03-05 15:51:16 -06:00
|
|
|
addAllOption() {
|
2020-03-10 08:53:41 +01:00
|
|
|
this.options.unshift({ text: 'All', value: '$__all', selected: false });
|
2019-03-05 15:51:16 -06:00
|
|
|
}
|
|
|
|
|
|
2019-07-16 11:35:42 +02:00
|
|
|
dependsOn(variable: any) {
|
2017-03-06 23:39:19 -08:00
|
|
|
if (this.regex) {
|
|
|
|
|
return containsVariable(this.regex, variable.name);
|
|
|
|
|
}
|
2016-09-16 16:50:30 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
2016-09-17 11:28:45 +02:00
|
|
|
|
2019-07-16 11:35:42 +02:00
|
|
|
setValueFromUrl(urlValue: string | string[]) {
|
2016-09-17 11:28:45 +02:00
|
|
|
return this.variableSrv.setOptionFromUrl(this, urlValue);
|
|
|
|
|
}
|
2016-09-20 11:57:43 +02:00
|
|
|
|
|
|
|
|
getValueForUrl() {
|
2019-03-05 15:51:16 -06:00
|
|
|
if (this.current.text === 'All') {
|
|
|
|
|
return 'All';
|
|
|
|
|
}
|
2016-09-20 11:57:43 +02:00
|
|
|
return this.current.value;
|
|
|
|
|
}
|
2016-09-16 16:50:30 +02:00
|
|
|
}
|
2019-07-18 08:03:04 +02:00
|
|
|
|
2017-12-20 12:33:33 +01:00
|
|
|
variableTypes['datasource'] = {
|
|
|
|
|
name: 'Datasource',
|
2016-09-19 18:32:09 +02:00
|
|
|
ctor: DatasourceVariable,
|
2019-03-05 15:51:16 -06:00
|
|
|
supportsMulti: true,
|
2017-12-21 08:39:31 +01:00
|
|
|
description: 'Enabled you to dynamically switch the datasource for multiple panels',
|
2016-09-19 18:32:09 +02:00
|
|
|
};
|