This commit is contained in:
ponzischeme89
2026-08-12 08:25:15 +12:00
parent e30962245e
commit 4b31946635
28 changed files with 439 additions and 130 deletions
+13 -4
View File
@@ -108,9 +108,9 @@ func (s *Store) UserShows(ctx context.Context, userID string) ([]UserShow, error
return shows, rows.Err()
}
// RecordSonarrSeriesStatuses appends only first sightings and changes. A first sighting is
// the baseline and is deliberately absent from the returned changes, so enabling the
// scanner cannot announce every series that was already cancelled before it existed.
// RecordSonarrSeriesStatuses appends first sightings and changes. The first complete scan
// seeds a baseline; a first sighting on later scans is returned with an empty previous
// status so the lifecycle scanner can announce a show newly added to Sonarr.
func (s *Store) RecordSonarrSeriesStatuses(
ctx context.Context, observations []SonarrSeriesStatus,
) ([]SonarrSeriesStatusChange, error) {
@@ -119,6 +119,10 @@ func (s *Store) RecordSonarrSeriesStatuses(
return nil, fmt.Errorf("store: begin Sonarr status history: %w", err)
}
defer tx.Rollback(ctx)
var seeded bool
if err := tx.QueryRow(ctx, `SELECT EXISTS (SELECT 1 FROM sonarr_lifecycle_scan_state)`).Scan(&seeded); err != nil {
return nil, fmt.Errorf("store: read Sonarr lifecycle seed: %w", err)
}
rows, err := tx.Query(ctx, `
SELECT DISTINCT ON (series_key) series_key, status
@@ -172,13 +176,18 @@ func (s *Store) RecordSonarrSeriesStatuses(
if err != nil {
return nil, fmt.Errorf("store: append Sonarr status history: %w", err)
}
if known {
if known || seeded {
changes = append(changes, SonarrSeriesStatusChange{
HistoryID: historyID, PreviousStatus: prior, Current: observation,
})
}
previous[observation.SeriesKey] = observation.Status
}
if !seeded {
if _, err := tx.Exec(ctx, `INSERT INTO sonarr_lifecycle_scan_state (singleton) VALUES (true) ON CONFLICT DO NOTHING`); err != nil {
return nil, fmt.Errorf("store: seed Sonarr lifecycle scan: %w", err)
}
}
if err := tx.Commit(ctx); err != nil {
return nil, fmt.Errorf("store: commit Sonarr status history: %w", err)
}