queryCtrl.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. define([
  2. 'angular',
  3. 'lodash'
  4. ],
  5. function (angular, _) {
  6. 'use strict';
  7. var module = angular.module('grafana.controllers');
  8. var metricList = [];
  9. var targetLetters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O'];
  10. module.controller('KairosDBQueryCtrl', function($scope) {
  11. $scope.init = function() {
  12. $scope.panel.stack = false;
  13. if (!$scope.panel.downsampling) {
  14. $scope.panel.downsampling = 'avg';
  15. }
  16. if (!$scope.target.downsampling) {
  17. $scope.target.downsampling = $scope.panel.downsampling;
  18. $scope.target.sampling = $scope.panel.sampling;
  19. }
  20. $scope.targetLetters = targetLetters;
  21. $scope.target.errors = validateTarget($scope.target);
  22. };
  23. $scope.targetBlur = function() {
  24. $scope.target.errors = validateTarget($scope.target);
  25. if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) {
  26. $scope.oldTarget = angular.copy($scope.target);
  27. $scope.get_data();
  28. }
  29. };
  30. $scope.panelBlur = function() {
  31. _.each($scope.panel.targets, function(target) {
  32. target.downsampling = $scope.panel.downsampling;
  33. target.sampling = $scope.panel.sampling;
  34. });
  35. $scope.get_data();
  36. };
  37. $scope.duplicate = function() {
  38. var clone = angular.copy($scope.target);
  39. $scope.panel.targets.push(clone);
  40. };
  41. $scope.moveMetricQuery = function(fromIndex, toIndex) {
  42. _.move($scope.panel.targets, fromIndex, toIndex);
  43. };
  44. $scope.suggestMetrics = function(query, callback) {
  45. if (!_.isEmpty(metricList)) {
  46. return metricList;
  47. }
  48. else {
  49. $scope.datasource.performMetricSuggestQuery().then(function(result) {
  50. metricList = result;
  51. callback(metricList);
  52. });
  53. }
  54. };
  55. $scope.suggestTagKeys = function(query, callback) {
  56. callback($scope.datasource.performTagSuggestQuery($scope.target.metric, $scope.rangeUnparsed, 'key', ''));
  57. };
  58. $scope.suggestTagValues = function(query, callback) {
  59. callback($scope.datasource
  60. .performTagSuggestQuery($scope.target.metric, $scope.rangeUnparsed, 'value', $scope.target.currentTagKey));
  61. };
  62. // Filter metric by tag
  63. $scope.addFilterTag = function() {
  64. if (!$scope.addFilterTagMode) {
  65. $scope.addFilterTagMode = true;
  66. $scope.validateFilterTag();
  67. return;
  68. }
  69. if (!$scope.target.tags) {
  70. $scope.target.tags = {};
  71. }
  72. $scope.validateFilterTag();
  73. if (!$scope.target.errors.tags) {
  74. if (!_.has($scope.target.tags, $scope.target.currentTagKey)) {
  75. $scope.target.tags[$scope.target.currentTagKey] = [];
  76. }
  77. $scope.target.tags[$scope.target.currentTagKey].push($scope.target.currentTagValue);
  78. $scope.target.currentTagKey = '';
  79. $scope.target.currentTagValue = '';
  80. $scope.targetBlur();
  81. }
  82. $scope.addFilterTagMode = false;
  83. };
  84. $scope.removeFilterTag = function(key) {
  85. delete $scope.target.tags[key];
  86. if (_.size($scope.target.tags) === 0) {
  87. $scope.target.tags = null;
  88. }
  89. $scope.targetBlur();
  90. };
  91. $scope.validateFilterTag = function() {
  92. $scope.target.errors.tags = null;
  93. if (!$scope.target.currentTagKey || !$scope.target.currentTagValue) {
  94. $scope.target.errors.tags = "You must specify a tag name and value.";
  95. }
  96. };
  97. //////////////////////////////
  98. // GROUP BY
  99. //////////////////////////////
  100. $scope.addGroupBy = function() {
  101. if (!$scope.addGroupByMode) {
  102. $scope.addGroupByMode = true;
  103. $scope.target.currentGroupByType = 'tag';
  104. $scope.isTagGroupBy = true;
  105. $scope.validateGroupBy();
  106. return;
  107. }
  108. $scope.validateGroupBy();
  109. // nb: if error is found, means that user clicked on cross : cancels input
  110. if (_.isEmpty($scope.target.errors.groupBy)) {
  111. if ($scope.isTagGroupBy) {
  112. if (!$scope.target.groupByTags) {
  113. $scope.target.groupByTags = [];
  114. }
  115. if (!_.contains($scope.target.groupByTags, $scope.target.groupBy.tagKey)) {
  116. $scope.target.groupByTags.push($scope.target.groupBy.tagKey);
  117. $scope.targetBlur();
  118. }
  119. $scope.target.groupBy.tagKey = '';
  120. }
  121. else {
  122. if (!$scope.target.nonTagGroupBys) {
  123. $scope.target.nonTagGroupBys = [];
  124. }
  125. var groupBy = {
  126. name: $scope.target.currentGroupByType
  127. };
  128. if ($scope.isValueGroupBy) {groupBy.range_size = $scope.target.groupBy.valueRange;}
  129. else if ($scope.isTimeGroupBy) {
  130. groupBy.range_size = $scope.target.groupBy.timeInterval;
  131. groupBy.group_count = $scope.target.groupBy.groupCount;
  132. }
  133. $scope.target.nonTagGroupBys.push(groupBy);
  134. }
  135. $scope.targetBlur();
  136. }
  137. $scope.isTagGroupBy = false;
  138. $scope.isValueGroupBy = false;
  139. $scope.isTimeGroupBy = false;
  140. $scope.addGroupByMode = false;
  141. };
  142. $scope.removeGroupByTag = function(index) {
  143. $scope.target.groupByTags.splice(index, 1);
  144. if (_.size($scope.target.groupByTags) === 0) {
  145. $scope.target.groupByTags = null;
  146. }
  147. $scope.targetBlur();
  148. };
  149. $scope.removeNonTagGroupBy = function(index) {
  150. $scope.target.nonTagGroupBys.splice(index, 1);
  151. if (_.size($scope.target.nonTagGroupBys) === 0) {
  152. $scope.target.nonTagGroupBys = null;
  153. }
  154. $scope.targetBlur();
  155. };
  156. $scope.changeGroupByInput = function() {
  157. $scope.isTagGroupBy = $scope.target.currentGroupByType === 'tag';
  158. $scope.isValueGroupBy = $scope.target.currentGroupByType === 'value';
  159. $scope.isTimeGroupBy = $scope.target.currentGroupByType === 'time';
  160. $scope.validateGroupBy();
  161. };
  162. $scope.validateGroupBy = function() {
  163. delete $scope.target.errors.groupBy;
  164. var errors = {};
  165. $scope.isGroupByValid = true;
  166. if ($scope.isTagGroupBy) {
  167. if (!$scope.target.groupBy.tagKey) {
  168. $scope.isGroupByValid = false;
  169. errors.tagKey = 'You must supply a tag name';
  170. }
  171. }
  172. if ($scope.isValueGroupBy) {
  173. if (!$scope.target.groupBy.valueRange || !isInt($scope.target.groupBy.valueRange)) {
  174. errors.valueRange = "Range must be an integer";
  175. $scope.isGroupByValid = false;
  176. }
  177. }
  178. if ($scope.isTimeGroupBy) {
  179. try {
  180. $scope.datasource.convertToKairosInterval($scope.target.groupBy.timeInterval);
  181. } catch (err) {
  182. errors.timeInterval = err.message;
  183. $scope.isGroupByValid = false;
  184. }
  185. if (!$scope.target.groupBy.groupCount || !isInt($scope.target.groupBy.groupCount)) {
  186. errors.groupCount = "Group count must be an integer";
  187. $scope.isGroupByValid = false;
  188. }
  189. }
  190. if (!_.isEmpty(errors)) {
  191. $scope.target.errors.groupBy = errors;
  192. }
  193. };
  194. function isInt(n) {
  195. return parseInt(n) % 1 === 0;
  196. }
  197. //////////////////////////////
  198. // HORIZONTAL AGGREGATION
  199. //////////////////////////////
  200. $scope.addHorizontalAggregator = function() {
  201. if (!$scope.addHorizontalAggregatorMode) {
  202. $scope.addHorizontalAggregatorMode = true;
  203. $scope.target.currentHorizontalAggregatorName = 'avg';
  204. $scope.hasSamplingRate = true;
  205. $scope.validateHorizontalAggregator();
  206. return;
  207. }
  208. $scope.validateHorizontalAggregator();
  209. // nb: if error is found, means that user clicked on cross : cancels input
  210. if (_.isEmpty($scope.target.errors.horAggregator)) {
  211. if (!$scope.target.horizontalAggregators) {
  212. $scope.target.horizontalAggregators = [];
  213. }
  214. var aggregator = {
  215. name:$scope.target.currentHorizontalAggregatorName
  216. };
  217. if ($scope.hasSamplingRate) {aggregator.sampling_rate = $scope.target.horAggregator.samplingRate;}
  218. if ($scope.hasUnit) {aggregator.unit = $scope.target.horAggregator.unit;}
  219. if ($scope.hasFactor) {aggregator.factor = $scope.target.horAggregator.factor;}
  220. if ($scope.hasPercentile) {aggregator.percentile = $scope.target.horAggregator.percentile;}
  221. $scope.target.horizontalAggregators.push(aggregator);
  222. $scope.targetBlur();
  223. }
  224. $scope.addHorizontalAggregatorMode = false;
  225. $scope.hasSamplingRate = false;
  226. $scope.hasUnit = false;
  227. $scope.hasFactor = false;
  228. $scope.hasPercentile = false;
  229. };
  230. $scope.removeHorizontalAggregator = function(index) {
  231. $scope.target.horizontalAggregators.splice(index, 1);
  232. if (_.size($scope.target.horizontalAggregators) === 0) {
  233. $scope.target.horizontalAggregators = null;
  234. }
  235. $scope.targetBlur();
  236. };
  237. $scope.changeHorAggregationInput = function() {
  238. $scope.hasSamplingRate = _.contains(['avg','dev','max','min','sum','least_squares','count','percentile'],
  239. $scope.target.currentHorizontalAggregatorName);
  240. $scope.hasUnit = _.contains(['sampler','rate'], $scope.target.currentHorizontalAggregatorName);
  241. $scope.hasFactor = _.contains(['div','scale'], $scope.target.currentHorizontalAggregatorName);
  242. $scope.hasPercentile = 'percentile' === $scope.target.currentHorizontalAggregatorName;
  243. $scope.validateHorizontalAggregator();
  244. };
  245. $scope.validateHorizontalAggregator = function() {
  246. delete $scope.target.errors.horAggregator;
  247. var errors = {};
  248. $scope.isAggregatorValid = true;
  249. if ($scope.hasSamplingRate) {
  250. try {
  251. $scope.datasource.convertToKairosInterval($scope.target.horAggregator.samplingRate);
  252. } catch (err) {
  253. errors.samplingRate = err.message;
  254. $scope.isAggregatorValid = false;
  255. }
  256. }
  257. if ($scope.hasFactor) {
  258. if (!$scope.target.horAggregator.factor) {
  259. errors.factor = 'You must supply a numeric value for this aggregator';
  260. $scope.isAggregatorValid = false;
  261. }
  262. else if (parseInt($scope.target.horAggregator.factor) === 0 && $scope.target.currentHorizontalAggregatorName === 'div') {
  263. errors.factor = 'Cannot divide by 0';
  264. $scope.isAggregatorValid = false;
  265. }
  266. }
  267. if ($scope.hasPercentile) {
  268. if (!$scope.target.horAggregator.percentile ||
  269. $scope.target.horAggregator.percentile<=0 ||
  270. $scope.target.horAggregator.percentile>1) {
  271. errors.percentile = 'Percentile must be between 0 and 1';
  272. $scope.isAggregatorValid = false;
  273. }
  274. }
  275. if (!_.isEmpty(errors)) {
  276. $scope.target.errors.horAggregator = errors;
  277. }
  278. };
  279. $scope.alert = function(message) {
  280. alert(message);
  281. };
  282. // Validation
  283. function validateTarget(target) {
  284. var errs = {};
  285. if (!target.metric) {
  286. errs.metric = "You must supply a metric name.";
  287. }
  288. try {
  289. if (target.sampling) {
  290. $scope.datasource.convertToKairosInterval(target.sampling);
  291. }
  292. } catch (err) {
  293. errs.sampling = err.message;
  294. }
  295. return errs;
  296. }
  297. });
  298. });