Skip to content

Commit

Permalink
PMM-13417 Correctly handle summary response (#779)
Browse files Browse the repository at this point in the history
  • Loading branch information
matejkubinec authored Oct 30, 2024
1 parent f934347 commit daa43d5
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,17 @@ export interface DatabaseSummaryRequest {
};
}

export interface PTSummaryResponse {
export interface PTSummaryResult {
action_id: string;
pmm_agent_id: string;
}

export type PTSummaryResponse = {
pt_mongodb_summary?: PTSummaryResult;
pt_mysql_summary?: PTSummaryResult;
pt_postgres_summary?: PTSummaryResult;
};

export enum DatasourceType {
postgresql = 'postgresql',
mongodb = 'mongodb',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { FieldType, toDataFrame } from '@grafana/data';
import { ActionResult } from 'app/percona/shared/services/actions/Actions.types';

import { PTSummaryService } from './PTSummary.service';
import { PTSummaryResponse, PTSummaryResult } from './PTSummary.types';
import { PTSummaryDataSource } from './PTSummaryDataSource';

jest.mock('@grafana/runtime', () => ({
Expand All @@ -10,11 +11,11 @@ jest.mock('@grafana/runtime', () => ({
}),
}));
jest.mock('app/percona/shared/services/actions/Actions.utils', () => ({
getActionResult: async (): Promise<ActionResult<string>> =>
getActionResult: async (actionId: string): Promise<ActionResult<string>> =>
new Promise((resolve) => {
resolve({
loading: false,
value: 'Test data',
value: actionId,
error: '',
});
}),
Expand All @@ -25,13 +26,14 @@ describe('PTSummaryDatasource::', () => {
const expected = {
data: [
toDataFrame({
fields: [{ name: 'summary', values: ['Test data'], type: FieldType.string }],
fields: [{ name: 'summary', values: ['node'], type: FieldType.string }],
}),
],
};
const instance = new PTSummaryDataSource({});

PTSummaryService.getPTSummary = jest.fn().mockResolvedValueOnce({ value: 'Test data' });
const response: PTSummaryResult = { action_id: 'node', pmm_agent_id: 'node' };
PTSummaryService.getPTSummary = jest.fn().mockResolvedValueOnce(response);

const result = await instance.query({
targets: [{ refId: 'A', queryType: { type: 'node', variableName: undefined } }],
Expand All @@ -44,13 +46,14 @@ describe('PTSummaryDatasource::', () => {
const expected = {
data: [
toDataFrame({
fields: [{ name: 'summary', values: ['Test data'], type: FieldType.string }],
fields: [{ name: 'summary', values: ['mysql'], type: FieldType.string }],
}),
],
};
const instance = new PTSummaryDataSource({});

PTSummaryService.getMysqlPTSummary = jest.fn().mockResolvedValueOnce({ value: 'Test data' });
const response: PTSummaryResponse = { pt_mysql_summary: { action_id: 'mysql', pmm_agent_id: 'mysql' } };
PTSummaryService.getMysqlPTSummary = jest.fn().mockResolvedValueOnce(response);

const result = await instance.query({
targets: [{ refId: 'A', queryType: { type: 'mysql', variableName: undefined } }],
Expand All @@ -63,13 +66,14 @@ describe('PTSummaryDatasource::', () => {
const expected = {
data: [
toDataFrame({
fields: [{ name: 'summary', values: ['Test data'], type: FieldType.string }],
fields: [{ name: 'summary', values: ['mongo'], type: FieldType.string }],
}),
],
};
const instance = new PTSummaryDataSource({});

PTSummaryService.getMongodbPTSummary = jest.fn().mockResolvedValueOnce({ value: 'Test data' });
const response: PTSummaryResponse = { pt_mongodb_summary: { action_id: 'mongo', pmm_agent_id: 'mongo' } };
PTSummaryService.getMongodbPTSummary = jest.fn().mockResolvedValueOnce(response);

const result = await instance.query({
targets: [{ refId: 'A', queryType: { type: 'mongodb', variableName: undefined } }],
Expand All @@ -82,13 +86,16 @@ describe('PTSummaryDatasource::', () => {
const expected = {
data: [
toDataFrame({
fields: [{ name: 'summary', values: ['Test data'], type: FieldType.string }],
fields: [{ name: 'summary', values: ['postgresql'], type: FieldType.string }],
}),
],
};
const instance = new PTSummaryDataSource({});

PTSummaryService.getPostgresqlPTSummary = jest.fn().mockResolvedValueOnce({ value: 'Test data' });
const response: PTSummaryResponse = {
pt_postgres_summary: { action_id: 'postgresql', pmm_agent_id: 'postgresql' },
};
PTSummaryService.getPostgresqlPTSummary = jest.fn().mockResolvedValueOnce(response);

const result = await instance.query({
targets: [{ refId: 'A', queryType: { type: 'postgresql', variableName: undefined } }],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { DataQueryResponse, DataSourceApi, toDataFrame, FieldType } from '@grafa
import { getActionResult } from 'app/percona/shared/services/actions/Actions.utils';

import { PTSummaryService } from './PTSummary.service';
import { DatasourceType, PTSummaryResponse } from './PTSummary.types';
import { DatasourceType, PTSummaryResponse, PTSummaryResult } from './PTSummary.types';

export class PTSummaryDataSource extends DataSourceApi {
constructor(instanceSettings: any) {
Expand All @@ -27,11 +27,30 @@ export class PTSummaryDataSource extends DataSourceApi {
}
};

const getResult = (response: PTSummaryResponse, type: DatasourceType) => {
switch (type) {
case DatasourceType.mysql:
return response.pt_mysql_summary;
case DatasourceType.mongodb:
return response.pt_mongodb_summary;
case DatasourceType.postgresql:
return response.pt_postgres_summary;
default:
return response as unknown as PTSummaryResult;
}
};

return getRequest(type)
.then(async (response) => {
const result = await getActionResult<string>((response as PTSummaryResponse).action_id);
const summaryResult = getResult(response, type);

if (summaryResult) {
const result = await getActionResult<string>(summaryResult.action_id);

return this.newDataFrame(result.value ? result.value : result.error);
}

return this.newDataFrame(result.value ? result.value : result.error);
return this.newDataFrame('error');
})
.catch((error) => this.newDataFrame(error.response.data.message));
}
Expand Down

0 comments on commit daa43d5

Please sign in to comment.