From fe900d351101c8df2be27afc8391afe4a42f4776 Mon Sep 17 00:00:00 2001 From: bloodearnest Date: Mon, 11 Dec 2023 12:45:17 +0000 Subject: [PATCH] Expose metrics in job sync protocol --- jobrunner/sync.py | 6 ++++-- tests/test_sync.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/jobrunner/sync.py b/jobrunner/sync.py index f0a89439..d0086c38 100644 --- a/jobrunner/sync.py +++ b/jobrunner/sync.py @@ -9,7 +9,7 @@ import requests -from jobrunner import config, queries +from jobrunner import config, queries, record_stats from jobrunner.create_or_update_jobs import create_or_update_jobs from jobrunner.lib.database import find_where, select_values from jobrunner.lib.log_utils import configure_logging, set_log_context @@ -143,19 +143,21 @@ def job_to_remote_format(job): Convert our internal representation of a Job into whatever format the job-server expects """ + return { "identifier": job.id, "job_request_id": job.job_request_id, "action": job.action, "run_command": job.run_command, "status": job.state.value, - "status_code": job.status_code.value if job.status_code else "", + "status_code": job.status_code.value, "status_message": job.status_message or "", "created_at": job.created_at_isoformat, "updated_at": job.updated_at_isoformat, "started_at": job.started_at_isoformat, "completed_at": job.completed_at_isoformat, "trace_context": job.trace_context, + "metrics": record_stats.read_job_metrics(job.id), } diff --git a/tests/test_sync.py b/tests/test_sync.py index a4a15de4..0a63be14 100644 --- a/tests/test_sync.py +++ b/tests/test_sync.py @@ -4,6 +4,7 @@ from jobrunner import config, queries, sync from jobrunner.models import JobRequest +from tests.factories import job_factory, metrics_factory def test_job_request_from_remote_format(): @@ -76,6 +77,33 @@ def test_job_request_from_remote_format_database_name_fallback(): assert job_request == expected +def test_job_to_remote_format_default(db): + job = job_factory() + + json = sync.job_to_remote_format(job) + + assert json["action"] == "action_name" + assert json["run_command"] == "python myscript.py" + assert json["status"] == "pending" + assert json["status_code"] == "created" + assert json["metrics"] == {} + + +def test_job_to_remote_format_null_status_message(db): + job = job_factory(status_message=None) + json = sync.job_to_remote_format(job) + assert json["status_message"] == "" + + +def test_job_to_remote_format_metrics(db): + job = job_factory() + metrics_factory(job, metrics={"test": 0.0}) + + json = sync.job_to_remote_format(job) + + assert json["metrics"] == {"test": 0.0} + + def test_session_request_no_flags(db, responses): responses.add( method="GET",