from __future__ import annotations import htpy as h from htpy import Node, Renderable from repub.components import ( inline_button, inline_link, muted_action_link, page_shell, section_card, status_badge, table_section, ) RUNNING_EXECUTIONS: tuple[dict[str, str | bool], ...] = ( { "source": "Pangea mobile articles", "slug": "pangea-mobile", "job_id": "7", "execution_id": "104", "started_at": "Today, 11:42 UTC", "runtime": "running for 8m", "status": "Running", "stats": "26 requests • 7 items • 2.6 MB", "worker": "graceful stop after current item", "log_href": "/job/7/execution/104/logs", "is_running": True, }, { "source": "Guardian feed mirror", "slug": "guardian-feed", "job_id": "3", "execution_id": "103", "started_at": "Today, 11:33 UTC", "runtime": "running for 17m", "status": "Running", "stats": "91 requests • 13 items • 5.1 MB", "worker": "streaming stats from worker jsonl", "log_href": "/job/3/execution/103/logs", "is_running": True, }, { "source": "Podcast enclosure mirror", "slug": "podcast-audio", "job_id": "11", "execution_id": "105", "started_at": "Today, 11:48 UTC", "runtime": "running for 2m", "status": "Stopping", "stats": "4 requests • 0 items • 0.8 MB", "worker": "waiting for 15s graceful shutdown window", "log_href": "/job/11/execution/105/logs", "is_running": True, }, ) UPCOMING_JOBS: tuple[dict[str, str | bool], ...] = ( { "source": "Podcast enclosure mirror", "slug": "podcast-audio", "job_id": "11", "next_run": "Today, 12:15 local", "schedule": "15 */4 * * 1-6", "enabled_label": "Enabled", "enabled_tone": "scheduled", "run_disabled": True, "run_reason": "Already running", "toggle_label": "Disable", }, { "source": "Weekly digest feed", "slug": "weekly-digest", "job_id": "18", "next_run": "Tomorrow, 08:00 local", "schedule": "0 8 * * 1", "enabled_label": "Disabled", "enabled_tone": "idle", "run_disabled": False, "run_reason": "Ready", "toggle_label": "Enable", }, { "source": "Kenya health desk", "slug": "kenya-health", "job_id": "22", "next_run": "Today, 13:00 local", "schedule": "0 */6 * * *", "enabled_label": "Enabled", "enabled_tone": "scheduled", "run_disabled": False, "run_reason": "Ready", "toggle_label": "Disable", }, ) COMPLETED_EXECUTIONS: tuple[dict[str, str], ...] = ( { "source": "Guardian feed mirror", "slug": "guardian-feed", "job_id": "3", "execution_id": "102", "ended_at": "Today, 10:57 UTC", "status": "Succeeded", "status_tone": "done", "stats": "204 requests • 28 items • 9.4 MB", "summary": "Finished on schedule", "log_href": "/job/3/execution/102/logs", }, { "source": "Podcast enclosure mirror", "slug": "podcast-audio", "job_id": "11", "execution_id": "101", "ended_at": "Today, 09:12 UTC", "status": "Failed", "status_tone": "failed", "stats": "timeout after 3 retries", "summary": "Worker exited with failure", "log_href": "/job/11/execution/101/logs", }, { "source": "Pangea mobile articles", "slug": "pangea-mobile", "job_id": "7", "execution_id": "100", "ended_at": "Today, 05:48 UTC", "status": "Canceled", "status_tone": "idle", "stats": "stopped by operator after 11m", "summary": "Graceful stop completed", "log_href": "/job/7/execution/100/logs", }, ) def _running_row(execution: dict[str, str | bool]) -> tuple[Node, ...]: return ( h.div[ h.div(class_="font-semibold text-slate-950")[execution["source"]], h.p(class_="mt-1 font-mono text-xs text-slate-500")[execution["slug"]], ], h.div[ h.p(class_="font-medium text-slate-900")[f"#{execution['execution_id']}"], h.p(class_="mt-1 text-xs text-slate-500")[f"job {execution['job_id']}"], ], h.div[ h.p(class_="font-medium text-slate-900")[execution["started_at"]], h.p(class_="mt-1 text-xs text-slate-500")[execution["runtime"]], ], status_badge(label=str(execution["status"]), tone="running"), h.div(class_="min-w-56 whitespace-normal")[ h.p(class_="font-medium text-slate-900")[execution["stats"]], h.p(class_="mt-1 text-xs text-slate-500")[execution["worker"]], ], h.div(class_="flex flex-nowrap items-center gap-3")[ inline_link( href=str(execution["log_href"]), label="View log", tone="amber", ), inline_button(label="Stop", tone="danger"), ], ) def _upcoming_row(job: dict[str, str | bool]) -> tuple[Node, ...]: return ( h.div[ h.div(class_="font-semibold text-slate-950")[job["source"]], h.p(class_="mt-1 font-mono text-xs text-slate-500")[job["slug"]], ], h.div[ h.p(class_="font-medium text-slate-900")[job["next_run"]], h.p(class_="mt-1 text-xs text-slate-500")[f"job {job['job_id']}"], ], h.p(class_="font-mono text-xs text-slate-600")[job["schedule"]], status_badge( label=str(job["enabled_label"]), tone=str(job["enabled_tone"]), ), h.p(class_="max-w-40 whitespace-normal text-sm text-slate-500")[ job["run_reason"] ], h.div(class_="flex flex-nowrap items-center gap-2")[ inline_button(label="Run now", disabled=bool(job["run_disabled"])), inline_button(label=str(job["toggle_label"])), inline_button(label="Delete", tone="danger"), ], ) def _completed_row(execution: dict[str, str]) -> tuple[Node, ...]: return ( h.div[ h.div(class_="font-semibold text-slate-950")[execution["source"]], h.p(class_="mt-1 font-mono text-xs text-slate-500")[execution["slug"]], ], h.div[ h.p(class_="font-medium text-slate-900")[f"#{execution['execution_id']}"], h.p(class_="mt-1 text-xs text-slate-500")[f"job {execution['job_id']}"], ], h.div[ h.p(class_="font-medium text-slate-900")[execution["ended_at"]], h.p(class_="mt-1 text-xs text-slate-500")[execution["summary"]], ], status_badge( label=execution["status"], tone=execution["status_tone"], ), h.div(class_="min-w-48 whitespace-normal")[ h.p(class_="font-medium text-slate-900")[execution["stats"]] ], inline_link( href=execution["log_href"], label="View log", tone="amber", ), ) def delete_confirmation_preview() -> Renderable: return section_card( content=( h.div(class_="flex items-center justify-between gap-4")[ h.div[ h.p( class_="text-xs font-semibold uppercase tracking-[0.22em] text-amber-600" )["Modal preview"], h.h2(class_="mt-2 text-xl font-semibold text-slate-950")[ "Delete confirmation" ], h.p(class_="mt-2 max-w-2xl text-sm text-slate-600")[ "Upcoming jobs use a confirmation modal before deleting a job. This is the intended open state, placed inline for the static UI pass." ], ], status_badge(label="Preview", tone="scheduled"), ], h.div(class_="mt-3 rounded-[1.5rem] bg-stone-50 p-5")[ h.p( class_="text-sm font-semibold uppercase tracking-[0.18em] text-slate-500" )["Delete job"], h.h3(class_="mt-2 text-lg font-semibold text-slate-950")[ "Delete Weekly digest feed?" ], h.p(class_="mt-3 max-w-2xl text-sm leading-6 text-slate-600")[ "This removes the source-linked job record and its schedule. Existing execution history and log files remain available for inspection." ], h.div(class_="mt-6 flex flex-wrap gap-3")[ inline_button(label="Cancel"), inline_button(label="Delete job", tone="danger"), ], ], ) ) def runs_page() -> Renderable: running_rows = tuple(_running_row(execution) for execution in RUNNING_EXECUTIONS) upcoming_rows = tuple(_upcoming_row(job) for job in UPCOMING_JOBS) completed_rows = tuple( _completed_row(execution) for execution in COMPLETED_EXECUTIONS ) return page_shell( current_path="/runs", eyebrow="Execution control", title="Runs", description="Running executions first, then the schedule queue, then completed history. Logs are routed through app URLs instead of direct file serving.", actions=muted_action_link(href="/sources", label="Back to sources"), content=( table_section( eyebrow="Live work", title="Running job executions", subtitle="Operators can inspect the live log stream, request a graceful stop, and escalate to a hard kill after the 15 second deadline if needed.", headers=( "Source", "Execution", "Started", "Status", "Stats", "Actions", ), rows=running_rows, ), table_section( eyebrow="Queue", title="Upcoming jobs", subtitle="Scheduled work shows enable or disable state, run-now affordances, and delete controls. Run now is disabled while the job is already running.", headers=( "Source", "Next run", "Cron", "State", "Run now", "Actions", ), rows=upcoming_rows, ), table_section( eyebrow="History", title="Completed job executions", subtitle="Recent execution history keeps the summary counters visible and links back to the plain text log view.", headers=( "Source", "Execution", "Ended", "Status", "Summary", "Log", ), rows=completed_rows, ), delete_confirmation_preview(), ), ) def execution_logs_page(*, job_id: int, execution_id: int) -> Renderable: return page_shell( current_path=f"/job/{job_id}/execution/{execution_id}/logs", eyebrow="Execution log", title=f"Job {job_id} / execution {execution_id}", description="Plain text log view routed through the app. The final version will stream appended lines while the worker is still active.", actions=muted_action_link(href="/runs", label="Back to runs"), content=( section_card( content=( h.div(class_="flex items-end justify-between gap-4")[ h.div[ h.p( class_="text-xs font-semibold uppercase tracking-[0.22em] text-amber-600" )["Route"], h.h2(class_="mt-2 text-xl font-semibold text-slate-950")[ f"/job/{job_id}/execution/{execution_id}/logs" ], h.p(class_="mt-2 text-sm text-slate-600")[ "Streaming text log view. No arbitrary file paths are exposed in the UI." ], ], status_badge(label="Streaming", tone="running"), ], h.pre( class_="mt-3 overflow-x-auto rounded-[1.5rem] bg-slate-950 p-5 text-xs leading-6 text-emerald-200" )[ "\n".join( ( "11:42:01 scheduler: run_now requested for job 7", "11:42:02 worker[7]: starting pangea-mobile", "11:42:08 stats: requests=18 items=4 bytes=1.8MB", "11:42:11 stats: requests=26 items=7 bytes=2.6MB", "11:42:17 stats: requests=31 items=9 bytes=3.0MB", "11:42:24 worker[7]: waiting for more log lines ...", ) ) ], ) ), ), )