2016-09-19 17:03:29 +02:00
|
|
|
///<reference path="../../headers/common.d.ts" />
|
|
|
|
|
|
|
|
|
|
import _ from 'lodash';
|
|
|
|
|
import coreModule from 'app/core/core_module';
|
2016-09-19 18:32:09 +02:00
|
|
|
import {variableTypes} from './variable';
|
2014-08-26 14:17:46 +02:00
|
|
|
|
2016-09-19 17:03:29 +02:00
|
|
|
export class VariableEditorCtrl {
|
|
|
|
|
|
2016-09-22 10:22:16 +02:00
|
|
|
/** @ngInject **/
|
2017-09-21 16:40:18 +02:00
|
|
|
constructor($scope, datasourceSrv, variableSrv, templateSrv) {
|
2016-09-19 18:32:09 +02:00
|
|
|
$scope.variableTypes = variableTypes;
|
2016-09-20 17:34:38 +02:00
|
|
|
$scope.ctrl = {};
|
2017-05-19 11:14:11 +02:00
|
|
|
$scope.namePattern = /^(?!__).*$/;
|
2016-04-16 12:03:29 -04:00
|
|
|
|
2016-03-09 13:10:02 +01:00
|
|
|
$scope.refreshOptions = [
|
|
|
|
|
{value: 0, text: "Never"},
|
|
|
|
|
{value: 1, text: "On Dashboard Load"},
|
|
|
|
|
{value: 2, text: "On Time Range Change"},
|
|
|
|
|
];
|
|
|
|
|
|
2016-05-21 17:56:57 +09:00
|
|
|
$scope.sortOptions = [
|
2016-09-19 18:06:36 +02:00
|
|
|
{value: 0, text: "Disabled"},
|
2016-06-18 02:08:24 +09:00
|
|
|
{value: 1, text: "Alphabetical (asc)"},
|
|
|
|
|
{value: 2, text: "Alphabetical (desc)"},
|
|
|
|
|
{value: 3, text: "Numerical (asc)"},
|
|
|
|
|
{value: 4, text: "Numerical (desc)"},
|
2016-05-21 17:56:57 +09:00
|
|
|
];
|
|
|
|
|
|
2016-03-29 22:23:29 +02:00
|
|
|
$scope.hideOptions = [
|
|
|
|
|
{value: 0, text: ""},
|
|
|
|
|
{value: 1, text: "Label"},
|
|
|
|
|
{value: 2, text: "Variable"},
|
|
|
|
|
];
|
|
|
|
|
|
2014-08-26 14:17:46 +02:00
|
|
|
$scope.init = function() {
|
2015-09-08 15:54:08 +02:00
|
|
|
$scope.mode = 'list';
|
|
|
|
|
|
2016-09-15 14:53:36 +02:00
|
|
|
$scope.variables = variableSrv.variables;
|
2014-08-26 14:17:46 +02:00
|
|
|
$scope.reset();
|
2014-08-27 10:41:27 +02:00
|
|
|
|
2015-09-08 15:54:08 +02:00
|
|
|
$scope.$watch('mode', function(val) {
|
|
|
|
|
if (val === 'new') {
|
2014-08-27 15:54:30 +02:00
|
|
|
$scope.reset();
|
|
|
|
|
}
|
2014-08-27 10:41:27 +02:00
|
|
|
});
|
2014-08-26 14:17:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.add = function() {
|
2014-11-27 14:17:31 +01:00
|
|
|
if ($scope.isValid()) {
|
2017-08-02 16:15:22 +02:00
|
|
|
variableSrv.addVariable($scope.current);
|
2014-11-27 14:17:31 +01:00
|
|
|
$scope.update();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.isValid = function() {
|
2016-09-20 17:34:38 +02:00
|
|
|
if (!$scope.ctrl.form.$valid) {
|
2017-09-26 11:24:58 +02:00
|
|
|
return false;
|
2014-11-27 14:17:31 +01:00
|
|
|
}
|
|
|
|
|
|
2014-11-27 14:22:55 +01:00
|
|
|
if (!$scope.current.name.match(/^\w+$/)) {
|
|
|
|
|
$scope.appEvent('alert-warning', ['Validation', 'Only word and digit characters are allowed in variable names']);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-13 22:10:44 +02:00
|
|
|
var sameName = _.find($scope.variables, { name: $scope.current.name });
|
2014-11-27 14:17:31 +01:00
|
|
|
if (sameName && sameName !== $scope.current) {
|
|
|
|
|
$scope.appEvent('alert-warning', ['Validation', 'Variable with the same name already exists']);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-11 23:54:32 -05:00
|
|
|
if ($scope.current.type === 'query' && $scope.current.query.match(new RegExp('\\$' + $scope.current.name + '(/| |$)'))) {
|
2017-03-20 17:14:24 +01:00
|
|
|
$scope.appEvent('alert-warning', ['Validation', 'Query cannot contain a reference to itself. Variable: $' + $scope.current.name]);
|
2017-03-07 23:29:16 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-27 14:17:31 +01:00
|
|
|
return true;
|
2014-08-27 15:54:30 +02:00
|
|
|
};
|
|
|
|
|
|
2016-09-20 17:34:38 +02:00
|
|
|
$scope.validate = function() {
|
|
|
|
|
$scope.infoText = '';
|
|
|
|
|
if ($scope.current.type === 'adhoc' && $scope.current.datasource !== null) {
|
|
|
|
|
$scope.infoText = 'Adhoc filters are applied automatically to all queries that target this datasource';
|
|
|
|
|
datasourceSrv.get($scope.current.datasource).then(ds => {
|
2016-09-21 08:46:59 +02:00
|
|
|
if (!ds.getTagKeys) {
|
2016-09-20 17:34:38 +02:00
|
|
|
$scope.infoText = 'This datasource does not support adhoc filters yet.';
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2014-08-27 15:54:30 +02:00
|
|
|
$scope.runQuery = function() {
|
2016-09-15 14:53:36 +02:00
|
|
|
return variableSrv.updateOptions($scope.current).then(null, function(err) {
|
2015-10-02 09:04:46 +02:00
|
|
|
if (err.data && err.data.message) { err.message = err.data.message; }
|
|
|
|
|
$scope.appEvent("alert-error", ['Templating', 'Template variables could not be initialized: ' + err.message]);
|
2014-09-01 11:13:18 +02:00
|
|
|
});
|
2014-08-26 14:17:46 +02:00
|
|
|
};
|
|
|
|
|
|
2014-08-28 12:44:01 +02:00
|
|
|
$scope.edit = function(variable) {
|
|
|
|
|
$scope.current = variable;
|
2014-08-27 10:41:27 +02:00
|
|
|
$scope.currentIsNew = false;
|
2015-09-08 15:54:08 +02:00
|
|
|
$scope.mode = 'edit';
|
2016-09-20 17:34:38 +02:00
|
|
|
$scope.validate();
|
2014-08-27 10:41:27 +02:00
|
|
|
};
|
|
|
|
|
|
2015-09-23 22:13:38 +05:30
|
|
|
$scope.duplicate = function(variable) {
|
2016-11-22 14:20:06 +01:00
|
|
|
var clone = _.cloneDeep(variable.getSaveModel());
|
2016-09-19 17:03:29 +02:00
|
|
|
$scope.current = variableSrv.createVariableFromModel(clone);
|
2015-09-23 22:13:38 +05:30
|
|
|
$scope.current.name = 'copy_of_'+variable.name;
|
2017-08-09 14:28:23 +02:00
|
|
|
variableSrv.addVariable($scope.current);
|
2015-09-23 22:13:38 +05:30
|
|
|
};
|
|
|
|
|
|
2014-08-27 15:54:30 +02:00
|
|
|
$scope.update = function() {
|
2014-11-27 14:17:31 +01:00
|
|
|
if ($scope.isValid()) {
|
|
|
|
|
$scope.runQuery().then(function() {
|
|
|
|
|
$scope.reset();
|
2015-09-08 15:54:08 +02:00
|
|
|
$scope.mode = 'list';
|
2016-09-19 18:06:36 +02:00
|
|
|
templateSrv.updateTemplateData();
|
2014-11-27 14:17:31 +01:00
|
|
|
});
|
|
|
|
|
}
|
2014-08-27 15:54:30 +02:00
|
|
|
};
|
|
|
|
|
|
2014-08-26 14:17:46 +02:00
|
|
|
$scope.reset = function() {
|
|
|
|
|
$scope.currentIsNew = true;
|
2016-09-19 17:03:29 +02:00
|
|
|
$scope.current = variableSrv.createVariableFromModel({type: 'query'});
|
2017-08-08 17:08:35 +02:00
|
|
|
|
|
|
|
|
// this is done here in case a new data source type variable was added
|
|
|
|
|
$scope.datasources = _.filter(datasourceSrv.getMetricSources(), function(ds) {
|
|
|
|
|
return !ds.meta.mixed && ds.value !== null;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$scope.datasourceTypes = _($scope.datasources).uniqBy('meta.id').map(function(ds) {
|
|
|
|
|
return {text: ds.meta.name, value: ds.meta.id};
|
|
|
|
|
}).value();
|
2014-08-26 14:17:46 +02:00
|
|
|
};
|
|
|
|
|
|
2016-09-19 17:03:29 +02:00
|
|
|
$scope.typeChanged = function() {
|
|
|
|
|
var old = $scope.current;
|
|
|
|
|
$scope.current = variableSrv.createVariableFromModel({type: $scope.current.type});
|
|
|
|
|
$scope.current.name = old.name;
|
|
|
|
|
$scope.current.hide = old.hide;
|
|
|
|
|
$scope.current.label = old.label;
|
2016-04-16 12:03:29 -04:00
|
|
|
|
2016-09-19 17:03:29 +02:00
|
|
|
var oldIndex = _.indexOf(this.variables, old);
|
|
|
|
|
if (oldIndex !== -1) {
|
|
|
|
|
this.variables[oldIndex] = $scope.current;
|
2014-09-30 10:27:56 +02:00
|
|
|
}
|
2016-09-20 17:34:38 +02:00
|
|
|
|
|
|
|
|
$scope.validate();
|
2014-08-27 21:47:41 +02:00
|
|
|
};
|
|
|
|
|
|
2014-08-28 12:44:01 +02:00
|
|
|
$scope.removeVariable = function(variable) {
|
2017-08-02 16:15:22 +02:00
|
|
|
variableSrv.removeVariable(variable);
|
2014-08-26 14:17:46 +02:00
|
|
|
};
|
2016-09-19 17:03:29 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
coreModule.controller('VariableEditorCtrl', VariableEditorCtrl);
|
2014-08-26 14:17:46 +02:00
|
|
|
|