From 5eaafdabf0e22b04907d6639600d2270211c4da5 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Wed, 7 Jan 2026 15:05:34 -0600 Subject: Fetch jobs by their external IDs. --- gn_libs/jobs/__init__.py | 3 ++- gn_libs/jobs/jobs.py | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/gn_libs/jobs/__init__.py b/gn_libs/jobs/__init__.py index d6e4ce3..4960349 100644 --- a/gn_libs/jobs/__init__.py +++ b/gn_libs/jobs/__init__.py @@ -4,7 +4,8 @@ from .jobs import (job, launch_job, initialise_job, push_to_stream, - update_metadata) + update_metadata, + jobs_by_external_id) def init_app(flask_app): """Initialise the migrations for flask""" diff --git a/gn_libs/jobs/jobs.py b/gn_libs/jobs/jobs.py index 8453697..f5b7a5e 100644 --- a/gn_libs/jobs/jobs.py +++ b/gn_libs/jobs/jobs.py @@ -60,6 +60,27 @@ def job(conn: DbConnection, job_id: Union[str, uuid.UUID], fulldetails: bool = F return _job +def jobs_by_external_id(conn: DbConnection, external_id: Union[str, uuid.UUID]) -> tuple[dict, ...]: + """Fetch jobs by their external IDs.""" + with _cursor(conn) as cursor: + cursor.execute( + "SELECT jeids.external_id, jobs.* FROM jobs_external_ids AS jeids " + "INNER JOIN jobs ON jeids.job_id=jobs.job_id " + "WHERE jeids.external_id=?", + (str(external_id),)) + _jobs = {row["job_id"]: {**dict(row), "metadata": {}} for row in cursor.fetchall()} + _jobs_ids = tuple(_job["job_id"] for _job in _jobs.values()) + + _paramstr = ", ".join(["?"] * len(_jobs_ids)) + cursor.execute( + f"SELECT * FROM jobs_metadata WHERE job_id IN ({_paramstr})", + _jobs_ids) + for row in cursor.fetchall(): + _jobs[row["job_id"]]["metadata"][row["metadata_key"]] = row["metadata_value"] + + return tuple(_jobs.values()) + + def __save_job__( conn: DbConnection, the_job: dict, -- cgit 1.4.1