queryCtrl.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. define([
  2. 'angular',
  3. 'lodash',
  4. 'app/core/utils/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('suggest_tagk(' + query + ')')
  41. .then($scope.getTextValues)
  42. .then(callback);
  43. };
  44. $scope.suggestTagValues = function(query, callback) {
  45. $scope.datasource.metricFindQuery('suggest_tagv(' + query + ')')
  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. $scope.editTag = function(key, value) {
  71. $scope.removeTag(key);
  72. $scope.target.currentTagKey = key;
  73. $scope.target.currentTagValue = value;
  74. $scope.addTag();
  75. };
  76. function validateTarget(target) {
  77. var errs = {};
  78. if (target.shouldDownsample) {
  79. try {
  80. if (target.downsampleInterval) {
  81. kbn.describe_interval(target.downsampleInterval);
  82. } else {
  83. errs.downsampleInterval = "You must supply a downsample interval (e.g. '1m' or '1h').";
  84. }
  85. } catch(err) {
  86. errs.downsampleInterval = err.message;
  87. }
  88. }
  89. if (target.tags && _.has(target.tags, target.currentTagKey)) {
  90. errs.tags = "Duplicate tag key '" + target.currentTagKey + "'.";
  91. }
  92. return errs;
  93. }
  94. $scope.init();
  95. });
  96. });