queryCtrl.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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.panelCtrl = $scope.ctrl;
  11. $scope.init = function() {
  12. $scope.target.errors = validateTarget($scope.target);
  13. $scope.aggregators = ['avg', 'sum', 'min', 'max', 'dev', 'zimsum', 'mimmin', 'mimmax'];
  14. $scope.fillPolicies = ['none', 'nan', 'null', 'zero'];
  15. if (!$scope.target.aggregator) {
  16. $scope.target.aggregator = 'sum';
  17. }
  18. if (!$scope.target.downsampleAggregator) {
  19. $scope.target.downsampleAggregator = 'avg';
  20. }
  21. if (!$scope.target.downsampleFillPolicy) {
  22. $scope.target.downsampleFillPolicy = 'none';
  23. }
  24. $scope.datasource.getAggregators().then(function(aggs) {
  25. $scope.aggregators = aggs;
  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.getTextValues = function(metricFindResult) {
  37. return _.map(metricFindResult, function(value) { return value.text; });
  38. };
  39. $scope.suggestMetrics = function(query, callback) {
  40. $scope.datasource.metricFindQuery('metrics(' + query + ')')
  41. .then($scope.getTextValues)
  42. .then(callback);
  43. };
  44. $scope.suggestTagKeys = function(query, callback) {
  45. $scope.datasource.metricFindQuery('suggest_tagk(' + query + ')')
  46. .then($scope.getTextValues)
  47. .then(callback);
  48. };
  49. $scope.suggestTagValues = function(query, callback) {
  50. $scope.datasource.metricFindQuery('suggest_tagv(' + query + ')')
  51. .then($scope.getTextValues)
  52. .then(callback);
  53. };
  54. $scope.addTag = function() {
  55. if (!$scope.addTagMode) {
  56. $scope.addTagMode = true;
  57. return;
  58. }
  59. if (!$scope.target.tags) {
  60. $scope.target.tags = {};
  61. }
  62. $scope.target.errors = validateTarget($scope.target);
  63. if (!$scope.target.errors.tags) {
  64. $scope.target.tags[$scope.target.currentTagKey] = $scope.target.currentTagValue;
  65. $scope.target.currentTagKey = '';
  66. $scope.target.currentTagValue = '';
  67. $scope.targetBlur();
  68. }
  69. $scope.addTagMode = false;
  70. };
  71. $scope.removeTag = function(key) {
  72. delete $scope.target.tags[key];
  73. $scope.targetBlur();
  74. };
  75. $scope.editTag = function(key, value) {
  76. $scope.removeTag(key);
  77. $scope.target.currentTagKey = key;
  78. $scope.target.currentTagValue = value;
  79. $scope.addTag();
  80. };
  81. function validateTarget(target) {
  82. var errs = {};
  83. if (target.shouldDownsample) {
  84. try {
  85. if (target.downsampleInterval) {
  86. kbn.describe_interval(target.downsampleInterval);
  87. } else {
  88. errs.downsampleInterval = "You must supply a downsample interval (e.g. '1m' or '1h').";
  89. }
  90. } catch(err) {
  91. errs.downsampleInterval = err.message;
  92. }
  93. }
  94. if (target.tags && _.has(target.tags, target.currentTagKey)) {
  95. errs.tags = "Duplicate tag key '" + target.currentTagKey + "'.";
  96. }
  97. return errs;
  98. }
  99. $scope.init();
  100. });
  101. });