2022-05-19 15:52:52 -05:00
|
|
|
import { AnnotationQuery } from '@grafana/data';
|
|
|
|
|
|
|
|
import { createMockDatasource } from './__mocks__/cloudMonitoringDatasource';
|
|
|
|
import { CloudMonitoringAnnotationSupport } from './annotationSupport';
|
|
|
|
import {
|
|
|
|
AlignmentTypes,
|
|
|
|
CloudMonitoringQuery,
|
|
|
|
QueryType,
|
2023-05-22 11:19:54 -05:00
|
|
|
MetricKind,
|
|
|
|
LegacyCloudMonitoringAnnotationQuery,
|
|
|
|
} from './types/query';
|
2022-05-19 15:52:52 -05:00
|
|
|
|
|
|
|
const query: CloudMonitoringQuery = {
|
|
|
|
refId: 'query',
|
2022-11-10 02:30:47 -06:00
|
|
|
queryType: QueryType.ANNOTATION,
|
2022-05-19 15:52:52 -05:00
|
|
|
intervalMs: 0,
|
2022-12-20 05:47:49 -06:00
|
|
|
timeSeriesList: {
|
2022-05-19 15:52:52 -05:00
|
|
|
projectName: 'project-name',
|
|
|
|
filters: [],
|
|
|
|
title: '',
|
|
|
|
text: '',
|
|
|
|
crossSeriesReducer: 'REDUCE_NONE',
|
|
|
|
perSeriesAligner: AlignmentTypes.ALIGN_NONE,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const legacyQuery: LegacyCloudMonitoringAnnotationQuery = {
|
|
|
|
projectName: 'project-name',
|
|
|
|
metricType: 'metric-type',
|
|
|
|
filters: ['filter1', 'filter2'],
|
|
|
|
metricKind: MetricKind.CUMULATIVE,
|
|
|
|
valueType: 'value-type',
|
|
|
|
refId: 'annotationQuery',
|
|
|
|
title: 'title',
|
|
|
|
text: 'text',
|
|
|
|
};
|
|
|
|
|
|
|
|
const annotationQuery: AnnotationQuery<CloudMonitoringQuery> = {
|
|
|
|
name: 'Anno',
|
|
|
|
enable: false,
|
|
|
|
iconColor: '',
|
|
|
|
target: query,
|
|
|
|
};
|
|
|
|
|
|
|
|
const legacyAnnotationQuery: AnnotationQuery<LegacyCloudMonitoringAnnotationQuery> = {
|
|
|
|
name: 'Anno',
|
|
|
|
enable: false,
|
|
|
|
iconColor: '',
|
|
|
|
target: legacyQuery,
|
|
|
|
};
|
|
|
|
|
|
|
|
const ds = createMockDatasource();
|
|
|
|
const annotationSupport = CloudMonitoringAnnotationSupport(ds);
|
|
|
|
|
|
|
|
describe('CloudMonitoringAnnotationSupport', () => {
|
|
|
|
describe('prepareAnnotation', () => {
|
|
|
|
it('returns query if it is already a Cloud Monitoring annotation query', () => {
|
|
|
|
expect(annotationSupport.prepareAnnotation?.(annotationQuery)).toBe(annotationQuery);
|
|
|
|
});
|
|
|
|
it('returns an updated query if it is a legacy Cloud Monitoring annotation query', () => {
|
|
|
|
const expectedQuery = {
|
|
|
|
datasource: undefined,
|
|
|
|
enable: false,
|
|
|
|
iconColor: '',
|
|
|
|
name: 'Anno',
|
|
|
|
target: {
|
|
|
|
intervalMs: 0,
|
2022-12-20 05:47:49 -06:00
|
|
|
timeSeriesList: {
|
2022-05-19 15:52:52 -05:00
|
|
|
crossSeriesReducer: 'REDUCE_NONE',
|
|
|
|
filters: ['filter1', 'filter2'],
|
|
|
|
perSeriesAligner: 'ALIGN_NONE',
|
|
|
|
projectName: 'project-name',
|
|
|
|
text: 'text',
|
|
|
|
title: 'title',
|
|
|
|
},
|
2022-11-10 02:30:47 -06:00
|
|
|
queryType: 'annotation',
|
2022-05-19 15:52:52 -05:00
|
|
|
refId: 'annotationQuery',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
expect(annotationSupport.prepareAnnotation?.(legacyAnnotationQuery)).toEqual(expectedQuery);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('prepareQuery', () => {
|
2022-12-20 05:47:49 -06:00
|
|
|
it('should ensure queryType is set to "annotation"', () => {
|
2022-05-19 15:52:52 -05:00
|
|
|
const queryWithoutMetricsQueryType = { ...annotationQuery, queryType: 'blah' };
|
|
|
|
expect(annotationSupport.prepareQuery?.(queryWithoutMetricsQueryType)).toEqual(
|
2022-12-20 05:47:49 -06:00
|
|
|
expect.objectContaining({ queryType: QueryType.ANNOTATION })
|
2022-05-19 15:52:52 -05:00
|
|
|
);
|
|
|
|
});
|
|
|
|
it('should ensure type is set "annotationQuery"', () => {
|
|
|
|
const queryWithoutAnnotationQueryType = { ...annotationQuery, type: 'blah' };
|
|
|
|
expect(annotationSupport.prepareQuery?.(queryWithoutAnnotationQueryType)).toEqual(
|
|
|
|
expect.objectContaining({ type: 'annotationQuery' })
|
|
|
|
);
|
|
|
|
});
|
|
|
|
it('should return undefined if there is no query', () => {
|
|
|
|
const queryWithUndefinedTarget = { ...annotationQuery, target: undefined };
|
|
|
|
expect(annotationSupport.prepareQuery?.(queryWithUndefinedTarget)).toBeUndefined();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|