From 8b7dbe9633c5da947897c92337efe76716881782 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gast=C3=B3n=20Fournier?= Date: Tue, 8 Oct 2024 18:11:19 +0200 Subject: [PATCH 1/3] chore: wrapTimer function types --- src/lib/util/metrics-helper.ts | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/lib/util/metrics-helper.ts b/src/lib/util/metrics-helper.ts index c422bc5dcc5b..2f148e928241 100644 --- a/src/lib/util/metrics-helper.ts +++ b/src/lib/util/metrics-helper.ts @@ -1,4 +1,4 @@ -/* eslint-disable no-param-reassign */ +import type EventEmitter from 'events'; import timer from './timer'; // wrapTimer keeps track of the timing of a async operation and emits @@ -8,11 +8,14 @@ import timer from './timer'; // It transparently passes the data to the following .then() // // usage: promise.then(wrapTimer(bus, type, { payload: 'ok' })) -const wrapTimer: (EventEmitter, string, object) => (any) => any = ( - eventBus, - event, - args = {}, -) => { +type TimeMetrics = { + [key: string]: string | number; +}; +const wrapTimer: ( + eventBus: EventEmitter, + type: string, + args: TimeMetrics, +) => (args: any) => any = (eventBus, event, args = {}) => { const t = timer.new(); return (data) => { args.time = t(); From 25d4477f178a97512f947a8a0d122529b0b0df74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gast=C3=B3n=20Fournier?= Date: Mon, 14 Oct 2024 10:59:15 +0200 Subject: [PATCH 2/3] Incorporate PR comments --- .../client-feature-toggle-read-model.ts | 2 +- .../frontend-api/frontend-api-controller.ts | 2 +- src/lib/util/metrics-helper.ts | 22 +++++++++++++------ 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/lib/features/frontend-api/client-feature-toggle-read-model.ts b/src/lib/features/frontend-api/client-feature-toggle-read-model.ts index 5d81898da9a9..9a97f3ef83b8 100644 --- a/src/lib/features/frontend-api/client-feature-toggle-read-model.ts +++ b/src/lib/features/frontend-api/client-feature-toggle-read-model.ts @@ -22,7 +22,7 @@ export default class ClientFeatureToggleReadModel constructor(db: Db, eventBus: EventEmitter) { this.db = db; - this.timer = (action) => + this.timer = (action: string) => metricsHelper.wrapTimer(eventBus, DB_TIME, { store: 'client-feature-toggle-read-model', action, diff --git a/src/lib/features/frontend-api/frontend-api-controller.ts b/src/lib/features/frontend-api/frontend-api-controller.ts index de067273150c..62e594709673 100644 --- a/src/lib/features/frontend-api/frontend-api-controller.ts +++ b/src/lib/features/frontend-api/frontend-api-controller.ts @@ -49,7 +49,7 @@ export default class FrontendAPIController extends Controller { this.logger = config.getLogger('frontend-api-controller.ts'); this.services = services; - this.timer = (functionName) => + this.timer = (functionName: string) => metricsHelper.wrapTimer(config.eventBus, FUNCTION_TIME, { className: 'FrontendAPIController', functionName, diff --git a/src/lib/util/metrics-helper.ts b/src/lib/util/metrics-helper.ts index 2f148e928241..cbb930a4a977 100644 --- a/src/lib/util/metrics-helper.ts +++ b/src/lib/util/metrics-helper.ts @@ -4,17 +4,25 @@ import timer from './timer'; // wrapTimer keeps track of the timing of a async operation and emits // a event on the given eventBus once the operation is complete // -// the returned function is designed to be used as a .then() argument. -// It transparently passes the data to the following .then() +// the returned function is designed to stop the timer and emit the event // -// usage: promise.then(wrapTimer(bus, type, { payload: 'ok' })) -type TimeMetrics = { - [key: string]: string | number; -}; +// Usage: +// Define the timer function. It can be done once per class. +// this.timer = (action: string) => +// metricsHelper.wrapTimer(eventBus, DB_TIME, { +// store: 'client-feature-toggle-read-model', +// action, +// }); +// +// Before performing an operation, start the timer: +// const stopTimer = this.timer(`timer-name`); +// // perform operation and then stop timer +// stopTimer(); + const wrapTimer: ( eventBus: EventEmitter, type: string, - args: TimeMetrics, + args: Record, ) => (args: any) => any = (eventBus, event, args = {}) => { const t = timer.new(); return (data) => { From cc69837b617b17fa42558cb611f836fa136e4eeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gast=C3=B3n=20Fournier?= Date: Mon, 14 Oct 2024 15:24:19 +0200 Subject: [PATCH 3/3] Simplify signature MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Nuno Góis --- src/lib/util/metrics-helper.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/lib/util/metrics-helper.ts b/src/lib/util/metrics-helper.ts index cbb930a4a977..b4aa4ebd07b9 100644 --- a/src/lib/util/metrics-helper.ts +++ b/src/lib/util/metrics-helper.ts @@ -19,13 +19,13 @@ import timer from './timer'; // // perform operation and then stop timer // stopTimer(); -const wrapTimer: ( +const wrapTimer = ( eventBus: EventEmitter, - type: string, - args: Record, -) => (args: any) => any = (eventBus, event, args = {}) => { + event: string, + args: Record = {}, +) => { const t = timer.new(); - return (data) => { + return (data: unknown) => { args.time = t(); eventBus.emit(event, args); return data;