queryCtrl.js 11 KB

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