2015-07-29 21:37:31 -05:00
|
|
|
define([
|
|
|
|
'angular',
|
|
|
|
'lodash',
|
|
|
|
],
|
2015-08-05 10:05:40 -05:00
|
|
|
function (angular, _) {
|
2015-07-29 21:37:31 -05:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var module = angular.module('grafana.controllers');
|
|
|
|
|
2015-08-18 00:09:42 -05:00
|
|
|
module.controller('CloudWatchQueryCtrl', function($scope, templateSrv) {
|
2015-07-29 21:37:31 -05:00
|
|
|
|
|
|
|
$scope.init = function() {
|
|
|
|
$scope.target.namespace = $scope.target.namespace || '';
|
|
|
|
$scope.target.metricName = $scope.target.metricName || '';
|
|
|
|
$scope.target.dimensions = $scope.target.dimensions || {};
|
2015-08-21 06:09:23 -05:00
|
|
|
$scope.target.escapedDimensions = this.escapeDimensions($scope.target.dimensions);
|
2015-07-29 21:37:31 -05:00
|
|
|
$scope.target.statistics = $scope.target.statistics || {};
|
|
|
|
$scope.target.period = $scope.target.period || 60;
|
2015-08-10 09:15:25 -05:00
|
|
|
$scope.target.region = $scope.target.region || $scope.datasource.getDefaultRegion();
|
2015-07-29 21:37:31 -05:00
|
|
|
|
|
|
|
$scope.target.errors = validateTarget();
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.refreshMetricData = function() {
|
|
|
|
$scope.target.errors = validateTarget($scope.target);
|
|
|
|
|
|
|
|
// this does not work so good
|
|
|
|
if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) {
|
|
|
|
$scope.oldTarget = angular.copy($scope.target);
|
|
|
|
$scope.get_data();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.moveMetricQuery = function(fromIndex, toIndex) {
|
|
|
|
_.move($scope.panel.targets, fromIndex, toIndex);
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.duplicate = function() {
|
|
|
|
var clone = angular.copy($scope.target);
|
|
|
|
$scope.panel.targets.push(clone);
|
|
|
|
};
|
|
|
|
|
2015-08-10 09:15:25 -05:00
|
|
|
$scope.suggestRegion = function(query, callback) { // jshint unused:false
|
2015-08-18 00:09:42 -05:00
|
|
|
return _.union($scope.datasource.performSuggestRegion(), $scope.datasource.getTemplateVariableNames());
|
2015-08-10 09:15:25 -05:00
|
|
|
};
|
|
|
|
|
2015-08-05 10:05:40 -05:00
|
|
|
$scope.suggestNamespace = function(query, callback) { // jshint unused:false
|
2015-08-18 00:09:42 -05:00
|
|
|
return _.union($scope.datasource.performSuggestNamespace(), $scope.datasource.getTemplateVariableNames());
|
2015-07-29 21:37:31 -05:00
|
|
|
};
|
|
|
|
|
2015-08-05 10:05:40 -05:00
|
|
|
$scope.suggestMetrics = function(query, callback) { // jshint unused:false
|
2015-08-18 00:09:42 -05:00
|
|
|
return _.union($scope.datasource.performSuggestMetrics($scope.target.namespace), $scope.datasource.getTemplateVariableNames());
|
2015-07-29 21:37:31 -05:00
|
|
|
};
|
|
|
|
|
2015-08-05 10:05:40 -05:00
|
|
|
$scope.suggestDimensionKeys = function(query, callback) { // jshint unused:false
|
2015-08-18 00:09:42 -05:00
|
|
|
return _.union($scope.datasource.performSuggestDimensionKeys($scope.target.namespace), $scope.datasource.getTemplateVariableNames());
|
2015-07-29 21:37:31 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
$scope.suggestDimensionValues = function(query, callback) {
|
|
|
|
if (!$scope.target.namespace || !$scope.target.metricName) {
|
|
|
|
return callback([]);
|
|
|
|
}
|
|
|
|
|
2015-08-10 20:25:25 -05:00
|
|
|
$scope.datasource.performSuggestDimensionValues(
|
|
|
|
$scope.target.region,
|
|
|
|
$scope.target.namespace,
|
|
|
|
$scope.target.metricName,
|
2015-08-11 05:51:14 -05:00
|
|
|
$scope.target.dimensions
|
2015-08-10 20:25:25 -05:00
|
|
|
)
|
|
|
|
.then(function(result) {
|
2015-08-11 05:51:14 -05:00
|
|
|
var suggestData = _.chain(result)
|
|
|
|
.flatten(true)
|
|
|
|
.filter(function(dimension) {
|
2015-08-18 00:09:42 -05:00
|
|
|
return dimension.Name === templateSrv.replace($scope.target.currentDimensionKey);
|
2015-08-11 05:51:14 -05:00
|
|
|
})
|
|
|
|
.pluck('Value')
|
|
|
|
.uniq()
|
|
|
|
.value();
|
|
|
|
|
2015-08-18 00:09:42 -05:00
|
|
|
suggestData = _.union(suggestData, $scope.datasource.getTemplateVariableNames());
|
2015-08-11 05:51:14 -05:00
|
|
|
callback(suggestData);
|
2015-08-10 20:25:25 -05:00
|
|
|
}, function() {
|
|
|
|
callback([]);
|
|
|
|
});
|
2015-07-29 21:37:31 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
$scope.addDimension = function() {
|
|
|
|
if (!$scope.addDimensionMode) {
|
|
|
|
$scope.addDimensionMode = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$scope.target.dimensions) {
|
|
|
|
$scope.target.dimensions = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
$scope.target.dimensions[$scope.target.currentDimensionKey] = $scope.target.currentDimensionValue;
|
2015-08-21 06:09:23 -05:00
|
|
|
$scope.target.escapedDimensions = this.escapeDimensions($scope.target.dimensions);
|
2015-07-29 21:37:31 -05:00
|
|
|
$scope.target.currentDimensionKey = '';
|
|
|
|
$scope.target.currentDimensionValue = '';
|
|
|
|
$scope.refreshMetricData();
|
|
|
|
|
|
|
|
$scope.addDimensionMode = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.removeDimension = function(key) {
|
2015-08-21 06:09:23 -05:00
|
|
|
key = key.replace(/\\\$/g, '$');
|
2015-07-29 21:37:31 -05:00
|
|
|
delete $scope.target.dimensions[key];
|
2015-08-21 06:09:23 -05:00
|
|
|
$scope.target.escapedDimensions = this.escapeDimensions($scope.target.dimensions);
|
2015-07-29 21:37:31 -05:00
|
|
|
$scope.refreshMetricData();
|
|
|
|
};
|
|
|
|
|
2015-08-21 06:09:23 -05:00
|
|
|
$scope.escapeDimensions = function(d) {
|
|
|
|
var result = {};
|
|
|
|
_.chain(d)
|
|
|
|
.keys(d)
|
|
|
|
.each(function(k) {
|
|
|
|
var v = d[k];
|
|
|
|
result[k.replace(/\$/g, '\\$')] = v.replace(/\$/g, '\\$');
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
2015-07-29 21:37:31 -05:00
|
|
|
$scope.statisticsOptionChanged = function() {
|
|
|
|
$scope.refreshMetricData();
|
|
|
|
};
|
|
|
|
|
|
|
|
// TODO: validate target
|
|
|
|
function validateTarget() {
|
|
|
|
var errs = {};
|
|
|
|
|
|
|
|
if ($scope.target.period < 60 || ($scope.target.period % 60) !== 0) {
|
|
|
|
errs.period = 'Period must be at least 60 seconds and must be a multiple of 60';
|
|
|
|
}
|
|
|
|
|
|
|
|
return errs;
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|