mirror of
https://github.com/GSA/notifications-api.git
synced 2026-05-05 08:40:29 -04:00
Get ack files only from day before the ack file is received.
Take care of upper and lower case of file names and contents Add a test for s3 get_list_of_files_by_suffix
This commit is contained in:
@@ -109,7 +109,7 @@ def upload_letters_pdf(reference, crown, filedata):
|
||||
upload_file_name, current_app.config['LETTERS_PDF_BUCKET_NAME']))
|
||||
|
||||
|
||||
def get_list_of_files_by_suffix(bucket_name, subfolder='', suffix=''):
|
||||
def get_list_of_files_by_suffix(bucket_name, subfolder='', suffix='', last_modified=None):
|
||||
s3_client = client('s3', current_app.config['AWS_REGION'])
|
||||
paginator = s3_client.get_paginator('list_objects_v2')
|
||||
|
||||
@@ -120,6 +120,7 @@ def get_list_of_files_by_suffix(bucket_name, subfolder='', suffix=''):
|
||||
|
||||
for page in page_iterator:
|
||||
for obj in page['Contents']:
|
||||
key = obj['Key']
|
||||
if key.endswith(suffix):
|
||||
yield key
|
||||
key = obj['Key'].lower()
|
||||
if key.endswith(suffix.lower()):
|
||||
if not last_modified or obj['LastModified'] >= last_modified:
|
||||
yield key
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import pytz
|
||||
from datetime import (
|
||||
date,
|
||||
datetime,
|
||||
@@ -452,29 +453,39 @@ def daily_stats_template_usage_by_month():
|
||||
@notify_celery.task(name='raise-alert-if-no-letter-ack-file')
|
||||
@statsd(namespace="tasks")
|
||||
def letter_raise_alert_if_no_ack_file_for_zip():
|
||||
# get a list of today's zip files
|
||||
# get a list of zip files since yesterday
|
||||
zip_file_list = []
|
||||
|
||||
for key in s3.get_list_of_files_by_suffix(bucket_name=current_app.config['LETTERS_PDF_BUCKET_NAME'],
|
||||
subfolder=datetime.utcnow().strftime('%Y-%m-%d'), suffix='.ZIP'):
|
||||
subfolder=datetime.utcnow().strftime('%Y-%m-%d'),
|
||||
suffix='.zip'):
|
||||
zip_file_list.append(key)
|
||||
|
||||
# get acknowledgement file
|
||||
ack_file_list = []
|
||||
yesterday = datetime.now(tz=pytz.utc) - timedelta(days=1)
|
||||
for key in s3.get_list_of_files_by_suffix(bucket_name=current_app.config['DVLA_RESPONSE_BUCKET_NAME'],
|
||||
subfolder='root/dispatch', suffix='.ACK.txt'):
|
||||
subfolder='root/dispatch', suffix='.ACK.txt', lastModified=yesterday):
|
||||
ack_file_list.append(key)
|
||||
|
||||
todaystr = datetime.utcnow().strftime('%Y%m%d')
|
||||
today_str = datetime.utcnow().strftime('%Y%m%d')
|
||||
zip_not_today = []
|
||||
|
||||
for key in ack_file_list:
|
||||
if todaystr in key:
|
||||
if today_str in key:
|
||||
content = s3.get_s3_file(current_app.config['DVLA_RESPONSE_BUCKET_NAME'], key)
|
||||
|
||||
for zip_file in content.split('\n'): # each line
|
||||
s = zip_file.split('|')
|
||||
for zf in zip_file_list:
|
||||
if s[0] in zf:
|
||||
if s[0].lower() in zf.lower():
|
||||
zip_file_list.remove(zf)
|
||||
else:
|
||||
zip_not_today.append(s[0])
|
||||
|
||||
if zip_file_list:
|
||||
raise NoAckFileReceived(message=zip_file_list)
|
||||
|
||||
if zip_not_today:
|
||||
current_app.logger.info(
|
||||
"letter ack contains zip that is not for today {} ".format(zip_not_today)
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user