| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- define([
- 'angular',
- 'underscore',
- 'moment'
- ], function (angular, _, moment) {
- 'use strict';
- var module = angular.module('kibana.services');
- module.service('annotationsSrv', function(datasourceSrv, $q, alertSrv, $rootScope) {
- var promiseCached;
- var annotationPanel;
- var list = [];
- this.init = function() {
- $rootScope.$on('refresh', this.clearCache);
- };
- this.clearCache = function() {
- promiseCached = null;
- list = [];
- };
- this.getAnnotations = function(filterSrv, rangeUnparsed, dashboard) {
- annotationPanel = _.findWhere(dashboard.pulldowns, { type: 'annotations' });
- if (!annotationPanel.enable) {
- return $q.when(null);
- }
- if (promiseCached) {
- return promiseCached;
- }
- var annotations = _.where(annotationPanel.annotations, { enable: true });
- var promises = _.map(annotations, function(annotation) {
- var datasource = datasourceSrv.get(annotation.datasource);
- return datasource.annotationQuery(annotation, filterSrv, rangeUnparsed)
- .then(this.receiveAnnotationResults)
- .then(null, errorHandler);
- }, this);
- promiseCached = $q.all(promises)
- .then(function() {
- return list;
- });
- return promiseCached;
- };
- this.receiveAnnotationResults = function(results) {
- for (var i = 0; i < results.length; i++) {
- addAnnotation(results[i]);
- }
- };
- this.getGraphiteEvents = function(rangeUnparsed) {
- var annotations = this.getAnnotationsByType('graphite events');
- if (annotations.length === 0) {
- return [];
- }
- var promises = _.map(annotations, function(annotation) {
- return datasourceSrv.default.events({ range: rangeUnparsed, tags: annotation.tags })
- .then(function(results) {
- _.each(results.data, function (event) {
- addAnnotation({
- annotation: annotation,
- time: event.when * 1000,
- description: event.what,
- tags: event.tags,
- data: event.data
- });
- });
- })
- .then(null, errorHandler);
- });
- return promises;
- };
- this.getAnnotationsByType = function(type) {
- return _.where(annotationPanel.annotations, {
- type: type,
- enable: true
- });
- };
- this.getGraphiteMetrics = function(filterSrv, rangeUnparsed) {
- var annotations = this.getAnnotationsByType('graphite metric');
- if (annotations.length === 0) {
- return [];
- }
- var promises = _.map(annotations, function(annotation) {
- var graphiteQuery = {
- range: rangeUnparsed,
- targets: [{ target: annotation.target }],
- format: 'json',
- maxDataPoints: 100
- };
- var receiveFunc = _.partial(receiveGraphiteMetrics, annotation);
- return datasourceSrv.default.query(filterSrv, graphiteQuery)
- .then(receiveFunc)
- .then(null, errorHandler);
- });
- return promises;
- };
- function errorHandler(err) {
- console.log('Annotation error: ', err);
- alertSrv.set('Annotations','Could not fetch annotations','error');
- }
- function receiveGraphiteMetrics(annotation, results) {
- for (var i = 0; i < results.data.length; i++) {
- var target = results.data[i];
- for (var y = 0; y < target.datapoints.length; y++) {
- var datapoint = target.datapoints[y];
- if (datapoint[0]) {
- addAnnotation({
- annotation: annotation,
- time: datapoint[1] * 1000,
- description: target.target
- });
- }
- }
- }
- }
- function addAnnotation(options) {
- var tooltip = "<small><b>" + options.title + "</b><br/>";
- if (options.tags) {
- tooltip += (options.tags || '') + '<br/>';
- }
- tooltip += '<i>' + moment(options.time).format('YYYY-MM-DD HH:mm:ss') + '</i><br/>';
- if (options.data) {
- tooltip += options.data.replace(/\n/g, '<br/>');
- }
- tooltip += "</small>";
- list.push({
- annotation: options.annotation,
- min: options.time,
- max: options.time,
- eventType: options.annotation.name,
- title: null,
- description: tooltip,
- score: 1
- });
- }
- // Now init
- this.init();
- });
- });
|