From 5fa5ada2add325cb90ce6671851d8d9482b69fd4 Mon Sep 17 00:00:00 2001 From: malteish Date: Wed, 11 Sep 2024 10:19:15 +0200 Subject: [PATCH 1/3] unify capitalization in metrics --- .../src/persistence/metrics/getMetrics.test.ts | 12 ++++++------ .../src/persistence/metrics/getMetrics.ts | 4 ++-- .../src/persistence/metrics/metricTypes.ts | 4 ++-- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/packages/delivery-service/src/persistence/metrics/getMetrics.test.ts b/packages/delivery-service/src/persistence/metrics/getMetrics.test.ts index 9026fcd3b..9641fa518 100644 --- a/packages/delivery-service/src/persistence/metrics/getMetrics.test.ts +++ b/packages/delivery-service/src/persistence/metrics/getMetrics.test.ts @@ -51,8 +51,8 @@ describe('getMetrics', () => { ); expect(result).toHaveLength(24); expect(result[0]).toEqual({ - timestamp_start: 1680307200, - duration_seconds: 3600, + timestampStart: 1680307200, + durationSeconds: 3600, messageCount: 10, messageSizeBytes: 1000, notificationCount: 5, @@ -86,8 +86,8 @@ describe('getMetrics', () => { expect(result).toHaveLength(1); expect(result[0]).toEqual({ - timestamp_start: 1680307200, - duration_seconds: 3600, + timestampStart: 1680307200, + durationSeconds: 3600, messageCount: 10, messageSizeBytes: 0, notificationCount: 5, @@ -107,11 +107,11 @@ describe('getMetrics', () => { const result = await getMetricsFunction(mockDeliveryServiceProperties); expect(result).toHaveLength(1); - expect(result[0].timestamp_start).toBe(currentTimestamp - 3600); // 2023-04-01T11:00:00.000Z + expect(result[0].timestampStart).toBe(currentTimestamp - 3600); // 2023-04-01T11:00:00.000Z expect( result.every( (metric) => - metric.timestamp_start !== + metric.timestampStart !== Math.floor(mockDate.getTime() / 1000), ), ).toBe(true); diff --git a/packages/delivery-service/src/persistence/metrics/getMetrics.ts b/packages/delivery-service/src/persistence/metrics/getMetrics.ts index 6bdff3d2f..c54344ece 100644 --- a/packages/delivery-service/src/persistence/metrics/getMetrics.ts +++ b/packages/delivery-service/src/persistence/metrics/getMetrics.ts @@ -43,8 +43,8 @@ export function getMetrics( ); metrics.push({ - timestamp_start: timestamp, - duration_seconds: + timestampStart: timestamp, + durationSeconds: deliveryServiceProperties.metricsCollectionIntervalInSeconds, messageCount: parseInt(messageCount || '0', 10), messageSizeBytes: parseInt(messageSizeBytes || '0', 10), diff --git a/packages/delivery-service/src/persistence/metrics/metricTypes.ts b/packages/delivery-service/src/persistence/metrics/metricTypes.ts index 755f0ad73..7c11cb1df 100644 --- a/packages/delivery-service/src/persistence/metrics/metricTypes.ts +++ b/packages/delivery-service/src/persistence/metrics/metricTypes.ts @@ -1,6 +1,6 @@ export type IntervalMetric = { - timestamp_start: number; - duration_seconds: number; + timestampStart: number; + durationSeconds: number; messageCount: number; messageSizeBytes: number; notificationCount: number; From e608cca7c1a66dfc3379ca1065585fa89df736ad Mon Sep 17 00:00:00 2001 From: malteish Date: Wed, 11 Sep 2024 10:29:21 +0200 Subject: [PATCH 2/3] output csv instead of json on metrics endpoint --- packages/delivery-service/src/metrics.ts | 27 ++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/packages/delivery-service/src/metrics.ts b/packages/delivery-service/src/metrics.ts index 4a415f39e..9fa2705d2 100644 --- a/packages/delivery-service/src/metrics.ts +++ b/packages/delivery-service/src/metrics.ts @@ -1,6 +1,8 @@ import { DeliveryServiceProperties } from '@dm3-org/dm3-lib-delivery'; import express from 'express'; import { IDatabase } from './persistence/getDatabase'; +import { IntervalMetric } from './persistence/metrics/metricTypes'; + /** * The metrics endpoint returns the metrics of the delivery service. * These can include the number of messages received, the cumulative size of messages received, etc., @@ -13,9 +15,30 @@ export default ( ) => { const router = express.Router(); router.get('', async (req, res) => { - const metrics = await db.getMetrics(deliveryServiceProperties); + const metrics: IntervalMetric[] = await db.getMetrics( + deliveryServiceProperties, + ); + + if (metrics.length === 0) { + return res.status(204).send('No metrics data available'); + } + + // Convert metrics to CSV format + const headers = Object.keys(metrics[0]); + const csvRows = metrics.map((metric) => + headers + .map((header) => metric[header as keyof IntervalMetric]) + .join(','), + ); + + const csvData = [headers.join(','), ...csvRows].join('\n'); - return res.status(200).send(JSON.stringify(metrics)); + res.setHeader('Content-Type', 'text/csv'); + res.setHeader( + 'Content-Disposition', + 'attachment; filename=metrics.csv', + ); + return res.status(200).send(csvData); }); return router; From 9bb0fb7ea18fdf1b6d5ce10109ad43b928ffe8a7 Mon Sep 17 00:00:00 2001 From: malteish Date: Wed, 11 Sep 2024 10:35:32 +0200 Subject: [PATCH 3/3] remove the file download header --- packages/delivery-service/src/metrics.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/delivery-service/src/metrics.ts b/packages/delivery-service/src/metrics.ts index 9fa2705d2..b86b0dbad 100644 --- a/packages/delivery-service/src/metrics.ts +++ b/packages/delivery-service/src/metrics.ts @@ -34,10 +34,10 @@ export default ( const csvData = [headers.join(','), ...csvRows].join('\n'); res.setHeader('Content-Type', 'text/csv'); - res.setHeader( - 'Content-Disposition', - 'attachment; filename=metrics.csv', - ); + // res.setHeader( + // 'Content-Disposition', + // 'attachment; filename=metrics.csv', + // ); return res.status(200).send(csvData); });