Commit 7b906aa2 by Torkel Ödegaard

Annotations refactoring

parent f0f4c8cc
...@@ -10,6 +10,7 @@ define([ ...@@ -10,6 +10,7 @@ define([
module.service('annotationsSrv', function(dashboard, datasourceSrv, $q, alertSrv, $rootScope) { module.service('annotationsSrv', function(dashboard, datasourceSrv, $q, alertSrv, $rootScope) {
var promiseCached; var promiseCached;
var annotationPanel; var annotationPanel;
var list = [];
this.init = function() { this.init = function() {
$rootScope.$on('refresh', this.clearCache); $rootScope.$on('refresh', this.clearCache);
...@@ -24,11 +25,12 @@ define([ ...@@ -24,11 +25,12 @@ define([
this.clearCache = function() { this.clearCache = function() {
promiseCached = null; promiseCached = null;
list = [];
}; };
this.getAnnotations = function(rangeUnparsed) { this.getAnnotations = function(rangeUnparsed) {
if (!annotationPanel.enable) { if (!annotationPanel.enable) {
return $q.when(null); return $q.when([]);
} }
if (promiseCached) { if (promiseCached) {
...@@ -39,22 +41,21 @@ define([ ...@@ -39,22 +41,21 @@ define([
var graphiteEvents = this.getGraphiteEvents(rangeUnparsed); var graphiteEvents = this.getGraphiteEvents(rangeUnparsed);
promiseCached = $q.all([graphiteMetrics, graphiteEvents]) promiseCached = $q.all([graphiteMetrics, graphiteEvents])
.then(function(allAnnotations) { .then(function() {
var nonNull = _.filter(allAnnotations, function(value) { return value !== null; }); return list;
return _.flatten(nonNull);
}); });
return promiseCached; return promiseCached;
}; };
this.getGraphiteEvents = function(rangeUnparsed) { this.getGraphiteEvents = function(rangeUnparsed) {
var annotations = _.where(annotationPanel.annotations, { type: 'graphite events', enable: true }); var annotations = this.getAnnotationsByType('graphite events');
var tags = _.pluck(annotations, 'tags'); if (annotations.length === 0) {
if (tags.length === 0) {
return $q.when(null); return $q.when(null);
} }
var tags = _.pluck(annotations, 'tags');
var eventsQuery = { var eventsQuery = {
range: rangeUnparsed, range: rangeUnparsed,
tags: tags.join(' '), tags: tags.join(' '),
...@@ -62,69 +63,83 @@ define([ ...@@ -62,69 +63,83 @@ define([
return datasourceSrv.default.events(eventsQuery) return datasourceSrv.default.events(eventsQuery)
.then(function(results) { .then(function(results) {
var list = [];
_.each(results.data, function (event) { _.each(results.data, function (event) {
list.push(createAnnotation(annotations[0], event.when * 1000, event.what, event.tags, event.data)); list.push(createAnnotation(annotations[0], event.when * 1000, event.what, event.tags, event.data));
}); });
return list;
}) })
.then(null, function() { .then(null, errorHandler);
alertSrv.set('Annotations','Could not fetch annotations','error');
});
}; };
this.getGraphiteMetrics = function(rangeUnparsed) { this.getAnnotationsByType = function(type) {
var graphiteAnnotations = _.where(annotationPanel.annotations, { type: 'graphite metric', enable: true }); return _.where(annotationPanel.annotations, {
var graphiteTargets = _.map(graphiteAnnotations, function(annotation) { type: type,
return { target: annotation.target }; enable: true
}); });
};
if (graphiteTargets.length === 0) { this.getGraphiteMetrics = function(rangeUnparsed) {
var annotations = this.getAnnotationsByType('graphite metric');
if (annotations.length === 0) {
return $q.when(null); return $q.when(null);
} }
var graphiteQuery = { var promises = _.map(annotations, function(annotation) {
range: rangeUnparsed, var graphiteQuery = {
targets: graphiteTargets, range: rangeUnparsed,
format: 'json', targets: [{ target: annotation.target }],
maxDataPoints: 100 format: 'json',
}; maxDataPoints: 100
};
return datasourceSrv.default.query(graphiteQuery) var receiveFunc = _.partial(receiveGraphiteMetrics, annotation);
.then(function(results) {
return _.reduce(results.data, function(list, target) {
_.each(target.datapoints, function (values) {
if (values[0] === null) {
return;
}
list.push(createAnnotation(graphiteAnnotations[0], values[1] * 1000, target.target)); return datasourceSrv.default.query(graphiteQuery)
}); .then(receiveFunc)
.then(null, errorHandler);
});
return list; return $q.all(promises);
}, []);
})
.then(null, function() {
alertSrv.set('Annotations','Could not fetch annotations','error');
});
}; };
function createAnnotation(annotation, time, description, tags, data) { function errorHandler(err) {
var tooltip = "<small><b>" + description + "</b><br/>"; console.log('Annotation error: ', err);
if (tags) { alertSrv.set('Annotations','Could not fetch annotations','error');
tooltip += (tags || '') + '<br/>'; }
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] !== null) {
list.push(createAnnotation({
annotation: annotation,
time: datapoint[1] * 1000,
description: target.target
}));
}
}
}
}
function createAnnotation(options) {
var tooltip = "<small><b>" + options.description + "</b><br/>";
if (options.tags) {
tooltip += (options.tags || '') + '<br/>';
} }
tooltip += '<i>' + moment(time).format('YYYY-MM-DD HH:mm:ss') + '</i><br/>'; tooltip += '<i>' + moment(options.time).format('YYYY-MM-DD HH:mm:ss') + '</i><br/>';
if (data) { if (options.data) {
tooltip += data; tooltip += data;
} }
tooltip += "</small>"; tooltip += "</small>";
return { return {
annotation: annotation, annotation: options.annotation,
min: time, min: options.time,
max: time, max: options.time,
eventType: annotation.name, eventType: options.annotation.name,
title: null, title: null,
description: tooltip, description: tooltip,
score: 1 score: 1
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment