-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
REF: Factor out attributes into
EventAttributes
class
skipci
- Loading branch information
1 parent
9a86bce
commit 74f11d8
Showing
8 changed files
with
226 additions
and
492 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import datetime as dt | ||
import json | ||
import uuid as uuid_library | ||
|
||
from octue.cloud import LOCAL_SDK_VERSION | ||
from octue.utils.dictionaries import make_minimal_dictionary | ||
|
||
SENDER_TYPE_OPPOSITES = {"CHILD": "PARENT", "PARENT": "CHILD"} | ||
|
||
|
||
class EventAttributes: | ||
def __init__( | ||
self, | ||
originator_question_uuid, | ||
parent, | ||
originator, | ||
sender, | ||
sender_type, | ||
recipient, | ||
uuid=None, | ||
datetime=None, | ||
question_uuid=None, | ||
parent_question_uuid=None, | ||
sender_sdk_version=None, | ||
retry_count=0, | ||
forward_logs=True, | ||
save_diagnostics=True, | ||
cpus=None, | ||
memory=None, | ||
ephemeral_storage=None, | ||
): | ||
# Attributes for all event kinds. | ||
self.uuid = uuid or str(uuid_library.uuid4()) | ||
self.datetime = datetime or dt.datetime.now(tz=dt.timezone.utc).isoformat() | ||
self.question_uuid = question_uuid or str(uuid_library.uuid4()) | ||
self.parent_question_uuid = parent_question_uuid | ||
self.originator_question_uuid = originator_question_uuid | ||
self.parent = parent | ||
self.originator = originator | ||
self.sender = sender | ||
self.sender_type = sender_type | ||
self.sender_sdk_version = sender_sdk_version or LOCAL_SDK_VERSION | ||
self.recipient = recipient | ||
self.retry_count = int(retry_count) | ||
|
||
# Question event attributes. | ||
self.forward_logs = bool(forward_logs) | ||
self.save_diagnostics = save_diagnostics | ||
self.cpus = cpus | ||
self.memory = memory | ||
self.ephemeral_storage = ephemeral_storage | ||
|
||
def make_response_attributes(self): | ||
attributes = self.to_dict() | ||
attributes["sender"] = self.recipient | ||
attributes["sender_type"] = SENDER_TYPE_OPPOSITES[self.sender_type] | ||
attributes["sender_sdk_version"] = LOCAL_SDK_VERSION | ||
attributes["recipient"] = self.sender | ||
return EventAttributes(**attributes) | ||
|
||
def to_dict(self): | ||
return make_minimal_dictionary( | ||
uuid=self.uuid, | ||
datetime=self.datetime, | ||
question_uuid=self.question_uuid, | ||
parent_question_uuid=self.parent_question_uuid, | ||
originator_question_uuid=self.originator_question_uuid, | ||
parent=self.parent, | ||
originator=self.originator, | ||
sender=self.sender, | ||
sender_type=self.sender_type, | ||
sender_sdk_version=self.sender_sdk_version, | ||
recipient=self.recipient, | ||
retry_count=self.retry_count, | ||
forward_logs=self.forward_logs, | ||
save_diagnostics=self.save_diagnostics, | ||
cpus=self.cpus, | ||
memory=self.memory, | ||
ephemeral_storage=self.ephemeral_storage, | ||
) | ||
|
||
def to_serialised_attributes(self): | ||
serialised_attributes = {} | ||
|
||
for key, value in self.to_dict().items(): | ||
if isinstance(value, bool): | ||
value = str(int(value)) | ||
elif isinstance(value, (int, float)): | ||
value = str(value) | ||
elif value is None: | ||
value = json.dumps(value) | ||
|
||
serialised_attributes[key] = value | ||
|
||
return serialised_attributes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.