Commit 6dbf1f83 by Zoltán Bedi Committed by GitHub

Cloudwatch: Fix duplicate metric data (#28642)

* Cloudwatch: Fix duplicate metric data

* Refactor reduce function to for of
parent 9717ec77
import { CloudWatchDatasource } from './datasource'; import { DataQueryResponse, dateTime, DefaultTimeRange } from '@grafana/data';
import { TemplateSrv } from '../../../features/templating/template_srv';
import { setBackendSrv } from '@grafana/runtime'; import { setBackendSrv } from '@grafana/runtime';
import { DataQueryResponse, DefaultTimeRange } from '@grafana/data'; import { TemplateSrv } from '../../../features/templating/template_srv';
import { CloudWatchDatasource } from './datasource';
describe('datasource', () => { describe('datasource', () => {
describe('query', () => { describe('query', () => {
...@@ -35,6 +35,33 @@ describe('datasource', () => { ...@@ -35,6 +35,33 @@ describe('datasource', () => {
}); });
}); });
describe('performTimeSeriesQuery', () => {
it('should return the same length of data as result', async () => {
const { datasource } = setup();
const awsRequestMock = jest.spyOn(datasource, 'awsRequest');
const buildCloudwatchConsoleUrlMock = jest.spyOn(datasource, 'buildCloudwatchConsoleUrl');
buildCloudwatchConsoleUrlMock.mockImplementation(() => '');
awsRequestMock.mockImplementation(async () => {
return {
results: {
a: { refId: 'a', series: [{ name: 'cpu', points: [1, 1] }], meta: { gmdMeta: '' } },
b: { refId: 'b', series: [{ name: 'memory', points: [2, 2] }], meta: { gmdMeta: '' } },
},
};
});
const response: DataQueryResponse = await datasource.performTimeSeriesQuery(
{
queries: [
{ datasourceId: 1, refId: 'a' },
{ datasourceId: 1, refId: 'b' },
],
} as any,
{ from: dateTime(), to: dateTime() } as any
);
expect(response.data.length).toEqual(2);
});
});
describe('describeLogGroup', () => { describe('describeLogGroup', () => {
it('replaces region correctly in the query', async () => { it('replaces region correctly in the query', async () => {
const { datasource, datasourceRequestMock } = setup(); const { datasource, datasourceRequestMock } = setup();
......
...@@ -595,44 +595,45 @@ export class CloudWatchDatasource extends DataSourceApi<CloudWatchQuery, CloudWa ...@@ -595,44 +595,45 @@ export class CloudWatchDatasource extends DataSourceApi<CloudWatchQuery, CloudWa
return { data: [] }; return { data: [] };
} }
return Object.values(request.queries).reduce( const data = dataframes.map(frame => {
({ data, error }: any, queryRequest: any) => { const queryResult = res.results[frame.refId!];
const queryResult = res.results[queryRequest.refId]; const error = queryResult.error ? { message: queryResult.error } : null;
if (!queryResult) { if (!queryResult) {
return { data, error }; return { frame, error };
} }
const link = this.buildCloudwatchConsoleUrl( const requestQuery = request.queries.find(q => q.refId === frame.refId!) as any;
queryRequest,
from.toISOString(),
to.toISOString(),
queryRequest.refId,
queryResult.meta.gmdMeta
);
return { const link = this.buildCloudwatchConsoleUrl(
error: error || queryResult.error ? { message: queryResult.error } : null, requestQuery!,
data: [ from.toISOString(),
...data, to.toISOString(),
...dataframes.map(frame => { frame.refId!,
if (link) { queryResult.meta.gmdMeta
for (const field of frame.fields) { );
field.config.links = [
{ if (link) {
url: link, for (const field of frame.fields) {
title: 'View in CloudWatch console', field.config.links = [
targetBlank: true, {
}, url: link,
]; title: 'View in CloudWatch console',
} targetBlank: true,
} },
return frame; ];
}), }
], }
}; return { frame, error };
}, });
{ data: [], error: null }
); return {
data: data.map(o => o.frame),
error: data
.map(o => o.error)
.reduce((err, error) => {
return err || error;
}, null),
};
} catch (err) { } catch (err) {
if (/^Throttling:.*/.test(err.data.message)) { if (/^Throttling:.*/.test(err.data.message)) {
const failedRedIds = Object.keys(err.data.results); const failedRedIds = Object.keys(err.data.results);
......
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