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-10-08 04:37:47 -05:00
|
|
|
module.controller('CloudWatchQueryCtrl', function($scope, templateSrv, uiSegmentSrv, $q) {
|
2015-07-29 21:37:31 -05:00
|
|
|
|
|
|
|
$scope.init = function() {
|
|
|
|
$scope.target.namespace = $scope.target.namespace || '';
|
|
|
|
$scope.target.metricName = $scope.target.metricName || '';
|
2015-10-08 04:37:47 -05:00
|
|
|
$scope.target.statistics = $scope.target.statistics || {Average: true};
|
2015-07-29 21:37:31 -05:00
|
|
|
$scope.target.dimensions = $scope.target.dimensions || {};
|
|
|
|
$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
|
|
|
|
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-10-08 04:37:47 -05:00
|
|
|
|
|
|
|
$scope.dimSegments = _.reduce($scope.target.dimensions, function(memo, value, key) {
|
|
|
|
memo.push(uiSegmentSrv.newKey(key));
|
|
|
|
memo.push(uiSegmentSrv.newOperator("="));
|
|
|
|
memo.push(uiSegmentSrv.newKeyValue(value));
|
|
|
|
return memo;
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
$scope.fixSegments();
|
|
|
|
$scope.removeDimSegment = uiSegmentSrv.newSegment({fake: true, value: '-- remove dimension --'});
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.fixSegments = function() {
|
|
|
|
var count = $scope.dimSegments.length;
|
|
|
|
var lastSegment = $scope.dimSegments[Math.max(count-1, 0)];
|
|
|
|
|
|
|
|
if (!lastSegment || lastSegment.type !== 'plus-button') {
|
|
|
|
$scope.dimSegments.push(uiSegmentSrv.newPlusButton());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.getDimSegments = function(segment) {
|
|
|
|
if (segment.type === 'operator') { return $q.when([]); }
|
|
|
|
|
|
|
|
var target = $scope.target;
|
|
|
|
var query = $q.when([]);
|
|
|
|
|
|
|
|
if (segment.type === 'key' || segment.type === 'plus-button') {
|
|
|
|
query = $scope.datasource.getDimensionKeys($scope.target.namespace);
|
|
|
|
} else if (segment.type === 'value') {
|
|
|
|
query = $scope.datasource.getDimensionValues(target.region, target.namespace, target.metricName, {});
|
|
|
|
}
|
|
|
|
|
|
|
|
return query.then($scope.transformToSegments(true)).then(function(results) {
|
|
|
|
if (segment.type === 'key') {
|
|
|
|
results.splice(0, 0, angular.copy($scope.removeDimSegment));
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.dimSegmentChanged = function(segment, index) {
|
|
|
|
$scope.dimSegments[index] = segment;
|
|
|
|
|
|
|
|
if (segment.value === $scope.removeDimSegment.value) {
|
|
|
|
$scope.dimSegments.splice(index, 3);
|
|
|
|
}
|
|
|
|
else if (segment.type === 'plus-button') {
|
|
|
|
$scope.dimSegments.push(uiSegmentSrv.newOperator('='));
|
|
|
|
$scope.dimSegments.push(uiSegmentSrv.newFake('select dimension value', 'value', 'query-segment-value'));
|
|
|
|
segment.type = 'key';
|
|
|
|
segment.cssClass = 'query-segment-key';
|
|
|
|
}
|
|
|
|
|
|
|
|
$scope.fixSegments();
|
|
|
|
$scope.syncDimSegmentsWithModel();
|
|
|
|
$scope.get_data();
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.syncDimSegmentsWithModel = function() {
|
|
|
|
var dims = {};
|
|
|
|
var length = $scope.dimSegments.length;
|
|
|
|
|
|
|
|
for (var i = 0; i < length - 2; i += 3) {
|
|
|
|
var keySegment = $scope.dimSegments[i];
|
|
|
|
var valueSegment = $scope.dimSegments[i + 2];
|
|
|
|
if (!valueSegment.fake) {
|
|
|
|
dims[keySegment.value] = valueSegment.value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$scope.target.dimensions = dims;
|
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-10-08 04:37:47 -05:00
|
|
|
.then($scope.transformToSegments(true));
|
2015-09-02 05:36:56 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
$scope.getNamespaces = function() {
|
2015-10-02 04:10:21 -05:00
|
|
|
return $scope.datasource.metricFindQuery('namespaces()')
|
2015-10-08 04:37:47 -05:00
|
|
|
.then($scope.transformToSegments(true));
|
2015-09-02 05:36:56 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
$scope.getMetrics = function() {
|
|
|
|
return $scope.datasource.metricFindQuery('metrics(' + $scope.target.namespace + ')')
|
2015-10-08 04:37:47 -05:00
|
|
|
.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() {
|
2015-10-08 04:37:47 -05:00
|
|
|
if (!_.isEqual($scope.oldTarget, $scope.target)) {
|
2015-09-02 05:36:56 -05:00
|
|
|
$scope.oldTarget = angular.copy($scope.target);
|
|
|
|
$scope.get_data();
|
|
|
|
}
|
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();
|
|
|
|
};
|
|
|
|
|
2015-09-02 05:36:56 -05:00
|
|
|
$scope.init();
|
|
|
|
|
2015-07-29 21:37:31 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|