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