queryCtrl.js 11 KB

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