| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- import angular from 'angular';
- import _ from 'lodash';
- export class CloudWatchQueryParameter {
- constructor() {
- return {
- templateUrl: 'public/app/plugins/datasource/cloudwatch/partials/query.parameter.html',
- controller: 'CloudWatchQueryParameterCtrl',
- restrict: 'E',
- scope: {
- target: '=',
- datasource: '=',
- onChange: '&',
- },
- };
- }
- }
- export class CloudWatchQueryParameterCtrl {
- /** @ngInject */
- constructor($scope, templateSrv, uiSegmentSrv, datasourceSrv, $q) {
- $scope.init = function() {
- const target = $scope.target;
- target.namespace = target.namespace || '';
- target.metricName = target.metricName || '';
- target.statistics = target.statistics || ['Average'];
- target.dimensions = target.dimensions || {};
- target.period = target.period || '';
- target.region = target.region || 'default';
- target.id = target.id || '';
- target.expression = target.expression || '';
- target.returnData = target.returnData || false;
- target.highResolution = target.highResolution || false;
- $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');
- $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.statSegments = _.map($scope.target.statistics, function(stat) {
- return uiSegmentSrv.getSegmentForValue(stat);
- });
- $scope.ensurePlusButton($scope.statSegments);
- $scope.ensurePlusButton($scope.dimSegments);
- $scope.removeDimSegment = uiSegmentSrv.newSegment({
- fake: true,
- value: '-- remove dimension --',
- });
- $scope.removeStatSegment = uiSegmentSrv.newSegment({
- fake: true,
- value: '-- remove stat --',
- });
- if (_.isEmpty($scope.target.region)) {
- $scope.target.region = 'default';
- }
- if (!$scope.onChange) {
- $scope.onChange = function() {};
- }
- };
- $scope.getStatSegments = function() {
- return $q.when(
- _.flatten([
- angular.copy($scope.removeStatSegment),
- _.map($scope.datasource.standardStatistics, function(s) {
- return uiSegmentSrv.getSegmentForValue(s);
- }),
- uiSegmentSrv.getSegmentForValue('pNN.NN'),
- ])
- );
- };
- $scope.statSegmentChanged = function(segment, index) {
- if (segment.value === $scope.removeStatSegment.value) {
- $scope.statSegments.splice(index, 1);
- } else {
- segment.type = 'value';
- }
- $scope.target.statistics = _.reduce(
- $scope.statSegments,
- function(memo, seg) {
- if (!seg.fake) {
- memo.push(seg.value);
- }
- return memo;
- },
- []
- );
- $scope.ensurePlusButton($scope.statSegments);
- $scope.onChange();
- };
- $scope.ensurePlusButton = function(segments) {
- const count = segments.length;
- const lastSegment = segments[Math.max(count - 1, 0)];
- if (!lastSegment || lastSegment.type !== 'plus-button') {
- segments.push(uiSegmentSrv.newPlusButton());
- }
- };
- $scope.getDimSegments = function(segment, $index) {
- if (segment.type === 'operator') {
- return $q.when([]);
- }
- const target = $scope.target;
- var query = $q.when([]);
- if (segment.type === 'key' || segment.type === 'plus-button') {
- query = $scope.datasource.getDimensionKeys($scope.target.namespace, $scope.target.region);
- } else if (segment.type === 'value') {
- const dimensionKey = $scope.dimSegments[$index - 2].value;
- query = $scope.datasource.getDimensionValues(
- target.region,
- target.namespace,
- target.metricName,
- dimensionKey,
- target.dimensions
- );
- }
- 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.syncDimSegmentsWithModel();
- $scope.ensurePlusButton($scope.dimSegments);
- $scope.onChange();
- };
- $scope.syncDimSegmentsWithModel = function() {
- const dims = {};
- const length = $scope.dimSegments.length;
- for (var i = 0; i < length - 2; i += 3) {
- const keySegment = $scope.dimSegments[i];
- const valueSegment = $scope.dimSegments[i + 2];
- if (!valueSegment.fake) {
- dims[keySegment.value] = valueSegment.value;
- }
- }
- $scope.target.dimensions = dims;
- };
- $scope.getRegions = function() {
- return $scope.datasource
- .metricFindQuery('regions()')
- .then(function(results) {
- results.unshift({ text: 'default' });
- return results;
- })
- .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 + ',' + $scope.target.region + ')')
- .then($scope.transformToSegments(true));
- };
- $scope.regionChanged = function() {
- $scope.target.region = $scope.regionSegment.value;
- $scope.onChange();
- };
- $scope.namespaceChanged = function() {
- $scope.target.namespace = $scope.namespaceSegment.value;
- $scope.onChange();
- };
- $scope.metricChanged = function() {
- $scope.target.metricName = $scope.metricSegment.value;
- $scope.onChange();
- };
- $scope.transformToSegments = function(addTemplateVars) {
- return function(results) {
- const 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.init();
- }
- }
- angular.module('grafana.controllers').directive('cloudwatchQueryParameter', CloudWatchQueryParameter);
- angular.module('grafana.controllers').controller('CloudWatchQueryParameterCtrl', CloudWatchQueryParameterCtrl);
|