queryCtrl.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. $scope.fillPolicies = ['none', 'nan', 'null', 'zero'];
  14. if (!$scope.target.aggregator) {
  15. $scope.target.aggregator = 'sum';
  16. }
  17. if (!$scope.target.downsampleAggregator) {
  18. $scope.target.downsampleAggregator = 'avg';
  19. }
  20. if (!$scope.target.downsampleFillPolicy) {
  21. $scope.target.downsampleFillPolicy = 'none';
  22. }
  23. $scope.datasource.getAggregators().then(function(aggs) {
  24. $scope.aggregators = aggs;
  25. });
  26. };
  27. $scope.targetBlur = function() {
  28. $scope.target.errors = validateTarget($scope.target);
  29. // this does not work so good
  30. if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) {
  31. $scope.oldTarget = angular.copy($scope.target);
  32. $scope.get_data();
  33. }
  34. };
  35. $scope.getTextValues = function(metricFindResult) {
  36. return _.map(metricFindResult, function(value) { return value.text; });
  37. };
  38. $scope.suggestMetrics = function(query, callback) {
  39. $scope.datasource.metricFindQuery('metrics(' + query + ')')
  40. .then($scope.getTextValues)
  41. .then(callback);
  42. };
  43. $scope.suggestTagKeys = function(query, callback) {
  44. $scope.datasource.metricFindQuery('suggest_tagk(' + query + ')')
  45. .then($scope.getTextValues)
  46. .then(callback);
  47. };
  48. $scope.suggestTagValues = function(query, callback) {
  49. $scope.datasource.metricFindQuery('suggest_tagv(' + query + ')')
  50. .then($scope.getTextValues)
  51. .then(callback);
  52. };
  53. $scope.addTag = function() {
  54. if (!$scope.addTagMode) {
  55. $scope.addTagMode = true;
  56. return;
  57. }
  58. if (!$scope.target.tags) {
  59. $scope.target.tags = {};
  60. }
  61. $scope.target.errors = validateTarget($scope.target);
  62. if (!$scope.target.errors.tags) {
  63. $scope.target.tags[$scope.target.currentTagKey] = $scope.target.currentTagValue;
  64. $scope.target.currentTagKey = '';
  65. $scope.target.currentTagValue = '';
  66. $scope.targetBlur();
  67. }
  68. $scope.addTagMode = false;
  69. };
  70. $scope.removeTag = function(key) {
  71. delete $scope.target.tags[key];
  72. $scope.targetBlur();
  73. };
  74. $scope.editTag = function(key, value) {
  75. $scope.removeTag(key);
  76. $scope.target.currentTagKey = key;
  77. $scope.target.currentTagValue = value;
  78. $scope.addTag();
  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. });