2014-02-09 19:31:06 +01:00
|
|
|
define([
|
|
|
|
|
'angular',
|
2014-08-07 14:35:19 +02:00
|
|
|
'lodash',
|
2014-12-31 09:28:30 +01:00
|
|
|
'moment',
|
|
|
|
|
'./editorCtrl'
|
2014-02-09 19:31:06 +01:00
|
|
|
], function (angular, _, moment) {
|
|
|
|
|
'use strict';
|
|
|
|
|
|
2014-07-28 18:11:52 +02:00
|
|
|
var module = angular.module('grafana.services');
|
2014-02-09 19:31:06 +01:00
|
|
|
|
2014-11-27 14:46:01 +01:00
|
|
|
module.service('annotationsSrv', function(datasourceSrv, $q, alertSrv, $rootScope, $sanitize) {
|
2014-02-18 20:41:56 +01:00
|
|
|
var promiseCached;
|
2014-02-21 11:58:08 +01:00
|
|
|
var list = [];
|
2014-07-21 18:49:30 +02:00
|
|
|
var timezone;
|
2015-02-28 10:29:17 +01:00
|
|
|
var self = this;
|
2014-02-09 19:31:06 +01:00
|
|
|
|
|
|
|
|
this.init = function() {
|
2014-09-24 09:03:04 +02:00
|
|
|
$rootScope.onAppEvent('refresh', this.clearCache);
|
|
|
|
|
$rootScope.onAppEvent('setup-dashboard', this.clearCache);
|
2014-02-18 20:41:56 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.clearCache = function() {
|
|
|
|
|
promiseCached = null;
|
2014-02-21 11:58:08 +01:00
|
|
|
list = [];
|
2014-02-09 19:31:06 +01:00
|
|
|
};
|
|
|
|
|
|
2014-08-27 16:29:48 +02:00
|
|
|
this.getAnnotations = function(rangeUnparsed, dashboard) {
|
2015-03-12 15:50:39 +01:00
|
|
|
if (dashboard.annotations.list.length === 0) {
|
2014-02-21 13:56:27 +01:00
|
|
|
return $q.when(null);
|
2014-02-18 20:41:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (promiseCached) {
|
|
|
|
|
return promiseCached;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-21 18:52:06 +02:00
|
|
|
timezone = dashboard.timezone;
|
2015-02-28 10:29:17 +01:00
|
|
|
var annotations = _.where(dashboard.annotations.list, {enable: true});
|
2014-02-20 23:00:32 +01:00
|
|
|
|
2014-07-14 18:19:41 +02:00
|
|
|
var promises = _.map(annotations, function(annotation) {
|
2015-02-28 10:29:17 +01:00
|
|
|
return datasourceSrv.get(annotation.datasource).then(function(datasource) {
|
|
|
|
|
return datasource.annotationQuery(annotation, rangeUnparsed)
|
|
|
|
|
.then(self.receiveAnnotationResults)
|
|
|
|
|
.then(null, errorHandler);
|
|
|
|
|
}, this);
|
|
|
|
|
});
|
2014-07-14 18:19:41 +02:00
|
|
|
|
|
|
|
|
promiseCached = $q.all(promises)
|
2014-02-21 11:58:08 +01:00
|
|
|
.then(function() {
|
|
|
|
|
return list;
|
2014-02-20 23:00:32 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return promiseCached;
|
|
|
|
|
};
|
|
|
|
|
|
2014-07-14 18:19:41 +02:00
|
|
|
this.receiveAnnotationResults = function(results) {
|
|
|
|
|
for (var i = 0; i < results.length; i++) {
|
2014-07-17 10:24:30 +02:00
|
|
|
addAnnotation(results[i]);
|
2014-07-14 18:19:41 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2014-02-21 11:58:08 +01:00
|
|
|
function errorHandler(err) {
|
|
|
|
|
console.log('Annotation error: ', err);
|
2014-11-12 12:41:53 +01:00
|
|
|
var message = err.message || "Annotation query failed";
|
2014-09-20 08:30:59 +02:00
|
|
|
alertSrv.set('Annotations error', message,'error');
|
2014-02-21 11:58:08 +01:00
|
|
|
}
|
|
|
|
|
|
2014-02-21 13:56:27 +01:00
|
|
|
function addAnnotation(options) {
|
2014-11-27 14:46:01 +01:00
|
|
|
var title = $sanitize(options.title);
|
|
|
|
|
var tooltip = "<small><b>" + title + "</b><br/>";
|
2014-02-21 11:58:08 +01:00
|
|
|
if (options.tags) {
|
2014-11-27 14:46:01 +01:00
|
|
|
var tags = $sanitize(options.tags);
|
|
|
|
|
tooltip += '<span class="tag label label-tag">' + (tags || '') + '</span><br/>';
|
2014-02-20 23:00:32 +01:00
|
|
|
}
|
2014-07-17 10:24:30 +02:00
|
|
|
|
2014-07-21 18:49:30 +02:00
|
|
|
if (timezone === 'browser') {
|
|
|
|
|
tooltip += '<i>' + moment(options.time).format('YYYY-MM-DD HH:mm:ss') + '</i><br/>';
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
tooltip += '<i>' + moment.utc(options.time).format('YYYY-MM-DD HH:mm:ss') + '</i><br/>';
|
|
|
|
|
}
|
2014-07-17 10:24:30 +02:00
|
|
|
|
2014-07-18 19:19:30 +02:00
|
|
|
if (options.text) {
|
2014-11-27 14:46:01 +01:00
|
|
|
var text = $sanitize(options.text);
|
|
|
|
|
tooltip += text.replace(/\n/g, '<br/>');
|
2014-02-20 23:00:32 +01:00
|
|
|
}
|
2014-07-17 10:24:30 +02:00
|
|
|
|
2014-02-20 23:00:32 +01:00
|
|
|
tooltip += "</small>";
|
|
|
|
|
|
2014-02-21 13:56:27 +01:00
|
|
|
list.push({
|
2014-02-21 11:58:08 +01:00
|
|
|
annotation: options.annotation,
|
|
|
|
|
min: options.time,
|
|
|
|
|
max: options.time,
|
|
|
|
|
eventType: options.annotation.name,
|
2014-02-20 23:00:32 +01:00
|
|
|
title: null,
|
|
|
|
|
description: tooltip,
|
|
|
|
|
score: 1
|
2014-02-21 13:56:27 +01:00
|
|
|
});
|
2014-02-20 23:00:32 +01:00
|
|
|
}
|
|
|
|
|
|
2014-02-09 19:31:06 +01:00
|
|
|
// Now init
|
|
|
|
|
this.init();
|
|
|
|
|
});
|
|
|
|
|
|
2014-05-19 15:31:30 +02:00
|
|
|
});
|