Skip to content

Commit

Permalink
better output for consolidate_eg_requests and accept non-list input
Browse files Browse the repository at this point in the history
  • Loading branch information
abelsonlive committed Sep 14, 2024
1 parent b59faee commit 10f716b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
36 changes: 31 additions & 5 deletions core/bam_core/functions/consolidate_eg_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import List, Dict, Any

from .base import Function
from bam_core.utils.etc import to_bool
from bam_core.utils.etc import to_bool, to_list
from bam_core.constants import (
EG_REQUESTS_SCHEMA,
EG_REQUESTS_FIELD,
Expand Down Expand Up @@ -35,6 +35,12 @@
"furniture": FURNITURE_REQUESTS_FIELD,
}

STATUS_FIELD_MAP = {
"eg": EG_STATUS_FIELD,
"kitchen": EG_STATUS_FIELD,
"furniture": EG_STATUS_FIELD,
}


class ConsolidateEssentialGoodsRequests(Function):
"""
Expand Down Expand Up @@ -115,7 +121,7 @@ def consolidate_view(
delivered_tag: str,
source_view: str,
target_views: List[str],
status_field: str = EG_STATUS_FIELD,
status_field: str,
dry_run: bool = False,
) -> Dict[str, Counter]:
"""
Expand Down Expand Up @@ -243,11 +249,17 @@ def run(self, event, context):
raise ValueError(
f"Invalid REQUEST_VALUE {request_field}: {request_value}. Choose from: {schema['items'].keys()}"
)
# get the status field
status_field = STATUS_FIELD_MAP.get(request_field_shorthand)

# get the timeout flag from the schema
timeout_tag = schema["items"][request_value]["timeout"]
delivered_tag = schema["items"][request_value]["delivered"]

# parse source + target views to be a list
source_view = event["SOURCE_VIEW"].strip()
target_views = [t.strip() for t in to_list(event["TARGET_VIEWS"])]

# parse dry run flag
dry_run = to_bool(event.get("DRY_RUN", True))
if dry_run:
Expand All @@ -261,13 +273,27 @@ def run(self, event, context):
request_value=request_value,
timeout_tag=timeout_tag,
delivered_tag=delivered_tag,
source_view=event["SOURCE_VIEW"],
target_views=event["TARGET_VIEWS"],
source_view=source_view,
target_views=target_views,
status_field=status_field,
dry_run=dry_run,
)
results = {
"parameters_raw": event,
"parameters_parsed": {
"request_field": request_field,
"request_value": request_value,
"timeout_tag": timeout_tag,
"delivered_tag": delivered_tag,
"source_view": source_view,
"target_views": target_views,
"dry_run": dry_run,
},
"stats": consolidate_stats,
}
log.info("Consolidation finished with stats:\n")
pprint(consolidate_stats)
return consolidate_stats
return results


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion core/bam_core/functions/timeout_eg_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ def run(self, event, context):
"status_field": status_field,
"dry_run": dry_run,
},
"stats": timeout_stats
"stats": timeout_stats,
}
return result

Expand Down
4 changes: 2 additions & 2 deletions core/bam_core/utils/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,9 @@ def clean_email(email: str) -> str:
email = email.replace("@@", "@")

# check for @ symbol in username
at_count = email.count('@')
at_count = email.count("@")
if at_count > 1:
email = email.replace('@', '.', at_count-1)
email = email.replace("@", ".", at_count - 1)

return email

Expand Down
2 changes: 1 addition & 1 deletion core/bam_core/utils/etc.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def to_bool(val: Union[str, bool]) -> bool:
"""
Convert a string representation of truth to true or false.
True values are 'y', 'yes', 't', 'true', 'on', and '1'
False values are 'n', 'no', 'f', 'false', 'off', and '0'.
False values are 'n', 'no', 'f', 'false', 'off', and '0'.
Raises ValueError if 'val' is anything else.
:param val: A value
:return bool
Expand Down

0 comments on commit 10f716b

Please sign in to comment.