queryCtrl.js 11 KB

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