History imports: parsers, import jobs and the import worker
CI / validate-monitoring (push) Successful in 7s
CI / lint (push) Successful in 10s
CI / test (push) Successful in 41s
CI / python-package (push) Skipped
CI / assets (push) Successful in 8s
CI / images (push) Successful in 28s
CI / notify (push) Successful in 7s

- Import jobs (import_jobs table): uploaded exports stored gzipped,
  processed by `flask imports worker`, which claims jobs with
  SKIP LOCKED, inserts in chunks with progress, supports cancelling and
  re-queues jobs whose worker died
- CSV: lastfm-to-csv (no header) or any header-labelled CSV/TSV with
  comma, semicolon or tab delimiters; several date formats and Unix
  seconds/milliseconds
- JSON: pages of user.getRecentTracks (e.g. lastfm.ghan.nl), a single
  recenttracks response, or a flat list; gzipped files accepted
- Last.fm API pull (only when LASTFM_API_KEY is set): pages through
  user.getRecentTracks up to the job's creation time, rate limited,
  with retries and resume
- Imports keep plays older than 14 days, skip empty/future ones and
  dedupe against existing history
- Import metrics and pending/running job gauges

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MK52BGoJbPhZ1vSFuKcGUW
This commit is contained in:
mattandClaude Opus 5.5 committed 2026-09-25 15:02:31 +01:00
1 parent e29abe05dd
commit 6fc9d91fa2
9 files changed
+1004 -1

No files matched your search

+31
View File
@@ -5,6 +5,7 @@ from sqlalchemy import (
ForeignKey,
Index,
Integer,
LargeBinary,
String,
Text,
UniqueConstraint,
@@ -126,3 +127,33 @@ class NowPlaying(db.Model):
duration: Mapped[int | None] = mapped_column(Integer)
started_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utcnow)
expires_at: Mapped[datetime] = mapped_column(DateTime(timezone=True))
class ImportJob(db.Model):
"""A history import: an uploaded export file, or a pull from the Last.fm API.
Created by the API and processed by the import worker (`flask imports worker`).
"""
__tablename__ = "import_jobs"
id: Mapped[int] = mapped_column(primary_key=True)
user_id: Mapped[int] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"), index=True)
source: Mapped[str] = mapped_column(String(16)) # csv, json or lastfm
status: Mapped[str] = mapped_column(String(16), default="pending", index=True)
filename: Mapped[str | None] = mapped_column(String(255))
lastfm_username: Mapped[str | None] = mapped_column(String(64))
payload: Mapped[bytes | None] = mapped_column(LargeBinary, deferred=True) # gzipped upload
total: Mapped[int | None] = mapped_column(Integer) # rows or tracks, once known
processed: Mapped[int] = mapped_column(Integer, default=0)
imported: Mapped[int] = mapped_column(Integer, default=0)
duplicates: Mapped[int] = mapped_column(Integer, default=0)
skipped: Mapped[int] = mapped_column(Integer, default=0)
error: Mapped[str | None] = mapped_column(Text)
# Last.fm pulls resume from here, and only fetch scrobbles before pull_until.
next_page: Mapped[int | None] = mapped_column(Integer)
pull_until: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utcnow)
started_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
finished_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
heartbeat_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))