Skip to content

Commit 9ac3092

Browse files
Chore: [AEA-5970] - merge reports (#2407)
## Summary - 🤖 Operational or Infrastructure Change ### Details Currently a simple script to be run by hand and a couple of test data files to prove it out. Follow up tickets will convert to automated system. --------- Co-authored-by: Connor Avery <[email protected]>
1 parent 6ba2010 commit 9ac3092

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#!/usr/bin/env python3
2+
import csv
3+
from pathlib import Path
4+
import argparse
5+
from datetime import datetime
6+
7+
COL_ODS_CODE = "pharmacyODSCode"
8+
READY_TO_COLLECT_PSU_COUNT = "ready_to_collect_psu_count"
9+
TOTAL_NOTIFICATIONS = "total notifications"
10+
NOTIFICATIONS_REQUESTED_COUNT = "notifications_requested_count"
11+
12+
13+
def main():
14+
parser = argparse.ArgumentParser(
15+
description="Merge ready_to_collect_psu_count and total notifications into status report by ODS code."
16+
)
17+
parser.add_argument(
18+
"date_suffix", help="Date suffix in format YYYY-MM-DD, e.g. 2025-10-28"
19+
)
20+
args = parser.parse_args()
21+
22+
date = args.date_suffix
23+
24+
# Validate date format
25+
try:
26+
datetime.strptime(date, '%Y-%m-%d')
27+
except ValueError:
28+
print("Error: date_suffix must be in format YYYY-MM-DD (e.g., 2025-10-28)")
29+
parser.print_help()
30+
return
31+
sent_dist_path = Path(
32+
f"data/Prescription_Notifications_Sent_Distribution_Repor-{date}.csv"
33+
)
34+
freq_dist_path = Path(
35+
f"data/Notification_Frequency_Distribution-{date}.csv"
36+
)
37+
status_path = Path(f"data/Prescription_Notifications_Status_Report-{date}.csv")
38+
output_path = Path(
39+
f"data/Prescription_Notifications_Status_Report_final-{date}.csv"
40+
)
41+
42+
# Read ready_to_collect_psu_count by ODS code
43+
sent_dist = {}
44+
with sent_dist_path.open(newline="", encoding="utf-8") as f:
45+
reader = csv.DictReader(f)
46+
for row in reader:
47+
sent_dist[row[COL_ODS_CODE].upper()] = row[READY_TO_COLLECT_PSU_COUNT]
48+
f.close()
49+
# Read total notifications by ODS code
50+
freq_dist = {}
51+
freq_dist_rows = {}
52+
with freq_dist_path.open(newline="", encoding="utf-8") as f:
53+
reader = csv.DictReader(f)
54+
for row in reader:
55+
ods_upper = row[COL_ODS_CODE].upper()
56+
freq_dist[ods_upper] = row[TOTAL_NOTIFICATIONS]
57+
freq_dist_rows[ods_upper] = row
58+
f.close()
59+
# Read status file, replace ready_to_collect_psu_count and
60+
# notifications_requested_count where ODS_CODE matches
61+
# Track which frequency distribution codes we've processed
62+
processed_freq_codes = set()
63+
64+
with status_path.open(newline="", encoding="utf-8") as f_in, output_path.open(
65+
"w", newline="", encoding="utf-8"
66+
) as f_out:
67+
reader = csv.DictReader(f_in)
68+
fieldnames = reader.fieldnames
69+
writer = csv.DictWriter(f_out, fieldnames=fieldnames)
70+
writer.writeheader()
71+
72+
# Process existing rows from status file
73+
for row in reader:
74+
ods = row[COL_ODS_CODE]
75+
ods_upper = ods.upper()
76+
77+
# Uppercase the ODS code in the output
78+
row[COL_ODS_CODE] = ods_upper
79+
80+
if ods_upper in sent_dist:
81+
row[READY_TO_COLLECT_PSU_COUNT] = sent_dist[ods_upper]
82+
if ods_upper in freq_dist:
83+
row[NOTIFICATIONS_REQUESTED_COUNT] = freq_dist[ods_upper]
84+
processed_freq_codes.add(ods_upper)
85+
else:
86+
row[NOTIFICATIONS_REQUESTED_COUNT] = "0"
87+
writer.writerow(row)
88+
f_out.close()
89+
print(f"Merged file written to {output_path}")
90+
91+
92+
if __name__ == "__main__":
93+
main()

0 commit comments

Comments
 (0)