feat(workbench): persist operational workflows and events
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
from datetime import datetime
|
||||
from datetime import datetime, timezone
|
||||
from typing import Any
|
||||
from uuid import UUID, uuid4
|
||||
|
||||
from app.infra.db.postgresql.analysis import AnalysisRepository
|
||||
from app.native.wndb.core.connection import project_connection, project_transaction
|
||||
from app.infra.db.project_routing import get_active_project_routing
|
||||
from app.domain.time import parse_utc_time
|
||||
|
||||
|
||||
@@ -40,16 +41,21 @@ def create_analysis_run(
|
||||
started_at = parse_utc_time(scheme_start_time, field_name="scheme_start_time")
|
||||
run_id = uuid4()
|
||||
with project_connection(name) as conn:
|
||||
AnalysisRepository.create_run_sync(
|
||||
conn,
|
||||
run_id=run_id,
|
||||
name=scheme_name,
|
||||
run_type=scheme_type,
|
||||
created_by=username,
|
||||
started_at=started_at,
|
||||
status=status,
|
||||
parameters=scheme_detail,
|
||||
)
|
||||
with conn.transaction():
|
||||
AnalysisRepository.create_run_sync(
|
||||
conn,
|
||||
run_id=run_id,
|
||||
name=scheme_name,
|
||||
run_type=scheme_type,
|
||||
created_by=username,
|
||||
started_at=started_at,
|
||||
status=status,
|
||||
parameters=scheme_detail,
|
||||
)
|
||||
_append_run_event(
|
||||
conn, run_id, revision=1, status=status,
|
||||
occurred_at=started_at, results_committed=False,
|
||||
)
|
||||
return run_id
|
||||
|
||||
|
||||
@@ -84,6 +90,10 @@ def store_analysis_run_with_result(
|
||||
result_type=result_type,
|
||||
payload=result_payload,
|
||||
)
|
||||
_append_run_event(
|
||||
conn, run_id, revision=1, status="completed",
|
||||
occurred_at=started_at, results_committed=True,
|
||||
)
|
||||
return run_id
|
||||
|
||||
|
||||
@@ -96,13 +106,37 @@ def update_analysis_run(
|
||||
scheme_detail: dict[str, Any],
|
||||
) -> None:
|
||||
with project_connection(name) as conn:
|
||||
AnalysisRepository.update_run_sync(
|
||||
conn,
|
||||
run_id,
|
||||
status=status,
|
||||
created_by=username,
|
||||
parameters=scheme_detail,
|
||||
)
|
||||
with conn.transaction():
|
||||
revision = AnalysisRepository.update_run_sync(
|
||||
conn,
|
||||
run_id,
|
||||
status=status,
|
||||
created_by=username,
|
||||
parameters=scheme_detail,
|
||||
)
|
||||
_append_run_event(
|
||||
conn, run_id, revision=revision, status=status,
|
||||
occurred_at=datetime.now(timezone.utc),
|
||||
results_committed=status == "completed",
|
||||
)
|
||||
|
||||
|
||||
def _append_run_event(
|
||||
conn, run_id: UUID, *, revision: int, status: str,
|
||||
occurred_at: datetime, results_committed: bool,
|
||||
) -> None:
|
||||
routing = get_active_project_routing()
|
||||
if routing is None or routing.project_id is None:
|
||||
return
|
||||
AnalysisRepository.append_run_changed_event_sync(
|
||||
conn,
|
||||
project_id=routing.project_id,
|
||||
run_id=run_id,
|
||||
revision=revision,
|
||||
status=status,
|
||||
occurred_at=occurred_at,
|
||||
results_committed=results_committed,
|
||||
)
|
||||
|
||||
|
||||
def get_analysis_run(name: str, run_id: UUID) -> dict[str, Any]:
|
||||
|
||||
Reference in New Issue
Block a user