queryCtrl.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. };
  20. $scope.targetBlur = function() {
  21. $scope.target.errors = validateTarget($scope.target);
  22. // this does not work so good
  23. if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) {
  24. $scope.oldTarget = angular.copy($scope.target);
  25. $scope.get_data();
  26. }
  27. };
  28. $scope.duplicate = function() {
  29. var clone = angular.copy($scope.target);
  30. $scope.panel.targets.push(clone);
  31. };
  32. $scope.getTextValues = function(metricFindResult) {
  33. return _.map(metricFindResult, function(value) { return value.text; });
  34. };
  35. $scope.suggestMetrics = function(query, callback) {
  36. $scope.datasource.metricFindQuery('metrics(' + query + ')')
  37. .then($scope.getTextValues)
  38. .then(callback);
  39. };
  40. $scope.suggestTagKeys = function(query, callback) {
  41. $scope.datasource.metricFindQuery('tag_names(' + $scope.target.metric + ')')
  42. .then($scope.getTextValues)
  43. .then(callback);
  44. };
  45. $scope.suggestTagValues = function(query, callback) {
  46. $scope.datasource.metricFindQuery('tag_values(' + $scope.target.metric + ',' + $scope.target.currentTagKey + ')')
  47. .then($scope.getTextValues)
  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. $scope.init();
  90. });
  91. });