Skip to content

Commit

Permalink
Merge pull request #1177 from dm3-org/updateMetricsOutputFormat
Browse files Browse the repository at this point in the history
output metrics as array
  • Loading branch information
malteish authored Sep 10, 2024
2 parents 0d5cd69 + 5909efe commit 0b4d7d5
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 34 deletions.
4 changes: 2 additions & 2 deletions packages/delivery-service/src/persistence/getDatabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import Account from './account';
import { getIdEnsName } from './getIdEnsName';
import Messages from './messages';
import { syncAcknowledge } from './messages/syncAcknowledge';
import type { MetricsObject } from './metrics';
import type { IntervalMetric } from './metrics';
import Metrics from './metrics';
import Notification from './notification';
import Otp from './otp';
Expand Down Expand Up @@ -168,7 +168,7 @@ export interface IDatabase extends IAccountDatabase {
) => Promise<void>;
getMetrics: (
deliveryServiceProperties: DeliveryServiceProperties,
) => Promise<MetricsObject>;
) => Promise<IntervalMetric[]>;
countMessage: (
messageSizeBytes: number,
deliveryServiceProperties: DeliveryServiceProperties,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { getMetrics } from './getMetrics';
import { Redis, RedisPrefix } from '../getDatabase';
import { MetricsObject, IntervalMetric } from './metricTypes';

describe('getMetrics', () => {
let mockRedis: jest.Mocked<Redis>;
Expand All @@ -19,13 +18,13 @@ describe('getMetrics', () => {
} as unknown as jest.Mocked<Redis>;
});

it('should return an empty object when no metrics are found', async () => {
it('should return an empty array when no metrics are found', async () => {
mockRedis.keys.mockResolvedValue([]);

const getMetricsFunc = getMetrics(mockRedis);
const result = await getMetricsFunc(mockDeliveryServiceProperties);

expect(result).toEqual({});
expect(result).toEqual([]);
});

it('should return metrics for all available intervals', async () => {
Expand All @@ -50,8 +49,10 @@ describe('getMetrics', () => {
expect(mockRedis.keys).toHaveBeenCalledWith(
`${RedisPrefix.MetricsMessageCount}*`,
);
expect(Object.keys(result)).toHaveLength(24);
expect(result['2023-03-31T08:00:00.000Z']).toEqual({
expect(result).toHaveLength(24);
expect(result[0]).toEqual({
timestamp_start: 1680307200,
duration_seconds: 3600,
messageCount: 10,
messageSizeBytes: 1000,
notificationCount: 5,
Expand Down Expand Up @@ -83,11 +84,10 @@ describe('getMetrics', () => {
const result = await getMetricsFunction(mockDeliveryServiceProperties);
console.log(result);

expect(Object.keys(result)).toHaveLength(1);

expect(Object.keys(result)).toEqual(['2023-04-01T00:00:00.000Z']);

expect(result['2023-04-01T00:00:00.000Z']).toEqual({
expect(result).toHaveLength(1);
expect(result[0]).toEqual({
timestamp_start: 1680307200,
duration_seconds: 3600,
messageCount: 10,
messageSizeBytes: 0,
notificationCount: 5,
Expand All @@ -106,8 +106,14 @@ describe('getMetrics', () => {
const getMetricsFunction = getMetrics(mockRedis);
const result = await getMetricsFunction(mockDeliveryServiceProperties);

expect(Object.keys(result)).toHaveLength(1);
expect(Object.keys(result)[0]).toBe('2023-04-01T11:00:00.000Z');
expect(result).not.toHaveProperty(mockDate.toISOString());
expect(result).toHaveLength(1);
expect(result[0].timestamp_start).toBe(currentTimestamp - 3600); // 2023-04-01T11:00:00.000Z
expect(
result.every(
(metric) =>
metric.timestamp_start !==
Math.floor(mockDate.getTime() / 1000),
),
).toBe(true);
});
});
16 changes: 8 additions & 8 deletions packages/delivery-service/src/persistence/metrics/getMetrics.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DeliveryServiceProperties } from '@dm3-org/dm3-lib-delivery';
import { Redis, RedisPrefix } from '../getDatabase';
import { getCurrentIntervalTimestamp } from './getCurrentIntervalTimestamp';
import { MetricsObject } from './metricTypes';
import { IntervalMetric } from './metricTypes';

/**
* Get the metrics from the database, excluding the current interval.
Expand All @@ -15,9 +15,9 @@ export function getMetrics(
redis: Redis,
): (
deliveryServiceProperties: DeliveryServiceProperties,
) => Promise<MetricsObject> {
) => Promise<IntervalMetric[]> {
return async (deliveryServiceProperties: DeliveryServiceProperties) => {
const metrics: MetricsObject = {};
const metrics: IntervalMetric[] = [];
const messageCountKeys = await redis.keys(
`${RedisPrefix.MetricsMessageCount}*`,
);
Expand All @@ -34,9 +34,6 @@ export function getMetrics(
continue;
}

const date = new Date(timestamp * 1000);
const dateString = date.toISOString();

const messageCount = await redis.get(key);
const messageSizeBytes = await redis.get(
`${RedisPrefix.MetricsMessageSize}${timestamp}`,
Expand All @@ -45,11 +42,14 @@ export function getMetrics(
`${RedisPrefix.MetricsNotificationCount}${timestamp}`,
);

metrics[dateString] = {
metrics.push({
timestamp_start: timestamp,
duration_seconds:
deliveryServiceProperties.metricsCollectionIntervalInSeconds,
messageCount: parseInt(messageCount || '0', 10),
messageSizeBytes: parseInt(messageSizeBytes || '0', 10),
notificationCount: parseInt(notificationCount || '0', 10),
};
});
}

return metrics;
Expand Down
4 changes: 2 additions & 2 deletions packages/delivery-service/src/persistence/metrics/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getMetrics } from './getMetrics';
import type { IntervalMetric, MetricsMap, MetricsObject } from './metricTypes';
import type { IntervalMetric } from './metricTypes';
import { countMessage, countNotification } from './setMetrics';

export default {
Expand All @@ -8,4 +8,4 @@ export default {
countNotification,
};

export type { IntervalMetric, MetricsMap, MetricsObject };
export type { IntervalMetric };
12 changes: 3 additions & 9 deletions packages/delivery-service/src/persistence/metrics/metricTypes.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
type IntervalMetric = {
export type IntervalMetric = {
timestamp_start: number;
duration_seconds: number;
messageCount: number;
messageSizeBytes: number;
notificationCount: number;
};

type MetricsMap = Map<Date, IntervalMetric>;

export type { MetricsMap, IntervalMetric };

export interface MetricsObject {
[date: string]: IntervalMetric;
}

0 comments on commit 0b4d7d5

Please sign in to comment.