refactor: variable renaming in alerts package

This commit is contained in:
henrygd
2026-03-17 18:44:46 -04:00
parent c6c3950fb0
commit c3a0e645ee
6 changed files with 54 additions and 54 deletions

View File

@@ -64,7 +64,7 @@ type SystemAlertGPUData struct {
type SystemAlertData struct {
systemRecord *core.Record
alertRecord CachedAlertData
alertData CachedAlertData
name string
unit string
val float64

View File

@@ -125,11 +125,11 @@ func (c *AlertsCache) GetSystemAlerts(systemID string) []CachedAlertData {
c.store.Set(systemID, systemStore)
}
all := systemStore.GetAll()
records := make([]CachedAlertData, 0, len(all))
alerts := make([]CachedAlertData, 0, len(all))
for _, alert := range all {
records = append(records, alert)
alerts = append(alerts, alert)
}
return records
return alerts
}
// GetAlert returns a specific alert by its ID from the cache.
@@ -143,13 +143,13 @@ func (c *AlertsCache) GetAlert(systemID, alertID string) (CachedAlertData, bool)
// GetAlertsByName returns all alerts of a specific type for the specified system.
func (c *AlertsCache) GetAlertsByName(systemID, alertName string) []CachedAlertData {
allAlerts := c.GetSystemAlerts(systemID)
var alertRecords []CachedAlertData
var alerts []CachedAlertData
for _, record := range allAlerts {
if record.Name == alertName {
alertRecords = append(alertRecords, record)
alerts = append(alerts, record)
}
}
return alertRecords
return alerts
}
// GetAlertsExcludingNames returns all alerts for the specified system excluding the given types.
@@ -159,13 +159,13 @@ func (c *AlertsCache) GetAlertsExcludingNames(systemID string, excludedNames ...
excludeMap[name] = struct{}{}
}
allAlerts := c.GetSystemAlerts(systemID)
var alertRecords []CachedAlertData
var alerts []CachedAlertData
for _, record := range allAlerts {
if _, excluded := excludeMap[record.Name]; !excluded {
alertRecords = append(alertRecords, record)
alerts = append(alerts, record)
}
}
return alertRecords
return alerts
}
// Refresh returns the latest cached copy for an alert snapshot if it still exists.

View File

@@ -9,10 +9,10 @@ import (
)
type alertInfo struct {
systemName string
alertRecord CachedAlertData
expireTime time.Time
timer *time.Timer
systemName string
alertData CachedAlertData
expireTime time.Time
timer *time.Timer
}
// Stop cancels all pending status alert timers.
@@ -35,61 +35,61 @@ func (am *AlertManager) HandleStatusAlerts(newStatus string, systemRecord *core.
return nil
}
alertRecords := am.alertsCache.GetAlertsByName(systemRecord.Id, "Status")
if len(alertRecords) == 0 {
alerts := am.alertsCache.GetAlertsByName(systemRecord.Id, "Status")
if len(alerts) == 0 {
return nil
}
systemName := systemRecord.GetString("name")
if newStatus == "down" {
am.handleSystemDown(systemName, alertRecords)
am.handleSystemDown(systemName, alerts)
} else {
am.handleSystemUp(systemName, alertRecords)
am.handleSystemUp(systemName, alerts)
}
return nil
}
// handleSystemDown manages the logic when a system status changes to "down". It schedules pending alerts for each alert record.
func (am *AlertManager) handleSystemDown(systemName string, alertRecords []CachedAlertData) {
for _, alertRecord := range alertRecords {
min := max(1, int(alertRecord.Min))
am.schedulePendingStatusAlert(systemName, alertRecord, time.Duration(min)*time.Minute)
func (am *AlertManager) handleSystemDown(systemName string, alerts []CachedAlertData) {
for _, alertData := range alerts {
min := max(1, int(alertData.Min))
am.schedulePendingStatusAlert(systemName, alertData, time.Duration(min)*time.Minute)
}
}
// schedulePendingStatusAlert sets up a timer to send a "down" alert after the specified delay if the system is still down.
// It returns true if the alert was scheduled, or false if an alert was already pending for the given alert record.
func (am *AlertManager) schedulePendingStatusAlert(systemName string, alertRecord CachedAlertData, delay time.Duration) bool {
func (am *AlertManager) schedulePendingStatusAlert(systemName string, alertData CachedAlertData, delay time.Duration) bool {
alert := &alertInfo{
systemName: systemName,
alertRecord: alertRecord,
expireTime: time.Now().Add(delay),
systemName: systemName,
alertData: alertData,
expireTime: time.Now().Add(delay),
}
storedAlert, loaded := am.pendingAlerts.LoadOrStore(alertRecord.Id, alert)
storedAlert, loaded := am.pendingAlerts.LoadOrStore(alertData.Id, alert)
if loaded {
return false
}
stored := storedAlert.(*alertInfo)
stored.timer = time.AfterFunc(time.Until(stored.expireTime), func() {
am.processPendingAlert(alertRecord.Id)
am.processPendingAlert(alertData.Id)
})
return true
}
// handleSystemUp manages the logic when a system status changes to "up".
// It cancels any pending alerts and sends "up" alerts.
func (am *AlertManager) handleSystemUp(systemName string, alertRecords []CachedAlertData) {
for _, alertRecord := range alertRecords {
func (am *AlertManager) handleSystemUp(systemName string, alerts []CachedAlertData) {
for _, alertData := range alerts {
// If alert exists for record, delete and continue (down alert not sent)
if am.cancelPendingAlert(alertRecord.Id) {
if am.cancelPendingAlert(alertData.Id) {
continue
}
if !alertRecord.Triggered {
if !alertData.Triggered {
continue
}
if err := am.sendStatusAlert("up", systemName, alertRecord); err != nil {
if err := am.sendStatusAlert("up", systemName, alertData); err != nil {
am.hub.Logger().Error("Failed to send alert", "err", err)
}
}
@@ -117,20 +117,20 @@ func (am *AlertManager) processPendingAlert(alertID string) {
}
info := value.(*alertInfo)
alertRecord, ok := am.alertsCache.Refresh(info.alertRecord)
if !ok || alertRecord.Triggered {
refreshedAlertData, ok := am.alertsCache.Refresh(info.alertData)
if !ok || refreshedAlertData.Triggered {
return
}
if err := am.sendStatusAlert("down", info.systemName, alertRecord); err != nil {
if err := am.sendStatusAlert("down", info.systemName, refreshedAlertData); err != nil {
am.hub.Logger().Error("Failed to send alert", "err", err)
}
}
// sendStatusAlert sends a status alert ("up" or "down") to the users associated with the alert records.
func (am *AlertManager) sendStatusAlert(alertStatus string, systemName string, alertRecord CachedAlertData) error {
func (am *AlertManager) sendStatusAlert(alertStatus string, systemName string, alertData CachedAlertData) error {
// Update trigger state for alert record before sending alert
triggered := alertStatus == "down"
if err := am.setAlertTriggered(alertRecord, triggered); err != nil {
if err := am.setAlertTriggered(alertData, triggered); err != nil {
return err
}
@@ -145,10 +145,10 @@ func (am *AlertManager) sendStatusAlert(alertStatus string, systemName string, a
message := strings.TrimSuffix(title, emoji)
// Get system ID for the link
systemID := alertRecord.SystemID
systemID := alertData.SystemID
return am.SendAlert(AlertMessageData{
UserID: alertRecord.UserID,
UserID: alertData.UserID,
SystemID: systemID,
Title: title,
Message: message,
@@ -215,12 +215,12 @@ func (am *AlertManager) restorePendingStatusAlerts() error {
_ = am.alertsCache.PopulateFromDB(false)
for _, item := range pending {
alertRecord, ok := am.alertsCache.GetAlert(item.SystemID, item.AlertID)
alertData, ok := am.alertsCache.GetAlert(item.SystemID, item.AlertID)
if !ok {
continue
}
min := max(1, int(alertRecord.Min))
am.schedulePendingStatusAlert(item.SystemName, alertRecord, time.Duration(min)*time.Minute)
min := max(1, int(alertData.Min))
am.schedulePendingStatusAlert(item.SystemName, alertData, time.Duration(min)*time.Minute)
}
return nil

View File

@@ -14,8 +14,8 @@ import (
)
func (am *AlertManager) HandleSystemAlerts(systemRecord *core.Record, data *system.CombinedData) error {
alertRecords := am.alertsCache.GetAlertsExcludingNames(systemRecord.Id, "Status")
if len(alertRecords) == 0 {
alerts := am.alertsCache.GetAlertsExcludingNames(systemRecord.Id, "Status")
if len(alerts) == 0 {
return nil
}
@@ -23,8 +23,8 @@ func (am *AlertManager) HandleSystemAlerts(systemRecord *core.Record, data *syst
now := systemRecord.GetDateTime("updated").Time().UTC()
oldestTime := now
for _, alertRecord := range alertRecords {
name := alertRecord.Name
for _, alertData := range alerts {
name := alertData.Name
var val float64
unit := "%"
@@ -69,8 +69,8 @@ func (am *AlertManager) HandleSystemAlerts(systemRecord *core.Record, data *syst
val = float64(data.Stats.Battery[0])
}
triggered := alertRecord.Triggered
threshold := alertRecord.Value
triggered := alertData.Triggered
threshold := alertData.Value
// Battery alert has inverted logic: trigger when value is BELOW threshold
lowAlert := isLowAlert(name)
@@ -88,11 +88,11 @@ func (am *AlertManager) HandleSystemAlerts(systemRecord *core.Record, data *syst
}
}
min := max(1, alertRecord.Min)
min := max(1, alertData.Min)
alert := SystemAlertData{
systemRecord: systemRecord,
alertRecord: alertRecord,
alertData: alertData,
name: name,
unit: unit,
val: val,
@@ -340,12 +340,12 @@ func (am *AlertManager) sendSystemAlert(alert SystemAlertData) {
}
body := fmt.Sprintf("%s averaged %.2f%s for the previous %v %s.", alert.descriptor, alert.val, alert.unit, alert.min, minutesLabel)
if err := am.setAlertTriggered(alert.alertRecord, alert.triggered); err != nil {
if err := am.setAlertTriggered(alert.alertData, alert.triggered); err != nil {
// app.Logger().Error("failed to save alert record", "err", err)
return
}
am.SendAlert(AlertMessageData{
UserID: alert.alertRecord.UserID,
UserID: alert.alertData.UserID,
SystemID: alert.systemRecord.Id,
Title: subject,
Message: body,

View File

@@ -50,7 +50,7 @@ func (am *AlertManager) ProcessPendingAlerts() ([]CachedAlertData, error) {
info.timer.Stop()
}
am.processPendingAlert(key.(string))
processedAlerts = append(processedAlerts, info.alertRecord)
processedAlerts = append(processedAlerts, info.alertData)
}
return true
})