queryCtrl.js 3.3 KB

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