queryCtrl.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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.getTextValues = function(metricFindResult) {
  29. return _.map(metricFindResult, function(value) { return value.text; });
  30. };
  31. $scope.suggestMetrics = function(query, callback) {
  32. $scope.datasource.metricFindQuery('metrics(' + query + ')')
  33. .then($scope.getTextValues)
  34. .then(callback);
  35. };
  36. $scope.suggestTagKeys = function(query, callback) {
  37. $scope.datasource.metricFindQuery('tag_names(' + $scope.target.metric + ')')
  38. .then($scope.getTextValues)
  39. .then(callback);
  40. };
  41. $scope.suggestTagValues = function(query, callback) {
  42. $scope.datasource.metricFindQuery('tag_values(' + $scope.target.metric + ',' + $scope.target.currentTagKey + ')')
  43. .then($scope.getTextValues)
  44. .then(callback);
  45. };
  46. $scope.addTag = function() {
  47. if (!$scope.addTagMode) {
  48. $scope.addTagMode = true;
  49. return;
  50. }
  51. if (!$scope.target.tags) {
  52. $scope.target.tags = {};
  53. }
  54. $scope.target.errors = validateTarget($scope.target);
  55. if (!$scope.target.errors.tags) {
  56. $scope.target.tags[$scope.target.currentTagKey] = $scope.target.currentTagValue;
  57. $scope.target.currentTagKey = '';
  58. $scope.target.currentTagValue = '';
  59. $scope.targetBlur();
  60. }
  61. $scope.addTagMode = false;
  62. };
  63. $scope.removeTag = function(key) {
  64. delete $scope.target.tags[key];
  65. $scope.targetBlur();
  66. };
  67. function validateTarget(target) {
  68. var errs = {};
  69. if (target.shouldDownsample) {
  70. try {
  71. if (target.downsampleInterval) {
  72. kbn.describe_interval(target.downsampleInterval);
  73. } else {
  74. errs.downsampleInterval = "You must supply a downsample interval (e.g. '1m' or '1h').";
  75. }
  76. } catch(err) {
  77. errs.downsampleInterval = err.message;
  78. }
  79. }
  80. if (target.tags && _.has(target.tags, target.currentTagKey)) {
  81. errs.tags = "Duplicate tag key '" + target.currentTagKey + "'.";
  82. }
  83. return errs;
  84. }
  85. $scope.init();
  86. });
  87. });