queryCtrl.js 11 KB

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