2017-12-20 05:33:33 -06:00
|
|
|
import _ from 'lodash';
|
|
|
|
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';
|
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 */
|
2017-09-21 09:40:18 -05:00
|
|
|
constructor($scope, datasourceSrv, variableSrv, 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;
|
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
|
|
|
];
|
|
|
|
|
2017-12-21 01:39:31 -06:00
|
|
|
$scope.hideOptions = [{ value: 0, text: '' }, { value: 1, text: 'Label' }, { value: 2, text: 'Variable' }];
|
2016-03-29 15:23:29 -05:00
|
|
|
|
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
|
|
|
|
2018-09-05 00:47:30 -05:00
|
|
|
$scope.$watch('mode', val => {
|
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
|
|
|
};
|
|
|
|
|
2018-09-05 00:47:30 -05:00
|
|
|
$scope.setMode = mode => {
|
2017-12-11 06:04:06 -06:00
|
|
|
$scope.mode = mode;
|
|
|
|
};
|
|
|
|
|
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+$/)) {
|
2018-01-25 07:15:40 -06:00
|
|
|
appEvents.emit('alert-warning', ['Validation', 'Only word and digit characters are allowed in variable names']);
|
2014-11-27 07:22:55 -06:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-26 14:52:57 -05:00
|
|
|
const sameName = _.find($scope.variables, { name: $scope.current.name });
|
2014-11-27 07:17:31 -06:00
|
|
|
if (sameName && sameName !== $scope.current) {
|
2018-01-25 07:15:40 -06:00
|
|
|
appEvents.emit('alert-warning', ['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
|
|
|
) {
|
2018-01-25 07:15:40 -06:00
|
|
|
appEvents.emit('alert-warning', [
|
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';
|
2016-09-20 10:34:38 -05:00
|
|
|
datasourceSrv.get($scope.current.datasource).then(ds => {
|
2016-09-21 01:46:59 -05:00
|
|
|
if (!ds.getTagKeys) {
|
2017-12-21 01:39:31 -06:00
|
|
|
$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;
|
2018-01-25 07:15:40 -06:00
|
|
|
return variableSrv.updateOptions($scope.current).catch(err => {
|
2017-12-21 01:39:31 -06:00
|
|
|
if (err.data && err.data.message) {
|
|
|
|
err.message = err.data.message;
|
|
|
|
}
|
2018-01-25 07:15:40 -06:00
|
|
|
appEvents.emit('alert-error', ['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
|
|
|
};
|
|
|
|
|
2018-11-02 05:58:50 -05:00
|
|
|
$scope.onQueryChange = (query, definition) => {
|
|
|
|
$scope.current.query = query;
|
|
|
|
$scope.current.definition = definition;
|
2018-10-25 06:30:39 -05:00
|
|
|
$scope.runQuery();
|
|
|
|
};
|
|
|
|
|
2018-09-05 00:47:30 -05:00
|
|
|
$scope.edit = variable => {
|
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();
|
2018-11-01 07:40:04 -05:00
|
|
|
datasourceSrv.get($scope.current.datasource).then(ds => {
|
|
|
|
$scope.currentDatasource = ds;
|
|
|
|
});
|
2014-08-27 03:41:27 -05:00
|
|
|
};
|
|
|
|
|
2018-09-05 00:47:30 -05:00
|
|
|
$scope.duplicate = variable => {
|
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()) {
|
2018-09-05 00:47:30 -05:00
|
|
|
$scope.runQuery().then(() => {
|
2014-11-27 07:17:31 -06:00
|
|
|
$scope.reset();
|
2017-12-20 05:33:33 -06:00
|
|
|
$scope.mode = 'list';
|
2016-09-19 11:06:36 -05:00
|
|
|
templateSrv.updateTemplateData();
|
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')
|
2018-09-05 00:47:30 -05:00
|
|
|
.map(ds => {
|
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.hide = old.hide;
|
|
|
|
$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
|
|
|
};
|
|
|
|
|
2018-09-05 00:47:30 -05:00
|
|
|
$scope.removeVariable = variable => {
|
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 () => {
|
2018-10-25 06:30:39 -05:00
|
|
|
datasourceSrv.get($scope.current.datasource).then(ds => {
|
2018-11-01 04:17:39 -05:00
|
|
|
$scope.current.query = '';
|
2018-10-25 06:30:39 -05:00
|
|
|
$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);
|