From 9eb9bd8488b22db721f7b36324be78934c9c3478 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Mon, 14 Jul 2014 18:19:41 +0200 Subject: [PATCH] moved annotations graphite query to graphite datasource --- src/app/services/annotationsSrv.js | 26 ++++++++++++++-- .../services/graphite/graphiteDatasource.js | 31 +++++++++++++++++++ 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/src/app/services/annotationsSrv.js b/src/app/services/annotationsSrv.js index 5b21ee7c442..1efa8c3ac20 100644 --- a/src/app/services/annotationsSrv.js +++ b/src/app/services/annotationsSrv.js @@ -31,10 +31,16 @@ define([ return promiseCached; } - var graphiteMetrics = this.getGraphiteMetrics(filterSrv, rangeUnparsed); - var graphiteEvents = this.getGraphiteEvents(rangeUnparsed); + var annotations = _.where(annotationPanel.annotations, { enable: true }); - promiseCached = $q.all(graphiteMetrics.concat(graphiteEvents)) + 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; }); @@ -42,6 +48,20 @@ define([ return promiseCached; }; + this.receiveAnnotationResults = function(results) { + console.log('Annotation result!', results); + for (var i = 0; i < results.length; i++) { + var data = results[i]; + addAnnotation({ + annotation: data.annotation, + time: data.time, + description: data.description, + tags: data.tags, + data: data.text + }); + } + }; + this.getGraphiteEvents = function(rangeUnparsed) { var annotations = this.getAnnotationsByType('graphite events'); if (annotations.length === 0) { diff --git a/src/app/services/graphite/graphiteDatasource.js b/src/app/services/graphite/graphiteDatasource.js index 1c1579a72ee..1b0c5a2ac0a 100644 --- a/src/app/services/graphite/graphiteDatasource.js +++ b/src/app/services/graphite/graphiteDatasource.js @@ -57,6 +57,37 @@ function (angular, _, $, config, kbn, moment) { } }; + GraphiteDatasource.prototype.annotationQuery = function(annotation, filterSrv, rangeUnparsed) { + var graphiteQuery = { + range: rangeUnparsed, + targets: [{ target: annotation.target }], + format: 'json', + maxDataPoints: 100 + }; + + return this.query(filterSrv, graphiteQuery) + .then(function(result) { + var list = []; + + for (var i = 0; i < result.data.length; i++) { + var target = result.data[i]; + + for (var y = 0; y < target.datapoints.length; y++) { + var datapoint = target.datapoints[y]; + if (!datapoint[0]) { continue; } + + list.push({ + annotation: annotation, + time: datapoint[1] * 1000, + description: target.target + }); + } + } + + return list; + }); + }; + GraphiteDatasource.prototype.events = function(options) { try { var tags = '';