fix(metrics): fix metrics

This commit is contained in:
Juergen Kunz
2025-06-23 08:50:19 +00:00
parent be58700a2f
commit 753b03d3e9
2 changed files with 55 additions and 26 deletions

View File

@ -124,24 +124,35 @@ export class MetricsCollector implements IMetrics {
const now = Date.now();
const windowStart = now - (windowSeconds * 1000);
// Aggregate bytes by route from trackers
const routeBytes = new Map<string, { in: number; out: number }>();
// Aggregate bytes by route with proper time calculation
const routeData = new Map<string, { bytesIn: number; bytesOut: number; totalDuration: number }>();
for (const [_, tracker] of this.connectionByteTrackers) {
if (tracker.lastUpdate > windowStart) {
const current = routeBytes.get(tracker.routeName) || { in: 0, out: 0 };
current.in += tracker.bytesIn;
current.out += tracker.bytesOut;
routeBytes.set(tracker.routeName, current);
// Only include connections that were active within the window
if (tracker.lastUpdate > windowStart || tracker.startTime > windowStart) {
// Calculate the actual duration this connection was active within the window
const connectionStart = Math.max(tracker.startTime, windowStart);
const connectionEnd = tracker.lastUpdate;
const durationInWindow = (connectionEnd - connectionStart) / 1000; // Convert to seconds
if (durationInWindow > 0) {
const current = routeData.get(tracker.routeName) || { bytesIn: 0, bytesOut: 0, totalDuration: 0 };
current.bytesIn += tracker.bytesIn;
current.bytesOut += tracker.bytesOut;
current.totalDuration += durationInWindow;
routeData.set(tracker.routeName, current);
}
}
}
// Convert to rates
for (const [route, bytes] of routeBytes) {
routeThroughput.set(route, {
in: Math.round(bytes.in / windowSeconds),
out: Math.round(bytes.out / windowSeconds)
});
// Convert to rates (bytes per second)
for (const [route, data] of routeData) {
if (data.totalDuration > 0) {
routeThroughput.set(route, {
in: Math.round(data.bytesIn / data.totalDuration),
out: Math.round(data.bytesOut / data.totalDuration)
});
}
}
return routeThroughput;
@ -152,24 +163,35 @@ export class MetricsCollector implements IMetrics {
const now = Date.now();
const windowStart = now - (windowSeconds * 1000);
// Aggregate bytes by IP from trackers
const ipBytes = new Map<string, { in: number; out: number }>();
// Aggregate bytes by IP with proper time calculation
const ipData = new Map<string, { bytesIn: number; bytesOut: number; totalDuration: number }>();
for (const [_, tracker] of this.connectionByteTrackers) {
if (tracker.lastUpdate > windowStart) {
const current = ipBytes.get(tracker.remoteIP) || { in: 0, out: 0 };
current.in += tracker.bytesIn;
current.out += tracker.bytesOut;
ipBytes.set(tracker.remoteIP, current);
// Only include connections that were active within the window
if (tracker.lastUpdate > windowStart || tracker.startTime > windowStart) {
// Calculate the actual duration this connection was active within the window
const connectionStart = Math.max(tracker.startTime, windowStart);
const connectionEnd = tracker.lastUpdate;
const durationInWindow = (connectionEnd - connectionStart) / 1000; // Convert to seconds
if (durationInWindow > 0) {
const current = ipData.get(tracker.remoteIP) || { bytesIn: 0, bytesOut: 0, totalDuration: 0 };
current.bytesIn += tracker.bytesIn;
current.bytesOut += tracker.bytesOut;
current.totalDuration += durationInWindow;
ipData.set(tracker.remoteIP, current);
}
}
}
// Convert to rates
for (const [ip, bytes] of ipBytes) {
ipThroughput.set(ip, {
in: Math.round(bytes.in / windowSeconds),
out: Math.round(bytes.out / windowSeconds)
});
// Convert to rates (bytes per second)
for (const [ip, data] of ipData) {
if (data.totalDuration > 0) {
ipThroughput.set(ip, {
in: Math.round(data.bytesIn / data.totalDuration),
out: Math.round(data.bytesOut / data.totalDuration)
});
}
}
return ipThroughput;
@ -271,6 +293,7 @@ export class MetricsCollector implements IMetrics {
remoteIP,
bytesIn: 0,
bytesOut: 0,
startTime: now,
lastUpdate: now
});