package alert import ( "context" "fmt" "strconv" "time" "dynatron.me/x/stillbox/internal/trending" "dynatron.me/x/stillbox/pkg/database" "dynatron.me/x/stillbox/pkg/talkgroups" "github.com/google/uuid" "github.com/jackc/pgx/v5/pgtype" ) type Alert struct { ID uuid.UUID Timestamp time.Time TGName string Score trending.Score[talkgroups.ID] OrigScore float64 Weight float32 Suppressed bool } func (a *Alert) ToAddAlertParams() database.AddAlertParams { f32score := float32(a.Score.Score) f32origscore := float32(a.OrigScore) var origScore *float32 if a.Score.Score != a.OrigScore { origScore = &f32origscore } return database.AddAlertParams{ ID: a.ID, Time: pgtype.Timestamptz{Time: a.Timestamp, Valid: true}, SystemID: int(a.Score.ID.System), Tgid: int(a.Score.ID.Talkgroup), Weight: &a.Weight, Score: &f32score, OrigScore: origScore, Notified: !a.Suppressed, } } // Make creates an alert for later rendering or storage. func Make(ctx context.Context, store talkgroups.Store, score trending.Score[talkgroups.ID], origScore float64) (Alert, error) { d := Alert{ ID: uuid.New(), Score: score, Timestamp: time.Now(), Weight: 1.0, OrigScore: origScore, } tgRecord, err := store.TG(ctx, score.ID) switch err { case nil: d.Weight = tgRecord.Talkgroup.Weight if tgRecord.System.Name == "" { tgRecord.System.Name = strconv.Itoa(int(score.ID.System)) } if tgRecord.Talkgroup.Name != nil { d.TGName = fmt.Sprintf("%s %s [%d]", tgRecord.System.Name, *tgRecord.Talkgroup.Name, score.ID.Talkgroup) } else { d.TGName = fmt.Sprintf("%s:%d", tgRecord.System.Name, int(score.ID.Talkgroup)) } default: system, has := store.SystemName(ctx, int(score.ID.System)) if has { d.TGName = fmt.Sprintf("%s:%d", system, int(score.ID.Talkgroup)) } else { d.TGName = fmt.Sprintf("%d:%d", int(score.ID.System), int(score.ID.Talkgroup)) } } return d, nil }