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-09-02 05:36:56 -05:00
|
|
|
module.controller('CloudWatchQueryCtrl', function($scope, templateSrv, uiSegmentSrv) {
|
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();
|
|
|
|
|
2015-09-02 10:45:41 -05:00
|
|
|
$scope.regionSegment = uiSegmentSrv.getSegmentForValue($scope.target.region, 'select region');
|
|
|
|
$scope.namespaceSegment = uiSegmentSrv.getSegmentForValue($scope.target.namespace, 'select namespace');
|
|
|
|
$scope.metricSegment = uiSegmentSrv.getSegmentForValue($scope.target.metricName, 'select metric');
|
2015-07-29 21:37:31 -05:00
|
|
|
};
|
|
|
|
|
2015-09-02 05:36:56 -05:00
|
|
|
$scope.getRegions = function() {
|
2015-10-02 04:10:21 -05:00
|
|
|
return $scope.datasource.metricFindQuery('regions()')
|
2015-09-02 05:36:56 -05:00
|
|
|
.then($scope.transformToSegments(true));
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.getNamespaces = function() {
|
2015-10-02 04:10:21 -05:00
|
|
|
return $scope.datasource.metricFindQuery('namespaces()')
|
2015-09-02 05:36:56 -05:00
|
|
|
.then($scope.transformToSegments(true));
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.getMetrics = function() {
|
|
|
|
return $scope.datasource.metricFindQuery('metrics(' + $scope.target.namespace + ')')
|
|
|
|
.then($scope.transformToSegments(true));
|
2015-07-29 21:37:31 -05:00
|
|
|
};
|
|
|
|
|
2015-09-02 05:36:56 -05:00
|
|
|
$scope.regionChanged = function() {
|
|
|
|
$scope.target.region = $scope.regionSegment.value;
|
|
|
|
$scope.get_data();
|
2015-07-29 21:37:31 -05:00
|
|
|
};
|
|
|
|
|
2015-09-02 05:36:56 -05:00
|
|
|
$scope.namespaceChanged = function() {
|
|
|
|
$scope.target.namespace = $scope.namespaceSegment.value;
|
|
|
|
$scope.get_data();
|
2015-08-10 09:15:25 -05:00
|
|
|
};
|
|
|
|
|
2015-09-02 05:36:56 -05:00
|
|
|
$scope.metricChanged = function() {
|
|
|
|
$scope.target.metricName = $scope.metricSegment.value;
|
|
|
|
$scope.get_data();
|
2015-07-29 21:37:31 -05:00
|
|
|
};
|
|
|
|
|
2015-09-02 05:36:56 -05:00
|
|
|
$scope.transformToSegments = function(addTemplateVars) {
|
|
|
|
return function(results) {
|
|
|
|
var segments = _.map(results, function(segment) {
|
|
|
|
return uiSegmentSrv.newSegment({ value: segment.text, expandable: segment.expandable });
|
|
|
|
});
|
|
|
|
|
|
|
|
if (addTemplateVars) {
|
|
|
|
_.each(templateSrv.variables, function(variable) {
|
|
|
|
segments.unshift(uiSegmentSrv.newSegment({ type: 'template', value: '$' + variable.name, expandable: true }));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return segments;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
$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();
|
|
|
|
}
|
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-10-01 14:20:52 -05:00
|
|
|
$scope.datasource.getDimensionKeys($scope.target.namespace).then(function(result) {
|
2015-10-02 13:25:28 -05:00
|
|
|
callback(_.pluck(result, 'text'));
|
2015-10-01 14:20:52 -05:00
|
|
|
});
|
2015-07-29 21:37:31 -05:00
|
|
|
};
|
|
|
|
|
2015-10-01 14:20:52 -05:00
|
|
|
// TODO: Removed template variables from the suggest
|
|
|
|
// add this feature back after improving the editor
|
2015-07-29 21:37:31 -05:00
|
|
|
$scope.suggestDimensionValues = function(query, callback) {
|
|
|
|
if (!$scope.target.namespace || !$scope.target.metricName) {
|
|
|
|
return callback([]);
|
|
|
|
}
|
|
|
|
|
2015-10-01 14:20:52 -05:00
|
|
|
return $scope.datasource.getDimensionValues(
|
2015-08-10 20:25:25 -05:00
|
|
|
$scope.target.region,
|
|
|
|
$scope.target.namespace,
|
|
|
|
$scope.target.metricName,
|
2015-08-11 05:51:14 -05:00
|
|
|
$scope.target.dimensions
|
2015-10-01 14:20:52 -05:00
|
|
|
).then(function(result) {
|
2015-10-02 02:04:46 -05:00
|
|
|
callback(result);
|
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];
|
2015-08-30 00:59:58 -05:00
|
|
|
result[k.replace(/\$/g, '\uFF04')] = v.replace(/\$/g, '\$');
|
2015-08-21 06:09:23 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-09-02 05:36:56 -05:00
|
|
|
$scope.init();
|
|
|
|
|
2015-07-29 21:37:31 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|