query_parameter_ctrl.ts 7.5 KB

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