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

This commit is contained in:
Ryan McKinley 2020-09-03 10:41:25 -07:00 committed by GitHub
parent cac0e6ecd0
commit b867050cfb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 10 deletions

View File

@ -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<TQuery>): Promise<AnnotationEvent[]>;
annotationQuery?(options: AnnotationQueryRequest<TAnno>): Promise<AnnotationEvent[]>;
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<MoreOptions = {}> {
export interface AnnotationQueryRequest<TAnno = {}> {
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<TQuery extends DataQuery = DataQuery> {

View File

@ -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,

View File

@ -539,6 +539,9 @@ function makeAnnotationQueryRequest(): AnnotationQueryRequest<LokiQuery> {
raw: timeRange,
},
rangeRaw: timeRange,
app: 'test',
interval: '1m',
intervalMs: 6000,
};
}