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 7 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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and uses [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [1.2.0](https://github.com/ASFHyP3/hyp3-sdk/compare/v1.1.3...v1.2.0)

### Added
- `Job` class now has a `logs` attribute containing links to job log files
- Added missing [container methods](https://docs.python.org/3/reference/datamodel.html#emulating-container-types)
- batches are now subscriptable: `batch[0]`
- jobs can be searched for in batches:`job in batch`
Expand All @@ -22,7 +23,7 @@ and uses [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
### Fixed
- [#92](https://github.com/ASFHyP3/hyp3-sdk/issues/92) -- `ImportError` being
raised when showing a progress bar because `ipywidgets` may not always be
installed when running in a Jupyter kernel
installed when running in a Jupyter kernel

## [1.1.3](https://github.com/ASFHyP3/hyp3-sdk/compare/v1.1.2...v1.1.3)

Expand Down
31 changes: 16 additions & 15 deletions hyp3_sdk/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
# TODO: actually looks like a good candidate for a dataclass (python 3.7+)
# https://docs.python.org/3/library/dataclasses.html
class Job:
_attributes_for_resubmit = {'name', 'job_parameters', 'job_type'}

def __init__(
self,
job_type: str,
Expand All @@ -24,6 +26,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 +39,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,30 +65,27 @@ 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
)

def to_dict(self, for_resubmit: bool = False):
job_dict = {
'job_type': self.job_type,
}
job_dict = {}
if for_resubmit:
keys_to_process = Job._attributes_for_resubmit
else:
keys_to_process = vars(self).keys()

for key in ['name', 'job_parameters']:
for key in keys_to_process:
value = self.__getattribute__(key)
if value is not None:
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']:
value = self.__getattribute__(key)
if value is not None:
if isinstance(value, datetime):
job_dict[key] = value.isoformat(timespec='seconds')
else:
job_dict[key] = value
if isinstance(value, datetime):
job_dict[key] = value.isoformat(timespec='seconds')
else:
job_dict[key] = value

return job_dict

def succeeded(self) -> bool:
Expand Down
26 changes: 26 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,37 @@
}


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

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

unprovided_attributes = set(vars(job).keys()) - set(FAILED_JOB.keys())
for key in unprovided_attributes:
assert job.__getattribute__(key) is None


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

retry = job.to_dict(for_resubmit=True)
for key in set(SUCCEEDED_JOB.keys()) - Job._attributes_for_resubmit:
assert job.__getattribute__(key)
assert not retry.get(key, False)

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

retry = job.to_dict(for_resubmit=True)
for key in set(FAILED_JOB.keys()) - Job._attributes_for_resubmit:
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