2014-03-14 14:14:16 -05:00
|
|
|
define([
|
|
|
|
'angular',
|
2014-08-07 07:35:19 -05:00
|
|
|
'lodash',
|
2015-10-30 08:44:40 -05:00
|
|
|
'app/core/utils/kbn'
|
2014-03-14 14:14:16 -05:00
|
|
|
],
|
|
|
|
function (angular, _, kbn) {
|
|
|
|
'use strict';
|
|
|
|
|
2014-07-28 11:11:52 -05:00
|
|
|
var module = angular.module('grafana.controllers');
|
2014-03-14 14:14:16 -05:00
|
|
|
|
2015-08-25 02:39:42 -05:00
|
|
|
module.controller('OpenTSDBQueryCtrl', function($scope) {
|
2016-02-01 10:06:44 -06:00
|
|
|
$scope.panelCtrl = $scope.ctrl;
|
2014-03-14 14:14:16 -05:00
|
|
|
|
|
|
|
$scope.init = function() {
|
|
|
|
$scope.target.errors = validateTarget($scope.target);
|
|
|
|
$scope.aggregators = ['avg', 'sum', 'min', 'max', 'dev', 'zimsum', 'mimmin', 'mimmax'];
|
2016-01-21 10:38:39 -06:00
|
|
|
$scope.fillPolicies = ['none', 'nan', 'null', 'zero'];
|
2014-06-14 04:38:27 -05:00
|
|
|
|
|
|
|
if (!$scope.target.aggregator) {
|
|
|
|
$scope.target.aggregator = 'sum';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$scope.target.downsampleAggregator) {
|
2015-02-28 05:33:20 -06:00
|
|
|
$scope.target.downsampleAggregator = 'avg';
|
2014-06-14 04:38:27 -05:00
|
|
|
}
|
2015-09-23 02:17:37 -05:00
|
|
|
|
2016-01-21 10:38:39 -06:00
|
|
|
if (!$scope.target.downsampleFillPolicy) {
|
|
|
|
$scope.target.downsampleFillPolicy = 'none';
|
|
|
|
}
|
|
|
|
|
2015-09-23 02:17:37 -05:00
|
|
|
$scope.datasource.getAggregators().then(function(aggs) {
|
|
|
|
$scope.aggregators = aggs;
|
|
|
|
});
|
2014-03-14 14:14:16 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
$scope.targetBlur = function() {
|
|
|
|
$scope.target.errors = validateTarget($scope.target);
|
|
|
|
|
2014-06-14 04:38:27 -05:00
|
|
|
// this does not work so good
|
2014-03-14 14:14:16 -05:00
|
|
|
if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) {
|
|
|
|
$scope.oldTarget = angular.copy($scope.target);
|
|
|
|
$scope.get_data();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-06-30 08:32:31 -05:00
|
|
|
$scope.getTextValues = function(metricFindResult) {
|
|
|
|
return _.map(metricFindResult, function(value) { return value.text; });
|
|
|
|
};
|
|
|
|
|
2014-03-14 14:14:16 -05:00
|
|
|
$scope.suggestMetrics = function(query, callback) {
|
2015-07-02 03:04:15 -05:00
|
|
|
$scope.datasource.metricFindQuery('metrics(' + query + ')')
|
2015-06-30 08:32:31 -05:00
|
|
|
.then($scope.getTextValues)
|
2014-03-14 14:14:16 -05:00
|
|
|
.then(callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.suggestTagKeys = function(query, callback) {
|
2015-10-29 13:13:38 -05:00
|
|
|
$scope.datasource.metricFindQuery('suggest_tagk(' + query + ')')
|
2015-06-30 08:32:31 -05:00
|
|
|
.then($scope.getTextValues)
|
2014-03-14 14:14:16 -05:00
|
|
|
.then(callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.suggestTagValues = function(query, callback) {
|
2015-10-29 13:13:38 -05:00
|
|
|
$scope.datasource.metricFindQuery('suggest_tagv(' + query + ')')
|
2015-06-30 08:32:31 -05:00
|
|
|
.then($scope.getTextValues)
|
2014-03-14 14:14:16 -05:00
|
|
|
.then(callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.addTag = function() {
|
2014-06-14 04:38:27 -05:00
|
|
|
if (!$scope.addTagMode) {
|
|
|
|
$scope.addTagMode = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-14 14:14:16 -05:00
|
|
|
if (!$scope.target.tags) {
|
|
|
|
$scope.target.tags = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
$scope.target.errors = validateTarget($scope.target);
|
|
|
|
|
|
|
|
if (!$scope.target.errors.tags) {
|
|
|
|
$scope.target.tags[$scope.target.currentTagKey] = $scope.target.currentTagValue;
|
|
|
|
$scope.target.currentTagKey = '';
|
|
|
|
$scope.target.currentTagValue = '';
|
|
|
|
$scope.targetBlur();
|
|
|
|
}
|
2014-06-14 04:38:27 -05:00
|
|
|
|
|
|
|
$scope.addTagMode = false;
|
2014-03-14 14:14:16 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
$scope.removeTag = function(key) {
|
|
|
|
delete $scope.target.tags[key];
|
|
|
|
$scope.targetBlur();
|
|
|
|
};
|
|
|
|
|
2015-11-13 06:01:55 -06:00
|
|
|
$scope.editTag = function(key, value) {
|
|
|
|
$scope.removeTag(key);
|
|
|
|
$scope.target.currentTagKey = key;
|
|
|
|
$scope.target.currentTagValue = value;
|
|
|
|
$scope.addTag();
|
|
|
|
};
|
|
|
|
|
2014-03-14 14:14:16 -05:00
|
|
|
function validateTarget(target) {
|
|
|
|
var errs = {};
|
|
|
|
|
|
|
|
if (target.shouldDownsample) {
|
|
|
|
try {
|
|
|
|
if (target.downsampleInterval) {
|
|
|
|
kbn.describe_interval(target.downsampleInterval);
|
|
|
|
} else {
|
|
|
|
errs.downsampleInterval = "You must supply a downsample interval (e.g. '1m' or '1h').";
|
|
|
|
}
|
|
|
|
} catch(err) {
|
|
|
|
errs.downsampleInterval = err.message;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (target.tags && _.has(target.tags, target.currentTagKey)) {
|
|
|
|
errs.tags = "Duplicate tag key '" + target.currentTagKey + "'.";
|
|
|
|
}
|
|
|
|
|
|
|
|
return errs;
|
|
|
|
}
|
|
|
|
|
2015-08-16 02:52:45 -05:00
|
|
|
$scope.init();
|
2014-03-14 14:14:16 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|