refactor: cleanup bandwidth tracking functionality

This commit is contained in:
zhom
2025-11-30 16:55:23 +04:00
parent cdba9aac33
commit f098128988
10 changed files with 379 additions and 62 deletions
+9 -2
View File
@@ -884,7 +884,10 @@ export function ProfilesDataTable({
const newSnapshots: Record<string, TrafficSnapshot> = {};
for (const snapshot of allSnapshots) {
if (snapshot.profile_id) {
newSnapshots[snapshot.profile_id] = snapshot;
const existing = newSnapshots[snapshot.profile_id];
if (!existing || snapshot.last_update > existing.last_update) {
newSnapshots[snapshot.profile_id] = snapshot;
}
}
}
setTrafficSnapshots(newSnapshots);
@@ -1693,13 +1696,17 @@ export function ProfilesDataTable({
if (isRunning && meta.trafficSnapshots) {
// Find the traffic snapshot for this profile by matching profile_id
const snapshot = meta.trafficSnapshots[profile.id];
const bandwidthData = snapshot?.recent_bandwidth || [];
// Create a new array reference to ensure React detects changes
const bandwidthData = snapshot?.recent_bandwidth
? [...snapshot.recent_bandwidth]
: [];
const currentBandwidth =
(snapshot?.current_bytes_sent || 0) +
(snapshot?.current_bytes_received || 0);
return (
<BandwidthMiniChart
key={`${profile.id}-${snapshot?.last_update || 0}-${bandwidthData.length}`}
data={bandwidthData}
currentBandwidth={currentBandwidth}
onClick={() => meta.onOpenTrafficDialog?.(profile.id)}
+10 -2
View File
@@ -83,8 +83,16 @@ export function TrafficDetailsDialog({
const fetchStats = async () => {
try {
const allStats = await invoke<TrafficStats[]>("get_all_traffic_stats");
const profileStats = allStats.find((s) => s.profile_id === profileId);
setStats(profileStats || null);
const matchingStats = allStats.filter(
(s) => s.profile_id === profileId,
);
const profileStats =
matchingStats.length > 0
? matchingStats.reduce((latest, current) =>
current.last_update > latest.last_update ? current : latest,
)
: null;
setStats(profileStats);
} catch (error) {
console.error("Failed to fetch traffic stats:", error);
}