From 45e1bfe42128112dbd6b3d73580a625a85391c41 Mon Sep 17 00:00:00 2001 From: Adela Almasan <88068998+adela-almasan@users.noreply.github.com> Date: Mon, 17 Apr 2023 17:05:09 -0500 Subject: [PATCH] Testdata: Update testdata annotations editor (#66620) Co-authored-by: Ryan McKinley --- .betterer.results | 13 ++++----- .../components/AnnotationResultMapper.tsx | 3 +- .../StandardAnnotationQueryEditor.tsx | 2 +- .../annotations/executeAnnotationQuery.ts | 7 +++-- .../datasource/testdata/QueryEditor.tsx | 22 +++++++++++++-- .../plugins/datasource/testdata/datasource.ts | 28 ++++++++++++++----- .../plugins/datasource/testdata/module.tsx | 7 ----- 7 files changed, 52 insertions(+), 30 deletions(-) diff --git a/.betterer.results b/.betterer.results index ffb18144921..5608c47eebe 100644 --- a/.betterer.results +++ b/.betterer.results @@ -5049,10 +5049,11 @@ exports[`better eslint`] = { "public/app/plugins/datasource/testdata/QueryEditor.tsx:5381": [ [0, 0, 0, "Unexpected any. Specify a different type.", "0"], [0, 0, 0, "Do not use any type assertions.", "1"], - [0, 0, 0, "Unexpected any. Specify a different type.", "2"], + [0, 0, 0, "Do not use any type assertions.", "2"], [0, 0, 0, "Unexpected any. Specify a different type.", "3"], - [0, 0, 0, "Do not use any type assertions.", "4"], - [0, 0, 0, "Unexpected any. Specify a different type.", "5"] + [0, 0, 0, "Unexpected any. Specify a different type.", "4"], + [0, 0, 0, "Do not use any type assertions.", "5"], + [0, 0, 0, "Unexpected any. Specify a different type.", "6"] ], "public/app/plugins/datasource/testdata/components/RandomWalkEditor.tsx:5381": [ [0, 0, 0, "Do not use any type assertions.", "0"], @@ -5078,11 +5079,7 @@ exports[`better eslint`] = { ], "public/app/plugins/datasource/testdata/datasource.ts:5381": [ [0, 0, 0, "Do not use any type assertions.", "0"], - [0, 0, 0, "Unexpected any. Specify a different type.", "1"], - [0, 0, 0, "Unexpected any. Specify a different type.", "2"] - ], - "public/app/plugins/datasource/testdata/module.tsx:5381": [ - [0, 0, 0, "Unexpected any. Specify a different type.", "0"] + [0, 0, 0, "Unexpected any. Specify a different type.", "1"] ], "public/app/plugins/datasource/testdata/nodeGraphUtils.ts:5381": [ [0, 0, 0, "Do not use any type assertions.", "0"], diff --git a/public/app/features/annotations/components/AnnotationResultMapper.tsx b/public/app/features/annotations/components/AnnotationResultMapper.tsx index f4dc02c07d0..fabef95b7e4 100644 --- a/public/app/features/annotations/components/AnnotationResultMapper.tsx +++ b/public/app/features/annotations/components/AnnotationResultMapper.tsx @@ -42,7 +42,8 @@ export class AnnotationFieldMapper extends PureComponent { } updateFields = () => { - const frame = this.props.response?.panelData?.series[0]; + const panelData = this.props.response?.panelData; + const frame = panelData?.series?.[0] ?? panelData?.annotations?.[0]; if (frame && frame.fields) { const fieldNames = frame.fields.map((f) => { const name = getFieldDisplayName(f, frame); diff --git a/public/app/features/annotations/components/StandardAnnotationQueryEditor.tsx b/public/app/features/annotations/components/StandardAnnotationQueryEditor.tsx index e6df175dc2e..4bce32eddb1 100644 --- a/public/app/features/annotations/components/StandardAnnotationQueryEditor.tsx +++ b/public/app/features/annotations/components/StandardAnnotationQueryEditor.tsx @@ -132,7 +132,7 @@ export default class StandardAnnotationQueryEditor extends PureComponent { - if (!panelData.series) { + // Some annotations set the topic already + const data = panelData?.series.length ? panelData.series : panelData.annotations; + if (!data?.length) { return of({ panelData, events: [] }); } - - return processor.processEvents!(annotation, panelData.series).pipe(map((events) => ({ panelData, events }))); + return processor.processEvents!(annotation, data).pipe(map((events) => ({ panelData, events }))); }) ); } diff --git a/public/app/plugins/datasource/testdata/QueryEditor.tsx b/public/app/plugins/datasource/testdata/QueryEditor.tsx index 0aa6a27c558..1fb08b1aad6 100644 --- a/public/app/plugins/datasource/testdata/QueryEditor.tsx +++ b/public/app/plugins/datasource/testdata/QueryEditor.tsx @@ -57,10 +57,10 @@ export const QueryEditor = ({ query, datasource, onChange, onRunQuery }: Props) } const vals = await datasource.getScenarios(); - const hideAlias = ['simulation']; + const hideAlias = [TestDataQueryType.Simulation, TestDataQueryType.Annotations]; return vals.map((v) => ({ ...v, - hideAliasField: hideAlias.includes(v.id), + hideAliasField: hideAlias.includes(v.id as TestDataQueryType), })); }, []); @@ -114,6 +114,9 @@ export const QueryEditor = ({ query, datasource, onChange, onRunQuery }: Props) case TestDataQueryType.PredictableCSVWave: update.csvWave = defaultCSVWaveQuery; break; + case TestDataQueryType.Annotations: + update.lines = 10; + break; case TestDataQueryType.USA: update.usa = { mode: usaQueryModes[0].value, @@ -277,7 +280,20 @@ export const QueryEditor = ({ query, datasource, onChange, onRunQuery }: Props) )} - + {scenarioId === TestDataQueryType.Annotations && ( + + + + + + )} {scenarioId === TestDataQueryType.USA && } {scenarioId === TestDataQueryType.GrafanaAPI && ( diff --git a/public/app/plugins/datasource/testdata/datasource.ts b/public/app/plugins/datasource/testdata/datasource.ts index c662332e9ee..739e7bdade6 100644 --- a/public/app/plugins/datasource/testdata/datasource.ts +++ b/public/app/plugins/datasource/testdata/datasource.ts @@ -15,6 +15,7 @@ import { ScopedVars, toDataFrame, MutableDataFrame, + AnnotationQuery, } from '@grafana/data'; import { DataSourceWithBackend, getBackendSrv, getGrafanaLiveSrv, getTemplateSrv, TemplateSrv } from '@grafana/runtime'; import { getSearchFilterScopedVar } from 'app/features/variables/utils'; @@ -35,6 +36,24 @@ export class TestDataDataSource extends DataSourceWithBackend { ) { super(instanceSettings); this.variables = new TestDataVariableSupport(); + this.annotations = { + getDefaultQuery: () => ({ scenarioId: TestDataQueryType.Annotations, lines: 10 }), + + // Make sure annotations have scenarioId set + prepareAnnotation: (old: AnnotationQuery) => { + if (old.target?.scenarioId?.length) { + return old; + } + return { + ...old, + target: { + refId: 'Anno', + scenarioId: TestDataQueryType.Annotations, + lines: 10, + }, + }; + }, + }; } getDefaultQuery(): Partial { @@ -66,7 +85,7 @@ export class TestDataDataSource extends DataSourceWithBackend { case 'grafana_api': streams.push(runGrafanaAPI(target, options)); break; - case 'annotations': + case TestDataQueryType.Annotations: streams.push(this.annotationDataTopicTest(target, options)); break; case 'variables-query': @@ -144,10 +163,9 @@ export class TestDataDataSource extends DataSourceWithBackend { } annotationDataTopicTest(target: TestData, req: DataQueryRequest): Observable { - const events = this.buildFakeAnnotationEvents(req.range, 50); + const events = this.buildFakeAnnotationEvents(req.range, target.lines ?? 10); const dataFrame = new ArrayDataFrame(events); dataFrame.meta = { dataTopic: DataTopic.Annotations }; - return of({ key: target.refId, data: [dataFrame] }).pipe(delay(100)); } @@ -169,10 +187,6 @@ export class TestDataDataSource extends DataSourceWithBackend { return events; } - annotationQuery(options: any) { - return Promise.resolve(this.buildFakeAnnotationEvents(options.range, 10)); - } - getQueryDisplayText(query: TestData) { const scenario = query.scenarioId ?? 'Default scenario'; diff --git a/public/app/plugins/datasource/testdata/module.tsx b/public/app/plugins/datasource/testdata/module.tsx index df57421b387..ad727cf0f87 100644 --- a/public/app/plugins/datasource/testdata/module.tsx +++ b/public/app/plugins/datasource/testdata/module.tsx @@ -5,16 +5,9 @@ import { QueryEditor } from './QueryEditor'; import { TestInfoTab } from './TestInfoTab'; import { TestDataDataSource } from './datasource'; -class TestDataAnnotationsQueryCtrl { - annotation: any; - constructor() {} - static template = '

Annotation scenario

'; -} - export const plugin = new DataSourcePlugin(TestDataDataSource) .setConfigEditor(ConfigEditor) .setQueryEditor(QueryEditor) - .setAnnotationQueryCtrl(TestDataAnnotationsQueryCtrl) .addConfigPage({ title: 'Setup', icon: 'list-ul',