2016-09-16 16:50:30 +02:00
|
|
|
///<reference path="../../headers/common.d.ts" />
|
|
|
|
|
|
|
|
|
|
import kbn from 'app/core/utils/kbn';
|
2017-03-06 23:39:19 -08:00
|
|
|
import {Variable, containsVariable, assignModelProperties, variableTypes} from './variable';
|
2016-09-16 16:50:30 +02:00
|
|
|
|
|
|
|
|
export class DatasourceVariable implements Variable {
|
|
|
|
|
regex: any;
|
|
|
|
|
query: string;
|
|
|
|
|
options: any;
|
2016-09-20 11:57:43 +02:00
|
|
|
current: any;
|
2016-09-28 12:12:38 +02:00
|
|
|
refresh: any;
|
2016-09-16 16:50:30 +02:00
|
|
|
|
2016-09-19 17:03:29 +02:00
|
|
|
defaults = {
|
|
|
|
|
type: 'datasource',
|
|
|
|
|
name: '',
|
|
|
|
|
hide: 0,
|
|
|
|
|
label: '',
|
2016-09-20 11:57:43 +02:00
|
|
|
current: {},
|
2016-09-19 17:03:29 +02:00
|
|
|
regex: '',
|
|
|
|
|
options: [],
|
|
|
|
|
query: '',
|
2016-09-28 12:12:38 +02:00
|
|
|
refresh: 1,
|
2016-09-19 17:03:29 +02:00
|
|
|
};
|
|
|
|
|
|
2016-09-22 10:22:16 +02:00
|
|
|
/** @ngInject **/
|
2017-03-06 23:39:19 -08:00
|
|
|
constructor(private model, private datasourceSrv, private variableSrv, private 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
|
|
|
|
|
|
|
|
// dont 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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setValue(option) {
|
2016-09-19 18:06:36 +02:00
|
|
|
return this.variableSrv.setOptionAsCurrent(this, option);
|
2016-09-16 16:50:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateOptions() {
|
|
|
|
|
var options = [];
|
|
|
|
|
var sources = this.datasourceSrv.getMetricSources({skipVariables: true});
|
|
|
|
|
var regex;
|
|
|
|
|
|
|
|
|
|
if (this.regex) {
|
2017-03-06 23:39:19 -08:00
|
|
|
regex = this.templateSrv.replace(this.regex, null, 'regex');
|
|
|
|
|
regex = kbn.stringToJsRegex(regex);
|
2016-09-16 16:50:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < sources.length; i++) {
|
|
|
|
|
var source = sources[i];
|
|
|
|
|
// must match on type
|
|
|
|
|
if (source.meta.id !== this.query) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (regex && !regex.exec(source.name)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
options.push({text: source.name, value: source.name});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (options.length === 0) {
|
|
|
|
|
options.push({text: 'No data sources found', value: ''});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.options = options;
|
2016-09-19 18:06:36 +02:00
|
|
|
return this.variableSrv.validateVariableSelectionState(this);
|
2016-09-16 16:50:30 +02:00
|
|
|
}
|
|
|
|
|
|
2016-09-17 11:28:45 +02:00
|
|
|
dependsOn(variable) {
|
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
|
|
|
|
|
|
|
|
setValueFromUrl(urlValue) {
|
|
|
|
|
return this.variableSrv.setOptionFromUrl(this, urlValue);
|
|
|
|
|
}
|
2016-09-20 11:57:43 +02:00
|
|
|
|
|
|
|
|
getValueForUrl() {
|
|
|
|
|
return this.current.value;
|
|
|
|
|
}
|
2016-09-16 16:50:30 +02:00
|
|
|
}
|
|
|
|
|
|
2016-09-19 18:32:09 +02:00
|
|
|
variableTypes['datasource'] = {
|
|
|
|
|
name: 'Datasource',
|
|
|
|
|
ctor: DatasourceVariable,
|
|
|
|
|
description: 'Enabled you to dynamically switch the datasource for multiple panels',
|
|
|
|
|
};
|