Annotations: Fix so adds, updates and deletes are shown correctly (#35184)

* Annotations: Fix for update/save/delete annotation not appearing/disappearing

* Annotations: Fix so adds, updates and deletes are shown correctly

* Chore: updates after PR comments
This commit is contained in:
Hugo Häggmark
2021-06-03 16:55:04 +02:00
committed by GitHub
parent b0adc806f8
commit abd4e70792

View File

@ -3,6 +3,7 @@ import { coreModule } from 'app/core/core';
import { AnnotationEvent, dateTime } from '@grafana/data'; import { AnnotationEvent, dateTime } from '@grafana/data';
import { MetricsPanelCtrl } from '../panel/metrics_panel_ctrl'; import { MetricsPanelCtrl } from '../panel/metrics_panel_ctrl';
import { deleteAnnotation, saveAnnotation, updateAnnotation } from './api'; import { deleteAnnotation, saveAnnotation, updateAnnotation } from './api';
import { getDashboardQueryRunner } from '../query/state/DashboardQueryRunner/DashboardQueryRunner';
export class EventEditorCtrl { export class EventEditorCtrl {
// @ts-ignore initialized through Angular not constructor // @ts-ignore initialized through Angular not constructor
@ -18,7 +19,7 @@ export class EventEditorCtrl {
constructor() {} constructor() {}
$onInit() { $onInit() {
this.event.panelId = this.panelCtrl.panel.id; this.event.panelId = this.panelCtrl.panel.editSourceId ?? this.panelCtrl.panel.id; // set correct id if in panel edit
this.event.dashboardId = this.panelCtrl.dashboard.id; this.event.dashboardId = this.panelCtrl.dashboard.id;
// Annotations query returns time as Unix timestamp in milliseconds // Annotations query returns time as Unix timestamp in milliseconds
@ -30,7 +31,7 @@ export class EventEditorCtrl {
this.timeFormated = this.panelCtrl.dashboard.formatDate(this.event.time!); this.timeFormated = this.panelCtrl.dashboard.formatDate(this.event.time!);
} }
save() { async save(): Promise<void> {
if (!this.form.$valid) { if (!this.form.$valid) {
return; return;
} }
@ -48,39 +49,30 @@ export class EventEditorCtrl {
} }
} }
let crudFunction = saveAnnotation;
if (saveModel.id) { if (saveModel.id) {
updateAnnotation(saveModel) crudFunction = updateAnnotation;
.then(() => { }
this.panelCtrl.refresh();
this.close(); try {
}) await crudFunction(saveModel);
.catch(() => { } catch (err) {
this.panelCtrl.refresh(); console.log(err);
this.close(); } finally {
}); this.close();
} else { getDashboardQueryRunner().run({ dashboard: this.panelCtrl.dashboard, range: this.panelCtrl.range });
saveAnnotation(saveModel)
.then(() => {
this.panelCtrl.refresh();
this.close();
})
.catch(() => {
this.panelCtrl.refresh();
this.close();
});
} }
} }
delete() { async delete(): Promise<void> {
return deleteAnnotation(this.event) try {
.then(() => { await deleteAnnotation(this.event);
this.panelCtrl.refresh(); } catch (err) {
this.close(); console.log(err);
}) } finally {
.catch(() => { this.close();
this.panelCtrl.refresh(); getDashboardQueryRunner().run({ dashboard: this.panelCtrl.dashboard, range: this.panelCtrl.range });
this.close(); }
});
} }
} }