Skip to content

Commit 2c59234

Browse files
authored
Merge pull request #124 from SAP/fetch-job-by-model-name
Support For Reading Jobs By Model Name
2 parents 1242ccd + 4de5fa2 commit 2c59234

File tree

6 files changed

+68
-8
lines changed

6 files changed

+68
-8
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [0.11.0]
10+
11+
* Support for reading training jobs using model name in `read_job_by_model_name`
12+
13+
[#124]: https://github.com/SAP/data-attribute-recommendation-python-sdk/pull/124
14+
915
## [0.10.0]
1016

1117
### Added
@@ -245,6 +251,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
245251
* First public release
246252

247253
[Unreleased]: https://github.com/SAP/data-attribute-recommendation-python-sdk/compare/rel/0.10.0...HEAD
254+
[0.11.0]: https://github.com/SAP/data-attribute-recommendation-python-sdk/compare/rel/0.10.0...rel/0.11.0
248255
[0.10.0]: https://github.com/SAP/data-attribute-recommendation-python-sdk/compare/rel/0.9.2...rel/0.10.0
249256
[0.9.2]: https://github.com/SAP/data-attribute-recommendation-python-sdk/compare/rel/0.9.1...rel/0.9.2
250257
[0.9.1]: https://github.com/SAP/data-attribute-recommendation-python-sdk/compare/rel/0.9.0...rel/0.9.1

sap/aibus/dar/client/exceptions.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,14 @@ class CreateTrainingJobFailed(DARException):
115115
pass
116116

117117

118+
class JobNotFound(DARException):
119+
"""
120+
Training job not found
121+
"""
122+
123+
pass
124+
125+
118126
class ModelAlreadyExists(DARException):
119127
"""
120128
Model already exists and must be deleted first.

sap/aibus/dar/client/model_manager_client.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
DeploymentTimeOut,
1515
DeploymentFailed,
1616
CreateTrainingJobFailed,
17+
JobNotFound,
1718
)
1819
from sap.aibus.dar.client.model_manager_constants import (
1920
JobStatus,
@@ -112,11 +113,28 @@ def read_job_by_id(self, job_id: str) -> dict:
112113
113114
:param job_id: ID of the Job to be retrieved.
114115
:return: a single Job as dict
116+
:raises JobNotFound: when no Job with given model name is found
115117
"""
116118
endpoint = ModelManagerPaths.format_job_endpoint_by_id(job_id)
117119
response = self.session.get_from_endpoint(endpoint)
118120
return response.json()
119121

122+
def read_job_by_model_name(self, model_name: str) -> dict:
123+
"""
124+
Reads Job with the given *model_name*
125+
:param model_name: name of model
126+
:return: a single Job as dict
127+
:raises
128+
"""
129+
jobs_response = self.read_job_collection()
130+
jobs = jobs_response["jobs"]
131+
for job in jobs:
132+
if job["modelName"] == model_name:
133+
return job
134+
raise JobNotFound(
135+
"Job with model name '{}' could not be found".format(model_name)
136+
)
137+
120138
def delete_job_by_id(self, job_id: str) -> None:
121139
"""
122140
Deletes the Job with the given *job_id*.

system_tests/workflow/test_end_to_end.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,9 @@ def test_create(
143143
# Job
144144
# The Model resource does not have a jobId property, so we
145145
# have to look up the job ID via the job collection
146-
job_collection = model_manager_client.read_job_collection()
147-
job_id = None
148-
for job in job_collection["jobs"]:
149-
if job["modelName"] == model_name:
150-
job_id = job["id"]
151-
break
152-
assert job_id is not None
146+
job = model_manager_client.read_job_by_model_name(model_name)
147+
assert job["id"] is not None
148+
job_id = job["id"]
153149

154150
self._assert_job_exists(model_manager_client, job_id)
155151
# Get dataset ID used in this job

tests/sap/aibus/dar/client/test_model_manager_client.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
DeploymentTimeOut,
1414
DeploymentFailed,
1515
CreateTrainingJobFailed,
16+
JobNotFound,
1617
)
1718
from sap.aibus.dar.client.model_manager_client import ModelManagerClient
1819
from sap.aibus.dar.client.util.polling import Polling, PollingTimeoutException
@@ -463,6 +464,36 @@ def test_is_job_failed(self):
463464
non_failed_job = self._make_job_resource(non_failed_state)
464465
assert ModelManagerClient.is_job_failed(non_failed_job) is False
465466

467+
def test_read_job_by_model_name(self, model_manager_client: ModelManagerClient):
468+
job1 = self._make_job_resource("SUCCEEDED")
469+
job2 = self._make_job_resource("SUCCEEDED")
470+
job2["modelName"] = "my-model-2"
471+
job_collection_response = {"count": 2, "jobs": [job1, job2]}
472+
model_manager_client.read_job_collection = create_autospec(
473+
model_manager_client.read_job_collection,
474+
return_value=job_collection_response,
475+
)
476+
response = model_manager_client.read_job_by_model_name("my-model-1")
477+
assert response["modelName"] == "my-model-1"
478+
assert model_manager_client.read_job_collection.call_count == 1
479+
480+
def test_read_job_by_model_name_job_not_found(
481+
self, model_manager_client: ModelManagerClient
482+
):
483+
job1 = self._make_job_resource("SUCCEEDED")
484+
job2 = self._make_job_resource("SUCCEEDED")
485+
job2["modelName"] = "my-model-2"
486+
job_collection_response = {"count": 2, "jobs": [job1, job2]}
487+
model_manager_client.read_job_collection = create_autospec(
488+
model_manager_client.read_job_collection,
489+
return_value=job_collection_response,
490+
)
491+
with pytest.raises(JobNotFound) as exc:
492+
model_manager_client.read_job_by_model_name("my-model-3")
493+
expected_message = "Job with model name 'my-model-3' could not be found"
494+
assert str(exc.value) == expected_message
495+
assert model_manager_client.read_job_collection.call_count == 1
496+
466497
@staticmethod
467498
def _make_job_resource(state):
468499
job_resource = {

version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.10.0
1+
0.11.0

0 commit comments

Comments
 (0)