Datasources: Use getDefaultQuery in annotations editors (#61870)

+ Add Cloudwatch default annotation
This commit is contained in:
Ida Štambuk
2023-01-30 16:45:03 +01:00
committed by GitHub
parent b6f477ae03
commit f1a2a76897
8 changed files with 106 additions and 11 deletions

View File

@@ -0,0 +1,70 @@
import { render } from '@testing-library/react';
import React from 'react';
import { AnnotationQuery, DataSourceApi, DataSourceInstanceSettings } from '@grafana/data/src';
import StandardAnnotationQueryEditor, { Props as EditorProps } from './StandardAnnotationQueryEditor';
const setup = (customProps: Partial<EditorProps>) => {
const props: EditorProps = {
datasource: {} as unknown as DataSourceApi,
datasourceInstanceSettings: {} as DataSourceInstanceSettings,
annotation: {} as AnnotationQuery,
onChange: jest.fn(),
...customProps,
};
const { rerender } = render(<StandardAnnotationQueryEditor {...props} />);
return { rerender, props };
};
jest.mock('app/features/dashboard/services/DashboardSrv', () => ({
getDashboardSrv: jest.fn().mockReturnValue({
getCurrent: jest.fn().mockReturnValue(null),
}),
}));
jest.mock('app/features/dashboard/services/TimeSrv', () => ({
getTimeSrv: jest.fn().mockReturnValue({
timeRange: jest.fn().mockReturnValue({}),
}),
}));
describe('StandardAnnotationQueryEditor', () => {
it('should fill out a default query if it is defined and pass it to the Query Editor', () => {
const { props } = setup({
annotation: { name: 'initialAnn', target: { refId: 'initialAnnotationRef' } } as AnnotationQuery,
datasource: {
annotations: {
QueryEditor: jest.fn(() => <div>Editor</div>),
getDefaultQuery: jest.fn().mockImplementation(() => ({ queryType: 'defaultAnnotationsQuery' })),
prepareAnnotation: (annotation: AnnotationQuery) => annotation,
},
} as unknown as DataSourceApi,
});
expect(props.datasource?.annotations?.getDefaultQuery).toBeDefined();
expect(props.datasource?.annotations?.QueryEditor).toHaveBeenCalledWith(
expect.objectContaining({
query: expect.objectContaining({ queryType: 'defaultAnnotationsQuery', refId: 'initialAnnotationRef' }),
}),
expect.anything()
);
});
it('should keep and pass the initial query if the defaultQuery is not defined', () => {
const { props } = setup({
annotation: { name: 'initialAnn', target: { refId: 'initialAnnotationRef' } } as AnnotationQuery,
datasource: {
annotations: {
QueryEditor: jest.fn(() => <div>Editor</div>),
prepareAnnotation: (annotation: AnnotationQuery) => annotation,
},
} as unknown as DataSourceApi,
});
expect(props.datasource?.annotations?.QueryEditor).toHaveBeenCalledWith(
expect.objectContaining({
query: expect.objectContaining({ refId: 'initialAnnotationRef' }),
}),
expect.anything()
);
});
});

View File

@@ -22,7 +22,7 @@ import { AnnotationQueryResponse } from '../types';
import { AnnotationFieldMapper } from './AnnotationResultMapper';
interface Props {
export interface Props {
datasource: DataSourceApi;
datasourceInstanceSettings: DataSourceInstanceSettings;
annotation: AnnotationQuery<DataQuery>;
@@ -186,7 +186,11 @@ export default class StandardAnnotationQueryEditor extends PureComponent<Props,
return <div>Annotations are not supported. This datasource needs to export a QueryEditor</div>;
}
const query = annotation.target ?? { refId: 'Anno' };
const query = {
...datasource.annotations?.getDefaultQuery?.(),
...(annotation.target ?? { refId: 'Anno' }),
};
return (
<>
<DataSourcePluginContextProvider instanceSettings={datasourceInstanceSettings}>

View File

@@ -23,7 +23,11 @@ export function executeAnnotationQuery(
...datasource.annotations,
};
const annotation = processor.prepareAnnotation!(savedJsonAnno);
const annotationWithDefaults = {
...processor.getDefaultQuery?.(),
...savedJsonAnno,
};
const annotation = processor.prepareAnnotation!(annotationWithDefaults);
if (!annotation) {
return of({});
}