Add command to get notification details from 'zips sent' files

Added a command to get more detailed information about the letters that
are listed in the files of zip files sent that are stored on S3. This takes
one or more file paths as arguments and creates a CSV file with a row for each
letter.
This commit is contained in:
Katie Smith
2019-04-04 15:16:56 +01:00
parent 7feb2f128d
commit 1f1c94be9d

View File

@@ -1,3 +1,4 @@
import csv
import functools
import uuid
from datetime import datetime, timedelta
@@ -720,3 +721,39 @@ def populate_organisations_from_file(file_name):
except IntegrityError:
print("duplicate domain", d.strip())
db.session.rollback()
@notify_command(name='get-letter-details-from-zips-sent-file')
@click.argument('file_paths', required=True, nargs=-1)
@statsd(namespace="tasks")
def get_letter_details_from_zips_sent_file(file_paths):
"""Get notification details from letters listed in zips_sent file(s)
This takes one or more file paths for the zips_sent files in S3 as its parameters, for example:
get-letter-details-from-zips-sent-file '2019-04-01/zips_sent/filename_1' '2019-04-01/zips_sent/filename_2'
"""
rows_from_file = []
for path in file_paths:
file_contents = s3.get_s3_file(
bucket_name=current_app.config['LETTERS_PDF_BUCKET_NAME'],
file_location=path
)
rows_from_file.extend(json.loads(file_contents))
notification_references = tuple(row[18:34] for row in rows_from_file)
sql = """
SELECT id, service_id, reference, job_id, created_at
FROM notifications
WHERE reference IN :notification_references
ORDER BY service_id, job_id"""
result = db.session.execute(sql, {'notification_references': notification_references}).fetchall()
with open('zips_sent_details.csv', 'w') as csvfile:
csv_writer = csv.writer(csvfile)
csv_writer.writerow(['notification_id', 'service_id', 'reference', 'job_id', 'created_at'])
for row in result:
csv_writer.writerow(row)