2017-12-20 05:33:33 -06:00
|
|
|
import _ from 'lodash';
|
2019-12-17 23:13:58 -06:00
|
|
|
import { AppEvents } from '@grafana/data';
|
|
|
|
import { e2e } from '@grafana/e2e';
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
import coreModule from 'app/core/core_module';
|
|
|
|
import { variableTypes } from './variable';
|
2018-01-25 07:15:40 -06:00
|
|
|
import appEvents from 'app/core/app_events';
|
2019-07-16 04:35:42 -05:00
|
|
|
import DatasourceSrv from '../plugins/datasource_srv';
|
|
|
|
import { VariableSrv } from './all';
|
|
|
|
import { TemplateSrv } from './template_srv';
|
2019-12-13 02:47:53 -06:00
|
|
|
import { promiseToDigest } from '../../core/utils/promiseToDigest';
|
2014-08-26 07:17:46 -05:00
|
|
|
|
2016-09-19 10:03:29 -05:00
|
|
|
export class VariableEditorCtrl {
|
2018-08-31 09:40:43 -05:00
|
|
|
/** @ngInject */
|
2019-07-16 04:35:42 -05:00
|
|
|
constructor($scope: any, datasourceSrv: DatasourceSrv, variableSrv: VariableSrv, templateSrv: TemplateSrv) {
|
2016-09-19 11:32:09 -05:00
|
|
|
$scope.variableTypes = variableTypes;
|
2016-09-20 10:34:38 -05:00
|
|
|
$scope.ctrl = {};
|
2017-05-19 04:14:11 -05:00
|
|
|
$scope.namePattern = /^(?!__).*$/;
|
2017-12-31 05:24:42 -06:00
|
|
|
$scope._ = _;
|
2018-04-23 06:00:24 -05:00
|
|
|
$scope.optionsLimit = 20;
|
2019-08-20 10:19:21 -05:00
|
|
|
$scope.emptyListCta = {
|
|
|
|
title: 'There are no variables yet',
|
|
|
|
buttonTitle: 'Add variable',
|
|
|
|
buttonIcon: 'gicon gicon-variable',
|
|
|
|
infoBox: {
|
|
|
|
__html: ` <p>
|
|
|
|
Variables enable more interactive and dynamic dashboards. Instead of hard-coding things like server or
|
|
|
|
sensor names in your metric queries you can use variables in their place. Variables are shown as dropdown
|
|
|
|
select boxes at the top of the dashboard. These dropdowns make it easy to change the data being displayed in
|
|
|
|
your dashboard. Check out the
|
|
|
|
<a class="external-link" href="http://docs.grafana.org/reference/templating/" target="_blank">
|
|
|
|
Templating documentation
|
|
|
|
</a>
|
|
|
|
for more information.
|
|
|
|
</p>`,
|
|
|
|
infoBoxTitle: 'What do variables do?',
|
|
|
|
},
|
|
|
|
};
|
2016-04-16 11:03:29 -05:00
|
|
|
|
2016-03-09 06:10:02 -06:00
|
|
|
$scope.refreshOptions = [
|
2017-12-20 05:33:33 -06:00
|
|
|
{ value: 0, text: 'Never' },
|
|
|
|
{ value: 1, text: 'On Dashboard Load' },
|
|
|
|
{ value: 2, text: 'On Time Range Change' },
|
2016-03-09 06:10:02 -06:00
|
|
|
];
|
|
|
|
|
2016-05-21 03:56:57 -05:00
|
|
|
$scope.sortOptions = [
|
2017-12-20 05:33:33 -06:00
|
|
|
{ value: 0, text: 'Disabled' },
|
|
|
|
{ value: 1, text: 'Alphabetical (asc)' },
|
|
|
|
{ value: 2, text: 'Alphabetical (desc)' },
|
|
|
|
{ value: 3, text: 'Numerical (asc)' },
|
|
|
|
{ value: 4, text: 'Numerical (desc)' },
|
2018-03-16 11:56:39 -05:00
|
|
|
{ value: 5, text: 'Alphabetical (case-insensitive, asc)' },
|
|
|
|
{ value: 6, text: 'Alphabetical (case-insensitive, desc)' },
|
2016-05-21 03:56:57 -05:00
|
|
|
];
|
|
|
|
|
2019-11-19 07:59:39 -06:00
|
|
|
$scope.hideOptions = [
|
|
|
|
{ value: 0, text: '' },
|
|
|
|
{ value: 1, text: 'Label' },
|
|
|
|
{ value: 2, text: 'Variable' },
|
|
|
|
];
|
2016-03-29 15:23:29 -05:00
|
|
|
|
2019-12-17 23:13:58 -06:00
|
|
|
$scope.selectors = {
|
|
|
|
...e2e.pages.Dashboard.Settings.Variables.List.selectors,
|
|
|
|
...e2e.pages.Dashboard.Settings.Variables.Edit.General.selectors,
|
|
|
|
...e2e.pages.Dashboard.Settings.Variables.Edit.QueryVariable.selectors,
|
|
|
|
...e2e.pages.Dashboard.Settings.Variables.Edit.ConstantVariable.selectors,
|
|
|
|
};
|
|
|
|
|
2018-09-05 00:47:30 -05:00
|
|
|
$scope.init = () => {
|
2017-12-20 05:33:33 -06:00
|
|
|
$scope.mode = 'list';
|
2015-09-08 08:54:08 -05:00
|
|
|
|
2016-09-15 07:53:36 -05:00
|
|
|
$scope.variables = variableSrv.variables;
|
2014-08-26 07:17:46 -05:00
|
|
|
$scope.reset();
|
2014-08-27 03:41:27 -05:00
|
|
|
|
2019-07-16 04:35:42 -05:00
|
|
|
$scope.$watch('mode', (val: string) => {
|
2017-12-20 05:33:33 -06:00
|
|
|
if (val === 'new') {
|
2014-08-27 08:54:30 -05:00
|
|
|
$scope.reset();
|
|
|
|
}
|
2014-08-27 03:41:27 -05:00
|
|
|
});
|
2014-08-26 07:17:46 -05:00
|
|
|
};
|
|
|
|
|
2019-07-16 04:35:42 -05:00
|
|
|
$scope.setMode = (mode: any) => {
|
2017-12-11 06:04:06 -06:00
|
|
|
$scope.mode = mode;
|
|
|
|
};
|
|
|
|
|
2019-08-20 10:19:21 -05:00
|
|
|
$scope.setNewMode = () => {
|
|
|
|
$scope.setMode('new');
|
|
|
|
};
|
|
|
|
|
2018-09-05 00:47:30 -05:00
|
|
|
$scope.add = () => {
|
2014-11-27 07:17:31 -06:00
|
|
|
if ($scope.isValid()) {
|
2017-08-02 09:15:22 -05:00
|
|
|
variableSrv.addVariable($scope.current);
|
2014-11-27 07:17:31 -06:00
|
|
|
$scope.update();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-09-05 00:47:30 -05:00
|
|
|
$scope.isValid = () => {
|
2016-09-20 10:34:38 -05:00
|
|
|
if (!$scope.ctrl.form.$valid) {
|
2017-09-26 04:24:58 -05:00
|
|
|
return false;
|
2014-11-27 07:17:31 -06:00
|
|
|
}
|
|
|
|
|
2014-11-27 07:22:55 -06:00
|
|
|
if (!$scope.current.name.match(/^\w+$/)) {
|
2019-10-14 03:27:47 -05:00
|
|
|
appEvents.emit(AppEvents.alertWarning, [
|
|
|
|
'Validation',
|
|
|
|
'Only word and digit characters are allowed in variable names',
|
|
|
|
]);
|
2014-11-27 07:22:55 -06:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-04-15 05:11:52 -05:00
|
|
|
const sameName: any = _.find($scope.variables, { name: $scope.current.name });
|
2014-11-27 07:17:31 -06:00
|
|
|
if (sameName && sameName !== $scope.current) {
|
2019-10-14 03:27:47 -05:00
|
|
|
appEvents.emit(AppEvents.alertWarning, ['Validation', 'Variable with the same name already exists']);
|
2014-11-27 07:17:31 -06:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
if (
|
2017-12-20 05:33:33 -06:00
|
|
|
$scope.current.type === 'query' &&
|
2018-10-26 07:03:05 -05:00
|
|
|
_.isString($scope.current.query) &&
|
2017-12-21 01:39:31 -06:00
|
|
|
$scope.current.query.match(new RegExp('\\$' + $scope.current.name + '(/| |$)'))
|
2017-12-19 09:06:54 -06:00
|
|
|
) {
|
2019-10-14 03:27:47 -05:00
|
|
|
appEvents.emit(AppEvents.alertWarning, [
|
2017-12-20 05:33:33 -06:00
|
|
|
'Validation',
|
2017-12-21 01:39:31 -06:00
|
|
|
'Query cannot contain a reference to itself. Variable: $' + $scope.current.name,
|
2017-12-19 09:06:54 -06:00
|
|
|
]);
|
2017-03-07 16:29:16 -06:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-11-27 07:17:31 -06:00
|
|
|
return true;
|
2014-08-27 08:54:30 -05:00
|
|
|
};
|
|
|
|
|
2018-09-05 00:47:30 -05:00
|
|
|
$scope.validate = () => {
|
2017-12-20 05:33:33 -06:00
|
|
|
$scope.infoText = '';
|
2017-12-21 01:39:31 -06:00
|
|
|
if ($scope.current.type === 'adhoc' && $scope.current.datasource !== null) {
|
|
|
|
$scope.infoText = 'Adhoc filters are applied automatically to all queries that target this datasource';
|
2019-12-13 02:47:53 -06:00
|
|
|
promiseToDigest($scope)(
|
|
|
|
datasourceSrv.get($scope.current.datasource).then(ds => {
|
|
|
|
if (!ds.getTagKeys) {
|
|
|
|
$scope.infoText = 'This datasource does not support adhoc filters yet.';
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
2016-09-20 10:34:38 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-09-05 00:47:30 -05:00
|
|
|
$scope.runQuery = () => {
|
2018-04-24 10:40:03 -05:00
|
|
|
$scope.optionsLimit = 20;
|
2019-07-16 04:35:42 -05:00
|
|
|
return variableSrv.updateOptions($scope.current).catch((err: { data: { message: any }; message: string }) => {
|
2017-12-21 01:39:31 -06:00
|
|
|
if (err.data && err.data.message) {
|
|
|
|
err.message = err.data.message;
|
|
|
|
}
|
2019-10-14 03:27:47 -05:00
|
|
|
appEvents.emit(AppEvents.alertError, [
|
|
|
|
'Templating',
|
|
|
|
'Template variables could not be initialized: ' + err.message,
|
|
|
|
]);
|
2017-12-21 01:39:31 -06:00
|
|
|
});
|
2014-08-26 07:17:46 -05:00
|
|
|
};
|
|
|
|
|
2019-07-16 04:35:42 -05:00
|
|
|
$scope.onQueryChange = (query: any, definition: any) => {
|
2018-11-02 05:58:50 -05:00
|
|
|
$scope.current.query = query;
|
|
|
|
$scope.current.definition = definition;
|
2018-10-25 06:30:39 -05:00
|
|
|
$scope.runQuery();
|
|
|
|
};
|
|
|
|
|
2019-07-16 04:35:42 -05:00
|
|
|
$scope.edit = (variable: any) => {
|
2014-08-28 05:44:01 -05:00
|
|
|
$scope.current = variable;
|
2014-08-27 03:41:27 -05:00
|
|
|
$scope.currentIsNew = false;
|
2017-12-20 05:33:33 -06:00
|
|
|
$scope.mode = 'edit';
|
2016-09-20 10:34:38 -05:00
|
|
|
$scope.validate();
|
2019-12-13 02:47:53 -06:00
|
|
|
promiseToDigest($scope)(
|
|
|
|
datasourceSrv.get($scope.current.datasource).then(ds => {
|
|
|
|
$scope.currentDatasource = ds;
|
|
|
|
})
|
|
|
|
);
|
2014-08-27 03:41:27 -05:00
|
|
|
};
|
|
|
|
|
2019-07-16 04:35:42 -05:00
|
|
|
$scope.duplicate = (variable: { getSaveModel: () => void; name: string }) => {
|
2018-08-26 14:52:57 -05:00
|
|
|
const clone = _.cloneDeep(variable.getSaveModel());
|
2016-09-19 10:03:29 -05:00
|
|
|
$scope.current = variableSrv.createVariableFromModel(clone);
|
2017-12-20 05:33:33 -06:00
|
|
|
$scope.current.name = 'copy_of_' + variable.name;
|
2017-08-09 07:28:23 -05:00
|
|
|
variableSrv.addVariable($scope.current);
|
2015-09-23 11:43:38 -05:00
|
|
|
};
|
|
|
|
|
2018-09-05 00:47:30 -05:00
|
|
|
$scope.update = () => {
|
2014-11-27 07:17:31 -06:00
|
|
|
if ($scope.isValid()) {
|
2019-12-13 02:47:53 -06:00
|
|
|
promiseToDigest($scope)(
|
|
|
|
$scope.runQuery().then(() => {
|
|
|
|
$scope.reset();
|
|
|
|
$scope.mode = 'list';
|
|
|
|
templateSrv.updateIndex();
|
|
|
|
})
|
|
|
|
);
|
2014-11-27 07:17:31 -06:00
|
|
|
}
|
2014-08-27 08:54:30 -05:00
|
|
|
};
|
|
|
|
|
2018-09-05 00:47:30 -05:00
|
|
|
$scope.reset = () => {
|
2014-08-26 07:17:46 -05:00
|
|
|
$scope.currentIsNew = true;
|
2017-12-20 05:33:33 -06:00
|
|
|
$scope.current = variableSrv.createVariableFromModel({ type: 'query' });
|
2017-08-08 10:08:35 -05:00
|
|
|
|
|
|
|
// this is done here in case a new data source type variable was added
|
2018-09-05 00:47:30 -05:00
|
|
|
$scope.datasources = _.filter(datasourceSrv.getMetricSources(), ds => {
|
2017-08-08 10:08:35 -05:00
|
|
|
return !ds.meta.mixed && ds.value !== null;
|
|
|
|
});
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
$scope.datasourceTypes = _($scope.datasources)
|
2017-12-20 05:33:33 -06:00
|
|
|
.uniqBy('meta.id')
|
2019-04-15 05:11:52 -05:00
|
|
|
.map((ds: any) => {
|
2017-12-19 09:06:54 -06:00
|
|
|
return { text: ds.meta.name, value: ds.meta.id };
|
|
|
|
})
|
|
|
|
.value();
|
2014-08-26 07:17:46 -05:00
|
|
|
};
|
|
|
|
|
2016-09-19 10:03:29 -05:00
|
|
|
$scope.typeChanged = function() {
|
2018-08-26 14:52:57 -05:00
|
|
|
const old = $scope.current;
|
2017-12-19 09:06:54 -06:00
|
|
|
$scope.current = variableSrv.createVariableFromModel({
|
2017-12-20 05:33:33 -06:00
|
|
|
type: $scope.current.type,
|
2017-12-19 09:06:54 -06:00
|
|
|
});
|
2016-09-19 10:03:29 -05:00
|
|
|
$scope.current.name = old.name;
|
|
|
|
$scope.current.label = old.label;
|
2016-04-16 11:03:29 -05:00
|
|
|
|
2018-08-26 14:52:57 -05:00
|
|
|
const oldIndex = _.indexOf(this.variables, old);
|
2016-09-19 10:03:29 -05:00
|
|
|
if (oldIndex !== -1) {
|
|
|
|
this.variables[oldIndex] = $scope.current;
|
2014-09-30 03:27:56 -05:00
|
|
|
}
|
2016-09-20 10:34:38 -05:00
|
|
|
|
|
|
|
$scope.validate();
|
2014-08-27 14:47:41 -05:00
|
|
|
};
|
|
|
|
|
2019-07-16 04:35:42 -05:00
|
|
|
$scope.removeVariable = (variable: any) => {
|
2017-08-02 09:15:22 -05:00
|
|
|
variableSrv.removeVariable(variable);
|
2014-08-26 07:17:46 -05:00
|
|
|
};
|
2018-04-23 06:00:24 -05:00
|
|
|
|
2018-09-05 00:47:30 -05:00
|
|
|
$scope.showMoreOptions = () => {
|
2018-04-23 06:00:24 -05:00
|
|
|
$scope.optionsLimit += 20;
|
|
|
|
};
|
2018-10-24 07:10:20 -05:00
|
|
|
|
|
|
|
$scope.datasourceChanged = async () => {
|
2019-12-13 02:47:53 -06:00
|
|
|
promiseToDigest($scope)(
|
|
|
|
datasourceSrv.get($scope.current.datasource).then(ds => {
|
|
|
|
$scope.current.query = '';
|
|
|
|
$scope.currentDatasource = ds;
|
|
|
|
})
|
|
|
|
);
|
2018-10-24 07:10:20 -05:00
|
|
|
};
|
2016-09-19 10:03:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
coreModule.controller('VariableEditorCtrl', VariableEditorCtrl);
|