Skip to content

Commit

Permalink
Fix return value for job pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
totycro committed Aug 8, 2024
1 parent 6da56ef commit 0ff5d55
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 1.5.1
* Jobs pagination fix

## 1.5.0
* Set autosave-every to 60

Expand Down
14 changes: 10 additions & 4 deletions pygeoapi_kubernetes_papermill/kubernetes.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def __init__(self, manager_def: dict) -> None:
self.batch_v1 = k8s_client.BatchV1Api()
self.core_api = k8s_client.CoreV1Api()

def get_jobs(self, status=None, limit=None, offset=None) -> list[JobDict]:
def get_jobs(self, status=None, limit=None, offset=None) -> dict:
"""
Get process jobs, optionally filtered by status
Expand All @@ -154,6 +154,8 @@ def get_jobs(self, status=None, limit=None, offset=None) -> list[JobDict]:
if is_k8s_job_name(k8s_job.metadata.name)
]

number_matched = len(k8s_jobs)

# NOTE: need to paginate before expensive single job serialization
if offset:
k8s_jobs = k8s_jobs[offset:]
Expand All @@ -163,9 +165,13 @@ def get_jobs(self, status=None, limit=None, offset=None) -> list[JobDict]:

# TODO: implement status filter

return [
job_from_k8s(k8s_job, self._job_message(k8s_job)) for k8s_job in k8s_jobs
]
return {
"jobs": [
job_from_k8s(k8s_job, self._job_message(k8s_job))
for k8s_job in k8s_jobs
],
"numberMatched": number_matched,
}

def get_job(self, job_id) -> Optional[JobDict]:
"""
Expand Down
12 changes: 7 additions & 5 deletions tests/test_kubernetes_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,8 @@ def test_get_jobs_handles_container_status_null(
):
# NOTE: this test could be reduced to only test _job_message() if the excessive
# mocking causes issues
jobs = manager.get_jobs()
assert [job["message"] for job in jobs] == [""]
job_data = manager.get_jobs()
assert [job["message"] for job in job_data["jobs"]] == [""]


def test_execute_process_sync_also_returns_mime_type(
Expand Down Expand Up @@ -292,9 +292,9 @@ def test_accepted_jobs_show_events(
mock_list_events,
manager: KubernetesManager,
):
jobs = manager.get_jobs()
job_data = manager.get_jobs()

assert jobs[0]["message"] == "last event"
assert job_data["jobs"][0]["message"] == "last event"


def test_secret_job_annotation_parameters_are_hidden():
Expand Down Expand Up @@ -374,7 +374,9 @@ def test_kubernetes_manager_handles_pagination(
many_k8s_jobs,
):
with mock_list_jobs_with(*many_k8s_jobs):
jobs = manager.get_jobs(offset=3, limit=2)
job_data = manager.get_jobs(offset=3, limit=2)

jobs = job_data["jobs"]
assert len(jobs) == 2
assert [job["identifier"] for job in jobs] == ["job-3", "job-4"]
assert job_data["numberMatched"] == 13

0 comments on commit 0ff5d55

Please sign in to comment.