-
Notifications
You must be signed in to change notification settings - Fork 5
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
chore: refactor common code across lambda functions and fixes incorrect typing #403
Changes from 4 commits
c6d44f0
df9bae4
95eaaeb
892121d
47a7307
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,12 +12,13 @@ | |
from openpyxl.worksheet.worksheet import Worksheet | ||
from pydantic import BaseModel | ||
|
||
from src.lib.constants import OUTPUT_TEMPLATE_FILENAME_BY_PROJECT | ||
from src.lib.logging import get_logger, reset_contextvars | ||
from src.lib.s3_helper import download_s3_object, upload_generated_file_to_s3 | ||
from src.lib.treasury_generation_common import ( | ||
OrganizationObj, | ||
OutputFileType, | ||
UserObj, | ||
get_generated_output_file_key, | ||
get_output_template, | ||
) | ||
from src.lib.workbook_utils import convert_xlsx_to_csv | ||
|
@@ -177,6 +178,10 @@ def process_event(payload: ProjectLambdaPayload, logger): | |
with tempfile.NamedTemporaryFile() as file: | ||
# Download projects from S3 | ||
created_at = file_info.createdAt | ||
|
||
# TODO: Add a verification to ensure that the objectKey here is formatted correctly. | ||
# Path must be: uploads/{organization_id}/{agency_id}/{reporting_period_id}/{upload_id}/{filename}.xlsm | ||
|
||
download_s3_object( | ||
s3_client, | ||
os.environ["REPORTING_DATA_BUCKET_NAME"], | ||
|
@@ -200,13 +205,16 @@ def process_event(payload: ProjectLambdaPayload, logger): | |
|
||
### 5) Save data | ||
# Output XLSX file | ||
file_destination_prefix = f"treasuryreports/{organization.id}/{organization.preferences.current_reporting_period_id}/{OUTPUT_TEMPLATE_FILENAME_BY_PROJECT[project_use_code]}" | ||
with tempfile.NamedTemporaryFile("w") as new_output_file: | ||
output_workbook.save(new_output_file.name) | ||
upload_generated_file_to_s3( | ||
client=s3_client, | ||
bucket=os.environ["REPORTING_DATA_BUCKET_NAME"], | ||
key=f"{file_destination_prefix}.xlsx", | ||
key=get_generated_output_file_key( | ||
file_type=OutputFileType.XLSX, | ||
project=project_use_code, | ||
organization=organization, | ||
), | ||
file=new_output_file, | ||
) | ||
# Output CSV file for treasury | ||
|
@@ -215,16 +223,24 @@ def process_event(payload: ProjectLambdaPayload, logger): | |
upload_generated_file_to_s3( | ||
client=s3_client, | ||
bucket=os.environ["REPORTING_DATA_BUCKET_NAME"], | ||
key=f"{file_destination_prefix}.csv", | ||
key=get_generated_output_file_key( | ||
file_type=OutputFileType.CSV, | ||
project=project_use_code, | ||
organization=organization, | ||
), | ||
file=csv_file, | ||
) | ||
# Store project_id_agency_id to row number in a json file | ||
with tempfile.NamedTemporaryFile("w") as json_file: | ||
json_file = json.dump(project_agency_id_to_row_map) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @vshia would be great to get your input here. I updated this based on typing errors I noticed. However I wasn't sure if this breaks any of the existing functionality. |
||
json.dump(project_agency_id_to_row_map, fp=json_file) | ||
upload_generated_file_to_s3( | ||
client=s3_client, | ||
bucket=os.environ["REPORTING_DATA_BUCKET_NAME"], | ||
key=f"{file_destination_prefix}.json", | ||
key=get_generated_output_file_key( | ||
file_type=OutputFileType.JSON, | ||
project=project_use_code, | ||
organization=organization, | ||
), | ||
file=json_file, | ||
) | ||
|
||
|
@@ -244,7 +260,11 @@ def download_output_file( | |
download_s3_object( | ||
client=s3_client, | ||
bucket=os.environ["REPORTING_DATA_BUCKET_NAME"], | ||
key=f"treasuryreports/{organization.id}/{organization.preferences.current_reporting_period_id}/{OUTPUT_TEMPLATE_FILENAME_BY_PROJECT[project_use_code]}.xlsx", | ||
key=get_generated_output_file_key( | ||
file_type=OutputFileType.XLSX, | ||
project=project_use_code, | ||
organization=organization, | ||
), | ||
destination=output_file, | ||
) | ||
highest_row_num = max(project_agency_id_to_row_map.values()) | ||
|
@@ -272,7 +292,11 @@ def get_existing_output_metadata( | |
download_s3_object( | ||
s3_client, | ||
os.environ["REPORTING_DATA_BUCKET_NAME"], | ||
f"treasuryreports/{organization.id}/{organization.preferences.current_reporting_period_id}/{OUTPUT_TEMPLATE_FILENAME_BY_PROJECT[project_use_code]}.json", | ||
get_generated_output_file_key( | ||
file_type=OutputFileType.JSON, | ||
project=project_use_code, | ||
organization=organization, | ||
), | ||
existing_file, | ||
) | ||
except Exception: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import os | ||
from enum import Enum | ||
from typing import IO | ||
|
||
from mypy_boto3_s3.client import S3Client | ||
|
@@ -22,6 +23,17 @@ class UserObj(BaseModel): | |
id: int | ||
email: str | ||
|
||
class OutputFileType(Enum): | ||
XLSX = "xlsx" | ||
CSV = "csv" | ||
JSON = "json" | ||
|
||
|
||
def get_generated_output_file_key( | ||
file_type: OutputFileType, project: str, organization: OrganizationObj | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. any appetite for making There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💯 - I'll create a separate PR with this updated |
||
) -> str: | ||
return f"treasuryreports/{organization.id}/{organization.preferences.current_reporting_period_id}/{OUTPUT_TEMPLATE_FILENAME_BY_PROJECT[project]}.{file_type.value}" | ||
|
||
|
||
def get_output_template( | ||
s3_client: S3Client, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could potentially borrow logic here from
matchRegex
inprocessValidationJson
, which has the same key structureThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💯 - will address in a separate PR