diff --git a/frontend/src/component/admin/network/NetworkTrafficUsage/chart-functions.test.ts b/frontend/src/component/admin/network/NetworkTrafficUsage/chart-functions.test.ts index 097f9988fa..c462a8b192 100644 --- a/frontend/src/component/admin/network/NetworkTrafficUsage/chart-functions.test.ts +++ b/frontend/src/component/admin/network/NetworkTrafficUsage/chart-functions.test.ts @@ -1,8 +1,11 @@ import type { TrafficUsageDataSegmentedCombinedSchema } from 'openapi'; -import { toTrafficUsageChartData } from './chart-functions'; +import { + toConnectionChartData, + toTrafficUsageChartData, +} from './chart-functions'; import { endpointsInfo } from './endpoint-info'; -describe('toChartData', () => { +describe('toTrafficUsageChartData', () => { const dataPoint = (period: string, count: number) => ({ period, trafficTypes: [{ count, group: 'successful-requests' }], @@ -149,3 +152,105 @@ describe('toChartData', () => { expect(toTrafficUsageChartData(input)).toMatchObject(expectedOutput); }); }); + +describe('toConnectionChartData', () => { + const dataPoint = (period: string, count: number) => ({ + period, + trafficTypes: [{ count, group: 'successful-requests' }], + }); + + const fromEndpointInfo = (endpoint: keyof typeof endpointsInfo) => { + const info = endpointsInfo[endpoint]; + return { + backgroundColor: info.color, + hoverBackgroundColor: info.color, + label: info.label, + }; + }; + + test('monthly data conversion', () => { + const input: TrafficUsageDataSegmentedCombinedSchema = { + grouping: 'monthly', + dateRange: { + from: '2025-01-01', + to: '2025-06-30', + }, + apiData: [ + { + apiPath: '/api/admin', // filter out + dataPoints: [dataPoint('2025-06', 5)], + }, + { + apiPath: '/api/client', + dataPoints: [ + dataPoint('2025-06', 10 * 5 * 60 * 24 * 30), + dataPoint('2025-01', 7 * 5 * 60 * 24 * 31), + dataPoint('2025-03', 11 * 5 * 60 * 24 * 31), + dataPoint('2025-04', 13 * 5 * 60 * 24 * 30), + ], + }, + ], + }; + + const expectedOutput = { + datasets: [ + { + data: [7, 0, 11, 13, 0, 10], + ...fromEndpointInfo('/api/client'), + }, + ], + labels: [ + '2025-01', + '2025-02', + '2025-03', + '2025-04', + '2025-05', + 'Current month', + ], + }; + + expect(toConnectionChartData(input)).toMatchObject(expectedOutput); + }); + + test('daily data conversion', () => { + const input: TrafficUsageDataSegmentedCombinedSchema = { + grouping: 'daily', + dateRange: { + from: '2025-01-01', + to: '2025-01-31', + }, + apiData: [ + { + apiPath: '/api/admin', // filter out + dataPoints: [dataPoint('2025-01-01', 5)], + }, + { + apiPath: '/api/client', + dataPoints: [ + dataPoint('2025-01-02', 2 * 5 * 60 * 24), + dataPoint('2025-01-17', 6 * 5 * 60 * 24), + dataPoint('2025-01-19', 4 * 5 * 60 * 24), + dataPoint('2025-01-06', 8 * 5 * 60 * 24), + ], + }, + ], + }; + + const expectedOutput = { + datasets: [ + { + data: [ + 0, 2, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 4, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + ], + ...fromEndpointInfo('/api/client'), + }, + ], + labels: Array.from({ length: 31 }).map((_, index) => + (index + 1).toString(), + ), + }; + + expect(toConnectionChartData(input)).toMatchObject(expectedOutput); + }); +});