refactor: alerts package compatiblity with new systems package

This commit is contained in:
henrygd
2025-03-03 23:52:27 -05:00
parent f8a1d9fc5d
commit a1f6eeb9eb
2 changed files with 16 additions and 26 deletions

View File

@@ -26,8 +26,7 @@ type alertInfo struct {
// startWorker is a long-running goroutine that processes alert tasks
// every x seconds. It must be running to process status alerts.
func (am *AlertManager) startWorker() {
// no special reason for 13 seconds
tick := time.Tick(13 * time.Second)
tick := time.Tick(15 * time.Second)
for {
select {
case <-am.stopChan:
@@ -64,21 +63,12 @@ func (am *AlertManager) StopWorker() {
}
// HandleStatusAlerts manages the logic when system status changes.
func (am *AlertManager) HandleStatusAlerts(newStatus string, oldSystemRecord *core.Record) error {
switch newStatus {
case "up":
if oldSystemRecord.GetString("status") != "down" {
return nil
}
case "down":
if oldSystemRecord.GetString("status") != "up" {
return nil
}
default:
func (am *AlertManager) HandleStatusAlerts(newStatus string, systemRecord *core.Record) error {
if newStatus != "up" && newStatus != "down" {
return nil
}
alertRecords, err := am.getSystemStatusAlerts(oldSystemRecord.Id)
alertRecords, err := am.getSystemStatusAlerts(systemRecord.Id)
if err != nil {
return err
}
@@ -86,7 +76,7 @@ func (am *AlertManager) HandleStatusAlerts(newStatus string, oldSystemRecord *co
return nil
}
systemName := oldSystemRecord.GetString("name")
systemName := systemRecord.GetString("name")
if newStatus == "down" {
am.handleSystemDown(systemName, alertRecords)
} else {

View File

@@ -15,7 +15,7 @@ import (
"github.com/spf13/cast"
)
func (am *AlertManager) HandleSystemAlerts(systemRecord *core.Record, systemInfo system.Info, temperatures map[string]float64, extraFs map[string]*system.FsStats) error {
func (am *AlertManager) HandleSystemAlerts(systemRecord *core.Record, data *system.CombinedData) error {
alertRecords, err := am.app.FindAllRecords("alerts",
dbx.NewExp("system={:system}", dbx.Params{"system": systemRecord.Id}),
)
@@ -35,15 +35,15 @@ func (am *AlertManager) HandleSystemAlerts(systemRecord *core.Record, systemInfo
switch name {
case "CPU":
val = systemInfo.Cpu
val = data.Info.Cpu
case "Memory":
val = systemInfo.MemPct
val = data.Info.MemPct
case "Bandwidth":
val = systemInfo.Bandwidth
val = data.Info.Bandwidth
unit = " MB/s"
case "Disk":
maxUsedPct := systemInfo.DiskPct
for _, fs := range extraFs {
maxUsedPct := data.Info.DiskPct
for _, fs := range data.Stats.ExtraFs {
usedPct := fs.DiskUsed / fs.DiskTotal * 100
if usedPct > maxUsedPct {
maxUsedPct = usedPct
@@ -51,10 +51,10 @@ func (am *AlertManager) HandleSystemAlerts(systemRecord *core.Record, systemInfo
}
val = maxUsedPct
case "Temperature":
if temperatures == nil {
if data.Stats.Temperatures == nil {
continue
}
for _, temp := range temperatures {
for _, temp := range data.Stats.Temperatures {
if temp > val {
val = temp
}
@@ -111,7 +111,7 @@ func (am *AlertManager) HandleSystemAlerts(systemRecord *core.Record, systemInfo
)).
OrderBy("created").
All(&systemStats)
if err != nil {
if err != nil || len(systemStats) == 0 {
return err
}
@@ -163,7 +163,7 @@ func (am *AlertManager) HandleSystemAlerts(systemRecord *core.Record, systemInfo
alert.val += stats.NetSent + stats.NetRecv
case "Disk":
if alert.mapSums == nil {
alert.mapSums = make(map[string]float32, len(extraFs)+1)
alert.mapSums = make(map[string]float32, len(data.Stats.ExtraFs)+1)
}
// add root disk
if _, ok := alert.mapSums["root"]; !ok {
@@ -171,7 +171,7 @@ func (am *AlertManager) HandleSystemAlerts(systemRecord *core.Record, systemInfo
}
alert.mapSums["root"] += float32(stats.Disk)
// add extra disks
for key, fs := range extraFs {
for key, fs := range data.Stats.ExtraFs {
if _, ok := alert.mapSums[key]; !ok {
alert.mapSums[key] = 0.0
}