import html
from fastapi import APIRouter, Depends, HTTPException, Request
from fastapi.responses import HTMLResponse
from app.auth import require_user
from app.db.apps import get_deployment, get_deployments
from app.templates.layout import page
router = APIRouter()
def render_status_pill(status: str | None) -> str:
status_value = status or ""
normalized = status_value.strip().lower()
if normalized in {"ok", "success", "succeeded", "done", "deployed", "completed"}:
class_name = "pill pill-success"
elif normalized in {"running", "pending", "queued", "in_progress", "starting"}:
class_name = "pill pill-warning"
elif normalized in {"failed", "failure", "error", "cancelled", "canceled"}:
class_name = "pill pill-danger"
else:
class_name = "pill pill-muted"
return f'{html.escape(status_value)}'
@router.get("/deployments", response_class=HTMLResponse)
async def deployments_page(request: Request, user=Depends(require_user)):
deployments = get_deployments()
rows = ""
for deployment in deployments:
deployment_id = html.escape(str(deployment.get("id", "")))
app_id = html.escape(deployment.get("app_id", "") or "")
kind = html.escape(deployment.get("kind", "") or "")
status = render_status_pill(deployment.get("status"))
started_at = html.escape(deployment.get("started_at", "") or "")
finished_at = html.escape(deployment.get("finished_at", "") or "")
returncode = deployment.get("returncode")
returncode_label = "" if returncode is None else html.escape(str(returncode))
rows += f"""
#{deployment_id}
{started_at}
|
{app_id} |
{kind} |
{status} |
{returncode_label} |
{finished_at} |
Detail
|
"""
if not rows:
rows = '| Zatím nejsou evidovaná žádná nasazení. |
'
return page(
"Nasazení",
f"""
Historie nasazení
Přehled posledních běhů nasazení a jejich výsledků.
← Zpět na portál
Nasazení
| ID |
Aplikace |
Typ |
Status |
Kód |
Dokončeno |
Akce |
{rows}
""",
user=user,
)
@router.get("/deployments/{deployment_id}", response_class=HTMLResponse)
async def deployment_detail_page(
deployment_id: int,
request: Request,
user=Depends(require_user),
):
deployment = get_deployment(deployment_id)
if not deployment:
raise HTTPException(status_code=404, detail="Deployment not found")
title = f"Nasazení #{html.escape(str(deployment.get('id', deployment_id)))}"
app_id = html.escape(deployment.get("app_id", "") or "")
kind = html.escape(deployment.get("kind", "") or "")
status = render_status_pill(deployment.get("status"))
started_at = html.escape(deployment.get("started_at", "") or "")
finished_at = html.escape(deployment.get("finished_at", "") or "")
returncode = deployment.get("returncode")
returncode_label = "" if returncode is None else html.escape(str(returncode))
stdout = html.escape(deployment.get("stdout", "") or "")
stderr = html.escape(deployment.get("stderr", "") or "")
return page(
title,
f"""
Souhrn
| Aplikace | {app_id} |
| Typ | {kind} |
| Status | {status} |
| Návratový kód | {returncode_label} |
| Spuštěno | {started_at} |
| Dokončeno | {finished_at} |
Výstup
{stdout}
Chyba
{stderr}
""",
user=user,
)