52 lines
1.8 KiB
Python
52 lines
1.8 KiB
Python
"""Query/formatting helpers for the "Uploaded files" list (project-owner
|
|
follow-up request). Kept separate from routes.py to match this project's
|
|
established per-blueprint queries.py convention (Ch08/09/10).
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from app.models.log_file import LogFile
|
|
|
|
|
|
def get_uploaded_files(page: int, per_page: int) -> tuple[list[list], int]:
|
|
"""Newest first, independent of the dashboard date-range picker —
|
|
this lists uploads by when they arrived, not by which log dates they
|
|
contain (a single file can span many dates).
|
|
"""
|
|
query = LogFile.query.order_by(LogFile.uploaded_at.desc())
|
|
total = query.count()
|
|
rows = query.offset((page - 1) * per_page).limit(per_page).all()
|
|
|
|
result = []
|
|
for lf in rows:
|
|
result.append([
|
|
lf.id,
|
|
lf.filename,
|
|
lf.server_type,
|
|
_status_display(lf),
|
|
lf.uploaded_at.strftime("%Y-%m-%d %H:%M"),
|
|
_human_size(lf.size_bytes),
|
|
lf.status, # raw status (hidden column) — lets the client disable
|
|
# the select checkbox for files still "processing"
|
|
])
|
|
return result, total
|
|
|
|
|
|
def _status_display(lf: LogFile) -> str:
|
|
if lf.status == "processing":
|
|
if lf.total_lines:
|
|
pct = round(lf.processed_lines / lf.total_lines * 100)
|
|
return f"processing ({pct}%)"
|
|
return "processing"
|
|
if lf.status == "error":
|
|
return f"error: {lf.error_message}" if lf.error_message else "error"
|
|
return lf.status
|
|
|
|
|
|
def _human_size(num_bytes: int) -> str:
|
|
size = float(num_bytes)
|
|
for unit in ("B", "KB", "MB", "GB"):
|
|
if size < 1024 or unit == "GB":
|
|
return f"{size:.0f} {unit}" if unit == "B" else f"{size:.1f} {unit}"
|
|
size /= 1024
|
|
return f"{size:.1f} GB"
|