Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logs #128

Merged
merged 9 commits into from
Jun 29, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions hyp3_sdk/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
# TODO: actually looks like a good candidate for a dataclass (python 3.7+)
# https://docs.python.org/3/library/dataclasses.html
class Job:
_finished_job_keys = {'job_id', 'logs', 'request_time', 'status_code', 'user_id'}
_successful_job_keys = _finished_job_keys.union({'browse_images', 'expiration_time', 'files', 'thumbnail_images'})
jhkennedy marked this conversation as resolved.
Show resolved Hide resolved

def __init__(
self,
job_type: str,
Expand All @@ -24,6 +27,7 @@ def __init__(
name: Optional[str] = None,
job_parameters: Optional[dict] = None,
files: Optional[List] = None,
logs: Optional[List] = None,
browse_images: Optional[List] = None,
thumbnail_images: Optional[List] = None,
expiration_time: Optional[datetime] = None
Expand All @@ -36,6 +40,7 @@ def __init__(
self.name = name
self.job_parameters = job_parameters
self.files = files
self.logs = logs
self.browse_images = browse_images
self.thumbnail_images = thumbnail_images
self.expiration_time = expiration_time
Expand All @@ -61,6 +66,7 @@ def from_dict(input_dict: dict):
name=input_dict.get('name'),
job_parameters=input_dict.get('job_parameters'),
files=input_dict.get('files'),
logs=input_dict.get('logs'),
browse_images=input_dict.get('browse_images'),
thumbnail_images=input_dict.get('thumbnail_images'),
expiration_time=expiration_time
Expand All @@ -77,8 +83,7 @@ def to_dict(self, for_resubmit: bool = False):
job_dict[key] = value

if not for_resubmit:
for key in ['files', 'browse_images', 'thumbnail_images', 'job_id', 'status_code', 'user_id',
'expiration_time', 'request_time']:
for key in self._successful_job_keys:
value = self.__getattribute__(key)
if value is not None:
if isinstance(value, datetime):
Expand Down
28 changes: 28 additions & 0 deletions tests/test_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"browse_images": ["https://PAIR_PROCESS.png"],
"expiration_time": "2020-10-08T00:00:00+00:00",
"files": [{"filename": "PAIR_PROCESS.nc", "size": 5949932, "url": "https://PAIR_PROCESS.nc"}],
"logs": ["https://d1c05104-b455-4f35-a95a-84155d63f855.log"],
"job_id": "d1c05104-b455-4f35-a95a-84155d63f855",
"job_parameters": {"granules": [
"S1A_IW_SLC__1SDH_20180511T204719_20180511T204746_021862_025C12_6F77",
Expand All @@ -26,6 +27,7 @@
}

FAILED_JOB = {
"logs": ["https://281b2087-9e7d-4d17-a9b3-aebeb2ad23c6.log"],
"job_id": "281b2087-9e7d-4d17-a9b3-aebeb2ad23c6",
"job_parameters": {
"granules": [
Expand All @@ -41,13 +43,39 @@
}


def test_job_attributes():
job = Job.from_dict(SUCCEEDED_JOB)
for key in Job._successful_job_keys:
assert job.__getattribute__(key)

job = Job.from_dict(FAILED_JOB)
for key in Job._finished_job_keys:
assert job.__getattribute__(key)

job = Job.from_dict(FAILED_JOB)
for key in Job._successful_job_keys.difference(Job._finished_job_keys):
assert job.__getattribute__(key) is None


def test_job_dict_transforms():
job = Job.from_dict(SUCCEEDED_JOB)
assert job.to_dict() == SUCCEEDED_JOB

job = Job.from_dict(FAILED_JOB)
assert job.to_dict() == FAILED_JOB

job = Job.from_dict(SUCCEEDED_JOB)
retry = job.to_dict(for_resubmit=True)
for key in Job._successful_job_keys:
assert job.__getattribute__(key)
assert not retry.get(key, False)

job = Job.from_dict(FAILED_JOB)
retry = job.to_dict(for_resubmit=True)
for key in Job._finished_job_keys:
assert job.__getattribute__(key)
assert not retry.get(key, False)


def test_job_complete_succeeded_failed_running():
job = Job.from_dict(SUCCEEDED_JOB)
Expand Down