Files
notifications-api/app/aws/s3.py

195 lines
6.0 KiB
Python
Raw Normal View History

2024-01-08 14:31:28 -08:00
import re
import botocore
2023-03-03 16:01:12 -05:00
from boto3 import Session
2024-01-05 10:35:14 -08:00
from expiringdict import ExpiringDict
2021-03-10 13:55:06 +00:00
from flask import current_app
from app import redis_store
from app.clients import AWS_CLIENT_CONFIG
2023-08-29 14:54:30 -07:00
FILE_LOCATION_STRUCTURE = "service-{}-notify/{}.csv"
2022-10-14 14:45:27 +00:00
2024-01-18 13:54:23 -08:00
JOBS = ExpiringDict(max_len=1000, max_age_seconds=3600 * 4)
2024-01-05 10:35:14 -08:00
JOBS_CACHE_HITS = "JOBS_CACHE_HITS"
JOBS_CACHE_MISSES = "JOBS_CACHE_MISSES"
2024-01-05 10:35:14 -08:00
2023-08-29 14:54:30 -07:00
def get_s3_file(bucket_name, file_location, access_key, secret_key, region):
s3_file = get_s3_object(bucket_name, file_location, access_key, secret_key, region)
2023-08-29 14:54:30 -07:00
return s3_file.get()["Body"].read().decode("utf-8")
2023-08-29 14:54:30 -07:00
def get_s3_object(bucket_name, file_location, access_key, secret_key, region):
session = Session(
aws_access_key_id=access_key,
aws_secret_access_key=secret_key,
2023-08-29 14:54:30 -07:00
region_name=region,
)
2023-08-29 14:54:30 -07:00
s3 = session.resource("s3", config=AWS_CLIENT_CONFIG)
return s3.Object(bucket_name, file_location)
2023-11-09 08:46:53 -08:00
def purge_bucket(bucket_name, access_key, secret_key, region):
session = Session(
aws_access_key_id=access_key,
aws_secret_access_key=secret_key,
region_name=region,
)
s3 = session.resource("s3", config=AWS_CLIENT_CONFIG)
bucket = s3.Bucket(bucket_name)
bucket.objects.all().delete()
2023-08-29 14:54:30 -07:00
def file_exists(bucket_name, file_location, access_key, secret_key, region):
try:
# try and access metadata of object
2023-08-29 14:54:30 -07:00
get_s3_object(
bucket_name, file_location, access_key, secret_key, region
).metadata
return True
except botocore.exceptions.ClientError as e:
2023-08-29 14:54:30 -07:00
if e.response["ResponseMetadata"]["HTTPStatusCode"] == 404:
return False
raise
def get_job_location(service_id, job_id):
return (
2023-08-29 14:54:30 -07:00
current_app.config["CSV_UPLOAD_BUCKET"]["bucket"],
FILE_LOCATION_STRUCTURE.format(service_id, job_id),
2023-08-29 14:54:30 -07:00
current_app.config["CSV_UPLOAD_BUCKET"]["access_key_id"],
current_app.config["CSV_UPLOAD_BUCKET"]["secret_access_key"],
current_app.config["CSV_UPLOAD_BUCKET"]["region"],
)
2019-11-08 10:30:26 +00:00
def get_job_and_metadata_from_s3(service_id, job_id):
obj = get_s3_object(*get_job_location(service_id, job_id))
2023-08-29 14:54:30 -07:00
return obj.get()["Body"].read().decode("utf-8"), obj.get()["Metadata"]
2019-11-08 10:30:26 +00:00
def get_job_from_s3(service_id, job_id):
obj = get_s3_object(*get_job_location(service_id, job_id))
2023-08-29 14:54:30 -07:00
return obj.get()["Body"].read().decode("utf-8")
def incr_jobs_cache_misses():
if not redis_store.get(JOBS_CACHE_MISSES):
redis_store.set(JOBS_CACHE_MISSES, 1)
else:
redis_store.incr(JOBS_CACHE_MISSES)
hits = redis_store.get(JOBS_CACHE_HITS).decode("utf-8")
misses = redis_store.get(JOBS_CACHE_MISSES).decode("utf-8")
current_app.logger.info(f"JOBS CACHE MISS hits {hits} misses {misses}")
def incr_jobs_cache_hits():
if not redis_store.get(JOBS_CACHE_HITS):
redis_store.set(JOBS_CACHE_HITS, 1)
else:
redis_store.incr(JOBS_CACHE_HITS)
hits = redis_store.get(JOBS_CACHE_HITS).decode("utf-8")
misses = redis_store.get(JOBS_CACHE_MISSES).decode("utf-8")
current_app.logger.info(f"JOBS CACHE MISS hits {hits} misses {misses}")
2024-01-18 13:54:23 -08:00
def extract_phones(job):
job = job.split("\r\n")
first_row = job[0]
job.pop(0)
first_row = first_row.split(",")
phone_index = 0
for item in first_row:
if item.lower() == "phone number":
break
phone_index = phone_index + 1
phones = {}
job_row = 0
for row in job:
row = row.split(",")
phone_index = 0
for item in first_row:
if item.lower() == "phone number":
break
phone_index = phone_index + 1
my_phone = row[phone_index]
my_phone = re.sub(r"[\+\s\(\)\-\.]*", "", my_phone)
phones[job_row] = my_phone
job_row = job_row + 1
return phones
2024-01-05 10:35:14 -08:00
def get_phone_number_from_s3(service_id, job_id, job_row_number):
# We don't want to constantly pull down a job from s3 every time we need a phone number.
# At the same time we don't want to store it in redis or the db
# So this is a little recycling mechanism to reduce the number of downloads.
job = JOBS.get(job_id)
if job is None:
job = get_job_from_s3(service_id, job_id)
JOBS[job_id] = job
incr_jobs_cache_misses()
else:
incr_jobs_cache_hits()
2024-01-18 13:54:23 -08:00
if job is None:
current_app.logger.warning(
"Couldnt find phone for job_id {job_id} row number {job_row_number} because job is missing"
)
return "Unknown Phone"
if JOBS.get(f"{job_id}_phones") is None:
JOBS[f"{job_id}_phones"] = extract_phones(job)
if JOBS.get(f"{job_id}_phones") is not None:
phone_to_return = JOBS.get(f"{job_id}_phones").get(job_row_number)
if phone_to_return:
return phone_to_return
2024-01-05 10:35:14 -08:00
job = job.split("\r\n")
first_row = job[0]
job.pop(0)
first_row = first_row.split(",")
phone_index = 0
for item in first_row:
if item.lower() == "phone number":
2024-01-05 10:35:14 -08:00
break
phone_index = phone_index + 1
2024-01-18 10:16:14 -08:00
2024-01-05 10:35:14 -08:00
correct_row = job[job_row_number]
correct_row = correct_row.split(",")
my_phone = correct_row[phone_index]
2024-01-08 14:31:28 -08:00
my_phone = re.sub(r"[\+\s\(\)\-\.]*", "", my_phone)
2024-01-18 13:54:23 -08:00
if not my_phone:
return "Unknown Phone"
2024-01-05 10:35:14 -08:00
return my_phone
def get_job_metadata_from_s3(service_id, job_id):
obj = get_s3_object(*get_job_location(service_id, job_id))
2023-08-29 14:54:30 -07:00
return obj.get()["Metadata"]
def remove_job_from_s3(service_id, job_id):
return remove_s3_object(*get_job_location(service_id, job_id))
def remove_s3_object(bucket_name, object_key, access_key, secret_key, region):
obj = get_s3_object(bucket_name, object_key, access_key, secret_key, region)
return obj.delete()
def remove_csv_object(object_key):
obj = get_s3_object(
2023-08-29 14:54:30 -07:00
current_app.config["CSV_UPLOAD_BUCKET"]["bucket"],
object_key,
2023-08-29 14:54:30 -07:00
current_app.config["CSV_UPLOAD_BUCKET"]["access_key_id"],
current_app.config["CSV_UPLOAD_BUCKET"]["secret_access_key"],
current_app.config["CSV_UPLOAD_BUCKET"]["region"],
)
return obj.delete()