panel_helper.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. define([
  2. 'angular',
  3. 'lodash',
  4. 'jquery',
  5. 'app/core/utils/kbn',
  6. 'app/core/utils/datemath',
  7. 'app/core/utils/rangeutil',
  8. ],
  9. function (angular, _, $, kbn, dateMath, rangeUtil) {
  10. 'use strict';
  11. var module = angular.module('grafana.services');
  12. module.service('panelHelper', function(timeSrv, $rootScope, $q) {
  13. var self = this;
  14. this.setTimeQueryStart = function(scope) {
  15. scope.timing = {};
  16. scope.timing.queryStart = new Date().getTime();
  17. };
  18. this.setTimeQueryEnd = function(scope) {
  19. scope.timing.queryEnd = new Date().getTime();
  20. };
  21. this.setTimeRenderStart = function(scope) {
  22. scope.timing = scope.timing || {};
  23. scope.timing.renderStart = new Date().getTime();
  24. };
  25. this.setTimeRenderEnd = function(scope) {
  26. scope.timing.renderEnd = new Date().getTime();
  27. };
  28. this.broadcastRender = function(scope, arg1, arg2) {
  29. this.setTimeRenderStart(scope);
  30. scope.$broadcast('render', arg1, arg2);
  31. this.setTimeRenderEnd(scope);
  32. if ($rootScope.profilingEnabled) {
  33. $rootScope.performance.panels.push({
  34. panelId: scope.panel.id,
  35. query: scope.timing.queryEnd - scope.timing.queryStart,
  36. render: scope.timing.renderEnd - scope.timing.renderStart,
  37. });
  38. }
  39. };
  40. this.updateTimeRange = function(scope) {
  41. scope.range = timeSrv.timeRange();
  42. scope.rangeRaw = timeSrv.timeRange(false);
  43. this.applyPanelTimeOverrides(scope);
  44. if (scope.panel.maxDataPoints) {
  45. scope.resolution = scope.panel.maxDataPoints;
  46. }
  47. else {
  48. scope.resolution = Math.ceil($(window).width() * (scope.panel.span / 12));
  49. }
  50. var panelInterval = scope.panel.interval;
  51. var datasourceInterval = (scope.datasource || {}).interval;
  52. scope.interval = kbn.calculateInterval(scope.range, scope.resolution, panelInterval || datasourceInterval);
  53. };
  54. this.applyPanelTimeOverrides = function(scope) {
  55. scope.panelMeta.timeInfo = '';
  56. // check panel time overrrides
  57. if (scope.panel.timeFrom) {
  58. var timeFromInfo = rangeUtil.describeTextRange(scope.panel.timeFrom);
  59. if (timeFromInfo.invalid) {
  60. scope.panelMeta.timeFromInfo = 'invalid time override';
  61. return;
  62. }
  63. if (_.isString(scope.rangeRaw.from)) {
  64. var timeFromDate = dateMath.parse(timeFromInfo.from);
  65. scope.panelMeta.timeInfo = timeFromInfo.display;
  66. scope.rangeRaw.from = timeFromInfo.from;
  67. scope.rangeRaw.to = timeFromInfo.to;
  68. scope.range.from = timeFromDate;
  69. }
  70. }
  71. if (scope.panel.timeShift) {
  72. var timeShiftInfo = rangeUtil.describeTextRange(scope.panel.timeShift);
  73. if (timeShiftInfo.invalid) {
  74. scope.panelMeta.timeInfo = 'invalid timeshift';
  75. return;
  76. }
  77. var timeShift = '-' + scope.panel.timeShift;
  78. scope.panelMeta.timeInfo += ' timeshift ' + timeShift;
  79. scope.range.from = dateMath.parseDateMath(timeShift, scope.range.from, false);
  80. scope.range.to = dateMath.parseDateMath(timeShift, scope.range.to, true);
  81. scope.rangeRaw = scope.range;
  82. }
  83. if (scope.panel.hideTimeOverride) {
  84. scope.panelMeta.timeInfo = '';
  85. }
  86. };
  87. this.issueMetricQuery = function(scope, datasource) {
  88. if (!scope.panel.targets || scope.panel.targets.length === 0) {
  89. return $q.when([]);
  90. }
  91. var metricsQuery = {
  92. range: scope.range,
  93. rangeRaw: scope.rangeRaw,
  94. interval: scope.interval,
  95. targets: scope.panel.targets,
  96. format: scope.panel.renderer === 'png' ? 'png' : 'json',
  97. maxDataPoints: scope.resolution,
  98. scopedVars: scope.panel.scopedVars,
  99. cacheTimeout: scope.panel.cacheTimeout
  100. };
  101. this.setTimeQueryStart(scope);
  102. return datasource.query(metricsQuery).then(function(results) {
  103. self.setTimeQueryEnd(scope);
  104. if (scope.dashboard.snapshot) {
  105. scope.panel.snapshotData = results;
  106. }
  107. return results;
  108. });
  109. };
  110. });
  111. });