2018-10-25 04:35:32 -05:00
|
|
|
// Libaries
|
2020-01-21 03:08:07 -06:00
|
|
|
import flattenDeep from 'lodash/flattenDeep';
|
|
|
|
import cloneDeep from 'lodash/cloneDeep';
|
2018-10-25 04:35:32 -05:00
|
|
|
// Components
|
|
|
|
import './editor_ctrl';
|
2017-12-20 05:33:33 -06:00
|
|
|
import coreModule from 'app/core/core_module';
|
2018-10-25 04:35:32 -05:00
|
|
|
// Utils & Services
|
2019-08-16 03:49:30 -05:00
|
|
|
import { dedupAnnotations } from './events_processing';
|
2018-10-25 04:35:32 -05:00
|
|
|
// Types
|
2020-03-27 00:40:14 -05:00
|
|
|
import { DashboardModel, PanelModel } from '../dashboard/state';
|
|
|
|
import { AnnotationEvent, AppEvents, DataSourceApi, PanelEvents, TimeRange } from '@grafana/data';
|
2020-01-21 03:08:07 -06:00
|
|
|
import { getBackendSrv, getDataSourceSrv } from '@grafana/runtime';
|
2020-02-12 02:37:36 -06:00
|
|
|
import { appEvents } from 'app/core/core';
|
2020-01-21 03:08:07 -06:00
|
|
|
import { getTimeSrv } from '../dashboard/services/TimeSrv';
|
2018-10-25 04:35:32 -05:00
|
|
|
|
2016-09-08 07:31:30 -05:00
|
|
|
export class AnnotationsSrv {
|
|
|
|
globalAnnotationsPromise: any;
|
2016-09-30 10:37:47 -05:00
|
|
|
alertStatesPromise: any;
|
2018-09-19 14:10:48 -05:00
|
|
|
datasourcePromises: any;
|
2016-09-08 07:31:30 -05:00
|
|
|
|
2018-10-25 04:35:32 -05:00
|
|
|
init(dashboard: DashboardModel) {
|
2019-01-28 11:19:12 -06:00
|
|
|
// always clearPromiseCaches when loading new dashboard
|
|
|
|
this.clearPromiseCaches();
|
2018-10-25 04:35:32 -05:00
|
|
|
// clear promises on refresh events
|
2019-10-14 03:27:47 -05:00
|
|
|
dashboard.on(PanelEvents.refresh, this.clearPromiseCaches.bind(this));
|
2019-01-28 11:19:12 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
clearPromiseCaches() {
|
|
|
|
this.globalAnnotationsPromise = null;
|
|
|
|
this.alertStatesPromise = null;
|
|
|
|
this.datasourcePromises = null;
|
2016-09-08 07:31:30 -05:00
|
|
|
}
|
|
|
|
|
2019-11-15 09:38:25 -06:00
|
|
|
getAnnotations(options: { dashboard: DashboardModel; panel: PanelModel; range: TimeRange }) {
|
2019-12-05 03:04:03 -06:00
|
|
|
return Promise.all([this.getGlobalAnnotations(options), this.getAlertStates(options)])
|
2017-10-07 03:31:39 -05:00
|
|
|
.then(results => {
|
|
|
|
// combine the annotations and flatten results
|
2020-01-21 03:08:07 -06:00
|
|
|
let annotations: AnnotationEvent[] = flattenDeep(results[0]);
|
2020-04-10 09:37:26 -05:00
|
|
|
// when in edit mode we need to use this function to get the saved id
|
|
|
|
let panelFilterId = options.panel.getSavedId();
|
2017-10-07 03:31:39 -05:00
|
|
|
|
|
|
|
// filter out annotations that do not belong to requesting panel
|
2020-01-21 03:08:07 -06:00
|
|
|
annotations = annotations.filter(item => {
|
2017-10-07 03:31:39 -05:00
|
|
|
// 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') {
|
2020-03-27 00:40:14 -05:00
|
|
|
return item.panelId === panelFilterId;
|
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);
|
2016-09-09 04:30:55 -05:00
|
|
|
|
2017-10-07 03:31:39 -05:00
|
|
|
// look for alert state for this panel
|
2020-03-27 00:40:14 -05:00
|
|
|
const alertState: any = results[1].find((res: any) => res.panelId === panelFilterId);
|
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);
|
2020-01-21 03:08:07 -06:00
|
|
|
appEvents.emit(AppEvents.alertError, ['Annotation Query Failed', err.message || err]);
|
2017-10-07 03:31:39 -05:00
|
|
|
return [];
|
2016-09-09 04:30:55 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-06-27 06:21:04 -05:00
|
|
|
getAlertStates(options: any) {
|
2016-09-30 10:37:47 -05:00
|
|
|
if (!options.dashboard.id) {
|
2019-12-05 03:04:03 -06:00
|
|
|
return Promise.resolve([]);
|
2016-09-30 10:37:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ignore if no alerts
|
|
|
|
if (options.panel && !options.panel.alert) {
|
2019-12-05 03:04:03 -06:00
|
|
|
return Promise.resolve([]);
|
2016-09-30 10:37:47 -05:00
|
|
|
}
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
if (options.range.raw.to !== 'now') {
|
2019-12-05 03:04:03 -06:00
|
|
|
return Promise.resolve([]);
|
2016-09-30 10:37:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.alertStatesPromise) {
|
|
|
|
return this.alertStatesPromise;
|
|
|
|
}
|
|
|
|
|
2020-02-12 02:37:36 -06:00
|
|
|
this.alertStatesPromise = getBackendSrv().get(
|
|
|
|
'/api/alerts/states-for-dashboard',
|
|
|
|
{
|
|
|
|
dashboardId: options.dashboard.id,
|
|
|
|
},
|
|
|
|
`get-alert-states-${options.dashboard.id}`
|
|
|
|
);
|
2020-02-09 03:53:34 -06:00
|
|
|
|
2016-09-30 10:37:47 -05:00
|
|
|
return this.alertStatesPromise;
|
|
|
|
}
|
|
|
|
|
2019-11-15 09:38:25 -06:00
|
|
|
getGlobalAnnotations(options: { dashboard: DashboardModel; panel: PanelModel; range: TimeRange }) {
|
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;
|
|
|
|
}
|
|
|
|
|
2020-01-21 03:08:07 -06:00
|
|
|
const range = getTimeSrv().timeRange();
|
2018-08-29 07:27:29 -05:00
|
|
|
const promises = [];
|
2018-09-19 14:10:48 -05:00
|
|
|
const dsPromises = [];
|
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
|
|
|
}
|
2020-01-21 03:08:07 -06:00
|
|
|
const datasourcePromise = getDataSourceSrv().get(annotation.datasource);
|
2018-09-19 14:10:48 -05:00
|
|
|
dsPromises.push(datasourcePromise);
|
2017-10-07 03:31:39 -05:00
|
|
|
promises.push(
|
2018-09-19 14:10:48 -05:00
|
|
|
datasourcePromise
|
2019-09-10 04:04:44 -05:00
|
|
|
.then((datasource: DataSourceApi) => {
|
2017-10-07 03:31:39 -05:00
|
|
|
// issue query against data source
|
|
|
|
return datasource.annotationQuery({
|
2019-11-15 09:38:25 -06:00
|
|
|
range,
|
2017-10-07 03:31:39 -05:00
|
|
|
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) {
|
2020-01-21 03:08:07 -06:00
|
|
|
annotation.snapshotData = cloneDeep(results);
|
2017-10-07 03:31:39 -05:00
|
|
|
}
|
|
|
|
// translate result
|
|
|
|
return this.translateQueryResult(annotation, results);
|
2017-12-19 09:06:54 -06:00
|
|
|
})
|
2017-10-07 03:31:39 -05:00
|
|
|
);
|
|
|
|
}
|
2019-12-05 03:04:03 -06:00
|
|
|
this.datasourcePromises = Promise.all(dsPromises);
|
|
|
|
this.globalAnnotationsPromise = Promise.all(promises);
|
2016-09-08 07:31:30 -05:00
|
|
|
return this.globalAnnotationsPromise;
|
|
|
|
}
|
|
|
|
|
2019-08-01 07:38:34 -05:00
|
|
|
saveAnnotationEvent(annotation: AnnotationEvent) {
|
2017-04-14 05:23:32 -05:00
|
|
|
this.globalAnnotationsPromise = null;
|
2020-01-21 03:08:07 -06:00
|
|
|
return getBackendSrv().post('/api/annotations', annotation);
|
2017-04-08 05:32:16 -05:00
|
|
|
}
|
|
|
|
|
2019-08-01 07:38:34 -05:00
|
|
|
updateAnnotationEvent(annotation: AnnotationEvent) {
|
2017-10-07 03:31:39 -05:00
|
|
|
this.globalAnnotationsPromise = null;
|
2020-01-21 03:08:07 -06:00
|
|
|
return getBackendSrv().put(`/api/annotations/${annotation.id}`, annotation);
|
2017-10-07 03:31:39 -05:00
|
|
|
}
|
|
|
|
|
2019-08-01 07:38:34 -05:00
|
|
|
deleteAnnotationEvent(annotation: AnnotationEvent) {
|
2017-10-07 03:31:39 -05:00
|
|
|
this.globalAnnotationsPromise = null;
|
2019-08-16 03:49:30 -05:00
|
|
|
const deleteUrl = `/api/annotations/${annotation.id}`;
|
2017-10-07 03:31:39 -05:00
|
|
|
|
2020-01-21 03:08:07 -06:00
|
|
|
return getBackendSrv().delete(deleteUrl);
|
2017-10-07 03:31:39 -05:00
|
|
|
}
|
|
|
|
|
2019-06-27 06:21:04 -05:00
|
|
|
translateQueryResult(annotation: any, results: any) {
|
2017-04-25 07:27:41 -05:00
|
|
|
// if annotation has snapshotData
|
|
|
|
// make clone and remove it
|
|
|
|
if (annotation.snapshotData) {
|
2020-01-21 03:08:07 -06:00
|
|
|
annotation = cloneDeep(annotation);
|
2017-04-25 07:27:41 -05:00
|
|
|
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;
|
2019-09-05 01:14:06 -05:00
|
|
|
item.isRegion = item.timeEnd && item.time !== item.timeEnd;
|
2016-09-08 07:31:30 -05:00
|
|
|
}
|
2019-09-05 01:14:06 -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);
|