console-ctrl.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. define([
  2. 'angular',
  3. 'lodash',
  4. 'moment',
  5. 'store'
  6. ],
  7. function (angular, _, moment, store) {
  8. 'use strict';
  9. var module = angular.module('grafana.controllers');
  10. var consoleEnabled = store.getBool('grafanaConsole');
  11. if (!consoleEnabled) {
  12. return;
  13. }
  14. var events = [];
  15. function ConsoleEvent(type, title, data) {
  16. this.type = type;
  17. this.title = title;
  18. this.data = data;
  19. this.time = moment().format('hh:mm:ss');
  20. if (data.config) {
  21. this.method = data.config.method;
  22. this.elapsed = (new Date().getTime() - data.config.$grafana_timestamp) + ' ms';
  23. if (data.config.params && data.config.params.q) {
  24. this.field2 = data.config.params.q;
  25. }
  26. if (_.isString(data.config.data)) {
  27. this.field2 = data.config.data;
  28. }
  29. if (data.status !== 200) {
  30. this.error = true;
  31. this.field3 = data.data;
  32. }
  33. if (_.isArray(data.data)) {
  34. this.extractTimeseriesInfo(data.data);
  35. }
  36. }
  37. }
  38. ConsoleEvent.prototype.extractTimeseriesInfo = function(series) {
  39. if (series.length === 0) {
  40. return;
  41. }
  42. var points = 0;
  43. var ok = false;
  44. if (series[0].datapoints) {
  45. points = _.reduce(series, function(memo, val) {
  46. return memo + val.datapoints.length;
  47. }, 0);
  48. ok = true;
  49. }
  50. if (series[0].columns) {
  51. points = _.reduce(series, function(memo, val) {
  52. return memo + val.points.length;
  53. }, 0);
  54. ok = true;
  55. }
  56. if (ok) {
  57. this.field1 = '(' + series.length + ' series';
  58. this.field1 += ', ' + points + ' points)';
  59. }
  60. };
  61. module.config(function($provide, $httpProvider) {
  62. $provide.factory('mupp', function($q) {
  63. return {
  64. 'request': function(config) {
  65. if (config.inspect) {
  66. config.$grafana_timestamp = new Date().getTime();
  67. }
  68. return config;
  69. },
  70. 'response': function(response) {
  71. if (response.config.inspect) {
  72. events.push(new ConsoleEvent(response.config.inspect.type, response.config.url, response));
  73. }
  74. return response;
  75. },
  76. 'requestError': function(rejection) {
  77. console.log('requestError', rejection);
  78. return $q.reject(rejection);
  79. },
  80. 'responseError': function (rejection) {
  81. var inspect = rejection.config.inspect || { type: 'error' };
  82. events.push(new ConsoleEvent(inspect.type, rejection.config.url, rejection));
  83. return $q.reject(rejection);
  84. }
  85. };
  86. });
  87. $httpProvider.interceptors.push('mupp');
  88. });
  89. module.controller('ConsoleCtrl', function($scope) {
  90. $scope.events = events;
  91. });
  92. });