Commit 1e4846d4 by Ryan McKinley Committed by GitHub

Annotation: use DataFrame[] rather than a single DataFrame (#27587)

parent e04e3e7d
...@@ -78,7 +78,7 @@ export interface AnnotationSupport<TQuery extends DataQuery = DataQuery, TAnno = ...@@ -78,7 +78,7 @@ export interface AnnotationSupport<TQuery extends DataQuery = DataQuery, TAnno =
/** /**
* When the standard frame > event processing is insufficient, this allows explicit control of the mappings * When the standard frame > event processing is insufficient, this allows explicit control of the mappings
*/ */
processEvents?(anno: TAnno, data: DataFrame): AnnotationEvent[] | undefined; processEvents?(anno: TAnno, data: DataFrame[]): AnnotationEvent[] | undefined;
/** /**
* Specify a custom QueryEditor for the annotation page. If not specified, the standard one will be used * Specify a custom QueryEditor for the annotation page. If not specified, the standard one will be used
......
...@@ -24,7 +24,7 @@ import { getTimeSrv } from '../dashboard/services/TimeSrv'; ...@@ -24,7 +24,7 @@ import { getTimeSrv } from '../dashboard/services/TimeSrv';
import { Observable, of } from 'rxjs'; import { Observable, of } from 'rxjs';
import { map } from 'rxjs/operators'; import { map } from 'rxjs/operators';
import { AnnotationQueryResponse, AnnotationQueryOptions } from './types'; import { AnnotationQueryResponse, AnnotationQueryOptions } from './types';
import { standardAnnotationSupport, singleFrameFromPanelData } from './standardAnnotationSupport'; import { standardAnnotationSupport } from './standardAnnotationSupport';
import { runRequest } from '../dashboard/state/runRequest'; import { runRequest } from '../dashboard/state/runRequest';
let counter = 100; let counter = 100;
...@@ -263,9 +263,8 @@ export function executeAnnotationQuery( ...@@ -263,9 +263,8 @@ export function executeAnnotationQuery(
return runRequest(datasource, queryRequest).pipe( return runRequest(datasource, queryRequest).pipe(
map(panelData => { map(panelData => {
const frame = singleFrameFromPanelData(panelData); const events = panelData.series ? processor.processEvents!(annotation, panelData.series) : [];
const events = frame ? processor.processEvents!(annotation, frame) : []; return { panelData, events };
return { panelData, frame, events };
}) })
); );
} }
......
...@@ -42,7 +42,7 @@ export class AnnotationFieldMapper extends PureComponent<Props, State> { ...@@ -42,7 +42,7 @@ export class AnnotationFieldMapper extends PureComponent<Props, State> {
} }
updateFields = () => { updateFields = () => {
const frame = this.props.response?.frame; const frame = this.props.response?.panelData?.series[0];
if (frame && frame.fields) { if (frame && frame.fields) {
const fieldNames = frame.fields.map(f => { const fieldNames = frame.fields.map(f => {
const name = getFieldDisplayName(f, frame); const name = getFieldDisplayName(f, frame);
......
...@@ -97,7 +97,7 @@ export default class StandardAnnotationQueryEditor extends PureComponent<Props, ...@@ -97,7 +97,7 @@ export default class StandardAnnotationQueryEditor extends PureComponent<Props,
if (running || response?.panelData?.state === LoadingState.Loading || !response) { if (running || response?.panelData?.state === LoadingState.Loading || !response) {
text = 'loading...'; text = 'loading...';
} else { } else {
const { events, panelData, frame } = response; const { events, panelData } = response;
if (panelData?.error) { if (panelData?.error) {
rowStyle = 'alert-error'; rowStyle = 'alert-error';
...@@ -108,6 +108,8 @@ export default class StandardAnnotationQueryEditor extends PureComponent<Props, ...@@ -108,6 +108,8 @@ export default class StandardAnnotationQueryEditor extends PureComponent<Props,
icon = 'exclamation-triangle'; icon = 'exclamation-triangle';
text = 'No events found'; text = 'No events found';
} else { } else {
const frame = panelData?.series[0];
text = `${events.length} events (from ${frame?.fields.length} fields)`; text = `${events.length} events (from ${frame?.fields.length} fields)`;
} }
} }
......
import { toDataFrame, FieldType } from '@grafana/data'; import { toDataFrame, FieldType } from '@grafana/data';
import { getAnnotationsFromFrame } from './standardAnnotationSupport'; import { getAnnotationsFromData } from './standardAnnotationSupport';
describe('DataFrame to annotations', () => { describe('DataFrame to annotations', () => {
test('simple conversion', () => { test('simple conversion', () => {
...@@ -11,7 +11,7 @@ describe('DataFrame to annotations', () => { ...@@ -11,7 +11,7 @@ describe('DataFrame to annotations', () => {
], ],
}); });
const events = getAnnotationsFromFrame(frame); const events = getAnnotationsFromData([frame]);
expect(events).toMatchInlineSnapshot(` expect(events).toMatchInlineSnapshot(`
Array [ Array [
Object { Object {
...@@ -51,7 +51,7 @@ describe('DataFrame to annotations', () => { ...@@ -51,7 +51,7 @@ describe('DataFrame to annotations', () => {
], ],
}); });
const events = getAnnotationsFromFrame(frame, { const events = getAnnotationsFromData([frame], {
text: { value: 'bbbbb' }, text: { value: 'bbbbb' },
time: { value: 'time2' }, time: { value: 'time2' },
timeEnd: { value: 'time1' }, timeEnd: { value: 'time1' },
......
...@@ -2,7 +2,6 @@ import { ...@@ -2,7 +2,6 @@ import {
DataFrame, DataFrame,
AnnotationQuery, AnnotationQuery,
AnnotationSupport, AnnotationSupport,
PanelData,
transformDataFrame, transformDataFrame,
FieldType, FieldType,
Field, Field,
...@@ -43,20 +42,20 @@ export const standardAnnotationSupport: AnnotationSupport = { ...@@ -43,20 +42,20 @@ export const standardAnnotationSupport: AnnotationSupport = {
/** /**
* When the standard frame > event processing is insufficient, this allows explicit control of the mappings * When the standard frame > event processing is insufficient, this allows explicit control of the mappings
*/ */
processEvents: (anno: AnnotationQuery, data: DataFrame) => { processEvents: (anno: AnnotationQuery, data: DataFrame[]) => {
return getAnnotationsFromFrame(data, anno.mappings); return getAnnotationsFromData(data, anno.mappings);
}, },
}; };
/** /**
* Flatten all panel data into a single frame * Flatten all panel data into a single frame
*/ */
export function singleFrameFromPanelData(rsp: PanelData): DataFrame | undefined { export function singleFrameFromPanelData(data: DataFrame[]): DataFrame | undefined {
if (!rsp?.series?.length) { if (!data?.length) {
return undefined; return undefined;
} }
if (rsp.series.length === 1) { if (data.length === 1) {
return rsp.series[0]; return data[0];
} }
return transformDataFrame( return transformDataFrame(
...@@ -66,7 +65,7 @@ export function singleFrameFromPanelData(rsp: PanelData): DataFrame | undefined ...@@ -66,7 +65,7 @@ export function singleFrameFromPanelData(rsp: PanelData): DataFrame | undefined
options: { byField: 'Time' }, options: { byField: 'Time' },
}, },
], ],
rsp.series data
)[0]; )[0];
} }
...@@ -108,7 +107,8 @@ export const annotationEventNames: AnnotationFieldInfo[] = [ ...@@ -108,7 +107,8 @@ export const annotationEventNames: AnnotationFieldInfo[] = [
// { key: 'email' }, // { key: 'email' },
]; ];
export function getAnnotationsFromFrame(frame: DataFrame, options?: AnnotationEventMappings): AnnotationEvent[] { export function getAnnotationsFromData(data: DataFrame[], options?: AnnotationEventMappings): AnnotationEvent[] {
const frame = singleFrameFromPanelData(data);
if (!frame?.length) { if (!frame?.length) {
return []; return [];
} }
......
import { PanelData, DataFrame, AnnotationEvent, TimeRange } from '@grafana/data'; import { PanelData, AnnotationEvent, TimeRange } from '@grafana/data';
import { DashboardModel, PanelModel } from '../dashboard/state'; import { DashboardModel, PanelModel } from '../dashboard/state';
export interface AnnotationQueryOptions { export interface AnnotationQueryOptions {
...@@ -9,11 +9,6 @@ export interface AnnotationQueryOptions { ...@@ -9,11 +9,6 @@ export interface AnnotationQueryOptions {
export interface AnnotationQueryResponse { export interface AnnotationQueryResponse {
/** /**
* All the data flattened to a single frame
*/
frame?: DataFrame;
/**
* The processed annotation events * The processed annotation events
*/ */
events?: AnnotationEvent[]; events?: AnnotationEvent[];
......
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