2017-12-20 05:33:33 -06:00
|
|
|
import './editor_ctrl';
|
2016-09-08 07:31:30 -05:00
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
import angular from 'angular';
|
|
|
|
import _ from 'lodash';
|
|
|
|
import coreModule from 'app/core/core_module';
|
|
|
|
import { makeRegions, dedupAnnotations } from './events_processing';
|
2016-09-08 07:31:30 -05:00
|
|
|
|
|
|
|
export class AnnotationsSrv {
|
|
|
|
globalAnnotationsPromise: any;
|
2016-09-30 10:37:47 -05:00
|
|
|
alertStatesPromise: any;
|
2016-09-08 07:31:30 -05:00
|
|
|
|
|
|
|
/** @ngInject */
|
2017-12-21 01:39:31 -06:00
|
|
|
constructor(private $rootScope, private $q, private datasourceSrv, private backendSrv, private timeSrv) {
|
2017-12-20 05:33:33 -06:00
|
|
|
$rootScope.onAppEvent('refresh', this.clearCache.bind(this), $rootScope);
|
2017-12-21 01:39:31 -06:00
|
|
|
$rootScope.onAppEvent('dashboard-initialized', this.clearCache.bind(this), $rootScope);
|
2016-09-08 07:31:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
clearCache() {
|
|
|
|
this.globalAnnotationsPromise = null;
|
2016-09-30 10:37:47 -05:00
|
|
|
this.alertStatesPromise = null;
|
2016-09-08 07:31:30 -05:00
|
|
|
}
|
|
|
|
|
2016-09-09 04:30:55 -05:00
|
|
|
getAnnotations(options) {
|
2017-10-07 03:31:39 -05:00
|
|
|
return this.$q
|
|
|
|
.all([this.getGlobalAnnotations(options), this.getAlertStates(options)])
|
|
|
|
.then(results => {
|
|
|
|
// combine the annotations and flatten results
|
|
|
|
var annotations = _.flattenDeep(results[0]);
|
|
|
|
|
|
|
|
// filter out annotations that do not belong to requesting panel
|
|
|
|
annotations = _.filter(annotations, item => {
|
|
|
|
// if event has panel id and query is of type dashboard then panel and requesting panel id must match
|
2017-12-20 05:33:33 -06:00
|
|
|
if (item.panelId && item.source.type === 'dashboard') {
|
2017-10-07 03:31:39 -05:00
|
|
|
return item.panelId === options.panel.id;
|
2017-04-14 05:46:02 -05:00
|
|
|
}
|
2017-10-07 03:31:39 -05:00
|
|
|
return true;
|
|
|
|
});
|
2017-05-22 15:18:20 -05:00
|
|
|
|
2017-10-07 03:31:39 -05:00
|
|
|
annotations = dedupAnnotations(annotations);
|
|
|
|
annotations = makeRegions(annotations, options);
|
2016-09-09 04:30:55 -05:00
|
|
|
|
2017-10-07 03:31:39 -05:00
|
|
|
// look for alert state for this panel
|
2018-08-29 07:27:29 -05:00
|
|
|
const alertState = _.find(results[1], { panelId: options.panel.id });
|
2016-09-09 04:30:55 -05:00
|
|
|
|
2017-10-07 03:31:39 -05:00
|
|
|
return {
|
|
|
|
annotations: annotations,
|
2017-12-20 05:33:33 -06:00
|
|
|
alertState: alertState,
|
2017-10-07 03:31:39 -05:00
|
|
|
};
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
if (!err.message && err.data && err.data.message) {
|
|
|
|
err.message = err.data.message;
|
|
|
|
}
|
2017-12-20 05:33:33 -06:00
|
|
|
console.log('AnnotationSrv.query error', err);
|
2017-12-21 01:39:31 -06:00
|
|
|
this.$rootScope.appEvent('alert-error', ['Annotation Query Failed', err.message || err]);
|
2017-10-07 03:31:39 -05:00
|
|
|
return [];
|
2016-09-09 04:30:55 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-09-30 10:37:47 -05:00
|
|
|
getAlertStates(options) {
|
|
|
|
if (!options.dashboard.id) {
|
|
|
|
return this.$q.when([]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ignore if no alerts
|
|
|
|
if (options.panel && !options.panel.alert) {
|
|
|
|
return this.$q.when([]);
|
|
|
|
}
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
if (options.range.raw.to !== 'now') {
|
2016-09-30 10:37:47 -05:00
|
|
|
return this.$q.when([]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.alertStatesPromise) {
|
|
|
|
return this.alertStatesPromise;
|
|
|
|
}
|
|
|
|
|
2017-12-21 01:39:31 -06:00
|
|
|
this.alertStatesPromise = this.backendSrv.get('/api/alerts/states-for-dashboard', {
|
|
|
|
dashboardId: options.dashboard.id,
|
|
|
|
});
|
2016-09-30 10:37:47 -05:00
|
|
|
return this.alertStatesPromise;
|
|
|
|
}
|
|
|
|
|
2016-09-09 04:30:55 -05:00
|
|
|
getGlobalAnnotations(options) {
|
2018-08-29 07:27:29 -05:00
|
|
|
const dashboard = options.dashboard;
|
2016-09-09 04:30:55 -05:00
|
|
|
|
2016-09-08 07:31:30 -05:00
|
|
|
if (this.globalAnnotationsPromise) {
|
|
|
|
return this.globalAnnotationsPromise;
|
|
|
|
}
|
|
|
|
|
2018-08-29 07:27:29 -05:00
|
|
|
const range = this.timeSrv.timeRange();
|
|
|
|
const promises = [];
|
2017-10-07 03:31:39 -05:00
|
|
|
|
2018-08-26 10:14:40 -05:00
|
|
|
for (const annotation of dashboard.annotations.list) {
|
2017-10-07 03:31:39 -05:00
|
|
|
if (!annotation.enable) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-09-08 07:31:30 -05:00
|
|
|
|
|
|
|
if (annotation.snapshotData) {
|
2016-09-09 04:30:55 -05:00
|
|
|
return this.translateQueryResult(annotation, annotation.snapshotData);
|
2016-09-08 07:31:30 -05:00
|
|
|
}
|
|
|
|
|
2017-10-07 03:31:39 -05:00
|
|
|
promises.push(
|
|
|
|
this.datasourceSrv
|
|
|
|
.get(annotation.datasource)
|
|
|
|
.then(datasource => {
|
|
|
|
// issue query against data source
|
|
|
|
return datasource.annotationQuery({
|
|
|
|
range: range,
|
|
|
|
rangeRaw: range.raw,
|
|
|
|
annotation: annotation,
|
2017-12-20 05:33:33 -06:00
|
|
|
dashboard: dashboard,
|
2017-10-07 03:31:39 -05:00
|
|
|
});
|
|
|
|
})
|
|
|
|
.then(results => {
|
|
|
|
// store response in annotation object if this is a snapshot call
|
|
|
|
if (dashboard.snapshot) {
|
|
|
|
annotation.snapshotData = angular.copy(results);
|
|
|
|
}
|
|
|
|
// translate result
|
|
|
|
return this.translateQueryResult(annotation, results);
|
2017-12-19 09:06:54 -06:00
|
|
|
})
|
2017-10-07 03:31:39 -05:00
|
|
|
);
|
|
|
|
}
|
2016-09-08 07:31:30 -05:00
|
|
|
|
2017-10-07 03:31:39 -05:00
|
|
|
this.globalAnnotationsPromise = this.$q.all(promises);
|
2016-09-08 07:31:30 -05:00
|
|
|
return this.globalAnnotationsPromise;
|
|
|
|
}
|
|
|
|
|
2017-04-14 04:41:02 -05:00
|
|
|
saveAnnotationEvent(annotation) {
|
2017-04-14 05:23:32 -05:00
|
|
|
this.globalAnnotationsPromise = null;
|
2017-12-20 05:33:33 -06:00
|
|
|
return this.backendSrv.post('/api/annotations', annotation);
|
2017-04-08 05:32:16 -05:00
|
|
|
}
|
|
|
|
|
2017-10-07 03:31:39 -05:00
|
|
|
updateAnnotationEvent(annotation) {
|
|
|
|
this.globalAnnotationsPromise = null;
|
|
|
|
return this.backendSrv.put(`/api/annotations/${annotation.id}`, annotation);
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteAnnotationEvent(annotation) {
|
|
|
|
this.globalAnnotationsPromise = null;
|
|
|
|
let deleteUrl = `/api/annotations/${annotation.id}`;
|
|
|
|
if (annotation.isRegion) {
|
|
|
|
deleteUrl = `/api/annotations/region/${annotation.regionId}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.backendSrv.delete(deleteUrl);
|
|
|
|
}
|
|
|
|
|
2016-09-09 04:30:55 -05:00
|
|
|
translateQueryResult(annotation, results) {
|
2017-04-25 07:27:41 -05:00
|
|
|
// if annotation has snapshotData
|
|
|
|
// make clone and remove it
|
|
|
|
if (annotation.snapshotData) {
|
|
|
|
annotation = angular.copy(annotation);
|
|
|
|
delete annotation.snapshotData;
|
|
|
|
}
|
|
|
|
|
2018-08-29 07:27:29 -05:00
|
|
|
for (const item of results) {
|
2016-09-09 04:30:55 -05:00
|
|
|
item.source = annotation;
|
2016-09-08 07:31:30 -05:00
|
|
|
}
|
2016-09-09 04:30:55 -05:00
|
|
|
return results;
|
2016-09-08 07:31:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
coreModule.service('annotationsSrv', AnnotationsSrv);
|