Commit b867050c by Ryan McKinley Committed by GitHub

Annotations: improve datasource annotation types and add basic query properties (#27342)

parent cac0e6ec
...@@ -155,7 +155,8 @@ export interface DataSourceConstructor< ...@@ -155,7 +155,8 @@ export interface DataSourceConstructor<
*/ */
export abstract class DataSourceApi< export abstract class DataSourceApi<
TQuery extends DataQuery = DataQuery, TQuery extends DataQuery = DataQuery,
TOptions extends DataSourceJsonData = DataSourceJsonData TOptions extends DataSourceJsonData = DataSourceJsonData,
TAnno = TQuery // defatult to direct query
> { > {
/** /**
* Set in constructor * Set in constructor
...@@ -270,7 +271,7 @@ export abstract class DataSourceApi< ...@@ -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 * 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. * in the annotation editor `annotations` capability also needs to be enabled in plugin.json.
*/ */
annotationQuery?(options: AnnotationQueryRequest<TQuery>): Promise<AnnotationEvent[]>; annotationQuery?(options: AnnotationQueryRequest<TAnno>): Promise<AnnotationEvent[]>;
interpolateVariablesInQueries?(queries: TQuery[], scopedVars: ScopedVars | {}): TQuery[]; interpolateVariablesInQueries?(queries: TQuery[], scopedVars: ScopedVars | {}): TQuery[];
} }
...@@ -466,6 +467,12 @@ export interface MetricFindValue { ...@@ -466,6 +467,12 @@ export interface MetricFindValue {
expandable?: boolean; expandable?: boolean;
} }
export interface BaseAnnotationQuery {
datasource: string;
enable: boolean;
name: string;
}
export interface DataSourceJsonData { export interface DataSourceJsonData {
authType?: string; authType?: string;
defaultRegion?: string; defaultRegion?: string;
...@@ -535,19 +542,19 @@ export interface DataSourceSelectItem { ...@@ -535,19 +542,19 @@ export interface DataSourceSelectItem {
/** /**
* Options passed to the datasource.annotationQuery method. See docs/plugins/developing/datasource.md * Options passed to the datasource.annotationQuery method. See docs/plugins/developing/datasource.md
*/ */
export interface AnnotationQueryRequest<MoreOptions = {}> { export interface AnnotationQueryRequest<TAnno = {}> {
range: TimeRange; range: TimeRange;
rangeRaw: RawTimeRange; 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. // Should be DataModel but cannot import that here from the main app. Needs to be moved to package first.
dashboard: any; dashboard: any;
// The annotation query, typically extends DataQuery // The annotation query and common properties
annotation: { annotation: BaseAnnotationQuery & TAnno;
datasource: string;
enable: boolean;
name: string;
} & MoreOptions;
} }
export interface HistoryItem<TQuery extends DataQuery = DataQuery> { export interface HistoryItem<TQuery extends DataQuery = DataQuery> {
......
...@@ -8,10 +8,11 @@ import coreModule from 'app/core/core_module'; ...@@ -8,10 +8,11 @@ import coreModule from 'app/core/core_module';
import { dedupAnnotations } from './events_processing'; import { dedupAnnotations } from './events_processing';
// Types // Types
import { DashboardModel, PanelModel } from '../dashboard/state'; 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 { getBackendSrv, getDataSourceSrv } from '@grafana/runtime';
import { appEvents } from 'app/core/core'; import { appEvents } from 'app/core/core';
import { getTimeSrv } from '../dashboard/services/TimeSrv'; import { getTimeSrv } from '../dashboard/services/TimeSrv';
import kbn from 'app/core/utils/kbn';
export class AnnotationsSrv { export class AnnotationsSrv {
globalAnnotationsPromise: any; globalAnnotationsPromise: any;
...@@ -113,6 +114,9 @@ export class AnnotationsSrv { ...@@ -113,6 +114,9 @@ export class AnnotationsSrv {
const promises = []; const promises = [];
const dsPromises = []; const dsPromises = [];
// No more points than pixels
const maxDataPoints = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
for (const annotation of dashboard.annotations.list) { for (const annotation of dashboard.annotations.list) {
if (!annotation.enable) { if (!annotation.enable) {
continue; continue;
...@@ -130,7 +134,12 @@ export class AnnotationsSrv { ...@@ -130,7 +134,12 @@ export class AnnotationsSrv {
return []; return [];
} }
// Add interval to annotation queries
const interval = kbn.calculateInterval(range, maxDataPoints, datasource.interval);
return datasource.annotationQuery({ return datasource.annotationQuery({
...interval,
app: CoreApp.Dashboard,
range, range,
rangeRaw: range.raw, rangeRaw: range.raw,
annotation: annotation, annotation: annotation,
......
...@@ -539,6 +539,9 @@ function makeAnnotationQueryRequest(): AnnotationQueryRequest<LokiQuery> { ...@@ -539,6 +539,9 @@ function makeAnnotationQueryRequest(): AnnotationQueryRequest<LokiQuery> {
raw: timeRange, raw: timeRange,
}, },
rangeRaw: timeRange, rangeRaw: timeRange,
app: 'test',
interval: '1m',
intervalMs: 6000,
}; };
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment