2016-09-19 10:03:29 -05:00
|
|
|
///<reference path="../../headers/common.d.ts" />
|
|
|
|
|
|
|
|
import angular from 'angular';
|
|
|
|
import _ from 'lodash';
|
|
|
|
import $ from 'jquery';
|
|
|
|
import kbn from 'app/core/utils/kbn';
|
|
|
|
import coreModule from 'app/core/core_module';
|
|
|
|
import appEvents from 'app/core/app_events';
|
2014-08-26 07:17:46 -05:00
|
|
|
|
2016-09-19 10:03:29 -05:00
|
|
|
export class VariableEditorCtrl {
|
|
|
|
|
|
|
|
/** @ngInject */
|
|
|
|
constructor(private $scope, private datasourceSrv, private variableSrv) {
|
2016-04-16 11:03:29 -05:00
|
|
|
$scope.variableTypes = [
|
|
|
|
{value: "query", text: "Query"},
|
2016-09-14 04:20:58 -05:00
|
|
|
{value: "adhoc", text: "Ad hoc filters"},
|
2016-04-16 11:03:29 -05:00
|
|
|
{value: "interval", text: "Interval"},
|
2016-04-29 01:40:38 -05:00
|
|
|
{value: "datasource", text: "Data source"},
|
2016-04-16 11:03:29 -05:00
|
|
|
{value: "custom", text: "Custom"},
|
2016-05-28 09:59:29 -05:00
|
|
|
{value: "constant", text: "Constant"},
|
2016-04-16 11:03:29 -05:00
|
|
|
];
|
|
|
|
|
2016-03-09 06:10:02 -06:00
|
|
|
$scope.refreshOptions = [
|
|
|
|
{value: 0, text: "Never"},
|
|
|
|
{value: 1, text: "On Dashboard Load"},
|
|
|
|
{value: 2, text: "On Time Range Change"},
|
|
|
|
];
|
|
|
|
|
2016-05-21 03:56:57 -05:00
|
|
|
$scope.sortOptions = [
|
2016-09-19 08:15:15 -05:00
|
|
|
{value: 0, text: "Query sort"},
|
2016-06-17 12:08:24 -05:00
|
|
|
{value: 1, text: "Alphabetical (asc)"},
|
|
|
|
{value: 2, text: "Alphabetical (desc)"},
|
|
|
|
{value: 3, text: "Numerical (asc)"},
|
|
|
|
{value: 4, text: "Numerical (desc)"},
|
2016-05-21 03:56:57 -05:00
|
|
|
];
|
|
|
|
|
2016-03-29 15:23:29 -05:00
|
|
|
$scope.hideOptions = [
|
|
|
|
{value: 0, text: ""},
|
|
|
|
{value: 1, text: "Label"},
|
|
|
|
{value: 2, text: "Variable"},
|
|
|
|
];
|
|
|
|
|
2014-08-26 07:17:46 -05:00
|
|
|
$scope.init = function() {
|
2015-09-08 08:54:08 -05:00
|
|
|
$scope.mode = 'list';
|
|
|
|
|
2015-08-17 14:20:09 -05:00
|
|
|
$scope.datasources = _.filter(datasourceSrv.getMetricSources(), function(ds) {
|
2016-09-14 10:36:28 -05:00
|
|
|
return !ds.meta.builtIn && ds.value !== null;
|
2015-08-17 14:20:09 -05:00
|
|
|
});
|
|
|
|
|
2016-09-19 10:03:29 -05:00
|
|
|
$scope.datasourceTypes = _($scope.datasources).uniqBy('meta.id').map(function(ds) {
|
|
|
|
return {text: ds.meta.name, value: ds.meta.id};
|
|
|
|
}).value();
|
2016-04-16 11:03:29 -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
|
|
|
|
2015-09-08 08:54:08 -05:00
|
|
|
$scope.$watch('mode', function(val) {
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
$scope.add = function() {
|
2014-11-27 07:17:31 -06:00
|
|
|
if ($scope.isValid()) {
|
|
|
|
$scope.variables.push($scope.current);
|
|
|
|
$scope.update();
|
2015-02-20 05:20:10 -06:00
|
|
|
$scope.updateSubmenuVisibility();
|
2014-11-27 07:17:31 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.isValid = function() {
|
|
|
|
if (!$scope.current.name) {
|
|
|
|
$scope.appEvent('alert-warning', ['Validation', 'Template variable requires a name']);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-11-27 07:22:55 -06: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 15:10:44 -05:00
|
|
|
var sameName = _.find($scope.variables, { name: $scope.current.name });
|
2014-11-27 07:17:31 -06:00
|
|
|
if (sameName && sameName !== $scope.current) {
|
|
|
|
$scope.appEvent('alert-warning', ['Validation', 'Variable with the same name already exists']);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2014-08-27 08:54:30 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
$scope.runQuery = function() {
|
2016-09-15 07:53:36 -05:00
|
|
|
return variableSrv.updateOptions($scope.current).then(null, function(err) {
|
2015-10-02 02:04:46 -05: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 04:13:18 -05:00
|
|
|
});
|
2014-08-26 07:17:46 -05:00
|
|
|
};
|
|
|
|
|
2014-08-28 05:44:01 -05:00
|
|
|
$scope.edit = function(variable) {
|
|
|
|
$scope.current = variable;
|
2014-08-27 03:41:27 -05:00
|
|
|
$scope.currentIsNew = false;
|
2015-09-08 08:54:08 -05:00
|
|
|
$scope.mode = 'edit';
|
2014-08-27 03:41:27 -05:00
|
|
|
};
|
|
|
|
|
2015-09-23 11:43:38 -05:00
|
|
|
$scope.duplicate = function(variable) {
|
2016-09-19 10:03:29 -05:00
|
|
|
var clone = _.cloneDeep(variable.getModel());
|
|
|
|
$scope.current = variableSrv.createVariableFromModel(clone);
|
2015-09-23 11:43:38 -05:00
|
|
|
$scope.variables.push($scope.current);
|
|
|
|
$scope.current.name = 'copy_of_'+variable.name;
|
|
|
|
$scope.updateSubmenuVisibility();
|
|
|
|
};
|
|
|
|
|
2014-08-27 08:54:30 -05:00
|
|
|
$scope.update = function() {
|
2014-11-27 07:17:31 -06:00
|
|
|
if ($scope.isValid()) {
|
|
|
|
$scope.runQuery().then(function() {
|
|
|
|
$scope.reset();
|
2015-09-08 08:54:08 -05:00
|
|
|
$scope.mode = 'list';
|
2014-11-27 07:17:31 -06:00
|
|
|
});
|
|
|
|
}
|
2014-08-27 08:54:30 -05:00
|
|
|
};
|
|
|
|
|
2014-08-26 07:17:46 -05:00
|
|
|
$scope.reset = function() {
|
|
|
|
$scope.currentIsNew = true;
|
2016-09-19 10:03:29 -05:00
|
|
|
$scope.current = variableSrv.createVariableFromModel({type: 'query'});
|
2014-08-26 07:17:46 -05:00
|
|
|
};
|
|
|
|
|
2016-05-28 09:59:29 -05:00
|
|
|
$scope.showSelectionOptions = function() {
|
|
|
|
if ($scope.current) {
|
|
|
|
if ($scope.current.type === 'query') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if ($scope.current.type === 'custom') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2016-09-19 10:03:29 -05: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 11:03:29 -05:00
|
|
|
|
2016-09-19 10:03:29 -05:00
|
|
|
var oldIndex = _.indexOf(this.variables, old);
|
|
|
|
if (oldIndex !== -1) {
|
|
|
|
this.variables[oldIndex] = $scope.current;
|
2014-09-30 03:27:56 -05:00
|
|
|
}
|
2016-04-16 11:03:29 -05:00
|
|
|
|
2016-09-19 10:03:29 -05:00
|
|
|
// if ($scope.current.type === 'interval') {
|
|
|
|
// $scope.current.query = '1m,10m,30m,1h,6h,12h,1d,7d,14d,30d';
|
|
|
|
// $scope.current.refresh = 0;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// if ($scope.current.type === 'query') {
|
|
|
|
// $scope.current.query = '';
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// if ($scope.current.type === 'constant') {
|
|
|
|
// $scope.current.query = '';
|
|
|
|
// $scope.current.refresh = 0;
|
|
|
|
// $scope.current.hide = 2;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// if ($scope.current.type === 'datasource') {
|
|
|
|
// $scope.current.query = $scope.datasourceTypes[0].value;
|
|
|
|
// $scope.current.regex = '';
|
|
|
|
// $scope.current.refresh = 1;
|
|
|
|
// }
|
2014-08-27 14:47:41 -05:00
|
|
|
};
|
|
|
|
|
2014-08-28 05:44:01 -05:00
|
|
|
$scope.removeVariable = function(variable) {
|
|
|
|
var index = _.indexOf($scope.variables, variable);
|
|
|
|
$scope.variables.splice(index, 1);
|
2015-02-20 05:20:10 -06:00
|
|
|
$scope.updateSubmenuVisibility();
|
2014-08-26 07:17:46 -05:00
|
|
|
};
|
|
|
|
|
2016-09-19 10:03:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
coreModule.controller('VariableEditorCtrl', VariableEditorCtrl);
|
2014-08-26 07:17:46 -05:00
|
|
|
|