AnnotationsQueryRunner.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import { Observable, of } from 'rxjs';
  2. import { catchError, map } from 'rxjs/operators';
  3. import { AnnotationEvent, DataSourceApi } from '@grafana/data';
  4. import { executeAnnotationQuery } from '../../../annotations/executeAnnotationQuery';
  5. import { PanelModel } from '../../../dashboard/state';
  6. import { AnnotationQueryRunner, AnnotationQueryRunnerOptions } from './types';
  7. import { handleAnnotationQueryRunnerError } from './utils';
  8. export class AnnotationsQueryRunner implements AnnotationQueryRunner {
  9. canRun(datasource?: DataSourceApi): boolean {
  10. if (!datasource) {
  11. return false;
  12. }
  13. return Boolean(!datasource.annotationQuery || datasource.annotations);
  14. }
  15. run({ annotation, datasource, dashboard, range }: AnnotationQueryRunnerOptions): Observable<AnnotationEvent[]> {
  16. if (!this.canRun(datasource)) {
  17. return of([]);
  18. }
  19. const panel: PanelModel = {} as unknown as PanelModel; // deliberate setting panel to empty object because executeAnnotationQuery shouldn't depend on panelModel
  20. return executeAnnotationQuery({ dashboard, range, panel }, datasource!, annotation).pipe(
  21. map((result) => {
  22. return result.events ?? [];
  23. }),
  24. catchError(handleAnnotationQueryRunnerError)
  25. );
  26. }
  27. }