|
|
@@ -14,6 +14,7 @@ export class AnnotationsSrv {
|
|
|
constructor(private $rootScope,
|
|
|
private $q,
|
|
|
private datasourceSrv,
|
|
|
+ private backendSrv,
|
|
|
private timeSrv) {
|
|
|
$rootScope.onAppEvent('refresh', this.clearCache.bind(this), $rootScope);
|
|
|
$rootScope.onAppEvent('dashboard-initialized', this.clearCache.bind(this), $rootScope);
|
|
|
@@ -23,9 +24,41 @@ export class AnnotationsSrv {
|
|
|
this.globalAnnotationsPromise = null;
|
|
|
}
|
|
|
|
|
|
- getAnnotations(dashboard) {
|
|
|
+ getAnnotations(options) {
|
|
|
+ return this.$q.all([
|
|
|
+ this.getGlobalAnnotations(options),
|
|
|
+ this.getPanelAnnotations(options)
|
|
|
+ ]).then(allResults => {
|
|
|
+ return _.flatten(allResults);
|
|
|
+ }).catch(err => {
|
|
|
+ this.$rootScope.appEvent('alert-error', ['Annotations failed', (err.message || err)]);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ getPanelAnnotations(options) {
|
|
|
+ var panel = options.panel;
|
|
|
+ var dashboard = options.dashboard;
|
|
|
+
|
|
|
+ if (panel && panel.alert && panel.alert.enabled) {
|
|
|
+ return this.backendSrv.get('/api/annotations', {
|
|
|
+ from: options.range.from.valueOf(),
|
|
|
+ to: options.range.to.valueOf(),
|
|
|
+ limit: 100,
|
|
|
+ panelId: panel.id,
|
|
|
+ dashboardId: dashboard.id,
|
|
|
+ }).then(results => {
|
|
|
+ return this.translateQueryResult({iconColor: '#AA0000', name: 'panel-alert'}, results);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ return this.$q.when([]);
|
|
|
+ }
|
|
|
+
|
|
|
+ getGlobalAnnotations(options) {
|
|
|
+ var dashboard = options.dashboard;
|
|
|
+
|
|
|
if (dashboard.annotations.list.length === 0) {
|
|
|
- return this.$q.when(null);
|
|
|
+ return this.$q.when([]);
|
|
|
}
|
|
|
|
|
|
if (this.globalAnnotationsPromise) {
|
|
|
@@ -34,21 +67,15 @@ export class AnnotationsSrv {
|
|
|
|
|
|
var annotations = _.where(dashboard.annotations.list, {enable: true});
|
|
|
var range = this.timeSrv.timeRange();
|
|
|
- var rangeRaw = this.timeSrv.timeRange(false);
|
|
|
|
|
|
this.globalAnnotationsPromise = this.$q.all(_.map(annotations, annotation => {
|
|
|
if (annotation.snapshotData) {
|
|
|
- return this.translateQueryResult(annotation.snapshotData);
|
|
|
+ return this.translateQueryResult(annotation, annotation.snapshotData);
|
|
|
}
|
|
|
|
|
|
return this.datasourceSrv.get(annotation.datasource).then(datasource => {
|
|
|
// issue query against data source
|
|
|
- return datasource.annotationQuery({
|
|
|
- range: range,
|
|
|
- rangeRaw:
|
|
|
- rangeRaw,
|
|
|
- annotation: annotation
|
|
|
- });
|
|
|
+ return datasource.annotationQuery({range: range, rangeRaw: range.raw, annotation: annotation});
|
|
|
})
|
|
|
.then(results => {
|
|
|
// store response in annotation object if this is a snapshot call
|
|
|
@@ -56,35 +83,22 @@ export class AnnotationsSrv {
|
|
|
annotation.snapshotData = angular.copy(results);
|
|
|
}
|
|
|
// translate result
|
|
|
- return this.translateQueryResult(results);
|
|
|
+ return this.translateQueryResult(annotation, results);
|
|
|
});
|
|
|
- }))
|
|
|
- .then(allResults => {
|
|
|
- return _.flatten(allResults);
|
|
|
- }).catch(err => {
|
|
|
- this.$rootScope.appEvent('alert-error', ['Annotations failed', (err.message || err)]);
|
|
|
- });
|
|
|
+ }));
|
|
|
|
|
|
return this.globalAnnotationsPromise;
|
|
|
}
|
|
|
|
|
|
- translateQueryResult(results) {
|
|
|
- var translated = [];
|
|
|
-
|
|
|
+ translateQueryResult(annotation, results) {
|
|
|
for (var item of results) {
|
|
|
- translated.push({
|
|
|
- annotation: item.annotation,
|
|
|
- min: item.time,
|
|
|
- max: item.time,
|
|
|
- eventType: item.annotation.name,
|
|
|
- title: item.title,
|
|
|
- tags: item.tags,
|
|
|
- text: item.text,
|
|
|
- score: 1
|
|
|
- });
|
|
|
+ item.source = annotation;
|
|
|
+ item.min = item.time;
|
|
|
+ item.max = item.time;
|
|
|
+ item.scope = 1;
|
|
|
+ item.eventType = annotation.name;
|
|
|
}
|
|
|
-
|
|
|
- return translated;
|
|
|
+ return results;
|
|
|
}
|
|
|
}
|
|
|
|