queryCtrl.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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.directive('metricQueryEditorOpentsdb', function() {
  10. return {
  11. controller: 'OpenTSDBQueryCtrl',
  12. templateUrl: 'app/plugins/datasource/opentsdb/partials/query.editor.html',
  13. };
  14. });
  15. module.controller('OpenTSDBQueryCtrl', function($scope, $timeout) {
  16. $scope.init = function() {
  17. $scope.target.errors = validateTarget($scope.target);
  18. $scope.aggregators = ['avg', 'sum', 'min', 'max', 'dev', 'zimsum', 'mimmin', 'mimmax'];
  19. if (!$scope.target.aggregator) {
  20. $scope.target.aggregator = 'sum';
  21. }
  22. if (!$scope.target.downsampleAggregator) {
  23. $scope.target.downsampleAggregator = 'avg';
  24. }
  25. $scope.$on('typeahead-updated', function() {
  26. $timeout($scope.targetBlur);
  27. });
  28. };
  29. $scope.targetBlur = function() {
  30. $scope.target.errors = validateTarget($scope.target);
  31. // this does not work so good
  32. if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) {
  33. $scope.oldTarget = angular.copy($scope.target);
  34. $scope.get_data();
  35. }
  36. };
  37. $scope.duplicate = function() {
  38. var clone = angular.copy($scope.target);
  39. $scope.panel.targets.push(clone);
  40. };
  41. $scope.getTextValues = function(metricFindResult) {
  42. return _.map(metricFindResult, function(value) { return value.text; });
  43. };
  44. $scope.suggestMetrics = function(query, callback) {
  45. $scope.datasource.metricFindQuery('metrics(' + query + ')')
  46. .then($scope.getTextValues)
  47. .then(callback);
  48. };
  49. $scope.suggestTagKeys = function(query, callback) {
  50. $scope.datasource.metricFindQuery('tag_names(' + $scope.target.metric + ')')
  51. .then($scope.getTextValues)
  52. .then(callback);
  53. };
  54. $scope.suggestTagValues = function(query, callback) {
  55. $scope.datasource.metricFindQuery('tag_values(' + $scope.target.metric + ',' + $scope.target.currentTagKey + ')')
  56. .then($scope.getTextValues)
  57. .then(callback);
  58. };
  59. $scope.addTag = function() {
  60. if (!$scope.addTagMode) {
  61. $scope.addTagMode = true;
  62. return;
  63. }
  64. if (!$scope.target.tags) {
  65. $scope.target.tags = {};
  66. }
  67. $scope.target.errors = validateTarget($scope.target);
  68. if (!$scope.target.errors.tags) {
  69. $scope.target.tags[$scope.target.currentTagKey] = $scope.target.currentTagValue;
  70. $scope.target.currentTagKey = '';
  71. $scope.target.currentTagValue = '';
  72. $scope.targetBlur();
  73. }
  74. $scope.addTagMode = false;
  75. };
  76. $scope.removeTag = function(key) {
  77. delete $scope.target.tags[key];
  78. $scope.targetBlur();
  79. };
  80. function validateTarget(target) {
  81. var errs = {};
  82. if (target.shouldDownsample) {
  83. try {
  84. if (target.downsampleInterval) {
  85. kbn.describe_interval(target.downsampleInterval);
  86. } else {
  87. errs.downsampleInterval = "You must supply a downsample interval (e.g. '1m' or '1h').";
  88. }
  89. } catch(err) {
  90. errs.downsampleInterval = err.message;
  91. }
  92. }
  93. if (target.tags && _.has(target.tags, target.currentTagKey)) {
  94. errs.tags = "Duplicate tag key '" + target.currentTagKey + "'.";
  95. }
  96. return errs;
  97. }
  98. $scope.init();
  99. });
  100. });