Skip to content

Lightweight server monitoring with historical data, docker stats, and alerts.

2.8 KB
Raw Blame
//go:build testing

package alerts

import (
	"sync"
	"time"

	"ganigeorgiev.com/pocketbase/pocketbase/core"
)

func NewTestAlertManagerWithoutWorker(app hubLike) *AlertManager {
	am := &AlertManager{
		hub:             app,
		alertsCache:     NewAlertsCache(app),
		networkMonitors: newNetworkMonitorCache(app),
	}
	// Standalone managers can own status timers even without the serve hooks.
	app.OnTerminate().BindFunc(func(e *core.TerminateEvent) error {
		am.Stop()
		return e.Next()
	})
	return am
}

// GetSystemAlertsCache returns the internal system alerts cache.
func (am *AlertManager) GetSystemAlertsCache() *AlertsCache {
	return am.alertsCache
}

func (am *AlertManager) GetAlertManager() *AlertManager {
	return am
}

func (am *AlertManager) GetPendingAlerts() *sync.Map {
	return &am.pendingAlerts
}

func (am *AlertManager) GetPendingAlertsCount() int {
	count := 0
	am.pendingAlerts.Range(func(key, value any) bool {
		count++
		return true
	})
	return count
}

// ProcessPendingAlerts manually processes all expired alerts (for testing)
func (am *AlertManager) ProcessPendingAlerts() ([]CachedAlertData, error) {
	now := time.Now()
	var lastErr error
	var processedAlerts []CachedAlertData
	am.pendingAlerts.Range(func(key, value any) bool {
		info := value.(*alertInfo)
		if now.After(info.expireTime) {
			if info.timer != nil {
				info.timer.Stop()
			}
			am.processPendingAlert(key.(string))
			processedAlerts = append(processedAlerts, info.alertData)
		}
		return true
	})
	return processedAlerts, lastErr
}

// ForceExpirePendingAlerts sets all pending alerts to expire immediately (for testing)
func (am *AlertManager) ForceExpirePendingAlerts() {
	now := time.Now()
	am.pendingAlerts.Range(func(key, value any) bool {
		info := value.(*alertInfo)
		info.expireTime = now.Add(-time.Second) // Set to 1 second ago
		return true
	})
}

func (am *AlertManager) ResetPendingAlertTimer(alertID string, delay time.Duration) bool {
	value, loaded := am.pendingAlerts.Load(alertID)
	if !loaded {
		return false
	}

	info := value.(*alertInfo)
	if info.timer != nil {
		info.timer.Stop()
	}
	info.expireTime = time.Now().Add(delay)
	info.timer = time.AfterFunc(delay, func() {
		am.processPendingAlert(alertID)
	})
	return true
}

func ResolveStatusAlerts(app core.App) error {
	return resolveStatusAlerts(app)
}

func ResolveSystemdAlerts(app core.App) error {
	return resolveSystemdAlerts(app)
}

func (am *AlertManager) RestorePendingStatusAlerts() error {
	return am.restorePendingStatusAlerts()
}

func (am *AlertManager) SetAlertTriggered(alert CachedAlertData, triggered bool) error {
	return am.setAlertTriggered(alert, triggered)
}

// BuildContainerLogExcerpt exposes buildContainerLogExcerpt for testing.
func BuildContainerLogExcerpt(raw string) string {
	return buildContainerLogExcerpt(raw)
}