diff --git a/packages/grafana-data/src/types/datasource.ts b/packages/grafana-data/src/types/datasource.ts index 26ae515bd43..72bcdeaf96f 100644 --- a/packages/grafana-data/src/types/datasource.ts +++ b/packages/grafana-data/src/types/datasource.ts @@ -155,7 +155,8 @@ export interface DataSourceConstructor< */ export abstract class DataSourceApi< TQuery extends DataQuery = DataQuery, - TOptions extends DataSourceJsonData = DataSourceJsonData + TOptions extends DataSourceJsonData = DataSourceJsonData, + TAnno = TQuery // defatult to direct query > { /** * Set in constructor @@ -270,7 +271,7 @@ export abstract class DataSourceApi< * Can be optionally implemented to allow datasource to be a source of annotations for dashboard. To be visible * in the annotation editor `annotations` capability also needs to be enabled in plugin.json. */ - annotationQuery?(options: AnnotationQueryRequest): Promise; + annotationQuery?(options: AnnotationQueryRequest): Promise; interpolateVariablesInQueries?(queries: TQuery[], scopedVars: ScopedVars | {}): TQuery[]; } @@ -466,6 +467,12 @@ export interface MetricFindValue { expandable?: boolean; } +export interface BaseAnnotationQuery { + datasource: string; + enable: boolean; + name: string; +} + export interface DataSourceJsonData { authType?: string; defaultRegion?: string; @@ -535,19 +542,19 @@ export interface DataSourceSelectItem { /** * Options passed to the datasource.annotationQuery method. See docs/plugins/developing/datasource.md */ -export interface AnnotationQueryRequest { +export interface AnnotationQueryRequest { range: TimeRange; rangeRaw: RawTimeRange; + interval: string; + intervalMs: number; + maxDataPoints?: number; + app: CoreApp | string; // Should be DataModel but cannot import that here from the main app. Needs to be moved to package first. dashboard: any; - // The annotation query, typically extends DataQuery - annotation: { - datasource: string; - enable: boolean; - name: string; - } & MoreOptions; + // The annotation query and common properties + annotation: BaseAnnotationQuery & TAnno; } export interface HistoryItem { diff --git a/public/app/features/annotations/annotations_srv.ts b/public/app/features/annotations/annotations_srv.ts index 971e5a245ff..2bb7d56cb7f 100644 --- a/public/app/features/annotations/annotations_srv.ts +++ b/public/app/features/annotations/annotations_srv.ts @@ -8,10 +8,11 @@ import coreModule from 'app/core/core_module'; import { dedupAnnotations } from './events_processing'; // Types import { DashboardModel, PanelModel } from '../dashboard/state'; -import { AnnotationEvent, AppEvents, DataSourceApi, PanelEvents, TimeRange } from '@grafana/data'; +import { AnnotationEvent, AppEvents, DataSourceApi, PanelEvents, TimeRange, CoreApp } from '@grafana/data'; import { getBackendSrv, getDataSourceSrv } from '@grafana/runtime'; import { appEvents } from 'app/core/core'; import { getTimeSrv } from '../dashboard/services/TimeSrv'; +import kbn from 'app/core/utils/kbn'; export class AnnotationsSrv { globalAnnotationsPromise: any; @@ -113,6 +114,9 @@ export class AnnotationsSrv { const promises = []; const dsPromises = []; + // No more points than pixels + const maxDataPoints = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth; + for (const annotation of dashboard.annotations.list) { if (!annotation.enable) { continue; @@ -130,7 +134,12 @@ export class AnnotationsSrv { return []; } + // Add interval to annotation queries + const interval = kbn.calculateInterval(range, maxDataPoints, datasource.interval); + return datasource.annotationQuery({ + ...interval, + app: CoreApp.Dashboard, range, rangeRaw: range.raw, annotation: annotation, diff --git a/public/app/plugins/datasource/loki/datasource.test.ts b/public/app/plugins/datasource/loki/datasource.test.ts index 6a5194b77e5..a8e677ce878 100644 --- a/public/app/plugins/datasource/loki/datasource.test.ts +++ b/public/app/plugins/datasource/loki/datasource.test.ts @@ -539,6 +539,9 @@ function makeAnnotationQueryRequest(): AnnotationQueryRequest { raw: timeRange, }, rangeRaw: timeRange, + app: 'test', + interval: '1m', + intervalMs: 6000, }; }