Dashboard detail, library filtering/pagination, simplified playlist times,
job descriptions Dashboard: format breakdown (FLAC/MP3/etc. counts), approximate library size (bitrate*length/8, same approximation `beet stats` itself uses -- verified to match its "100.5 GiB" output exactly against the real library), and a credential auth-state summary per scope (configured y/n plus, for Bandcamp, cookie expiry parsed locally from the stored cookie jar -- deliberately not a live network probe like pipeline-status.sh's Qobuz check, since this renders on every dashboard load). Library: was one unfiltered page dumping all 4072 tracks. Added search (artist/title/album), playlist and format filter dropdowns, and real SQL-level pagination (LIMIT/OFFSET, not fetch-everything-then-slice). beets_service gained count_items()/distinct_formats() to support this. Playlists: cron_expr is still the stored/scheduled representation, but the UI now shows and edits a plain daily time picker instead of raw cron syntax -- every playlist schedule today is a simple daily HH:MM anyway. playlist_service.cron_to_time()/time_to_cron() convert at the router boundary; verified round-trip against all real playlist cron values. Jobs: each maintenance job now shows a short one-line description of what it actually does (MAINTENANCE_JOB_DESCRIPTIONS), and "next run" is formatted consistently with playlists' time style (HH:MM, with a day qualifier for non-today runs -- maintenance jobs can be weekly/monthly, unlike playlists' plain daily schedule). Verified end-to-end against the real production data (4072 tracks, 100.5 GiB, real credentials): stats/format-breakdown/search/pagination all correct, all 7 credential scopes report configured with Bandcamp's real cookie expiry (325 days), and a full authenticated page-render sweep (dashboard, library with every filter combination, playlist detail, jobs) all returned 200 with no template errors.
This commit is contained in:
@@ -34,22 +34,74 @@ def _row_to_dict(row: sqlite3.Row) -> dict:
|
||||
return d
|
||||
|
||||
|
||||
def query_items(grouping: str | None = None) -> list[dict]:
|
||||
"""All items, optionally filtered to one playlist's grouping tag."""
|
||||
def query_items(
|
||||
grouping: str | None = None,
|
||||
search: str | None = None,
|
||||
format: str | None = None,
|
||||
limit: int | None = None,
|
||||
offset: int = 0,
|
||||
) -> list[dict]:
|
||||
"""Items, optionally filtered by playlist grouping, a free-text search
|
||||
over artist/title/album, and/or exact format match. limit/offset give
|
||||
SQL-level pagination -- the library has thousands of tracks, so this
|
||||
must not be a fetch-everything-then-slice-in-Python operation."""
|
||||
if not db_exists():
|
||||
return []
|
||||
cols = ", ".join(_ITEM_COLUMNS)
|
||||
where = []
|
||||
params: list = []
|
||||
if grouping is not None:
|
||||
where.append("grouping = ?")
|
||||
params.append(grouping)
|
||||
if format is not None:
|
||||
where.append("format = ?")
|
||||
params.append(format)
|
||||
if search:
|
||||
where.append("(artist LIKE ? OR title LIKE ? OR albumartist LIKE ?)")
|
||||
like = f"%{search}%"
|
||||
params.extend([like, like, like])
|
||||
where_sql = f"WHERE {' AND '.join(where)}" if where else ""
|
||||
limit_sql = ""
|
||||
if limit is not None:
|
||||
limit_sql = "LIMIT ? OFFSET ?"
|
||||
params.extend([limit, offset])
|
||||
|
||||
conn = _connect()
|
||||
try:
|
||||
if grouping is not None:
|
||||
cur = conn.execute(f"SELECT {cols} FROM items WHERE grouping = ?", (grouping,))
|
||||
else:
|
||||
cur = conn.execute(f"SELECT {cols} FROM items")
|
||||
cur = conn.execute(
|
||||
f"SELECT {cols} FROM items {where_sql} ORDER BY artist, album, track {limit_sql}",
|
||||
params,
|
||||
)
|
||||
return [_row_to_dict(row) for row in cur.fetchall()]
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
def count_items(grouping: str | None = None, search: str | None = None, format: str | None = None) -> int:
|
||||
"""Matching row count for query_items()'s filters -- for pagination."""
|
||||
if not db_exists():
|
||||
return 0
|
||||
where = []
|
||||
params: list = []
|
||||
if grouping is not None:
|
||||
where.append("grouping = ?")
|
||||
params.append(grouping)
|
||||
if format is not None:
|
||||
where.append("format = ?")
|
||||
params.append(format)
|
||||
if search:
|
||||
where.append("(artist LIKE ? OR title LIKE ? OR albumartist LIKE ?)")
|
||||
like = f"%{search}%"
|
||||
params.extend([like, like, like])
|
||||
where_sql = f"WHERE {' AND '.join(where)}" if where else ""
|
||||
|
||||
conn = _connect()
|
||||
try:
|
||||
return conn.execute(f"SELECT COUNT(*) FROM items {where_sql}", params).fetchone()[0]
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
def get_item(item_id: int) -> dict | None:
|
||||
if not db_exists():
|
||||
return None
|
||||
@@ -63,20 +115,55 @@ def get_item(item_id: int) -> dict | None:
|
||||
|
||||
|
||||
def stats() -> dict:
|
||||
"""Cheap summary for the dashboard and for migration-verification
|
||||
(compare against `beet stats` during Stage 0 cutover)."""
|
||||
"""Summary for the dashboard: total tracks, format breakdown, storage
|
||||
estimate (bitrate*length/8, same approximation `beet stats` itself
|
||||
uses -- not a filesystem stat() pass over every file), and groupings."""
|
||||
if not db_exists():
|
||||
return {"db_exists": False, "total_tracks": 0, "groupings": []}
|
||||
return {
|
||||
"db_exists": False,
|
||||
"total_tracks": 0,
|
||||
"groupings": [],
|
||||
"formats": [],
|
||||
"total_bytes": 0,
|
||||
}
|
||||
conn = _connect()
|
||||
try:
|
||||
total = conn.execute("SELECT COUNT(*) FROM items").fetchone()[0]
|
||||
rows = conn.execute(
|
||||
|
||||
grouping_rows = conn.execute(
|
||||
"SELECT DISTINCT grouping FROM items WHERE grouping IS NOT NULL AND grouping != ''"
|
||||
).fetchall()
|
||||
|
||||
format_rows = conn.execute(
|
||||
"SELECT format, COUNT(*) as n FROM items "
|
||||
"WHERE format IS NOT NULL AND format != '' "
|
||||
"GROUP BY format ORDER BY n DESC"
|
||||
).fetchall()
|
||||
|
||||
total_bytes = conn.execute(
|
||||
"SELECT SUM(length * bitrate) / 8 FROM items WHERE length IS NOT NULL AND bitrate IS NOT NULL"
|
||||
).fetchone()[0] or 0
|
||||
|
||||
return {
|
||||
"db_exists": True,
|
||||
"total_tracks": total,
|
||||
"groupings": sorted(r[0] for r in rows),
|
||||
"groupings": sorted(r[0] for r in grouping_rows),
|
||||
"formats": [{"format": r["format"], "count": r["n"]} for r in format_rows],
|
||||
"total_bytes": int(total_bytes),
|
||||
}
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
def distinct_formats() -> list[str]:
|
||||
"""For the library page's format filter dropdown."""
|
||||
if not db_exists():
|
||||
return []
|
||||
conn = _connect()
|
||||
try:
|
||||
rows = conn.execute(
|
||||
"SELECT DISTINCT format FROM items WHERE format IS NOT NULL AND format != '' ORDER BY format"
|
||||
).fetchall()
|
||||
return [r[0] for r in rows]
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
Reference in New Issue
Block a user