panelHelper.js 3.4 KB

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