Commit 7b906aa2 by Torkel Ödegaard

Annotations refactoring

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