queryCtrl.js 12 KB

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