76 lines
2.5 KiB
Python
76 lines
2.5 KiB
Python
import time
|
|
import requests
|
|
from datetime import timedelta
|
|
from math import floor
|
|
from rich.table import Table
|
|
from rich.live import Live
|
|
from configuration import Config
|
|
|
|
STATUS_MAPPING = {
|
|
"IN_QUEUE": "[yellow]queued[/yellow]",
|
|
"IN_PROGRESS": "[blue]running[/blue]",
|
|
"CANCELLED": "[orange1]cancelled[/orange1]",
|
|
"COMPLETED": "[green]complete[/green]",
|
|
"FAILED": "[red]failed[/red]",
|
|
}
|
|
|
|
|
|
def print_job_status(config: Config, job_id: str, once:bool = False) -> None:
|
|
result = _request_job_state(config, job_id)
|
|
if not result:
|
|
return
|
|
|
|
def result_to_values(result:dict)-> dict[str,str]:
|
|
return {
|
|
"status": STATUS_MAPPING[result["status"]],
|
|
"transcription": result.get("transcription_url", "..."),
|
|
"diarization": result.get("diarization_url", "..."),
|
|
}
|
|
values: dict[str, str] = result_to_values(result)
|
|
|
|
def rebuild_table():
|
|
table = Table()
|
|
table.add_column("Status")
|
|
table.add_column("Time running")
|
|
table.add_column("Job ID")
|
|
table.add_column("Diarization")
|
|
table.add_column("Transcription")
|
|
table.add_row(
|
|
values.get("status", "unknown"),
|
|
str(sw_current),
|
|
job_id,
|
|
values.get("diarization", "..."),
|
|
values.get("transcription", "..."),
|
|
)
|
|
return table
|
|
|
|
sw_start: float = time.time()
|
|
sw_current: timedelta = timedelta()
|
|
with Live(get_renderable=rebuild_table, refresh_per_second=2):
|
|
while not once:
|
|
result = _request_job_state(config, job_id, silent=True)
|
|
values: dict[str, str] = result_to_values(result)
|
|
sw_current = timedelta(seconds=floor(time.time() - sw_start))
|
|
|
|
if result["status"] != "IN_QUEUE" and result["status"] != "IN_PROGRESS":
|
|
break
|
|
time.sleep(1)
|
|
|
|
|
|
def _request_job_state(config: Config, id: str, silent: bool = False) -> dict:
|
|
endpoint_health = f"{config.endpoint}/status/{id}"
|
|
if silent:
|
|
response = requests.get(endpoint_health, headers=config.headers)
|
|
else:
|
|
with config.console.status(
|
|
f"[bold green]Requesting job[/bold green] {id}"
|
|
" [bold green]status...[/bold green]"
|
|
):
|
|
response = requests.get(endpoint_health, headers=config.headers)
|
|
if response.status_code == 404:
|
|
config.console.log(f"[red]Job[/red] {id} [red]not found on endpoint.[/red]")
|
|
return {}
|
|
if not response.ok:
|
|
raise requests.exceptions.HTTPError()
|
|
return response.json()
|
|
|