query_parameter_ctrl.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. import angular, { IQService } from 'angular';
  2. import coreModule from 'app/core/core_module';
  3. import _ from 'lodash';
  4. import { TemplateSrv } from 'app/features/templating/template_srv';
  5. import DatasourceSrv from 'app/features/plugins/datasource_srv';
  6. export class CloudWatchQueryParameterCtrl {
  7. /** @ngInject */
  8. constructor($scope: any, templateSrv: TemplateSrv, uiSegmentSrv: any, datasourceSrv: DatasourceSrv, $q: IQService) {
  9. $scope.init = () => {
  10. const target = $scope.target;
  11. target.namespace = target.namespace || '';
  12. target.metricName = target.metricName || '';
  13. target.statistics = target.statistics || ['Average'];
  14. target.dimensions = target.dimensions || {};
  15. target.period = target.period || '';
  16. target.region = target.region || 'default';
  17. target.id = target.id || '';
  18. target.expression = target.expression || '';
  19. target.returnData = target.returnData || false;
  20. target.highResolution = target.highResolution || false;
  21. $scope.regionSegment = uiSegmentSrv.getSegmentForValue($scope.target.region, 'select region');
  22. $scope.namespaceSegment = uiSegmentSrv.getSegmentForValue($scope.target.namespace, 'select namespace');
  23. $scope.metricSegment = uiSegmentSrv.getSegmentForValue($scope.target.metricName, 'select metric');
  24. $scope.dimSegments = _.reduce(
  25. $scope.target.dimensions,
  26. (memo, value, key) => {
  27. memo.push(uiSegmentSrv.newKey(key));
  28. memo.push(uiSegmentSrv.newOperator('='));
  29. memo.push(uiSegmentSrv.newKeyValue(value));
  30. return memo;
  31. },
  32. []
  33. );
  34. $scope.statSegments = _.map($scope.target.statistics, stat => {
  35. return uiSegmentSrv.getSegmentForValue(stat);
  36. });
  37. $scope.ensurePlusButton($scope.statSegments);
  38. $scope.ensurePlusButton($scope.dimSegments);
  39. $scope.removeDimSegment = uiSegmentSrv.newSegment({
  40. fake: true,
  41. value: '-- remove dimension --',
  42. });
  43. $scope.removeStatSegment = uiSegmentSrv.newSegment({
  44. fake: true,
  45. value: '-- remove stat --',
  46. });
  47. if (_.isEmpty($scope.target.region)) {
  48. $scope.target.region = 'default';
  49. }
  50. if (!$scope.onChange) {
  51. $scope.onChange = () => {};
  52. }
  53. };
  54. $scope.getStatSegments = () => {
  55. return $q.when(
  56. _.flatten([
  57. angular.copy($scope.removeStatSegment),
  58. _.map($scope.datasource.standardStatistics, s => {
  59. return uiSegmentSrv.getSegmentForValue(s);
  60. }),
  61. uiSegmentSrv.getSegmentForValue('pNN.NN'),
  62. ])
  63. );
  64. };
  65. $scope.statSegmentChanged = (segment: any, index: number) => {
  66. if (segment.value === $scope.removeStatSegment.value) {
  67. $scope.statSegments.splice(index, 1);
  68. } else {
  69. segment.type = 'value';
  70. }
  71. $scope.target.statistics = _.reduce(
  72. $scope.statSegments,
  73. (memo, seg) => {
  74. if (!seg.fake) {
  75. memo.push(seg.value);
  76. }
  77. return memo;
  78. },
  79. []
  80. );
  81. $scope.ensurePlusButton($scope.statSegments);
  82. $scope.onChange();
  83. };
  84. $scope.ensurePlusButton = (segments: any) => {
  85. const count = segments.length;
  86. const lastSegment = segments[Math.max(count - 1, 0)];
  87. if (!lastSegment || lastSegment.type !== 'plus-button') {
  88. segments.push(uiSegmentSrv.newPlusButton());
  89. }
  90. };
  91. $scope.getDimSegments = (segment: any, $index: number) => {
  92. if (segment.type === 'operator') {
  93. return $q.when([]);
  94. }
  95. const target = $scope.target;
  96. let query = $q.when([]);
  97. if (segment.type === 'key' || segment.type === 'plus-button') {
  98. query = $scope.datasource.getDimensionKeys($scope.target.namespace, $scope.target.region);
  99. } else if (segment.type === 'value') {
  100. const dimensionKey = $scope.dimSegments[$index - 2].value;
  101. delete target.dimensions[dimensionKey];
  102. query = $scope.datasource.getDimensionValues(
  103. target.region,
  104. target.namespace,
  105. target.metricName,
  106. dimensionKey,
  107. target.dimensions
  108. );
  109. }
  110. return query.then($scope.transformToSegments(true)).then(results => {
  111. if (segment.type === 'key') {
  112. results.splice(0, 0, angular.copy($scope.removeDimSegment));
  113. }
  114. return results;
  115. });
  116. };
  117. $scope.dimSegmentChanged = (segment: any, index: number) => {
  118. $scope.dimSegments[index] = segment;
  119. if (segment.value === $scope.removeDimSegment.value) {
  120. $scope.dimSegments.splice(index, 3);
  121. } else if (segment.type === 'plus-button') {
  122. $scope.dimSegments.push(uiSegmentSrv.newOperator('='));
  123. $scope.dimSegments.push(uiSegmentSrv.newFake('select dimension value', 'value', 'query-segment-value'));
  124. segment.type = 'key';
  125. segment.cssClass = 'query-segment-key';
  126. }
  127. $scope.syncDimSegmentsWithModel();
  128. $scope.ensurePlusButton($scope.dimSegments);
  129. $scope.onChange();
  130. };
  131. $scope.syncDimSegmentsWithModel = () => {
  132. const dims: any = {};
  133. const length = $scope.dimSegments.length;
  134. for (let i = 0; i < length - 2; i += 3) {
  135. const keySegment = $scope.dimSegments[i];
  136. const valueSegment = $scope.dimSegments[i + 2];
  137. if (!valueSegment.fake) {
  138. dims[keySegment.value] = valueSegment.value;
  139. }
  140. }
  141. $scope.target.dimensions = dims;
  142. };
  143. $scope.getRegions = () => {
  144. return $scope.datasource
  145. .metricFindQuery('regions()')
  146. .then((results: any) => {
  147. results.unshift({ text: 'default' });
  148. return results;
  149. })
  150. .then($scope.transformToSegments(true));
  151. };
  152. $scope.getNamespaces = () => {
  153. return $scope.datasource.metricFindQuery('namespaces()').then($scope.transformToSegments(true));
  154. };
  155. $scope.getMetrics = () => {
  156. return $scope.datasource
  157. .metricFindQuery('metrics(' + $scope.target.namespace + ',' + $scope.target.region + ')')
  158. .then($scope.transformToSegments(true));
  159. };
  160. $scope.regionChanged = () => {
  161. $scope.target.region = $scope.regionSegment.value;
  162. $scope.onChange();
  163. };
  164. $scope.namespaceChanged = () => {
  165. $scope.target.namespace = $scope.namespaceSegment.value;
  166. $scope.onChange();
  167. };
  168. $scope.metricChanged = () => {
  169. $scope.target.metricName = $scope.metricSegment.value;
  170. $scope.onChange();
  171. };
  172. $scope.transformToSegments = (addTemplateVars: any) => {
  173. return (results: any) => {
  174. const segments = _.map(results, segment => {
  175. return uiSegmentSrv.newSegment({
  176. value: segment.text,
  177. expandable: segment.expandable,
  178. });
  179. });
  180. if (addTemplateVars) {
  181. _.each(templateSrv.variables, variable => {
  182. segments.unshift(
  183. uiSegmentSrv.newSegment({
  184. type: 'template',
  185. value: '$' + variable.name,
  186. expandable: true,
  187. })
  188. );
  189. });
  190. }
  191. return segments;
  192. };
  193. };
  194. $scope.init();
  195. }
  196. }
  197. export function cloudWatchQueryParameter() {
  198. return {
  199. templateUrl: 'public/app/plugins/datasource/cloudwatch/partials/query.parameter.html',
  200. controller: CloudWatchQueryParameterCtrl,
  201. restrict: 'E',
  202. scope: {
  203. target: '=',
  204. datasource: '=',
  205. onChange: '&',
  206. },
  207. };
  208. }
  209. coreModule.directive('cloudwatchQueryParameter', cloudWatchQueryParameter);