Coverage for downtify/wanted_routes.py: 98%
140 statements
« prev ^ index » next coverage.py v7.17.4, created at 2026-11-21 22:18 +0000
« prev ^ index » next coverage.py v7.17.4, created at 2026-11-21 22:18 +0000
1"""The Wanted list: what the artist watches still miss, across artists.
3``GET /api/monitor/wanted`` gathers, per enabled artist watch (an admin's
4view covers every watch, a user's only their own), the releases the watch
5downloads that the library only partly has or lacks, with the tracks
6missing in total. It reads what the last sweep and "Check what I have"
7kept; nothing is fetched. A release whose tracklist was never read counts
8as unchecked (``checked`` false), even when the catalogue lists a track
9count: it may well be in the library. Only checked releases add to the
10missing tracks. Announced releases the watches download on their release
11day are listed too (state ``upcoming``, soonest first, after the rest);
12they add nothing to the missing tracks.
14``POST .../check-all`` reads the tracklists of the artists with unchecked
15releases, ``POST .../download-all`` downloads what the ones with gaps
16miss - one artist at a time, as one background job for everyone; ``POST
17.../stop`` ends it: a download-all after the artist it is on, a check-all
18at the next release it reads (what was read is kept; the next check-all
19picks the artist up again). ``GET .../job`` is the job alone, light
20enough to poll while it runs.
21"""
23from __future__ import annotations
25import asyncio
26import secrets
27from datetime import datetime, timezone
28from typing import Any
30from fastapi import APIRouter, Depends, HTTPException, Request
31from loguru import logger
33from . import api, artist_catalogue
34from .monitor import (
35 KIND_ARTIST,
36 MonitoredPlaylist,
37 PlaylistMonitorDB,
38 check_running,
39 check_watch,
40 take_inventory,
41)
43router = APIRouter(
44 prefix='/api/monitor/wanted',
45 dependencies=[Depends(api.library_only_guard)],
46)
49def scoped_artist_watches(
50 db: PlaylistMonitorDB, request: Request
51) -> list[MonitoredPlaylist]:
52 """The enabled artist watches *request* may see: all for an admin,
53 their own otherwise."""
55 scope = (
56 None if api.request_is_admin(request) else api.request_user_id(request)
57 )
58 return [
59 watch
60 for watch in db.list_playlists(scope)
61 if watch.kind == KIND_ARTIST and watch.enabled
62 ]
65def wanted_view(
66 db: PlaylistMonitorDB, watches: list[MonitoredPlaylist]
67) -> dict[str, Any]:
68 """The wanted releases of *watches*, grouped by artist (by name).
70 Each release says whether its tracklist was read (``checked``); an
71 artist's ``unchecked`` counts those that weren't, and its ``missing``
72 counts the tracks (and releases) of the checked ones only, plus
73 ``unread`` as the artist page reports it. The library is read once
74 for all of them. An artist with nothing wanted and nothing unread is
75 left out."""
77 artists: list[dict[str, Any]] = []
78 if watches:
79 index = api._library_songs()
80 for watch in watches:
81 view = api._artist_releases(db, watch, songs=index)
82 kept = db.catalogue.tracks(watch.id)
83 releases = [
84 {**r, 'checked': any(a in kept for a in r['album_ids'])}
85 for r in artist_catalogue.wanted_releases(view['releases'])
86 ]
87 coming = [
88 {**r, 'checked': True}
89 for r in artist_catalogue.upcoming_releases(view['releases'])
90 ]
91 missing = {
92 **view['missing'],
93 **artist_catalogue.missing([
94 r
95 for r in view['releases']
96 if r.get('wanted')
97 and any(a in kept for a in r['album_ids'])
98 ]),
99 'unread': view['missing']['unread'],
100 }
101 if not releases and not coming and not missing['unread']:
102 continue
103 artists.append({
104 'watch': view['watch'],
105 'releases': releases + coming,
106 'upcoming': len(coming),
107 'missing': missing,
108 'unchecked': sum(1 for r in releases if not r['checked']),
109 'checking': bool(view['checking']),
110 })
111 artists.sort(key=lambda a: str(a['watch']['name']).casefold())
112 return {
113 'artists': artists,
114 'totals': {
115 'tracks': sum(a['missing']['tracks'] for a in artists),
116 'releases': sum(a['missing']['releases'] for a in artists),
117 'artists': len(artists),
118 'unchecked': sum(1 for a in artists if a['unchecked']),
119 'unchecked_releases': sum(a['unchecked'] for a in artists),
120 'upcoming': sum(a['upcoming'] for a in artists),
121 },
122 }
125#: The one job (``{}`` until the first): ``id, kind, running, stopping,
126#: done, total, current, failed, skipped, stopped, started_at,
127#: finished_at`` and, kept private, ``cancel``, ``user_id`` and
128#: ``watch_ids``.
129_job: dict[str, Any] = {}
131_PRIVATE = ('cancel', 'user_id', 'watch_ids')
134def _now() -> str:
135 return datetime.now(timezone.utc).isoformat()
138def public_job(job: dict[str, Any]) -> dict[str, Any] | None:
139 """*job* as the page sees it (``None`` when there was none)."""
141 if not job:
142 return None
143 return {k: v for k, v in job.items() if k not in _PRIVATE}
146def _visible_job(request: Request) -> dict[str, Any] | None:
147 """The job, only for whoever started it and for admins."""
149 if not _job:
150 return None
151 if api.request_is_admin(request) or _job['user_id'] == (
152 api.request_user_id(request)
153 ):
154 return _job
155 return None
158async def _check_one(
159 job: dict[str, Any], watch: MonitoredPlaylist, db: PlaylistMonitorDB
160) -> bool:
161 """Read the unread tracklists of *watch*, mirroring the progress into
162 the artist's own "Check what I have" state. ``False``: skipped."""
164 if (api._inventories.get(watch.id) or {}).get('running'):
165 return False
166 state: dict[str, Any] = {
167 'running': True,
168 'done': 0,
169 'total': 0,
170 'checked': 0,
171 'failed': 0,
172 'started_at': _now(),
173 'finished_at': '',
174 }
175 api._inventories[watch.id] = state
177 def progress(done: int, total: int) -> None:
178 state.update(done=done, total=total)
180 try:
181 state.update(
182 await asyncio.to_thread(
183 take_inventory,
184 watch,
185 db,
186 progress=progress,
187 stop=lambda: bool(job['cancel']),
188 )
189 )
190 except Exception:
191 state['error'] = True
192 raise
193 finally:
194 state.update(running=False, finished_at=_now())
195 return True
198async def _download_one(
199 watch: MonitoredPlaylist, db: PlaylistMonitorDB
200) -> bool:
201 """Download what *watch* misses. ``False``: it is being checked."""
203 if check_running(watch.id):
204 return False
205 loop = api.state.loop or asyncio.get_running_loop()
206 await check_watch(
207 watch,
208 db,
209 api.state.downloader, # type: ignore[arg-type]
210 api.state.connections.broadcast,
211 loop,
212 api.state.settings,
213 api.library_stores(),
214 api.state.podcasts,
215 fill_missing=True,
216 )
217 return True
220async def _work(job: dict[str, Any], db: PlaylistMonitorDB) -> None:
221 """Go through the job's artists, one at a time."""
223 try:
224 for watch_id in job['watch_ids']:
225 if job['cancel']:
226 break
227 name = f'#{watch_id}'
228 try:
229 watch = await asyncio.to_thread(db.get_playlist, watch_id)
230 if job['cancel']: # Stop landed during the re-read
231 break
232 if watch is None or not watch.enabled:
233 job['skipped'] += 1
234 job['done'] += 1
235 continue
236 name = job['current'] = watch.name
237 if job['kind'] == 'check':
238 worked = await _check_one(job, watch, db)
239 else:
240 worked = await _download_one(watch, db)
241 if not worked:
242 job['skipped'] += 1
243 except Exception:
244 logger.opt(exception=True).warning(
245 'Wanted: {} failed for "{}"', job['kind'], name
246 )
247 job['failed'] += 1
248 job['done'] += 1
249 except asyncio.CancelledError:
250 # Shutting down: the inventory thread stops at its next release.
251 job['cancel'] = True
252 raise
253 finally:
254 job.update(
255 running=False,
256 stopping=False,
257 stopped=job['stopped'] or bool(job['cancel']),
258 current='',
259 finished_at=_now(),
260 )
263async def _start(kind: str, request: Request) -> dict[str, Any]:
264 db = api._require_monitor_db()
265 if _job.get('running'):
266 raise HTTPException(409, detail={'code': 'wanted_busy'})
267 if kind == 'download' and api.state.downloader is None:
268 raise HTTPException(status_code=500, detail='Downloader not ready')
269 watches = await asyncio.to_thread(scoped_artist_watches, db, request)
270 view = await asyncio.to_thread(wanted_view, db, watches)
271 if kind == 'check':
272 picked = [a for a in view['artists'] if a['unchecked'] > 0]
273 else:
274 picked = [
275 a
276 for a in view['artists']
277 if a['missing']['tracks'] > 0 or a['unchecked'] > 0
278 ]
279 if _job.get('running'): # another start got in while the view was read
280 raise HTTPException(409, detail={'code': 'wanted_busy'})
281 job: dict[str, Any] = {
282 'id': secrets.token_urlsafe(8),
283 'kind': kind,
284 'running': True,
285 'stopping': False,
286 'done': 0,
287 'total': len(picked),
288 'current': '',
289 'failed': 0,
290 'skipped': 0,
291 'stopped': False,
292 'started_at': _now(),
293 'finished_at': '',
294 'cancel': False,
295 'user_id': api.request_user_id(request),
296 'watch_ids': [a['watch']['id'] for a in picked],
297 }
298 # _job is the live record the worker updates; a refused spawn
299 # (library-only) puts the previous one back.
300 before = dict(_job)
301 _job.clear()
302 _job.update(job)
303 try:
304 api.spawn_task(_work(_job, db), name=f'monitor-wanted-{kind}')
305 except BaseException:
306 _job.clear()
307 _job.update(before)
308 raise
309 return public_job(_job) # type: ignore[return-value]
312@router.post('/check-all')
313async def check_all(request: Request) -> dict[str, Any]:
314 """Read the tracklists of every artist with unchecked releases."""
316 return await _start('check', request)
319@router.post('/download-all')
320async def download_all(request: Request) -> dict[str, Any]:
321 """Download what every artist with gaps (or unchecked releases)
322 misses."""
324 return await _start('download', request)
327@router.post('/stop')
328async def stop(request: Request) -> dict[str, Any] | None:
329 """Stop the job: a download-all after the artist it is on, a check-all
330 at the next release it reads. The job as the page sees it (``stopping``
331 while it winds down)."""
333 job = _visible_job(request)
334 if job is None and _job:
335 raise HTTPException(404, detail={'code': 'no_job'})
336 if job is not None and job['running']:
337 job['cancel'] = True
338 job['stopping'] = True
339 return public_job(job) if job is not None else None
342@router.get('/job')
343async def wanted_job(request: Request) -> dict[str, Any]:
344 """Just the job (``{'job': None}`` when there is none, or it is
345 someone else's), for polling while it runs."""
347 job = _visible_job(request)
348 return {'job': public_job(job) if job is not None else None}
351@router.get('')
352async def wanted(request: Request) -> dict[str, Any]:
353 """What the artist watches still miss; ``job`` is the running or last
354 check-all / download-all, to its starter and admins (``None``
355 otherwise)."""
357 db = api._require_monitor_db()
358 watches = await asyncio.to_thread(scoped_artist_watches, db, request)
359 view = await asyncio.to_thread(wanted_view, db, watches)
360 job = _visible_job(request)
361 return {**view, 'job': public_job(job) if job is not None else None}