Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion common/ingest.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def ingest(data: list[Any], log_type: str):
# [{"logText": str(log1)}, {"logText": str(log2)}, ...]
parsed_data = list(
map(
lambda i: {"logText": str(json.dumps(i).encode("utf-8"), "utf-8")},
lambda i: {"logText": str(json.dumps(utils.cleanup(i), separators=(',', ':')).encode("utf-8"), "utf-8")},
data,
)
)
Expand Down
17 changes: 17 additions & 0 deletions common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,20 @@ def cloud_logging(message: str, severity: str = "INFO") -> None:
severity (str): severity of the message. Defaults to "INFO".
"""
print(json.dumps({"severity": severity, "message": message}))


def cleanup(object: dict | list) -> dict | list:
"""Remove empty lists and dicts from an object

Args:
object (dict | list): The object to cleanup

Returns:
dict | list: The cleaned object
"""
if isinstance(object, dict):
return {k: cleanup(v) for k, v in object.items() if v and cleanup(v)}
elif isinstance(object, list):
return [cleanup(v) for v in object if v and cleanup(v)]
else:
return object