From 48b59c965334995cbfc3103581d4c6fd6b32ebdb Mon Sep 17 00:00:00 2001 From: Matej Kubinec Date: Thu, 24 Oct 2024 11:14:37 +0200 Subject: [PATCH] PMM-13417 Correctly handle summary response --- .../PTSummary.types.ts | 8 +++++- .../PTSummaryDataSource.test.ts | 27 ++++++++++++------- .../PTSummaryDataSource.ts | 25 ++++++++++++++--- 3 files changed, 46 insertions(+), 14 deletions(-) diff --git a/public/app/plugins/datasource/pmm-pt-summary-datasource/PTSummary.types.ts b/public/app/plugins/datasource/pmm-pt-summary-datasource/PTSummary.types.ts index 8abb6d3fb1489..ce5f24e4ba6bc 100644 --- a/public/app/plugins/datasource/pmm-pt-summary-datasource/PTSummary.types.ts +++ b/public/app/plugins/datasource/pmm-pt-summary-datasource/PTSummary.types.ts @@ -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', diff --git a/public/app/plugins/datasource/pmm-pt-summary-datasource/PTSummaryDataSource.test.ts b/public/app/plugins/datasource/pmm-pt-summary-datasource/PTSummaryDataSource.test.ts index 4ea3d277025b0..e8eed4a167cfb 100644 --- a/public/app/plugins/datasource/pmm-pt-summary-datasource/PTSummaryDataSource.test.ts +++ b/public/app/plugins/datasource/pmm-pt-summary-datasource/PTSummaryDataSource.test.ts @@ -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', () => ({ @@ -10,11 +11,11 @@ jest.mock('@grafana/runtime', () => ({ }), })); jest.mock('app/percona/shared/services/actions/Actions.utils', () => ({ - getActionResult: async (): Promise> => + getActionResult: async (actionId: string): Promise> => new Promise((resolve) => { resolve({ loading: false, - value: 'Test data', + value: actionId, error: '', }); }), @@ -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 } }], @@ -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 } }], @@ -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 } }], @@ -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 } }], diff --git a/public/app/plugins/datasource/pmm-pt-summary-datasource/PTSummaryDataSource.ts b/public/app/plugins/datasource/pmm-pt-summary-datasource/PTSummaryDataSource.ts index 94cca97410d6d..6f7b27586f30b 100644 --- a/public/app/plugins/datasource/pmm-pt-summary-datasource/PTSummaryDataSource.ts +++ b/public/app/plugins/datasource/pmm-pt-summary-datasource/PTSummaryDataSource.ts @@ -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) { @@ -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((response as PTSummaryResponse).action_id); + const summaryResult = getResult(response, type); + + if (summaryResult) { + const result = await getActionResult(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)); }