2014-03-14 14:14:16 -05:00
|
|
|
define([
|
|
|
|
'angular',
|
|
|
|
'underscore',
|
|
|
|
'kbn'
|
|
|
|
],
|
|
|
|
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
|
|
|
|
2014-06-14 04:38:27 -05:00
|
|
|
module.controller('OpenTSDBTargetCtrl', function($scope, $timeout) {
|
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'];
|
2014-06-14 04:38:27 -05:00
|
|
|
|
|
|
|
if (!$scope.target.aggregator) {
|
|
|
|
$scope.target.aggregator = 'sum';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$scope.target.downsampleAggregator) {
|
|
|
|
$scope.target.downsampleAggregator = 'sum';
|
|
|
|
}
|
|
|
|
|
|
|
|
$scope.$on('typeahead-updated', function() {
|
|
|
|
$timeout($scope.targetBlur);
|
|
|
|
});
|
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();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.duplicate = function() {
|
|
|
|
var clone = angular.copy($scope.target);
|
|
|
|
$scope.panel.targets.push(clone);
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.suggestMetrics = function(query, callback) {
|
|
|
|
$scope.datasource
|
|
|
|
.performSuggestQuery(query, 'metrics')
|
|
|
|
.then(callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.suggestTagKeys = function(query, callback) {
|
|
|
|
$scope.datasource
|
|
|
|
.performSuggestQuery(query, 'tagk')
|
|
|
|
.then(callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.suggestTagValues = function(query, callback) {
|
|
|
|
$scope.datasource
|
|
|
|
.performSuggestQuery(query, 'tagv')
|
|
|
|
.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();
|
|
|
|
};
|
|
|
|
|
|
|
|
function validateTarget(target) {
|
|
|
|
var errs = {};
|
|
|
|
|
|
|
|
if (!target.metric) {
|
|
|
|
errs.metric = "You must supply a metric name.";
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|