SnapshotWorker.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { Observable, of } from 'rxjs';
  2. import { AnnotationEvent } from '@grafana/data';
  3. import { DashboardModel } from '../../../dashboard/state';
  4. import { DashboardQueryRunnerOptions, DashboardQueryRunnerWorker, DashboardQueryRunnerWorkerResult } from './types';
  5. import { emptyResult, getAnnotationsByPanelId, translateQueryResult } from './utils';
  6. export class SnapshotWorker implements DashboardQueryRunnerWorker {
  7. canWork({ dashboard }: DashboardQueryRunnerOptions): boolean {
  8. return dashboard?.annotations?.list?.some((a) => a.enable && Boolean(a.snapshotData));
  9. }
  10. work(options: DashboardQueryRunnerOptions): Observable<DashboardQueryRunnerWorkerResult> {
  11. if (!this.canWork(options)) {
  12. return emptyResult();
  13. }
  14. const annotations = this.getAnnotationsFromSnapshot(options.dashboard);
  15. return of({ annotations, alertStates: [] });
  16. }
  17. private getAnnotationsFromSnapshot(dashboard: DashboardModel): AnnotationEvent[] {
  18. const dashAnnotations = dashboard?.annotations?.list?.filter((a) => a.enable);
  19. const snapshots = dashAnnotations.filter((a) => Boolean(a.snapshotData));
  20. const annotations = snapshots.reduce(
  21. (acc, curr) => acc.concat(translateQueryResult(curr, curr.snapshotData)),
  22. [] as AnnotationEvent[]
  23. );
  24. return annotations;
  25. }
  26. getAnnotationsInSnapshot(dashboard: DashboardModel, panelId?: number): AnnotationEvent[] {
  27. const annotations = this.getAnnotationsFromSnapshot(dashboard);
  28. return getAnnotationsByPanelId(annotations, panelId);
  29. }
  30. }