This commit is contained in:
ponzischeme89
2026-08-19 14:25:44 +12:00
parent 2b43b9ef12
commit 590e069366
83 changed files with 8948 additions and 1266 deletions
+34
View File
@@ -223,6 +223,40 @@ func (s *Store) MediaRatingsStats(ctx context.Context, staleBefore time.Time) (t
return total, stale, nil
}
// StaleRatingKeys is the oldest stored titles due to be re-checked, oldest first.
//
// Oldest first rather than by any measure of popularity, because the refresh is bounded
// per run: taking the oldest means every title comes round eventually, where taking the
// most-watched would leave the tail of the library permanently on scores from the year it
// was imported. The limit is what keeps a run's cost — and therefore the day's external
// allowance — a figure the operator can reason about.
func (s *Store) StaleRatingKeys(
ctx context.Context, staleBefore time.Time, limit int,
) ([]RatingKey, error) {
if limit <= 0 || limit > 500 {
limit = 100
}
rows, err := s.pool.Query(ctx, `
SELECT media_type, provider, provider_id
FROM external_media_ratings
WHERE fetched_at < $1
ORDER BY fetched_at ASC
LIMIT $2`, staleBefore, limit)
if err != nil {
return nil, fmt.Errorf("store: stale rating keys: %w", err)
}
defer rows.Close()
keys := []RatingKey{}
for rows.Next() {
var key RatingKey
if err := rows.Scan(&key.MediaType, &key.Provider, &key.ProviderID); err != nil {
return nil, fmt.Errorf("store: scan stale rating key: %w", err)
}
keys = append(keys, key)
}
return keys, rows.Err()
}
// SaveMediaRatings upserts a successfully loaded provider response.
func (s *Store) SaveMediaRatings(
ctx context.Context, mediaType, provider, providerID string, ratings json.RawMessage,