1import { from, Observable, of } from 'rxjs';
2import { catchError } from 'rxjs/operators';
3import { AnnotationEvent, DataSourceApi } from '@grafana/data';
4
5import { AnnotationQueryRunner, AnnotationQueryRunnerOptions } from './types';
6import { handleAnnotationQueryRunnerError } from './utils';
7
8export class LegacyAnnotationQueryRunner implements AnnotationQueryRunner {
9  canRun(datasource?: DataSourceApi): boolean {
10    if (!datasource) {
11      return false;
12    }
13
14    return Boolean(datasource.annotationQuery && !datasource.annotations);
15  }
16
17  run({ annotation, datasource, dashboard, range }: AnnotationQueryRunnerOptions): Observable<AnnotationEvent[]> {
18    if (!this.canRun(datasource)) {
19      return of([]);
20    }
21
22    return from(datasource!.annotationQuery!({ range, rangeRaw: range.raw, annotation, dashboard })).pipe(
23      catchError(handleAnnotationQueryRunnerError)
24    );
25  }
26}
27