Skip to content

Commit ec10b81

Browse files
committed
refactor: dedup threshold evaluation in sysinfo
Replace 3 structurally identical memory/swap/disk check blocks with a data-driven loop over a byteCheck slice. Same behavior, less repetition. Spec: specs/ast-audit-contributor-guide.md Signed-off-by: Jose Alekhinne <jose@ctx.ist>
1 parent 5d44ffc commit ec10b81

1 file changed

Lines changed: 53 additions & 43 deletions

File tree

internal/sysinfo/threshold.go

Lines changed: 53 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -32,56 +32,66 @@ import (
3232
func Evaluate(snap Snapshot) []ResourceAlert {
3333
var alerts []ResourceAlert
3434

35-
// Memory
36-
if snap.Memory.Supported && snap.Memory.TotalBytes > 0 {
37-
pct := percent(snap.Memory.UsedBytes, snap.Memory.TotalBytes)
38-
msg := fmt.Sprintf(desc.Text(text.DescKeyResourcesAlertMemory),
39-
pct, FormatGiB(snap.Memory.UsedBytes), FormatGiB(snap.Memory.TotalBytes))
40-
if pct >= stats.ThresholdMemoryDangerPct {
41-
alerts = append(alerts, ResourceAlert{
42-
Severity: SeverityDanger, Resource: cfgSysinfo.ResourceMemory, Message: msg,
43-
})
44-
} else if pct >= stats.ThresholdMemoryWarnPct {
45-
alerts = append(alerts, ResourceAlert{
46-
Severity: SeverityWarning,
47-
Resource: cfgSysinfo.ResourceMemory,
48-
Message: msg,
49-
})
50-
}
35+
type byteCheck struct {
36+
supported bool
37+
used uint64
38+
total uint64
39+
descKey string
40+
resource string
41+
dangerPct float64
42+
warnPct float64
5143
}
5244

53-
// Swap
54-
if snap.Memory.Supported && snap.Memory.SwapTotalBytes > 0 {
55-
pct := percent(snap.Memory.SwapUsedBytes, snap.Memory.SwapTotalBytes)
56-
msg := fmt.Sprintf(
57-
desc.Text(text.DescKeyResourcesAlertSwap),
58-
pct,
59-
FormatGiB(snap.Memory.SwapUsedBytes),
60-
FormatGiB(snap.Memory.SwapTotalBytes),
61-
)
62-
if pct >= stats.ThresholdSwapDangerPct {
63-
alerts = append(alerts, ResourceAlert{
64-
Severity: SeverityDanger, Resource: cfgSysinfo.ResourceSwap, Message: msg,
65-
})
66-
} else if pct >= stats.ThresholdSwapWarnPct {
67-
alerts = append(alerts, ResourceAlert{
68-
Severity: SeverityWarning, Resource: cfgSysinfo.ResourceSwap, Message: msg,
69-
})
70-
}
45+
checks := []byteCheck{
46+
{
47+
snap.Memory.Supported,
48+
snap.Memory.UsedBytes,
49+
snap.Memory.TotalBytes,
50+
text.DescKeyResourcesAlertMemory,
51+
cfgSysinfo.ResourceMemory,
52+
stats.ThresholdMemoryDangerPct,
53+
stats.ThresholdMemoryWarnPct,
54+
},
55+
{
56+
snap.Memory.Supported,
57+
snap.Memory.SwapUsedBytes,
58+
snap.Memory.SwapTotalBytes,
59+
text.DescKeyResourcesAlertSwap,
60+
cfgSysinfo.ResourceSwap,
61+
stats.ThresholdSwapDangerPct,
62+
stats.ThresholdSwapWarnPct,
63+
},
64+
{
65+
snap.Disk.Supported,
66+
snap.Disk.UsedBytes,
67+
snap.Disk.TotalBytes,
68+
text.DescKeyResourcesAlertDisk,
69+
cfgSysinfo.ResourceDisk,
70+
stats.ThresholdDiskDangerPct,
71+
stats.ThresholdDiskWarnPct,
72+
},
7173
}
7274

73-
// Disk
74-
if snap.Disk.Supported && snap.Disk.TotalBytes > 0 {
75-
pct := percent(snap.Disk.UsedBytes, snap.Disk.TotalBytes)
76-
msg := fmt.Sprintf(desc.Text(text.DescKeyResourcesAlertDisk),
77-
pct, FormatGiB(snap.Disk.UsedBytes), FormatGiB(snap.Disk.TotalBytes))
78-
if pct >= stats.ThresholdDiskDangerPct {
75+
for _, c := range checks {
76+
if !c.supported || c.total == 0 {
77+
continue
78+
}
79+
pct := percent(c.used, c.total)
80+
msg := fmt.Sprintf(
81+
desc.Text(c.descKey), pct,
82+
FormatGiB(c.used), FormatGiB(c.total),
83+
)
84+
if pct >= c.dangerPct {
7985
alerts = append(alerts, ResourceAlert{
80-
Severity: SeverityDanger, Resource: cfgSysinfo.ResourceDisk, Message: msg,
86+
Severity: SeverityDanger,
87+
Resource: c.resource,
88+
Message: msg,
8189
})
82-
} else if pct >= stats.ThresholdDiskWarnPct {
90+
} else if pct >= c.warnPct {
8391
alerts = append(alerts, ResourceAlert{
84-
Severity: SeverityWarning, Resource: cfgSysinfo.ResourceDisk, Message: msg,
92+
Severity: SeverityWarning,
93+
Resource: c.resource,
94+
Message: msg,
8595
})
8696
}
8797
}

0 commit comments

Comments
 (0)