2024-10-15 11:18:22 +02:00
|
|
|
import type EventEmitter from 'events';
|
2021-04-22 10:07:10 +02:00
|
|
|
import timer from './timer';
|
2019-08-04 11:10:51 +02:00
|
|
|
|
|
|
|
// wrapTimer keeps track of the timing of a async operation and emits
|
|
|
|
// a event on the given eventBus once the operation is complete
|
|
|
|
//
|
2024-10-15 11:18:22 +02:00
|
|
|
// the returned function is designed to stop the timer and emit the event
|
2019-08-04 11:10:51 +02:00
|
|
|
//
|
2024-10-15 11:18:22 +02:00
|
|
|
// 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,
|
|
|
|
event: string,
|
|
|
|
args: Record<string, unknown> = {},
|
2021-04-22 10:07:10 +02:00
|
|
|
) => {
|
2019-08-04 11:10:51 +02:00
|
|
|
const t = timer.new();
|
2024-10-15 11:18:22 +02:00
|
|
|
return (data: unknown) => {
|
2019-08-04 11:10:51 +02:00
|
|
|
args.time = t();
|
|
|
|
eventBus.emit(event, args);
|
|
|
|
return data;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
const metricsHelper = {
|
|
|
|
wrapTimer,
|
|
|
|
};
|
|
|
|
export default metricsHelper;
|
2019-08-04 11:10:51 +02:00
|
|
|
module.exports = {
|
|
|
|
wrapTimer,
|
|
|
|
};
|