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