Skip to content

Commit 3d42491

Browse files
committed
fix alerting escalation
1 parent 21b0503 commit 3d42491

2 files changed

Lines changed: 42 additions & 2 deletions

File tree

apps/flowlord/cache/sqlite.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,38 @@ func (s *SQLite) GetAlertsByDate(date time.Time) ([]AlertRecord, error) {
518518
return alerts, nil
519519
}
520520

521+
// GetAlertsAfterTime retrieves all alerts created after a specific time
522+
func (s *SQLite) GetAlertsAfterTime(afterTime time.Time) ([]AlertRecord, error) {
523+
s.mu.Lock()
524+
defer s.mu.Unlock()
525+
526+
query := `SELECT id, task_id, task_time, task_type, job, msg, created_at
527+
FROM alert_records
528+
WHERE created_at > ?
529+
ORDER BY created_at ASC`
530+
531+
rows, err := s.db.Query(query, afterTime.Format("2006-01-02 15:04:05"))
532+
if err != nil {
533+
return nil, err
534+
}
535+
defer rows.Close()
536+
537+
var alerts []AlertRecord
538+
for rows.Next() {
539+
var alert AlertRecord
540+
err := rows.Scan(
541+
&alert.ID, &alert.TaskID, &alert.TaskTime, &alert.Type,
542+
&alert.Job, &alert.Msg, &alert.CreatedAt,
543+
)
544+
if err != nil {
545+
continue
546+
}
547+
alerts = append(alerts, alert)
548+
}
549+
550+
return alerts, nil
551+
}
552+
521553
// BuildCompactSummary processes alerts in memory to create compact summary
522554
// Groups by TaskType:Job and collects task times for proper date formatting
523555
func BuildCompactSummary(alerts []AlertRecord) []SummaryLine {

apps/flowlord/taskmaster.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,11 @@ func (tm *taskMaster) readFiles(ctx context.Context) {
459459
func (tm *taskMaster) handleNotifications(taskChan chan task.Task, ctx context.Context) {
460460
sendChan := make(chan struct{})
461461
var alerts []cache.AlertRecord
462+
463+
// Initialize lastAlertTime to today at 00:00:00 (zero hour)
464+
now := time.Now()
465+
lastAlertTime := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
466+
462467
go func() {
463468
dur := tm.slack.MinFrequency
464469
for ; ; time.Sleep(dur) {
@@ -467,20 +472,23 @@ func (tm *taskMaster) handleNotifications(taskChan chan task.Task, ctx context.C
467472
// Check for incomplete tasks and add them to alerts
468473
tm.taskCache.CheckIncompleteTasks()
469474

470-
// Get all alerts (including newly added incomplete task alerts)
471-
alerts, err = tm.taskCache.GetAlertsByDate(time.Now())
475+
// Get NEW alerts only - those after the last time we sent
476+
alerts, err = tm.taskCache.GetAlertsAfterTime(lastAlertTime)
472477
if err != nil {
473478
log.Printf("failed to retrieve alerts: %v", err)
474479
continue
475480
}
476481

477482
if len(alerts) > 0 {
478483
sendChan <- struct{}{}
484+
// Update lastAlertTime to now (before we send, so we don't miss any)
485+
lastAlertTime = time.Now()
479486
if dur *= 2; dur > tm.slack.MaxFrequency {
480487
dur = tm.slack.MaxFrequency
481488
}
482489
log.Println("wait time ", dur)
483490
} else if dur != tm.slack.MinFrequency {
491+
// No NEW alerts - reset to minimum frequency
484492
dur = tm.slack.MinFrequency
485493
log.Println("Reset ", dur)
486494
}

0 commit comments

Comments
 (0)