fix(metrics): fix metrics
This commit is contained in:
@ -124,24 +124,35 @@ export class MetricsCollector implements IMetrics {
|
|||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
const windowStart = now - (windowSeconds * 1000);
|
const windowStart = now - (windowSeconds * 1000);
|
||||||
|
|
||||||
// Aggregate bytes by route from trackers
|
// Aggregate bytes by route with proper time calculation
|
||||||
const routeBytes = new Map<string, { in: number; out: number }>();
|
const routeData = new Map<string, { bytesIn: number; bytesOut: number; totalDuration: number }>();
|
||||||
|
|
||||||
for (const [_, tracker] of this.connectionByteTrackers) {
|
for (const [_, tracker] of this.connectionByteTrackers) {
|
||||||
if (tracker.lastUpdate > windowStart) {
|
// Only include connections that were active within the window
|
||||||
const current = routeBytes.get(tracker.routeName) || { in: 0, out: 0 };
|
if (tracker.lastUpdate > windowStart || tracker.startTime > windowStart) {
|
||||||
current.in += tracker.bytesIn;
|
// Calculate the actual duration this connection was active within the window
|
||||||
current.out += tracker.bytesOut;
|
const connectionStart = Math.max(tracker.startTime, windowStart);
|
||||||
routeBytes.set(tracker.routeName, current);
|
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
|
// Convert to rates (bytes per second)
|
||||||
for (const [route, bytes] of routeBytes) {
|
for (const [route, data] of routeData) {
|
||||||
routeThroughput.set(route, {
|
if (data.totalDuration > 0) {
|
||||||
in: Math.round(bytes.in / windowSeconds),
|
routeThroughput.set(route, {
|
||||||
out: Math.round(bytes.out / windowSeconds)
|
in: Math.round(data.bytesIn / data.totalDuration),
|
||||||
});
|
out: Math.round(data.bytesOut / data.totalDuration)
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return routeThroughput;
|
return routeThroughput;
|
||||||
@ -152,24 +163,35 @@ export class MetricsCollector implements IMetrics {
|
|||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
const windowStart = now - (windowSeconds * 1000);
|
const windowStart = now - (windowSeconds * 1000);
|
||||||
|
|
||||||
// Aggregate bytes by IP from trackers
|
// Aggregate bytes by IP with proper time calculation
|
||||||
const ipBytes = new Map<string, { in: number; out: number }>();
|
const ipData = new Map<string, { bytesIn: number; bytesOut: number; totalDuration: number }>();
|
||||||
|
|
||||||
for (const [_, tracker] of this.connectionByteTrackers) {
|
for (const [_, tracker] of this.connectionByteTrackers) {
|
||||||
if (tracker.lastUpdate > windowStart) {
|
// Only include connections that were active within the window
|
||||||
const current = ipBytes.get(tracker.remoteIP) || { in: 0, out: 0 };
|
if (tracker.lastUpdate > windowStart || tracker.startTime > windowStart) {
|
||||||
current.in += tracker.bytesIn;
|
// Calculate the actual duration this connection was active within the window
|
||||||
current.out += tracker.bytesOut;
|
const connectionStart = Math.max(tracker.startTime, windowStart);
|
||||||
ipBytes.set(tracker.remoteIP, current);
|
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
|
// Convert to rates (bytes per second)
|
||||||
for (const [ip, bytes] of ipBytes) {
|
for (const [ip, data] of ipData) {
|
||||||
ipThroughput.set(ip, {
|
if (data.totalDuration > 0) {
|
||||||
in: Math.round(bytes.in / windowSeconds),
|
ipThroughput.set(ip, {
|
||||||
out: Math.round(bytes.out / windowSeconds)
|
in: Math.round(data.bytesIn / data.totalDuration),
|
||||||
});
|
out: Math.round(data.bytesOut / data.totalDuration)
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return ipThroughput;
|
return ipThroughput;
|
||||||
@ -271,6 +293,7 @@ export class MetricsCollector implements IMetrics {
|
|||||||
remoteIP,
|
remoteIP,
|
||||||
bytesIn: 0,
|
bytesIn: 0,
|
||||||
bytesOut: 0,
|
bytesOut: 0,
|
||||||
|
startTime: now,
|
||||||
lastUpdate: now
|
lastUpdate: now
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -5,6 +5,11 @@ export interface IThroughputSample {
|
|||||||
timestamp: number;
|
timestamp: number;
|
||||||
bytesIn: number;
|
bytesIn: number;
|
||||||
bytesOut: number;
|
bytesOut: number;
|
||||||
|
tags?: {
|
||||||
|
route?: string;
|
||||||
|
ip?: string;
|
||||||
|
[key: string]: string | undefined;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -102,5 +107,6 @@ export interface IByteTracker {
|
|||||||
remoteIP: string;
|
remoteIP: string;
|
||||||
bytesIn: number;
|
bytesIn: number;
|
||||||
bytesOut: number;
|
bytesOut: number;
|
||||||
|
startTime: number;
|
||||||
lastUpdate: number;
|
lastUpdate: number;
|
||||||
}
|
}
|
Reference in New Issue
Block a user