Refine users table and reconnect handling
This commit is contained in:
@@ -11,7 +11,12 @@ export async function getDashboardSnapshot(
|
||||
const state = await store.read();
|
||||
const previewConfig = render3proxyConfig(state, runtimePaths);
|
||||
const traffic = await readObservedTraffic(runtimePaths, state.userRecords);
|
||||
const observedUsers = deriveObservedUsers(state.userRecords, traffic.userBytesByName, traffic.recentUsers);
|
||||
const observedUsers = deriveObservedUsers(
|
||||
state.userRecords,
|
||||
traffic.userBytesByName,
|
||||
traffic.lastSeenByName,
|
||||
traffic.recentUsers,
|
||||
);
|
||||
const observed: ObservedRuntimeState = {
|
||||
totalBytes: traffic.totalBytes,
|
||||
liveConnections: traffic.liveConnections,
|
||||
@@ -26,14 +31,17 @@ export async function getDashboardSnapshot(
|
||||
function deriveObservedUsers(
|
||||
users: ProxyUserRecord[],
|
||||
userBytesByName: Map<string, number>,
|
||||
lastSeenByName: Map<string, string | null>,
|
||||
recentUsers: Set<string>,
|
||||
): ProxyUserRecord[] {
|
||||
return users.map((user) => {
|
||||
const usedBytes = userBytesByName.get(user.username) ?? 0;
|
||||
const lastSeenAt = lastSeenByName.get(user.username) ?? null;
|
||||
|
||||
if (user.paused) {
|
||||
return {
|
||||
...user,
|
||||
lastSeenAt,
|
||||
usedBytes,
|
||||
status: 'idle',
|
||||
};
|
||||
@@ -42,6 +50,7 @@ function deriveObservedUsers(
|
||||
if (user.quotaBytes !== null && usedBytes >= user.quotaBytes) {
|
||||
return {
|
||||
...user,
|
||||
lastSeenAt,
|
||||
usedBytes,
|
||||
status: 'fail',
|
||||
};
|
||||
@@ -50,6 +59,7 @@ function deriveObservedUsers(
|
||||
if (user.quotaBytes !== null && user.quotaBytes > 0 && usedBytes / user.quotaBytes >= 0.8) {
|
||||
return {
|
||||
...user,
|
||||
lastSeenAt,
|
||||
usedBytes,
|
||||
status: recentUsers.has(user.username) ? 'live' : 'warn',
|
||||
};
|
||||
@@ -57,6 +67,7 @@ function deriveObservedUsers(
|
||||
|
||||
return {
|
||||
...user,
|
||||
lastSeenAt,
|
||||
usedBytes,
|
||||
status: recentUsers.has(user.username) ? 'live' : 'idle',
|
||||
};
|
||||
|
||||
@@ -14,6 +14,7 @@ export interface ObservedTraffic {
|
||||
activeUsers: number;
|
||||
daily: DailyTrafficBucket[];
|
||||
userBytesByName: Map<string, number>;
|
||||
lastSeenByName: Map<string, string | null>;
|
||||
recentUsers: Set<string>;
|
||||
}
|
||||
|
||||
@@ -34,6 +35,7 @@ export async function readObservedTraffic(
|
||||
const history = buildHistoryDays(historyStart, HISTORY_DAYS);
|
||||
const dailyTotals = new Map(history.map((entry) => [entry.key, 0]));
|
||||
const userBytesByName = new Map(users.map((user) => [user.username, 0]));
|
||||
const lastSeenByName = new Map<string, string | null>(users.map((user) => [user.username, null]));
|
||||
const recentUsers = new Set<string>();
|
||||
let liveConnections = 0;
|
||||
|
||||
@@ -47,6 +49,13 @@ export async function readObservedTraffic(
|
||||
userBytesByName.set(record.username, (userBytesByName.get(record.username) ?? 0) + record.bytes);
|
||||
}
|
||||
|
||||
if (lastSeenByName.has(record.username)) {
|
||||
const currentLastSeen = lastSeenByName.get(record.username);
|
||||
if (!currentLastSeen || record.timestamp.toISOString() > currentLastSeen) {
|
||||
lastSeenByName.set(record.username, record.timestamp.toISOString());
|
||||
}
|
||||
}
|
||||
|
||||
const ageMs = now.getTime() - record.timestamp.getTime();
|
||||
if (ageMs >= 0 && ageMs <= RECENT_WINDOW_MS) {
|
||||
recentUsers.add(record.username);
|
||||
@@ -76,6 +85,7 @@ export async function readObservedTraffic(
|
||||
activeUsers,
|
||||
daily,
|
||||
userBytesByName,
|
||||
lastSeenByName,
|
||||
recentUsers,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user