queryCtrl.js 12 KB

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