Skip to content

Commit

Permalink
Merge pull request #124 from SAP/fetch-job-by-model-name
Browse files Browse the repository at this point in the history
Support For Reading Jobs By Model Name
  • Loading branch information
SreevishnuAB authored Jan 12, 2022
2 parents 1242ccd + 4de5fa2 commit 2c59234
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 8 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.11.0]

* Support for reading training jobs using model name in `read_job_by_model_name`

[#124]: https://github.com/SAP/data-attribute-recommendation-python-sdk/pull/124

## [0.10.0]

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

[Unreleased]: https://github.com/SAP/data-attribute-recommendation-python-sdk/compare/rel/0.10.0...HEAD
[0.11.0]: https://github.com/SAP/data-attribute-recommendation-python-sdk/compare/rel/0.10.0...rel/0.11.0
[0.10.0]: https://github.com/SAP/data-attribute-recommendation-python-sdk/compare/rel/0.9.2...rel/0.10.0
[0.9.2]: https://github.com/SAP/data-attribute-recommendation-python-sdk/compare/rel/0.9.1...rel/0.9.2
[0.9.1]: https://github.com/SAP/data-attribute-recommendation-python-sdk/compare/rel/0.9.0...rel/0.9.1
Expand Down
8 changes: 8 additions & 0 deletions sap/aibus/dar/client/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ class CreateTrainingJobFailed(DARException):
pass


class JobNotFound(DARException):
"""
Training job not found
"""

pass


class ModelAlreadyExists(DARException):
"""
Model already exists and must be deleted first.
Expand Down
18 changes: 18 additions & 0 deletions sap/aibus/dar/client/model_manager_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
DeploymentTimeOut,
DeploymentFailed,
CreateTrainingJobFailed,
JobNotFound,
)
from sap.aibus.dar.client.model_manager_constants import (
JobStatus,
Expand Down Expand Up @@ -112,11 +113,28 @@ def read_job_by_id(self, job_id: str) -> dict:
:param job_id: ID of the Job to be retrieved.
:return: a single Job as dict
:raises JobNotFound: when no Job with given model name is found
"""
endpoint = ModelManagerPaths.format_job_endpoint_by_id(job_id)
response = self.session.get_from_endpoint(endpoint)
return response.json()

def read_job_by_model_name(self, model_name: str) -> dict:
"""
Reads Job with the given *model_name*
:param model_name: name of model
:return: a single Job as dict
:raises
"""
jobs_response = self.read_job_collection()
jobs = jobs_response["jobs"]
for job in jobs:
if job["modelName"] == model_name:
return job
raise JobNotFound(
"Job with model name '{}' could not be found".format(model_name)
)

def delete_job_by_id(self, job_id: str) -> None:
"""
Deletes the Job with the given *job_id*.
Expand Down
10 changes: 3 additions & 7 deletions system_tests/workflow/test_end_to_end.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,9 @@ def test_create(
# Job
# The Model resource does not have a jobId property, so we
# have to look up the job ID via the job collection
job_collection = model_manager_client.read_job_collection()
job_id = None
for job in job_collection["jobs"]:
if job["modelName"] == model_name:
job_id = job["id"]
break
assert job_id is not None
job = model_manager_client.read_job_by_model_name(model_name)
assert job["id"] is not None
job_id = job["id"]

self._assert_job_exists(model_manager_client, job_id)
# Get dataset ID used in this job
Expand Down
31 changes: 31 additions & 0 deletions tests/sap/aibus/dar/client/test_model_manager_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
DeploymentTimeOut,
DeploymentFailed,
CreateTrainingJobFailed,
JobNotFound,
)
from sap.aibus.dar.client.model_manager_client import ModelManagerClient
from sap.aibus.dar.client.util.polling import Polling, PollingTimeoutException
Expand Down Expand Up @@ -463,6 +464,36 @@ def test_is_job_failed(self):
non_failed_job = self._make_job_resource(non_failed_state)
assert ModelManagerClient.is_job_failed(non_failed_job) is False

def test_read_job_by_model_name(self, model_manager_client: ModelManagerClient):
job1 = self._make_job_resource("SUCCEEDED")
job2 = self._make_job_resource("SUCCEEDED")
job2["modelName"] = "my-model-2"
job_collection_response = {"count": 2, "jobs": [job1, job2]}
model_manager_client.read_job_collection = create_autospec(
model_manager_client.read_job_collection,
return_value=job_collection_response,
)
response = model_manager_client.read_job_by_model_name("my-model-1")
assert response["modelName"] == "my-model-1"
assert model_manager_client.read_job_collection.call_count == 1

def test_read_job_by_model_name_job_not_found(
self, model_manager_client: ModelManagerClient
):
job1 = self._make_job_resource("SUCCEEDED")
job2 = self._make_job_resource("SUCCEEDED")
job2["modelName"] = "my-model-2"
job_collection_response = {"count": 2, "jobs": [job1, job2]}
model_manager_client.read_job_collection = create_autospec(
model_manager_client.read_job_collection,
return_value=job_collection_response,
)
with pytest.raises(JobNotFound) as exc:
model_manager_client.read_job_by_model_name("my-model-3")
expected_message = "Job with model name 'my-model-3' could not be found"
assert str(exc.value) == expected_message
assert model_manager_client.read_job_collection.call_count == 1

@staticmethod
def _make_job_resource(state):
job_resource = {
Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.10.0
0.11.0

0 comments on commit 2c59234

Please sign in to comment.