mirror of
https://github.com/henrygd/beszel.git
synced 2026-03-22 05:36:15 +01:00
refactor: variable renaming in alerts package
This commit is contained in:
@@ -64,7 +64,7 @@ type SystemAlertGPUData struct {
|
|||||||
|
|
||||||
type SystemAlertData struct {
|
type SystemAlertData struct {
|
||||||
systemRecord *core.Record
|
systemRecord *core.Record
|
||||||
alertRecord CachedAlertData
|
alertData CachedAlertData
|
||||||
name string
|
name string
|
||||||
unit string
|
unit string
|
||||||
val float64
|
val float64
|
||||||
|
|||||||
@@ -125,11 +125,11 @@ func (c *AlertsCache) GetSystemAlerts(systemID string) []CachedAlertData {
|
|||||||
c.store.Set(systemID, systemStore)
|
c.store.Set(systemID, systemStore)
|
||||||
}
|
}
|
||||||
all := systemStore.GetAll()
|
all := systemStore.GetAll()
|
||||||
records := make([]CachedAlertData, 0, len(all))
|
alerts := make([]CachedAlertData, 0, len(all))
|
||||||
for _, alert := range 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.
|
// 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.
|
// GetAlertsByName returns all alerts of a specific type for the specified system.
|
||||||
func (c *AlertsCache) GetAlertsByName(systemID, alertName string) []CachedAlertData {
|
func (c *AlertsCache) GetAlertsByName(systemID, alertName string) []CachedAlertData {
|
||||||
allAlerts := c.GetSystemAlerts(systemID)
|
allAlerts := c.GetSystemAlerts(systemID)
|
||||||
var alertRecords []CachedAlertData
|
var alerts []CachedAlertData
|
||||||
for _, record := range allAlerts {
|
for _, record := range allAlerts {
|
||||||
if record.Name == alertName {
|
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.
|
// 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{}{}
|
excludeMap[name] = struct{}{}
|
||||||
}
|
}
|
||||||
allAlerts := c.GetSystemAlerts(systemID)
|
allAlerts := c.GetSystemAlerts(systemID)
|
||||||
var alertRecords []CachedAlertData
|
var alerts []CachedAlertData
|
||||||
for _, record := range allAlerts {
|
for _, record := range allAlerts {
|
||||||
if _, excluded := excludeMap[record.Name]; !excluded {
|
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.
|
// Refresh returns the latest cached copy for an alert snapshot if it still exists.
|
||||||
@@ -10,7 +10,7 @@ import (
|
|||||||
|
|
||||||
type alertInfo struct {
|
type alertInfo struct {
|
||||||
systemName string
|
systemName string
|
||||||
alertRecord CachedAlertData
|
alertData CachedAlertData
|
||||||
expireTime time.Time
|
expireTime time.Time
|
||||||
timer *time.Timer
|
timer *time.Timer
|
||||||
}
|
}
|
||||||
@@ -35,61 +35,61 @@ func (am *AlertManager) HandleStatusAlerts(newStatus string, systemRecord *core.
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
alertRecords := am.alertsCache.GetAlertsByName(systemRecord.Id, "Status")
|
alerts := am.alertsCache.GetAlertsByName(systemRecord.Id, "Status")
|
||||||
if len(alertRecords) == 0 {
|
if len(alerts) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
systemName := systemRecord.GetString("name")
|
systemName := systemRecord.GetString("name")
|
||||||
if newStatus == "down" {
|
if newStatus == "down" {
|
||||||
am.handleSystemDown(systemName, alertRecords)
|
am.handleSystemDown(systemName, alerts)
|
||||||
} else {
|
} else {
|
||||||
am.handleSystemUp(systemName, alertRecords)
|
am.handleSystemUp(systemName, alerts)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// handleSystemDown manages the logic when a system status changes to "down". It schedules pending alerts for each alert record.
|
// 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) {
|
func (am *AlertManager) handleSystemDown(systemName string, alerts []CachedAlertData) {
|
||||||
for _, alertRecord := range alertRecords {
|
for _, alertData := range alerts {
|
||||||
min := max(1, int(alertRecord.Min))
|
min := max(1, int(alertData.Min))
|
||||||
am.schedulePendingStatusAlert(systemName, alertRecord, time.Duration(min)*time.Minute)
|
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.
|
// 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.
|
// 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{
|
alert := &alertInfo{
|
||||||
systemName: systemName,
|
systemName: systemName,
|
||||||
alertRecord: alertRecord,
|
alertData: alertData,
|
||||||
expireTime: time.Now().Add(delay),
|
expireTime: time.Now().Add(delay),
|
||||||
}
|
}
|
||||||
|
|
||||||
storedAlert, loaded := am.pendingAlerts.LoadOrStore(alertRecord.Id, alert)
|
storedAlert, loaded := am.pendingAlerts.LoadOrStore(alertData.Id, alert)
|
||||||
if loaded {
|
if loaded {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
stored := storedAlert.(*alertInfo)
|
stored := storedAlert.(*alertInfo)
|
||||||
stored.timer = time.AfterFunc(time.Until(stored.expireTime), func() {
|
stored.timer = time.AfterFunc(time.Until(stored.expireTime), func() {
|
||||||
am.processPendingAlert(alertRecord.Id)
|
am.processPendingAlert(alertData.Id)
|
||||||
})
|
})
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// handleSystemUp manages the logic when a system status changes to "up".
|
// handleSystemUp manages the logic when a system status changes to "up".
|
||||||
// It cancels any pending alerts and sends "up" alerts.
|
// It cancels any pending alerts and sends "up" alerts.
|
||||||
func (am *AlertManager) handleSystemUp(systemName string, alertRecords []CachedAlertData) {
|
func (am *AlertManager) handleSystemUp(systemName string, alerts []CachedAlertData) {
|
||||||
for _, alertRecord := range alertRecords {
|
for _, alertData := range alerts {
|
||||||
// If alert exists for record, delete and continue (down alert not sent)
|
// If alert exists for record, delete and continue (down alert not sent)
|
||||||
if am.cancelPendingAlert(alertRecord.Id) {
|
if am.cancelPendingAlert(alertData.Id) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if !alertRecord.Triggered {
|
if !alertData.Triggered {
|
||||||
continue
|
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)
|
am.hub.Logger().Error("Failed to send alert", "err", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -117,20 +117,20 @@ func (am *AlertManager) processPendingAlert(alertID string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
info := value.(*alertInfo)
|
info := value.(*alertInfo)
|
||||||
alertRecord, ok := am.alertsCache.Refresh(info.alertRecord)
|
refreshedAlertData, ok := am.alertsCache.Refresh(info.alertData)
|
||||||
if !ok || alertRecord.Triggered {
|
if !ok || refreshedAlertData.Triggered {
|
||||||
return
|
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)
|
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.
|
// 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
|
// Update trigger state for alert record before sending alert
|
||||||
triggered := alertStatus == "down"
|
triggered := alertStatus == "down"
|
||||||
if err := am.setAlertTriggered(alertRecord, triggered); err != nil {
|
if err := am.setAlertTriggered(alertData, triggered); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -145,10 +145,10 @@ func (am *AlertManager) sendStatusAlert(alertStatus string, systemName string, a
|
|||||||
message := strings.TrimSuffix(title, emoji)
|
message := strings.TrimSuffix(title, emoji)
|
||||||
|
|
||||||
// Get system ID for the link
|
// Get system ID for the link
|
||||||
systemID := alertRecord.SystemID
|
systemID := alertData.SystemID
|
||||||
|
|
||||||
return am.SendAlert(AlertMessageData{
|
return am.SendAlert(AlertMessageData{
|
||||||
UserID: alertRecord.UserID,
|
UserID: alertData.UserID,
|
||||||
SystemID: systemID,
|
SystemID: systemID,
|
||||||
Title: title,
|
Title: title,
|
||||||
Message: message,
|
Message: message,
|
||||||
@@ -215,12 +215,12 @@ func (am *AlertManager) restorePendingStatusAlerts() error {
|
|||||||
_ = am.alertsCache.PopulateFromDB(false)
|
_ = am.alertsCache.PopulateFromDB(false)
|
||||||
|
|
||||||
for _, item := range pending {
|
for _, item := range pending {
|
||||||
alertRecord, ok := am.alertsCache.GetAlert(item.SystemID, item.AlertID)
|
alertData, ok := am.alertsCache.GetAlert(item.SystemID, item.AlertID)
|
||||||
if !ok {
|
if !ok {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
min := max(1, int(alertRecord.Min))
|
min := max(1, int(alertData.Min))
|
||||||
am.schedulePendingStatusAlert(item.SystemName, alertRecord, time.Duration(min)*time.Minute)
|
am.schedulePendingStatusAlert(item.SystemName, alertData, time.Duration(min)*time.Minute)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -14,8 +14,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (am *AlertManager) HandleSystemAlerts(systemRecord *core.Record, data *system.CombinedData) error {
|
func (am *AlertManager) HandleSystemAlerts(systemRecord *core.Record, data *system.CombinedData) error {
|
||||||
alertRecords := am.alertsCache.GetAlertsExcludingNames(systemRecord.Id, "Status")
|
alerts := am.alertsCache.GetAlertsExcludingNames(systemRecord.Id, "Status")
|
||||||
if len(alertRecords) == 0 {
|
if len(alerts) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -23,8 +23,8 @@ func (am *AlertManager) HandleSystemAlerts(systemRecord *core.Record, data *syst
|
|||||||
now := systemRecord.GetDateTime("updated").Time().UTC()
|
now := systemRecord.GetDateTime("updated").Time().UTC()
|
||||||
oldestTime := now
|
oldestTime := now
|
||||||
|
|
||||||
for _, alertRecord := range alertRecords {
|
for _, alertData := range alerts {
|
||||||
name := alertRecord.Name
|
name := alertData.Name
|
||||||
var val float64
|
var val float64
|
||||||
unit := "%"
|
unit := "%"
|
||||||
|
|
||||||
@@ -69,8 +69,8 @@ func (am *AlertManager) HandleSystemAlerts(systemRecord *core.Record, data *syst
|
|||||||
val = float64(data.Stats.Battery[0])
|
val = float64(data.Stats.Battery[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
triggered := alertRecord.Triggered
|
triggered := alertData.Triggered
|
||||||
threshold := alertRecord.Value
|
threshold := alertData.Value
|
||||||
|
|
||||||
// Battery alert has inverted logic: trigger when value is BELOW threshold
|
// Battery alert has inverted logic: trigger when value is BELOW threshold
|
||||||
lowAlert := isLowAlert(name)
|
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{
|
alert := SystemAlertData{
|
||||||
systemRecord: systemRecord,
|
systemRecord: systemRecord,
|
||||||
alertRecord: alertRecord,
|
alertData: alertData,
|
||||||
name: name,
|
name: name,
|
||||||
unit: unit,
|
unit: unit,
|
||||||
val: val,
|
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)
|
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)
|
// app.Logger().Error("failed to save alert record", "err", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
am.SendAlert(AlertMessageData{
|
am.SendAlert(AlertMessageData{
|
||||||
UserID: alert.alertRecord.UserID,
|
UserID: alert.alertData.UserID,
|
||||||
SystemID: alert.systemRecord.Id,
|
SystemID: alert.systemRecord.Id,
|
||||||
Title: subject,
|
Title: subject,
|
||||||
Message: body,
|
Message: body,
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ func (am *AlertManager) ProcessPendingAlerts() ([]CachedAlertData, error) {
|
|||||||
info.timer.Stop()
|
info.timer.Stop()
|
||||||
}
|
}
|
||||||
am.processPendingAlert(key.(string))
|
am.processPendingAlert(key.(string))
|
||||||
processedAlerts = append(processedAlerts, info.alertRecord)
|
processedAlerts = append(processedAlerts, info.alertData)
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user