queryCtrl.js 2.9 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, $timeout) {
  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.$on('typeahead-updated', function() {
  20. $timeout($scope.targetBlur);
  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.duplicate = function() {
  32. var clone = angular.copy($scope.target);
  33. $scope.panel.targets.push(clone);
  34. };
  35. $scope.suggestMetrics = function(query, callback) {
  36. $scope.datasource
  37. .performSuggestQuery(query, 'metrics')
  38. .then(callback);
  39. };
  40. $scope.suggestTagKeys = function(query, callback) {
  41. $scope.datasource
  42. .performSuggestQuery(query, 'tagk')
  43. .then(callback);
  44. };
  45. $scope.suggestTagValues = function(query, callback) {
  46. $scope.datasource
  47. .performSuggestQuery(query, 'tagv')
  48. .then(callback);
  49. };
  50. $scope.addTag = function() {
  51. if (!$scope.addTagMode) {
  52. $scope.addTagMode = true;
  53. return;
  54. }
  55. if (!$scope.target.tags) {
  56. $scope.target.tags = {};
  57. }
  58. $scope.target.errors = validateTarget($scope.target);
  59. if (!$scope.target.errors.tags) {
  60. $scope.target.tags[$scope.target.currentTagKey] = $scope.target.currentTagValue;
  61. $scope.target.currentTagKey = '';
  62. $scope.target.currentTagValue = '';
  63. $scope.targetBlur();
  64. }
  65. $scope.addTagMode = false;
  66. };
  67. $scope.removeTag = function(key) {
  68. delete $scope.target.tags[key];
  69. $scope.targetBlur();
  70. };
  71. function validateTarget(target) {
  72. var errs = {};
  73. if (target.shouldDownsample) {
  74. try {
  75. if (target.downsampleInterval) {
  76. kbn.describe_interval(target.downsampleInterval);
  77. } else {
  78. errs.downsampleInterval = "You must supply a downsample interval (e.g. '1m' or '1h').";
  79. }
  80. } catch(err) {
  81. errs.downsampleInterval = err.message;
  82. }
  83. }
  84. if (target.tags && _.has(target.tags, target.currentTagKey)) {
  85. errs.tags = "Duplicate tag key '" + target.currentTagKey + "'.";
  86. }
  87. return errs;
  88. }
  89. });
  90. });