2022-09-19 08:50:41 +02:00
|
|
|
import { of, throwError } from 'rxjs';
|
|
|
|
|
|
|
|
|
|
import { CustomVariableModel, DataQueryError, DataQueryRequest, DataSourceInstanceSettings } from '@grafana/data';
|
2023-11-20 14:44:22 -08:00
|
|
|
import { BackendDataSourceResponse, toDataQueryResponse } from '@grafana/runtime';
|
2022-09-19 08:50:41 +02:00
|
|
|
import { TemplateSrv } from 'app/features/templating/template_srv';
|
|
|
|
|
|
|
|
|
|
import { CloudWatchMetricsQueryRunner } from '../query-runner/CloudWatchMetricsQueryRunner';
|
|
|
|
|
import { CloudWatchJsonData, CloudWatchQuery } from '../types';
|
|
|
|
|
|
|
|
|
|
import { CloudWatchSettings, setupMockedTemplateService } from './CloudWatchDataSource';
|
2022-12-13 14:50:39 +01:00
|
|
|
import { TimeRangeMock } from './timeRange';
|
2022-09-19 08:50:41 +02:00
|
|
|
|
|
|
|
|
export function setupMockedMetricsQueryRunner({
|
|
|
|
|
data = {
|
|
|
|
|
results: {},
|
|
|
|
|
},
|
|
|
|
|
variables,
|
|
|
|
|
mockGetVariableName = true,
|
2023-11-20 14:44:22 -08:00
|
|
|
errorResponse,
|
2022-09-19 08:50:41 +02:00
|
|
|
instanceSettings = CloudWatchSettings,
|
|
|
|
|
}: {
|
2023-11-20 14:44:22 -08:00
|
|
|
data?: BackendDataSourceResponse;
|
2022-09-19 08:50:41 +02:00
|
|
|
variables?: CustomVariableModel[];
|
|
|
|
|
mockGetVariableName?: boolean;
|
2023-11-20 14:44:22 -08:00
|
|
|
errorResponse?: DataQueryError;
|
2022-09-19 08:50:41 +02:00
|
|
|
instanceSettings?: DataSourceInstanceSettings<CloudWatchJsonData>;
|
|
|
|
|
} = {}) {
|
|
|
|
|
let templateService = new TemplateSrv();
|
|
|
|
|
if (variables) {
|
|
|
|
|
templateService = setupMockedTemplateService(variables);
|
|
|
|
|
if (mockGetVariableName) {
|
|
|
|
|
templateService.getVariableName = (name: string) => name.replace('$', '');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-20 14:44:22 -08:00
|
|
|
const queryMock = errorResponse
|
|
|
|
|
? jest.fn().mockImplementation(() => throwError(errorResponse))
|
|
|
|
|
: jest.fn().mockReturnValue(of(toDataQueryResponse({ data })));
|
|
|
|
|
const runner = new CloudWatchMetricsQueryRunner(instanceSettings, templateService, queryMock);
|
2022-09-19 08:50:41 +02:00
|
|
|
|
|
|
|
|
const request: DataQueryRequest<CloudWatchQuery> = {
|
2022-12-13 14:50:39 +01:00
|
|
|
range: TimeRangeMock,
|
2022-09-19 08:50:41 +02:00
|
|
|
rangeRaw: { from: '1483228800', to: '1483232400' },
|
|
|
|
|
targets: [],
|
|
|
|
|
requestId: '',
|
|
|
|
|
interval: '',
|
|
|
|
|
intervalMs: 0,
|
|
|
|
|
scopedVars: {},
|
|
|
|
|
timezone: '',
|
|
|
|
|
app: '',
|
|
|
|
|
startTime: 0,
|
|
|
|
|
};
|
|
|
|
|
|
2023-11-20 14:44:22 -08:00
|
|
|
return { runner, queryMock, templateService, instanceSettings, request, timeRange: TimeRangeMock };
|
2022-09-19 08:50:41 +02:00
|
|
|
}
|