queryCtrl.js 11 KB

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