annotationsSrv.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. define([
  2. 'angular',
  3. 'underscore',
  4. 'moment'
  5. ], function (angular, _, moment) {
  6. 'use strict';
  7. var module = angular.module('kibana.services');
  8. module.service('annotationsSrv', function(datasourceSrv, $q, alertSrv, $rootScope) {
  9. var promiseCached;
  10. var annotationPanel;
  11. var list = [];
  12. this.init = function() {
  13. $rootScope.$on('refresh', this.clearCache);
  14. };
  15. this.clearCache = function() {
  16. promiseCached = null;
  17. list = [];
  18. };
  19. this.getAnnotations = function(filterSrv, rangeUnparsed, dashboard) {
  20. annotationPanel = _.findWhere(dashboard.pulldowns, { type: 'annotations' });
  21. if (!annotationPanel.enable) {
  22. return $q.when(null);
  23. }
  24. if (promiseCached) {
  25. return promiseCached;
  26. }
  27. var annotations = _.where(annotationPanel.annotations, { enable: true });
  28. var promises = _.map(annotations, function(annotation) {
  29. var datasource = datasourceSrv.get(annotation.datasource);
  30. return datasource.annotationQuery(annotation, filterSrv, rangeUnparsed)
  31. .then(this.receiveAnnotationResults)
  32. .then(null, errorHandler);
  33. }, this);
  34. promiseCached = $q.all(promises)
  35. .then(function() {
  36. return list;
  37. });
  38. return promiseCached;
  39. };
  40. this.receiveAnnotationResults = function(results) {
  41. for (var i = 0; i < results.length; i++) {
  42. addAnnotation(results[i]);
  43. }
  44. };
  45. this.getGraphiteEvents = function(rangeUnparsed) {
  46. var annotations = this.getAnnotationsByType('graphite events');
  47. if (annotations.length === 0) {
  48. return [];
  49. }
  50. var promises = _.map(annotations, function(annotation) {
  51. return datasourceSrv.default.events({ range: rangeUnparsed, tags: annotation.tags })
  52. .then(function(results) {
  53. _.each(results.data, function (event) {
  54. addAnnotation({
  55. annotation: annotation,
  56. time: event.when * 1000,
  57. description: event.what,
  58. tags: event.tags,
  59. data: event.data
  60. });
  61. });
  62. })
  63. .then(null, errorHandler);
  64. });
  65. return promises;
  66. };
  67. this.getAnnotationsByType = function(type) {
  68. return _.where(annotationPanel.annotations, {
  69. type: type,
  70. enable: true
  71. });
  72. };
  73. this.getGraphiteMetrics = function(filterSrv, rangeUnparsed) {
  74. var annotations = this.getAnnotationsByType('graphite metric');
  75. if (annotations.length === 0) {
  76. return [];
  77. }
  78. var promises = _.map(annotations, function(annotation) {
  79. var graphiteQuery = {
  80. range: rangeUnparsed,
  81. targets: [{ target: annotation.target }],
  82. format: 'json',
  83. maxDataPoints: 100
  84. };
  85. var receiveFunc = _.partial(receiveGraphiteMetrics, annotation);
  86. return datasourceSrv.default.query(filterSrv, graphiteQuery)
  87. .then(receiveFunc)
  88. .then(null, errorHandler);
  89. });
  90. return promises;
  91. };
  92. function errorHandler(err) {
  93. console.log('Annotation error: ', err);
  94. alertSrv.set('Annotations','Could not fetch annotations','error');
  95. }
  96. function receiveGraphiteMetrics(annotation, results) {
  97. for (var i = 0; i < results.data.length; i++) {
  98. var target = results.data[i];
  99. for (var y = 0; y < target.datapoints.length; y++) {
  100. var datapoint = target.datapoints[y];
  101. if (datapoint[0]) {
  102. addAnnotation({
  103. annotation: annotation,
  104. time: datapoint[1] * 1000,
  105. description: target.target
  106. });
  107. }
  108. }
  109. }
  110. }
  111. function addAnnotation(options) {
  112. var tooltip = "<small><b>" + options.title + "</b><br/>";
  113. if (options.tags) {
  114. tooltip += (options.tags || '') + '<br/>';
  115. }
  116. tooltip += '<i>' + moment(options.time).format('YYYY-MM-DD HH:mm:ss') + '</i><br/>';
  117. if (options.data) {
  118. tooltip += options.data.replace(/\n/g, '<br/>');
  119. }
  120. tooltip += "</small>";
  121. list.push({
  122. annotation: options.annotation,
  123. min: options.time,
  124. max: options.time,
  125. eventType: options.annotation.name,
  126. title: null,
  127. description: tooltip,
  128. score: 1
  129. });
  130. }
  131. // Now init
  132. this.init();
  133. });
  134. });