queryCtrl.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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('KairosDBTargetCtrl', 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. console.log($scope.target.groupBy.tagKey);
  128. if (!_.contains($scope.target.groupByTags, $scope.target.groupBy.tagKey)) {
  129. $scope.target.groupByTags.push($scope.target.groupBy.tagKey);
  130. $scope.targetBlur();
  131. }
  132. $scope.target.groupBy.tagKey = '';
  133. }
  134. else {
  135. if (!$scope.target.nonTagGroupBys) {
  136. $scope.target.nonTagGroupBys = [];
  137. }
  138. var groupBy = {
  139. name: $scope.target.currentGroupByType
  140. };
  141. if ($scope.isValueGroupBy) {groupBy.range_size = $scope.target.groupBy.valueRange;}
  142. else if ($scope.isTimeGroupBy) {
  143. groupBy.range_size = $scope.target.groupBy.timeInterval;
  144. groupBy.group_count = $scope.target.groupBy.groupCount;
  145. }
  146. $scope.target.nonTagGroupBys.push(groupBy);
  147. }
  148. $scope.targetBlur();
  149. }
  150. $scope.isTagGroupBy = false;
  151. $scope.isValueGroupBy = false;
  152. $scope.isTimeGroupBy = false;
  153. $scope.addGroupByMode = false;
  154. };
  155. $scope.removeGroupByTag = function(index) {
  156. $scope.target.groupByTags.splice(index, 1);
  157. if (_.size($scope.target.groupByTags) === 0) {
  158. $scope.target.groupByTags = null;
  159. }
  160. $scope.targetBlur();
  161. };
  162. $scope.removeNonTagGroupBy = function(index) {
  163. $scope.target.nonTagGroupBys.splice(index, 1);
  164. if (_.size($scope.target.nonTagGroupBys) === 0) {
  165. $scope.target.nonTagGroupBys = null;
  166. }
  167. $scope.targetBlur();
  168. };
  169. $scope.changeGroupByInput = function() {
  170. $scope.isTagGroupBy = $scope.target.currentGroupByType === 'tag';
  171. $scope.isValueGroupBy = $scope.target.currentGroupByType === 'value';
  172. $scope.isTimeGroupBy = $scope.target.currentGroupByType === 'time';
  173. $scope.validateGroupBy();
  174. };
  175. $scope.validateGroupBy = function() {
  176. delete $scope.target.errors.groupBy;
  177. var errors = {};
  178. $scope.isGroupByValid = true;
  179. if ($scope.isTagGroupBy) {
  180. if (!$scope.target.groupBy.tagKey) {
  181. $scope.isGroupByValid = false;
  182. errors.tagKey = 'You must supply a tag name';
  183. }
  184. }
  185. if ($scope.isValueGroupBy) {
  186. if (!$scope.target.groupBy.valueRange || !isInt($scope.target.groupBy.valueRange)) {
  187. errors.valueRange = "Range must be an integer";
  188. $scope.isGroupByValid = false;
  189. }
  190. }
  191. if ($scope.isTimeGroupBy) {
  192. try {
  193. $scope.datasource.convertToKairosInterval($scope.target.groupBy.timeInterval);
  194. } catch (err) {
  195. errors.timeInterval = err.message;
  196. $scope.isGroupByValid = false;
  197. }
  198. if (!$scope.target.groupBy.groupCount || !isInt($scope.target.groupBy.groupCount)) {
  199. errors.groupCount = "Group count must be an integer";
  200. $scope.isGroupByValid = false;
  201. }
  202. }
  203. if (!_.isEmpty(errors)) {
  204. $scope.target.errors.groupBy = errors;
  205. }
  206. };
  207. function isInt(n) {
  208. return parseInt(n) % 1 === 0;
  209. }
  210. //////////////////////////////
  211. // HORIZONTAL AGGREGATION
  212. //////////////////////////////
  213. $scope.addHorizontalAggregator = function() {
  214. if (!$scope.addHorizontalAggregatorMode) {
  215. $scope.addHorizontalAggregatorMode = true;
  216. $scope.target.currentHorizontalAggregatorName = 'avg';
  217. $scope.hasSamplingRate = true;
  218. $scope.validateHorizontalAggregator();
  219. return;
  220. }
  221. $scope.validateHorizontalAggregator();
  222. // nb: if error is found, means that user clicked on cross : cancels input
  223. if (_.isEmpty($scope.target.errors.horAggregator)) {
  224. if (!$scope.target.horizontalAggregators) {
  225. $scope.target.horizontalAggregators = [];
  226. }
  227. var aggregator = {
  228. name:$scope.target.currentHorizontalAggregatorName
  229. };
  230. if ($scope.hasSamplingRate) {aggregator.sampling_rate = $scope.target.horAggregator.samplingRate;}
  231. if ($scope.hasUnit) {aggregator.unit = $scope.target.horAggregator.unit;}
  232. if ($scope.hasFactor) {aggregator.factor = $scope.target.horAggregator.factor;}
  233. if ($scope.hasPercentile) {aggregator.percentile = $scope.target.horAggregator.percentile;}
  234. $scope.target.horizontalAggregators.push(aggregator);
  235. $scope.targetBlur();
  236. }
  237. $scope.addHorizontalAggregatorMode = false;
  238. $scope.hasSamplingRate = false;
  239. $scope.hasUnit = false;
  240. $scope.hasFactor = false;
  241. $scope.hasPercentile = false;
  242. };
  243. $scope.removeHorizontalAggregator = function(index) {
  244. $scope.target.horizontalAggregators.splice(index, 1);
  245. if (_.size($scope.target.horizontalAggregators) === 0) {
  246. $scope.target.horizontalAggregators = null;
  247. }
  248. $scope.targetBlur();
  249. };
  250. $scope.changeHorAggregationInput = function() {
  251. $scope.hasSamplingRate = _.contains(['avg','dev','max','min','sum','least_squares','count','percentile'],
  252. $scope.target.currentHorizontalAggregatorName);
  253. $scope.hasUnit = _.contains(['sampler','rate'], $scope.target.currentHorizontalAggregatorName);
  254. $scope.hasFactor = _.contains(['div','scale'], $scope.target.currentHorizontalAggregatorName);
  255. $scope.hasPercentile = 'percentile' === $scope.target.currentHorizontalAggregatorName;
  256. $scope.validateHorizontalAggregator();
  257. };
  258. $scope.validateHorizontalAggregator = function() {
  259. delete $scope.target.errors.horAggregator;
  260. var errors = {};
  261. $scope.isAggregatorValid = true;
  262. if ($scope.hasSamplingRate) {
  263. try {
  264. $scope.datasource.convertToKairosInterval($scope.target.horAggregator.samplingRate);
  265. } catch (err) {
  266. errors.samplingRate = err.message;
  267. $scope.isAggregatorValid = false;
  268. }
  269. }
  270. if ($scope.hasFactor) {
  271. if (!$scope.target.horAggregator.factor) {
  272. errors.factor = 'You must supply a numeric value for this aggregator';
  273. $scope.isAggregatorValid = false;
  274. }
  275. else if (parseInt($scope.target.horAggregator.factor) === 0 && $scope.target.currentHorizontalAggregatorName === 'div') {
  276. errors.factor = 'Cannot divide by 0';
  277. $scope.isAggregatorValid = false;
  278. }
  279. }
  280. if ($scope.hasPercentile) {
  281. if (!$scope.target.horAggregator.percentile ||
  282. $scope.target.horAggregator.percentile<=0 ||
  283. $scope.target.horAggregator.percentile>1) {
  284. errors.percentile = 'Percentile must be between 0 and 1';
  285. $scope.isAggregatorValid = false;
  286. }
  287. }
  288. if (!_.isEmpty(errors)) {
  289. $scope.target.errors.horAggregator = errors;
  290. }
  291. };
  292. $scope.alert = function(message) {
  293. alert(message);
  294. };
  295. // Validation
  296. function validateTarget(target) {
  297. var errs = {};
  298. if (!target.metric) {
  299. errs.metric = "You must supply a metric name.";
  300. }
  301. try {
  302. if (target.sampling) {
  303. $scope.datasource.convertToKairosInterval(target.sampling);
  304. }
  305. } catch (err) {
  306. errs.sampling = err.message;
  307. }
  308. return errs;
  309. }
  310. });
  311. });