From 19b4477a75aca379c64188e4827e8cb3ee5fc5d2 Mon Sep 17 00:00:00 2001 From: Chuangbo Li Date: Sun, 29 Dec 2024 06:06:37 +0800 Subject: [PATCH] fix: json.Unmarshal not resetting struct fields (#345) The json.Unmarshal function was not properly resetting the Stats struct fields between iterations in the loop. This caused incorrect values to be retained from previous iterations, leading to unexpected behavior. This commit fixes the issue by zero the struct in each loop iteration, ensures that each unmarshaling operation starts with a clean struct, preventing value carryover. --- beszel/internal/records/records.go | 1 + 1 file changed, 1 insertion(+) diff --git a/beszel/internal/records/records.go b/beszel/internal/records/records.go index a1df676..82e8ebf 100644 --- a/beszel/internal/records/records.go +++ b/beszel/internal/records/records.go @@ -155,6 +155,7 @@ func (rm *RecordManager) AverageSystemStats(records RecordStats) system.Stats { var stats system.Stats for i := range records { + stats = system.Stats{} // Zero the struct before unmarshalling json.Unmarshal(records[i].Stats, &stats) sum.Cpu += stats.Cpu sum.Mem += stats.Mem