9322c984fd
Replaces the three top-level routes with a single `/subscriptions` parent
owning the whole download-pipeline domain. Internal tab state via `?tab=`
query param, mirroring ArtistView's pattern. TopNav auto-drops the two
removed entries (route-driven via meta.title). Bookmark-safe redirects
from `/credentials` and `/downloads` route into the appropriate subtab.
**Subtab 1 — Subscriptions (default).** Carries over the existing
artist-grouped expandable table; adds (a) status filter dropdown, (b)
bulk-select column with Enable/Disable/Delete-all actions, (c) GS-style
color-coded `PlatformChip` per distinct platform in the collapsed row.
Reuses SourceRow, SourceHealthDot, SourceFormDialog, ArtistCreateDialog.
**Subtab 2 — Downloads.** Full GS dashboard. Five colored stat chips up
top (Queued/Running/Completed/Failed/Skipped, sourced from new
`GET /api/downloads/stats?window_hours=`). Popover-style filter UI
(Status/Source/FromDate/ToDate) with active-filter pills below.
Maintenance menu wraps existing /api/import/retry-failed and
/api/import/clear-stuck endpoints; Export-failed-logs item disabled with
a "v2" tooltip. Per-row Retry preserved via existing DownloadEventRow.
**Subtab 3 — Settings.** Four sections: ExtensionKeyBar (top), GS-style
per-platform CredentialCard grid (md=6 v-row/v-col, dashed border if
unset / accent border if set, expandable how-to panel), Downloader card
(rate limit, validate_files), Schedule defaults card (default interval,
event retention, failure warning threshold). The Downloader and Schedule
sections were extracted out of components/settings/ImportFiltersForm.vue
— SettingsView's Import tab now owns only image-import filters.
**Backend:** new `GET /api/downloads/stats` returns
{pending, running, ok, error, skipped} count grouped by status over the
configurable window. Status keys stay raw from the ENUM; UI does the
display-label mapping. Two integration tests pin the response shape +
window_hours validation.
**Util:** `frontend/src/utils/platformColor.js` — single source of truth
for the six platforms' color + icon + label, mirroring GS's palette
(patreon=red mdi-patreon, subscribestar=amber mdi-star,
hentaifoundry=purple mdi-palette, discord=indigo mdi-discord,
pixiv=blue mdi-alpha-p-box, deviantart=green mdi-deviantart). Unknown
platform falls back to grey + mdi-web.
Deferred (explicit non-goals): subscription import/export, "Trigger Due
Now" scheduler-tick button (needs new backend endpoint), Export Failed
Logs CSV dump.
142 lines
5.4 KiB
Python
142 lines
5.4 KiB
Python
"""FC-3c: GET /api/downloads (list) + GET /api/downloads/<id> (detail).
|
|
|
|
List view: keyset-paginated, newest first, optional filtering by
|
|
status/source/artist. Returns slim records.
|
|
Detail view: full DownloadEvent including the metadata JSONB.
|
|
"""
|
|
|
|
from datetime import datetime, timedelta, timezone
|
|
|
|
from quart import Blueprint, jsonify, request
|
|
from sqlalchemy import func, select
|
|
|
|
from ..extensions import get_session
|
|
from ..models import Artist, DownloadEvent, Source
|
|
|
|
downloads_bp = Blueprint("downloads", __name__, url_prefix="/api/downloads")
|
|
|
|
_ALLOWED_STATUSES = frozenset({"pending", "running", "ok", "error", "skipped"})
|
|
|
|
|
|
def _summary_from_metadata(metadata: dict | None) -> dict:
|
|
metadata = metadata or {}
|
|
run_stats = metadata.get("run_stats") or {}
|
|
return {
|
|
"downloaded": int(run_stats.get("downloaded_count", 0)),
|
|
"skipped": int(run_stats.get("skipped_count", 0)),
|
|
"quarantined": int(run_stats.get("quarantined_count", 0)),
|
|
"duration_seconds": metadata.get("duration_seconds"),
|
|
"error_type": metadata.get("error_type"),
|
|
}
|
|
|
|
|
|
def _list_record(event: DownloadEvent, source: Source | None, artist: Artist | None) -> dict:
|
|
return {
|
|
"id": event.id,
|
|
"source_id": event.source_id,
|
|
"artist_name": artist.name if artist else None,
|
|
"artist_slug": artist.slug if artist else None,
|
|
"platform": source.platform if source else None,
|
|
"status": event.status,
|
|
"started_at": event.started_at.isoformat() if event.started_at else None,
|
|
"finished_at": event.finished_at.isoformat() if event.finished_at else None,
|
|
"files_count": event.files_count,
|
|
"bytes_downloaded": event.bytes_downloaded,
|
|
"error": event.error,
|
|
"summary": _summary_from_metadata(event.metadata_),
|
|
}
|
|
|
|
|
|
def _detail_record(event: DownloadEvent, source: Source | None, artist: Artist | None) -> dict:
|
|
base = _list_record(event, source, artist)
|
|
base["metadata"] = event.metadata_ or {}
|
|
return base
|
|
|
|
|
|
@downloads_bp.route("", methods=["GET"])
|
|
async def list_downloads():
|
|
status = request.args.get("status")
|
|
if status and status not in _ALLOWED_STATUSES:
|
|
return jsonify({"error": "invalid_status"}), 400
|
|
try:
|
|
limit = int(request.args.get("limit", "50"))
|
|
except ValueError:
|
|
return jsonify({"error": "invalid_limit"}), 400
|
|
limit = max(1, min(200, limit))
|
|
before_raw = request.args.get("before")
|
|
before = None
|
|
if before_raw is not None:
|
|
try:
|
|
before = int(before_raw)
|
|
except ValueError:
|
|
return jsonify({"error": "invalid_before"}), 400
|
|
try:
|
|
source_id = int(request.args["source_id"]) if "source_id" in request.args else None
|
|
artist_id = int(request.args["artist_id"]) if "artist_id" in request.args else None
|
|
except (ValueError, KeyError):
|
|
return jsonify({"error": "invalid_filter"}), 400
|
|
|
|
async with get_session() as session:
|
|
stmt = (
|
|
select(DownloadEvent, Source, Artist)
|
|
.outerjoin(Source, Source.id == DownloadEvent.source_id)
|
|
.outerjoin(Artist, Artist.id == Source.artist_id)
|
|
.order_by(DownloadEvent.id.desc())
|
|
.limit(limit)
|
|
)
|
|
if status:
|
|
stmt = stmt.where(DownloadEvent.status == status)
|
|
if source_id is not None:
|
|
stmt = stmt.where(DownloadEvent.source_id == source_id)
|
|
if artist_id is not None:
|
|
stmt = stmt.where(Source.artist_id == artist_id)
|
|
if before is not None:
|
|
stmt = stmt.where(DownloadEvent.id < before)
|
|
rows = (await session.execute(stmt)).all()
|
|
|
|
return jsonify([_list_record(e, s, a) for e, s, a in rows])
|
|
|
|
|
|
@downloads_bp.route("/stats", methods=["GET"])
|
|
async def downloads_stats():
|
|
"""Status-grouped count over download_event for the dashboard stat chips.
|
|
|
|
`?window_hours=` (default 24) bounds by `started_at`. The full set of
|
|
statuses is always present in the response (zero for missing) so the
|
|
UI doesn't have to fill in defaults.
|
|
"""
|
|
try:
|
|
window_hours = int(request.args.get("window_hours", "24"))
|
|
except ValueError:
|
|
return jsonify({"error": "invalid_window_hours"}), 400
|
|
if window_hours < 1 or window_hours > 24 * 365:
|
|
return jsonify({"error": "invalid_window_hours"}), 400
|
|
|
|
since = datetime.now(timezone.utc) - timedelta(hours=window_hours)
|
|
out = {"pending": 0, "running": 0, "ok": 0, "error": 0, "skipped": 0}
|
|
async with get_session() as session:
|
|
stmt = (
|
|
select(DownloadEvent.status, func.count())
|
|
.where(DownloadEvent.started_at >= since)
|
|
.group_by(DownloadEvent.status)
|
|
)
|
|
for status, n in (await session.execute(stmt)).all():
|
|
if status in out:
|
|
out[status] = int(n)
|
|
return jsonify(out)
|
|
|
|
|
|
@downloads_bp.route("/<int:event_id>", methods=["GET"])
|
|
async def get_download(event_id: int):
|
|
async with get_session() as session:
|
|
row = (await session.execute(
|
|
select(DownloadEvent, Source, Artist)
|
|
.outerjoin(Source, Source.id == DownloadEvent.source_id)
|
|
.outerjoin(Artist, Artist.id == Source.artist_id)
|
|
.where(DownloadEvent.id == event_id)
|
|
)).first()
|
|
if row is None:
|
|
return jsonify({"error": "not_found"}), 404
|
|
event, source, artist = row
|
|
return jsonify(_detail_record(event, source, artist))
|