From f2683aab5e9abcda75b48bf386212c36c5f5f8c5 Mon Sep 17 00:00:00 2001 From: Denozordec Date: Mon, 30 Mar 2026 14:37:11 +0700 Subject: [PATCH] Refactor active IP-server data handling in user traffic table - Updated the data structure to store multiple servers associated with each user, enhancing the representation of active connections. - Improved the logic for generating active IP-server rows, utilizing a map for better performance and organization. - Modified the UI to display a list of servers for each user, improving clarity and usability in the user traffic table. --- web/src/routes/+page.svelte | 76 ++++++++++++++++++++++--------------- 1 file changed, 46 insertions(+), 30 deletions(-) diff --git a/web/src/routes/+page.svelte b/web/src/routes/+page.svelte index 6da8674..15ba906 100644 --- a/web/src/routes/+page.svelte +++ b/web/src/routes/+page.svelte @@ -213,17 +213,19 @@ username: string; ip: string; country_code: string | null | undefined; - server: string; + servers: string[]; activeLabel: string; }; let activeIpServerRows = $derived.by((): ActiveIpServerRow[] => { - const out: ActiveIpServerRow[] = []; - const seen = new Set(); + const map = new Map< + string, + { row: Omit; serversSet: Set } + >(); const maxUniqueIpRows = 80; const maxOutRows = 2000; - for (const ur of uniqueIps?.slice(0, maxUniqueIpRows) ?? []) { + outer: for (const ur of uniqueIps?.slice(0, maxUniqueIpRows) ?? []) { const username = ur.username ?? ''; if (!username) continue; @@ -231,32 +233,41 @@ const ip = ipa.ip ?? ''; if (!ip) continue; - const servers = ipa.active_on_servers ?? []; + const servers = (ipa.active_on_servers ?? []).filter(Boolean); if (servers.length === 0) continue; + const key = `${username}|${ip}`; + let item = map.get(key); + if (!item) { + if (map.size >= maxOutRows) break outer; + item = { + row: { + username, + ip, + country_code: ipa.country_code, + activeLabel: chipLabel(ipa) + }, + serversSet: new Set() + }; + map.set(key, item); + } + for (const server of servers) { if (!server) continue; - const key = `${username}|${ip}|${server}`; - if (seen.has(key)) continue; - seen.add(key); - - out.push({ - username, - ip, - country_code: ipa.country_code, - server, - activeLabel: chipLabel(ipa) - }); - - if (out.length >= maxOutRows) return out; + item.serversSet.add(server); } } } - out.sort( - (a, b) => - a.ip.localeCompare(b.ip) || a.server.localeCompare(b.server) || a.username.localeCompare(b.username) - ); + const out: ActiveIpServerRow[] = []; + for (const { row, serversSet } of map.values()) { + out.push({ + ...row, + servers: [...serversSet].sort((a, b) => a.localeCompare(b)) + }); + } + + out.sort((a, b) => a.ip.localeCompare(b.ip) || a.username.localeCompare(b.username)); return out; }); @@ -443,7 +454,7 @@ IP со страной Клиент - Сервер + Серверы @@ -454,7 +465,7 @@ {:else} - {#each activeIpServerRows as r (r.username + '|' + r.ip + '|' + r.server)} + {#each activeIpServerRows as r (r.username + '|' + r.ip)} {r.activeLabel} @@ -468,12 +479,17 @@ - - {r.server} - + {#each r.servers as srv, idx (srv)} + + {srv} + + {#if idx < r.servers.length - 1} + , + {/if} + {/each} {/each}