grafana/public/app/plugins/datasource/cloudwatch/query_ctrl.js

166 lines
5.1 KiB
JavaScript
Raw Normal View History

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');
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 || {};
$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.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
};
$scope.getRegions = function() {
return $scope.datasource.metricFindQuery('regions()')
.then($scope.transformToSegments(true));
};
$scope.getNamespaces = function() {
return $scope.datasource.metricFindQuery('namespaces()')
.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
};
$scope.regionChanged = function() {
$scope.target.region = $scope.regionSegment.value;
$scope.get_data();
2015-07-29 21:37:31 -05:00
};
$scope.namespaceChanged = function() {
$scope.target.namespace = $scope.namespaceSegment.value;
$scope.get_data();
2015-08-10 09:15:25 -05:00
};
$scope.metricChanged = function() {
$scope.target.metricName = $scope.metricSegment.value;
$scope.get_data();
2015-07-29 21:37:31 -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
$scope.datasource.getDimensionKeys($scope.target.namespace).then(function(result) {
callback(_.pluck(result, 'text'));
});
2015-07-29 21:37:31 -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([]);
}
return $scope.datasource.getDimensionValues(
2015-08-10 20:25:25 -05:00
$scope.target.region,
$scope.target.namespace,
$scope.target.metricName,
$scope.target.dimensions
).then(function(result) {
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;
$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) {
key = key.replace(/\\\$/g, '$');
2015-07-29 21:37:31 -05:00
delete $scope.target.dimensions[key];
$scope.target.escapedDimensions = this.escapeDimensions($scope.target.dimensions);
2015-07-29 21:37:31 -05:00
$scope.refreshMetricData();
};
$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, '\$');
});
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;
}
$scope.init();
2015-07-29 21:37:31 -05:00
});
});