@sailboat-computer/event-bus
Version:
Standardized event bus for sailboat computer v3 with resilience features and offline capabilities
203 lines • 5.79 kB
JavaScript
"use strict";
/**
* Metrics collector for event bus
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.MetricsCollector = void 0;
/**
* Metrics collector for event bus
*/
class MetricsCollector {
/**
* Create a new metrics collector
*
* @param detailedTimings - Whether to collect detailed timings
*/
constructor(detailedTimings = false) {
/**
* Number of published events
*/
this.publishedEvents = 0;
/**
* Number of failed publishes
*/
this.failedPublishes = 0;
/**
* Number of processed events
*/
this.processedEvents = 0;
/**
* Number of active subscriptions
*/
this.activeSubscriptions = 0;
/**
* Number of reconnection attempts
*/
this.reconnectionAttempts = 0;
/**
* Number of events in the offline buffer
*/
this.bufferedEvents = 0;
/**
* Last reconnection time
*/
this.lastReconnectionTime = null;
/**
* Total processing time
*/
this.totalProcessingTime = 0;
/**
* Number of events in the dead letter queue
*/
this.deadLetterQueueSize = 0;
/**
* Number of events sent to the dead letter queue
*/
this.deadLetterQueueEvents = 0;
/**
* Last time an event was sent to the dead letter queue
*/
this.lastDeadLetterQueueEventTime = null;
/**
* Processing times array for calculating average
*/
this.processingTimes = [];
/**
* Maximum number of processing times to store
*/
this.maxProcessingTimes = 100;
this.detailedTimings = detailedTimings;
}
/**
* Increment the published events counter
*/
incrementPublishedEvents() {
this.publishedEvents++;
}
/**
* Increment the failed publishes counter
*/
incrementFailedPublishes() {
this.failedPublishes++;
}
/**
* Increment the processed events counter
*/
incrementProcessedEvents() {
this.processedEvents++;
}
/**
* Increment the active subscriptions counter
*/
incrementActiveSubscriptions() {
this.activeSubscriptions++;
}
/**
* Decrement the active subscriptions counter
*/
decrementActiveSubscriptions() {
if (this.activeSubscriptions > 0) {
this.activeSubscriptions--;
}
}
/**
* Set the active subscriptions count
*
* @param count - Active subscriptions count
*/
setActiveSubscriptions(count) {
this.activeSubscriptions = count;
}
/**
* Increment the reconnection attempts counter
*/
incrementReconnectionAttempts() {
this.reconnectionAttempts++;
this.lastReconnectionTime = new Date();
}
/**
* Set the buffered events count
*
* @param count - Buffered events count
*/
setBufferedEvents(count) {
this.bufferedEvents = count;
}
/**
* Set the dead letter queue size
*
* @param size - Dead letter queue size
*/
setDeadLetterQueueSize(size) {
this.deadLetterQueueSize = size;
}
/**
* Increment the dead letter queue events counter
*/
incrementDeadLetterQueueEvents() {
this.deadLetterQueueEvents++;
this.lastDeadLetterQueueEventTime = new Date();
}
/**
* Record event processing time
*
* @param startTime - Start time in milliseconds
*/
recordProcessingTime(startTime) {
if (!this.detailedTimings) {
return;
}
const processingTime = Date.now() - startTime;
// Add to processing times array
this.processingTimes.push(processingTime);
// Limit the array size
if (this.processingTimes.length > this.maxProcessingTimes) {
this.processingTimes.shift();
}
// Calculate average
const sum = this.processingTimes.reduce((a, b) => a + b, 0);
const averageProcessingTime = sum / this.processingTimes.length;
// Update total processing time
this.totalProcessingTime = averageProcessingTime;
}
/**
* Get metrics
*
* @returns Event bus metrics
*/
getMetrics() {
return {
publishedEvents: this.publishedEvents,
failedPublishes: this.failedPublishes,
processedEvents: this.processedEvents,
activeSubscriptions: this.activeSubscriptions,
reconnectionAttempts: this.reconnectionAttempts,
bufferedEvents: this.bufferedEvents,
lastReconnectionTime: this.lastReconnectionTime,
averageProcessingTime: this.totalProcessingTime,
processingTimeSamples: [...this.processingTimes],
deadLetterQueueSize: this.deadLetterQueueSize,
deadLetterQueueEvents: this.deadLetterQueueEvents,
lastDeadLetterQueueEventTime: this.lastDeadLetterQueueEventTime
};
}
/**
* Reset metrics
*/
reset() {
this.publishedEvents = 0;
this.failedPublishes = 0;
this.processedEvents = 0;
this.activeSubscriptions = 0;
this.reconnectionAttempts = 0;
this.bufferedEvents = 0;
this.lastReconnectionTime = null;
this.totalProcessingTime = 0;
this.deadLetterQueueSize = 0;
this.deadLetterQueueEvents = 0;
this.lastDeadLetterQueueEventTime = null;
this.processingTimes = [];
}
}
exports.MetricsCollector = MetricsCollector;
//# sourceMappingURL=metrics.js.map