package notify import ( "sync" "time" ) // maxAlertHistory is the maximum number of alerts to retain. const maxAlertHistory = 100 // AlertEntry represents a single notification that was sent. type AlertEntry struct { Timestamp time.Time Title string Message string Priority string } // AlertHistory is a thread-safe ring buffer that stores // the most recent alerts. type AlertHistory struct { mu sync.RWMutex entries [maxAlertHistory]AlertEntry count int index int } // NewAlertHistory creates a new empty AlertHistory. func NewAlertHistory() *AlertHistory { return &AlertHistory{} } // Add records a new alert entry in the ring buffer. func (h *AlertHistory) Add(entry AlertEntry) { h.mu.Lock() defer h.mu.Unlock() h.entries[h.index] = entry h.index = (h.index + 1) % maxAlertHistory if h.count < maxAlertHistory { h.count++ } } // Recent returns the stored alerts in reverse chronological // order (newest first). Returns at most maxAlertHistory entries. func (h *AlertHistory) Recent() []AlertEntry { h.mu.RLock() defer h.mu.RUnlock() result := make([]AlertEntry, h.count) for i := range h.count { // Walk backwards from the most recent entry. idx := (h.index - 1 - i + maxAlertHistory) % maxAlertHistory result[i] = h.entries[idx] } return result }