Files
Pulse/internal/alerts/config_facade.go
rcourtman c22871c0bd Make alert threshold Off stick for Docker, PMG and backups
Several Off toggles on the Thresholds page did nothing or did the
opposite, because each layer read 0, -1 and "missing" differently:

- PMG and Docker restart/memory-limit defaults: the toggle sends -1
  and NormalizePMGDefaults/NormalizeDockerDefaults turned anything <= 0
  back into the factory value, so Off reverted on save. The Docker
  restart-loop and memory-limit checks also had no Off at all.
- Backup and snapshot critical age: the editor sanitizers and the save
  payload raised a 0 critical to the warning age, so turning critical
  off made backups go critical at the warning age.
- A Docker container override saved for CPU alone carried
  DisableConnectivity=false and re-enabled that container's state
  alerts over a disabled Docker default.
- Per-server PMG cells, PMG bulk edit and per-container restart and
  memory-limit cells wrote fields ThresholdConfig does not have, so
  the backend dropped them on save.

0 is now Off for these counts; negative values (the toggle's -1) are
stored as 0. Keys an older alerts.json or an import omits decode as
ThresholdCountUnset and take the factory value, so existing installs
keep their alerts; persistence resolves the marker before writing. A
PMG quarantine growth level with one half Off turns that level off
instead of invalidating the spec and skipping the whole check. Docker
container overrides re-enable state alerts only with their own
severity, as applyThresholdOverride does elsewhere. The frontend keeps
a 0 critical age Off, the group header says "Restart loop off", and
rows only offer metrics an override can store.

AI Patrol's threshold adapter still maps an Off alert threshold to its
factory value; that is a Patrol product decision and is left as is.
2026-10-11 18:07:53 +01:00

140 lines
6.1 KiB
Go

package alerts
import (
"errors"
alertconfig "github.com/rcourtman/pulse-go-rewrite/internal/alerts/config"
)
// Type aliases re-exported from alerts/config for backward compatibility.
// These guarantee compile-time type identity: alerts.AlertLevel = alertconfig.AlertLevel.
type AlertLevel = alertconfig.AlertLevel
type ActivationState = alertconfig.ActivationState
type HysteresisThreshold = alertconfig.HysteresisThreshold
type ThresholdConfig = alertconfig.ThresholdConfig
type QuietHours = alertconfig.QuietHours
type QuietHoursSuppression = alertconfig.QuietHoursSuppression
type EscalationLevel = alertconfig.EscalationLevel
type EscalationConfig = alertconfig.EscalationConfig
type GroupingConfig = alertconfig.GroupingConfig
type ScheduleConfig = alertconfig.ScheduleConfig
type FilterCondition = alertconfig.FilterCondition
type FilterStack = alertconfig.FilterStack
type CustomAlertRule = alertconfig.CustomAlertRule
type DockerThresholdConfig = alertconfig.DockerThresholdConfig
type PMGThresholdConfig = alertconfig.PMGThresholdConfig
type SnapshotAlertConfig = alertconfig.SnapshotAlertConfig
type BackupAlertConfig = alertconfig.BackupAlertConfig
type GuestLookup = alertconfig.GuestLookup
type AlertConfig = alertconfig.AlertConfig
type AlertIntentSignal = alertconfig.AlertIntentSignal
type BackupOfflineIntentPolicy = alertconfig.BackupOfflineIntentPolicy
type AlertIntentRule = alertconfig.AlertIntentRule
type AlertIntentPolicyDocument = alertconfig.AlertIntentPolicyDocument
const (
AlertLevelInfo = alertconfig.AlertLevelInfo
AlertLevelWarning = alertconfig.AlertLevelWarning
AlertLevelCritical = alertconfig.AlertLevelCritical
ActivationPending = alertconfig.ActivationPending
ActivationActive = alertconfig.ActivationActive
ActivationSnoozed = alertconfig.ActivationSnoozed
CurrentAlertIntentPolicySchemaVersion = alertconfig.CurrentAlertIntentPolicySchemaVersion
AlertIntentSignalDefault = alertconfig.AlertIntentSignalDefault
AlertIntentSignalOffline = alertconfig.AlertIntentSignalOffline
AlertIntentSignalAvailability = alertconfig.AlertIntentSignalAvailability
DefaultEscalationRepeatMinutes = alertconfig.DefaultEscalationRepeatMinutes
MinEscalationDelayMinutes = alertconfig.MinEscalationDelayMinutes
MaxEscalationDelayMinutes = alertconfig.MaxEscalationDelayMinutes
MinEscalationRepeatMinutes = alertconfig.MinEscalationRepeatMinutes
MaxEscalationRepeatMinutes = alertconfig.MaxEscalationRepeatMinutes
DefaultCPUEvaluationWindowSeconds = alertconfig.DefaultCPUEvaluationWindowSeconds
MaxMetricEvaluationWindowSeconds = alertconfig.MaxMetricEvaluationWindowSeconds
)
func NewAlertIntentPolicyDocument() AlertIntentPolicyDocument {
return alertconfig.NewAlertIntentPolicyDocument()
}
func MetricAlertIntentSignal(metric string) string {
return alertconfig.MetricAlertIntentSignal(metric)
}
func NormalizeAlertIntentPolicyDocument(document AlertIntentPolicyDocument) AlertIntentPolicyDocument {
return alertconfig.NormalizeAlertIntentPolicyDocument(document)
}
func ValidateAlertIntentPolicyDocument(document AlertIntentPolicyDocument) error {
return alertconfig.ValidateAlertIntentPolicyDocument(document)
}
func ValidAlertIntentSignal(signal string) bool {
return alertconfig.ValidAlertIntentSignal(signal)
}
var ErrAlertNotFound = errors.New("alert not found")
func NormalizeAlertConfigAliases(config *AlertConfig) {
alertconfig.NormalizeAlertConfigAliases(config)
}
func NormalizeMetricTimeThresholds(input map[string]map[string]int) map[string]map[string]int {
return alertconfig.NormalizeMetricTimeThresholds(input)
}
func NormalizeMetricEvaluationWindows(input map[string]map[string]int) map[string]map[string]int {
return alertconfig.NormalizeMetricEvaluationWindows(input)
}
func NormalizeHysteresisThreshold(threshold *HysteresisThreshold, defaultTrigger, defaultClear float64, metricName string) *HysteresisThreshold {
return alertconfig.NormalizeHysteresisThreshold(threshold, defaultTrigger, defaultClear, metricName)
}
func NormalizeDockerIgnoredPrefixes(prefixes []string) []string {
return alertconfig.NormalizeDockerIgnoredPrefixes(prefixes)
}
func NormalizeEscalationDestinationIDs(destinationIDs []string) []string {
return alertconfig.NormalizeEscalationDestinationIDs(destinationIDs)
}
func CanonicalResourceTypeKeys(resourceType string) []string {
return alertconfig.CanonicalResourceTypeKeys(resourceType)
}
func NormalizePoweredOffSeverity(level AlertLevel) AlertLevel {
return alertconfig.NormalizePoweredOffSeverity(level)
}
func NormalizeAlertLevel(level AlertLevel) AlertLevel {
return alertconfig.NormalizeAlertLevel(level)
}
func normalizePoweredOffSeverity(level AlertLevel) AlertLevel {
return alertconfig.NormalizePoweredOffSeverity(level)
}
func ensureValidHysteresis(threshold *HysteresisThreshold, metricName string) {
alertconfig.EnsureValidHysteresis(threshold, metricName)
}
// NormalizeDockerDefaults fills unset Docker container thresholds with their
// factory values and stores an Off count threshold as 0.
func NormalizeDockerDefaults(config *AlertConfig) { alertconfig.NormalizeDockerDefaults(config) }
// NormalizePMGDefaults fills unset PMG thresholds with their factory values
// and stores an Off threshold as 0.
func NormalizePMGDefaults(config *AlertConfig) { alertconfig.NormalizePMGDefaults(config) }
func normalizeSnapshotDefaults(config *AlertConfig) { alertconfig.NormalizeSnapshotDefaults(config) }
func normalizeBackupDefaults(config *AlertConfig) { alertconfig.NormalizeBackupDefaults(config) }
// NormalizeRecoveryOverrides converts per-guest backup/snapshot overrides that
// are exact threshold copies of the current global defaults into sparse
// enabled-only overrides so they track future global edits (#1126).
func NormalizeRecoveryOverrides(config *AlertConfig) {
alertconfig.NormalizeRecoveryOverrides(config)
}
func validateQuietHoursTimezone(config *AlertConfig) { alertconfig.ValidateQuietHoursTimezone(config) }