2020-03-10 02:53:41 -05:00
|
|
|
import {
|
|
|
|
assignModelProperties,
|
|
|
|
DataSourceVariableModel,
|
|
|
|
VariableActions,
|
|
|
|
VariableHide,
|
|
|
|
VariableOption,
|
|
|
|
VariableRefresh,
|
|
|
|
variableTypes,
|
2020-03-23 23:46:31 -05:00
|
|
|
} from './types';
|
2020-04-05 23:44:49 -05:00
|
|
|
import { VariableType, stringToJsRegex } from '@grafana/data';
|
2019-07-16 04:35:42 -05:00
|
|
|
import { VariableSrv } from './variable_srv';
|
|
|
|
import { TemplateSrv } from './template_srv';
|
|
|
|
import { DatasourceSrv } from '../plugins/datasource_srv';
|
2020-01-15 00:51:25 -06:00
|
|
|
import { config } from '@grafana/runtime';
|
2020-03-23 23:46:31 -05:00
|
|
|
import { containsVariable } from './utils';
|
2016-09-16 09:50:30 -05:00
|
|
|
|
2020-03-10 02:53:41 -05:00
|
|
|
export class DatasourceVariable implements DataSourceVariableModel, VariableActions {
|
|
|
|
type: VariableType;
|
|
|
|
name: string;
|
|
|
|
label: string;
|
|
|
|
hide: VariableHide;
|
2016-09-16 09:50:30 -05:00
|
|
|
regex: any;
|
|
|
|
query: string;
|
2020-03-10 02:53:41 -05:00
|
|
|
options: VariableOption[];
|
|
|
|
current: VariableOption;
|
2019-03-05 15:51:16 -06:00
|
|
|
multi: boolean;
|
|
|
|
includeAll: boolean;
|
2020-03-10 02:53:41 -05:00
|
|
|
refresh: VariableRefresh;
|
2018-07-11 12:06:36 -05:00
|
|
|
skipUrlSync: boolean;
|
2016-09-16 09:50:30 -05:00
|
|
|
|
2020-03-10 02:53:41 -05:00
|
|
|
defaults: DataSourceVariableModel = {
|
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: '',
|
2020-03-10 02:53:41 -05:00
|
|
|
current: {} as VariableOption,
|
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 */
|
2019-07-16 04:35:42 -05:00
|
|
|
constructor(
|
|
|
|
private model: any,
|
|
|
|
private datasourceSrv: DatasourceSrv,
|
|
|
|
private variableSrv: VariableSrv,
|
|
|
|
private templateSrv: 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
|
|
|
}
|
|
|
|
|
2019-07-16 04:35:42 -05:00
|
|
|
setValue(option: any) {
|
2016-09-19 11:06:36 -05:00
|
|
|
return this.variableSrv.setOptionAsCurrent(this, option);
|
2016-09-16 09:50:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
updateOptions() {
|
2020-03-10 02:53:41 -05:00
|
|
|
const options: VariableOption[] = [];
|
2018-08-26 14:52:57 -05:00
|
|
|
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) {
|
2020-03-10 02:53:41 -05:00
|
|
|
regex = this.templateSrv.replace(this.regex, undefined, '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;
|
|
|
|
}
|
|
|
|
|
2020-03-10 02:53:41 -05:00
|
|
|
options.push({ text: source.name, value: source.name, selected: false });
|
2016-09-16 09:50:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (options.length === 0) {
|
2020-03-10 02:53:41 -05:00
|
|
|
options.push({ text: 'No data sources found', value: '', selected: false });
|
2016-09-16 09:50:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
this.options = options;
|
2019-03-05 15:51:16 -06:00
|
|
|
if (this.includeAll) {
|
|
|
|
this.addAllOption();
|
|
|
|
}
|
2020-01-15 00:51:25 -06:00
|
|
|
const { defaultDatasource } = config.bootData.settings;
|
|
|
|
return this.variableSrv.validateVariableSelectionState(this, defaultDatasource);
|
2016-09-16 09:50:30 -05:00
|
|
|
}
|
|
|
|
|
2019-03-05 15:51:16 -06:00
|
|
|
addAllOption() {
|
2020-03-10 02:53:41 -05:00
|
|
|
this.options.unshift({ text: 'All', value: '$__all', selected: false });
|
2019-03-05 15:51:16 -06:00
|
|
|
}
|
|
|
|
|
2019-07-16 04:35:42 -05:00
|
|
|
dependsOn(variable: any) {
|
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
|
|
|
|
2019-07-16 04:35:42 -05:00
|
|
|
setValueFromUrl(urlValue: string | string[]) {
|
2016-09-17 04:28:45 -05:00
|
|
|
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
|
|
|
}
|
2019-07-18 01:03:04 -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
|
|
|
};
|