from datetime import datetime, date from typing import Callable from botocore.client import BaseClient from utility import parse_s3_path date_format = "%Y%m%d" def get_check(check_type) -> Callable[[BaseClient, str], bool]: """Returns check callable based on passed string""" match check_type: case "last_file_uploaded_today": return last_file_uploaded_today case "last_file_name_contains_current_date": return last_file_name_contains_current_date case _: raise ValueError(f"Check type {check_type} doesn't exist") def last_file_uploaded_today(client: BaseClient, s3_path: str) -> bool: bucket_name, prefix = parse_s3_path(s3_path) last_uploaded_date = None continuation_token = "" while True: if continuation_token != "": response = client.list_objects_v2( Bucket=bucket_name, Prefix=prefix, ContinuationToken=continuation_token, ) else: response = client.list_objects_v2( Bucket=bucket_name, Prefix=prefix, ) if "Contents" in response: objects = response["Contents"] for obj in objects: if not last_uploaded_date or obj["LastModified"] > last_uploaded_date: last_uploaded_date = obj["LastModified"] if not response["IsTruncated"]: break continuation_token = response["NextContinuationToken"] if last_uploaded_date: today = date.today() last_uploaded_date = last_uploaded_date.date() return last_uploaded_date == today return False def last_file_name_contains_current_date(client: BaseClient, s3_path: str) -> bool: bucket_name, prefix = parse_s3_path(s3_path) continuation_token = "" today = date.today() while True: if continuation_token != "": response = client.list_objects_v2( Bucket=bucket_name, Prefix=prefix, ContinuationToken=continuation_token, ) else: response = client.list_objects_v2( Bucket=bucket_name, Prefix=prefix, ) if "Contents" in response: objects = response["Contents"] for obj in objects: # Get only date from the name, trim prefix and minutes/seconds date_in_name = ( obj["Key"].rsplit(".")[0].replace(prefix, "").split("T")[0] ) parsed_date = (datetime.strptime(date_in_name, date_format)).date() if parsed_date == today: return True if not response["IsTruncated"]: break continuation_token = response["NextContinuationToken"] return False