-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into AMB-2327-Major-pipeline-issue-fix
- Loading branch information
Showing
76 changed files
with
2,287 additions
and
235 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
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,67 @@ | ||
"""Add the filename to the audit table and check for duplicates.""" | ||
|
||
import os | ||
from typing import Union | ||
from boto3.dynamodb.conditions import Key,Attr | ||
from clients import dynamodb_client, dynamodb_resource, logger | ||
from errors import UnhandledAuditTableError | ||
|
||
|
||
def update_audit_table_status(file_key: str) -> str: | ||
""" | ||
Update the status in the audit table. | ||
""" | ||
try: | ||
table_name = os.environ["AUDIT_TABLE_NAME"] | ||
file_name_gsi = "filename_index" | ||
file_name_response = dynamodb_resource.Table(table_name).query( | ||
IndexName=file_name_gsi, KeyConditionExpression=Key("filename").eq(file_key) | ||
) | ||
items = file_name_response.get("Items", []) | ||
message_id = items[0].get("message_id") | ||
queue_name = items[0].get("queue_name") | ||
# Add to the audit table | ||
dynamodb_client.update_item( | ||
TableName=table_name, | ||
Key={"message_id": {"S": message_id}}, | ||
UpdateExpression="SET #status = :status", | ||
ExpressionAttributeNames={"#status": "status"}, | ||
ExpressionAttributeValues={":status": {"S": "Processed"}}, | ||
ConditionExpression="attribute_exists(message_id)" | ||
) | ||
logger.info("%s file, with message id %s, and the status successfully updated to audit table", file_key, message_id) | ||
return queue_name | ||
except Exception as error: # pylint: disable = broad-exception-caught | ||
error_message = error #f"Error adding {file_key} to the audit table" | ||
logger.error(error_message) | ||
raise UnhandledAuditTableError(error_message) from error | ||
|
||
def get_queued_file_details(queue_name: str) -> tuple[Union[None,str],Union[None,str]]: | ||
""" | ||
Check for queued files which return none or oldest file queued for processing. | ||
Returns a tuple in the format (file_name, message_id) for the oldest file. | ||
Defaults to (none,none) if no file found in queued status | ||
""" | ||
table_name = os.environ["AUDIT_TABLE_NAME"] | ||
queue_name_gsi = "queue_name_index" | ||
|
||
queue_response = dynamodb_resource.Table(table_name).query( | ||
IndexName=queue_name_gsi, | ||
KeyConditionExpression=Key("queue_name").eq(queue_name) | ||
& Key("status").eq("Queued"), | ||
) | ||
if queue_response["Items"]: | ||
file_name, message_id = get_file_name(queue_response) | ||
return file_name, message_id | ||
else: | ||
return None, None | ||
|
||
def get_file_name(queue_response: dict) -> tuple[str,str]: | ||
""" | ||
Returns (file_name, message_id) for the oldest file. | ||
""" | ||
sorted_item = sorted(queue_response["Items"], key=lambda x: x["timestamp"]) | ||
first_record = sorted_item[0] | ||
file_name = first_record.get("filename") | ||
message_id = first_record.get("message_id") | ||
return file_name, message_id |
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,29 @@ | ||
"""Custom exceptions for the Filename Processor.""" | ||
|
||
|
||
class DuplicateFileError(Exception): | ||
"""A custom exception for when it is identified that the file is a duplicate.""" | ||
|
||
|
||
class ProcessingError(Exception): | ||
"""A custom exception for when it is identified that supplier_vaccine file is under processing""" | ||
|
||
|
||
class UnhandledAuditTableError(Exception): | ||
"""A custom exception for when an unexpected error occurs whilst adding the file to the audit table.""" | ||
|
||
|
||
class VaccineTypePermissionsError(Exception): | ||
"""A custom exception for when the supplier does not have the necessary vaccine type permissions.""" | ||
|
||
|
||
class InvalidFileKeyError(Exception): | ||
"""A custom exception for when the file key is invalid.""" | ||
|
||
|
||
class InvalidSupplierError(Exception): | ||
"""A custom exception for when the supplier has not been correctly identified.""" | ||
|
||
|
||
class UnhandledSqsError(Exception): | ||
"""A custom exception for when an unexpected error occurs whilst sending a message to SQS.""" |
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,10 @@ | ||
"""Utils for ack lambda""" | ||
|
||
import os | ||
|
||
|
||
def get_environment() -> str: | ||
"""Returns the current environment. Defaults to internal-dev for pr and user environments""" | ||
_env = os.getenv("ENVIRONMENT") | ||
# default to internal-dev for pr and user environments | ||
return _env if _env in ["internal-dev", "int", "ref", "sandbox", "prod"] else "internal-dev" |
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.