mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-12-01 01:52:30 +01:00
Reconcile the absent runtime portion of PR2693 aaf5398480 on current assigned source. TrueNAS disk heat follows its disk override, TrueNAS-wide default and inherited per-type policy rather than the system synthetic agent ID. Preserve agent-host overrides, Off, clear thresholds, alert switches, retained-reading limits and canonical identity. Include discriminating resolver, adapter, Patrol and Assistant tests; leave the frontend stack to Web.
Change-source: pulse-maintainer
114 lines
4.2 KiB
Go
114 lines
4.2 KiB
Go
package ai
|
|
|
|
import (
|
|
"github.com/rcourtman/pulse-go-rewrite/internal/alerts"
|
|
)
|
|
|
|
// AlertThresholdAdapter adapts alerts.Manager to the ThresholdProvider interface
|
|
// This allows the patrol service to use user-configured alert thresholds
|
|
type AlertThresholdAdapter struct {
|
|
manager *alerts.Manager
|
|
}
|
|
|
|
// NewAlertThresholdAdapter creates a new adapter for the alerts manager
|
|
func NewAlertThresholdAdapter(manager *alerts.Manager) *AlertThresholdAdapter {
|
|
return &AlertThresholdAdapter{manager: manager}
|
|
}
|
|
|
|
// getThreshold is a shared helper that extracts a trigger value from the alert config.
|
|
// extract should return the trigger value and true if the config field is present.
|
|
func (a *AlertThresholdAdapter) getThreshold(extract func(alerts.AlertConfig) (float64, bool), defaultVal float64) float64 {
|
|
if a.manager == nil {
|
|
return defaultVal
|
|
}
|
|
cfg := a.manager.GetConfig()
|
|
if trigger, ok := extract(cfg); ok && trigger > 0 {
|
|
return trigger
|
|
}
|
|
return defaultVal
|
|
}
|
|
|
|
// GetNodeCPUThreshold returns the CPU alert trigger threshold for nodes (0-100%)
|
|
func (a *AlertThresholdAdapter) GetNodeCPUThreshold() float64 {
|
|
return a.getThreshold(func(cfg alerts.AlertConfig) (float64, bool) {
|
|
if cfg.NodeDefaults.CPU != nil {
|
|
return cfg.NodeDefaults.CPU.Trigger, true
|
|
}
|
|
return 0, false
|
|
}, 80)
|
|
}
|
|
|
|
// GetNodeMemoryThreshold returns the memory alert trigger threshold for nodes (0-100%)
|
|
func (a *AlertThresholdAdapter) GetNodeMemoryThreshold() float64 {
|
|
return a.getThreshold(func(cfg alerts.AlertConfig) (float64, bool) {
|
|
if cfg.NodeDefaults.Memory != nil {
|
|
return cfg.NodeDefaults.Memory.Trigger, true
|
|
}
|
|
return 0, false
|
|
}, 85)
|
|
}
|
|
|
|
// GetGuestMemoryThreshold returns the memory alert trigger threshold for guests (0-100%)
|
|
func (a *AlertThresholdAdapter) GetGuestMemoryThreshold() float64 {
|
|
return a.getThreshold(func(cfg alerts.AlertConfig) (float64, bool) {
|
|
if cfg.GuestDefaults.Memory != nil {
|
|
return cfg.GuestDefaults.Memory.Trigger, true
|
|
}
|
|
return 0, false
|
|
}, 85)
|
|
}
|
|
|
|
// GetGuestDiskThreshold returns the disk alert trigger threshold for guests (0-100%)
|
|
func (a *AlertThresholdAdapter) GetGuestDiskThreshold() float64 {
|
|
return a.getThreshold(func(cfg alerts.AlertConfig) (float64, bool) {
|
|
if cfg.GuestDefaults.Disk != nil {
|
|
return cfg.GuestDefaults.Disk.Trigger, true
|
|
}
|
|
return 0, false
|
|
}, 90)
|
|
}
|
|
|
|
// GetDiskTemperatureThreshold returns the disk temperature alert trigger and
|
|
// clear values for a disk of a type that a host agent reports, resolved
|
|
// exactly as disk temperature alerts resolve them
|
|
// (alerts.Manager.HostDiskTemperatureThreshold), host overrides included.
|
|
func (a *AlertThresholdAdapter) GetDiskTemperatureThreshold(host alerts.DiskTemperatureHost, diskType string) (float64, float64) {
|
|
var threshold *alerts.HysteresisThreshold
|
|
if a.manager != nil {
|
|
threshold = a.manager.HostDiskTemperatureThreshold(host, diskType)
|
|
} else {
|
|
threshold = alerts.DefaultDiskTemperatureThreshold(diskType)
|
|
}
|
|
return diskTemperatureTriggerAndClear(threshold)
|
|
}
|
|
|
|
// GetTrueNASDiskTemperatureThreshold returns the temperature alert trigger and
|
|
// clear values for one TrueNAS disk, resolved exactly as its temperature alert
|
|
// resolves them (alerts.Manager.TrueNASDiskTemperatureThreshold): the disk's
|
|
// own override, then the TrueNAS-wide value, then the per-type policy.
|
|
func (a *AlertThresholdAdapter) GetTrueNASDiskTemperatureThreshold(resourceID, diskType string) (float64, float64) {
|
|
var threshold *alerts.HysteresisThreshold
|
|
if a.manager != nil {
|
|
threshold = a.manager.TrueNASDiskTemperatureThreshold(resourceID, diskType)
|
|
} else {
|
|
threshold = alerts.DefaultDiskTemperatureThreshold(diskType)
|
|
}
|
|
return diskTemperatureTriggerAndClear(threshold)
|
|
}
|
|
|
|
// diskTemperatureTriggerAndClear unpacks a disk temperature threshold, with a
|
|
// nil or non-positive trigger meaning off (0, 0).
|
|
func diskTemperatureTriggerAndClear(threshold *alerts.HysteresisThreshold) (float64, float64) {
|
|
if threshold == nil || threshold.Trigger <= 0 {
|
|
return 0, 0
|
|
}
|
|
return threshold.Trigger, threshold.Clear
|
|
}
|
|
|
|
// GetStorageThreshold returns the usage alert trigger threshold for storage (0-100%)
|
|
func (a *AlertThresholdAdapter) GetStorageThreshold() float64 {
|
|
return a.getThreshold(func(cfg alerts.AlertConfig) (float64, bool) {
|
|
return cfg.StorageDefault.Trigger, true
|
|
}, 85)
|
|
}
|