queryCtrl.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. define([
  2. 'angular',
  3. 'lodash',
  4. 'kbn',
  5. 'app/core/utils/datemath',
  6. ],
  7. function (angular, _, kbn, dateMath) {
  8. 'use strict';
  9. var module = angular.module('grafana.controllers');
  10. module.controller('PrometheusQueryCtrl', function($scope) {
  11. $scope.init = function() {
  12. $scope.target.errors = validateTarget();
  13. $scope.target.datasourceErrors = {};
  14. if (!$scope.target.expr) {
  15. $scope.target.expr = '';
  16. }
  17. $scope.target.metric = '';
  18. $scope.resolutions = [
  19. { factor: 1, },
  20. { factor: 2, },
  21. { factor: 3, },
  22. { factor: 5, },
  23. { factor: 10, },
  24. ];
  25. $scope.resolutions = _.map($scope.resolutions, function(r) {
  26. r.label = '1/' + r.factor;
  27. return r;
  28. });
  29. if (!$scope.target.intervalFactor) {
  30. $scope.target.intervalFactor = 2; // default resolution is 1/2
  31. }
  32. $scope.calculateInterval();
  33. $scope.$on('render', function() {
  34. $scope.calculateInterval(); // re-calculate interval when time range is updated
  35. });
  36. $scope.target.prometheusLink = $scope.linkToPrometheus();
  37. $scope.$on('typeahead-updated', function() {
  38. $scope.$apply($scope.inputMetric);
  39. $scope.refreshMetricData();
  40. });
  41. $scope.datasource.lastErrors = {};
  42. $scope.$watch('datasource.lastErrors', function() {
  43. $scope.target.datasourceErrors = $scope.datasource.lastErrors;
  44. }, true);
  45. };
  46. $scope.refreshMetricData = function() {
  47. $scope.target.errors = validateTarget($scope.target);
  48. $scope.calculateInterval();
  49. $scope.target.prometheusLink = $scope.linkToPrometheus();
  50. // this does not work so good
  51. if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) {
  52. $scope.oldTarget = angular.copy($scope.target);
  53. $scope.get_data();
  54. }
  55. };
  56. $scope.inputMetric = function() {
  57. $scope.target.expr += $scope.target.metric;
  58. $scope.target.metric = '';
  59. };
  60. $scope.moveMetricQuery = function(fromIndex, toIndex) {
  61. _.move($scope.panel.targets, fromIndex, toIndex);
  62. };
  63. $scope.suggestMetrics = function(query, callback) {
  64. $scope.datasource
  65. .performSuggestQuery(query)
  66. .then(callback);
  67. };
  68. $scope.linkToPrometheus = function() {
  69. var from = dateMath.parse($scope.dashboard.time.from, false);
  70. var to = dateMath.parse($scope.dashboard.time.to, true);
  71. if ($scope.panel.timeFrom) {
  72. from = dateMath.parseDateMath('-' + $scope.panel.timeFrom, to, false);
  73. }
  74. if ($scope.panel.timeShift) {
  75. from = dateMath.parseDateMath('-' + $scope.panel.timeShift, from, false);
  76. to = dateMath.parseDateMath('-' + $scope.panel.timeShift, to, true);
  77. }
  78. var range = Math.ceil((to.valueOf()- from.valueOf()) / 1000);
  79. var endTime = to.format('YYYY-MM-DD HH:MM');
  80. var step = kbn.interval_to_seconds(this.target.calculatedInterval);
  81. if (step !== 0 && range / step > 11000) {
  82. step = Math.floor(range / 11000);
  83. }
  84. var expr = {
  85. expr: $scope.target.expr,
  86. range_input: range + 's',
  87. end_input: endTime,
  88. //step_input: step,
  89. step_input: '',
  90. stacked: $scope.panel.stack,
  91. tab: 0
  92. };
  93. var hash = encodeURIComponent(JSON.stringify([expr]));
  94. return $scope.datasource.url + '/graph#' + hash;
  95. };
  96. $scope.calculateInterval = function() {
  97. var interval = $scope.target.interval || $scope.interval;
  98. var calculatedInterval = $scope.datasource.calculateInterval(interval, $scope.target.intervalFactor);
  99. $scope.target.calculatedInterval = calculatedInterval;
  100. };
  101. // TODO: validate target
  102. function validateTarget() {
  103. var errs = {};
  104. return errs;
  105. }
  106. $scope.init();
  107. });
  108. });