queryCtrl.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. define([
  2. 'angular',
  3. 'lodash',
  4. 'kbn'
  5. ],
  6. function (angular, _, kbn) {
  7. 'use strict';
  8. var module = angular.module('grafana.controllers');
  9. module.controller('OpenTSDBQueryCtrl', function($scope) {
  10. $scope.init = function() {
  11. $scope.target.errors = validateTarget($scope.target);
  12. $scope.aggregators = ['avg', 'sum', 'min', 'max', 'dev', 'zimsum', 'mimmin', 'mimmax'];
  13. if (!$scope.target.aggregator) {
  14. $scope.target.aggregator = 'sum';
  15. }
  16. if (!$scope.target.downsampleAggregator) {
  17. $scope.target.downsampleAggregator = 'avg';
  18. }
  19. $scope.datasource.getAggregators().then(function(aggs) {
  20. $scope.aggregators = aggs;
  21. });
  22. };
  23. $scope.targetBlur = function() {
  24. $scope.target.errors = validateTarget($scope.target);
  25. // this does not work so good
  26. if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) {
  27. $scope.oldTarget = angular.copy($scope.target);
  28. $scope.get_data();
  29. }
  30. };
  31. $scope.getTextValues = function(metricFindResult) {
  32. return _.map(metricFindResult, function(value) { return value.text; });
  33. };
  34. $scope.suggestMetrics = function(query, callback) {
  35. $scope.datasource.metricFindQuery('metrics(' + query + ')')
  36. .then($scope.getTextValues)
  37. .then(callback);
  38. };
  39. $scope.suggestTagKeys = function(query, callback) {
  40. $scope.datasource.metricFindQuery('tag_names(' + $scope.target.metric + ')')
  41. .then($scope.getTextValues)
  42. .then(callback);
  43. };
  44. $scope.suggestTagValues = function(query, callback) {
  45. $scope.datasource.metricFindQuery('tag_values(' + $scope.target.metric + ',' + $scope.target.currentTagKey + ')')
  46. .then($scope.getTextValues)
  47. .then(callback);
  48. };
  49. $scope.addTag = function() {
  50. if (!$scope.addTagMode) {
  51. $scope.addTagMode = true;
  52. return;
  53. }
  54. if (!$scope.target.tags) {
  55. $scope.target.tags = {};
  56. }
  57. $scope.target.errors = validateTarget($scope.target);
  58. if (!$scope.target.errors.tags) {
  59. $scope.target.tags[$scope.target.currentTagKey] = $scope.target.currentTagValue;
  60. $scope.target.currentTagKey = '';
  61. $scope.target.currentTagValue = '';
  62. $scope.targetBlur();
  63. }
  64. $scope.addTagMode = false;
  65. };
  66. $scope.removeTag = function(key) {
  67. delete $scope.target.tags[key];
  68. $scope.targetBlur();
  69. };
  70. function validateTarget(target) {
  71. var errs = {};
  72. if (target.shouldDownsample) {
  73. try {
  74. if (target.downsampleInterval) {
  75. kbn.describe_interval(target.downsampleInterval);
  76. } else {
  77. errs.downsampleInterval = "You must supply a downsample interval (e.g. '1m' or '1h').";
  78. }
  79. } catch(err) {
  80. errs.downsampleInterval = err.message;
  81. }
  82. }
  83. if (target.tags && _.has(target.tags, target.currentTagKey)) {
  84. errs.tags = "Duplicate tag key '" + target.currentTagKey + "'.";
  85. }
  86. return errs;
  87. }
  88. $scope.init();
  89. });
  90. });