-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathseed_sandbox_table.py
More file actions
executable file
·394 lines (318 loc) · 12.7 KB
/
seed_sandbox_table.py
File metadata and controls
executable file
·394 lines (318 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#!/usr/bin/env python
"""
Seeds a sandbox table with realistic pointer data using sample templates.
Creates 2 pointers of each type for 2 different custodians, one of which is the custodian that all sandbox users are represented by.
"""
import copy
import csv
import json
import os
import sys
from datetime import datetime, timezone
from pathlib import Path
from typing import Any
import boto3
from botocore.exceptions import ClientError
# Needed for when the script is run in Lambda where modules are in scripts subdirectory
try:
from seed_utils import TestNhsNumbersIterator
except ImportError:
# In Lambda, modules are in scripts subdirectory
from scripts.seed_utils import TestNhsNumbersIterator
try:
import fire
except ImportError:
fire = None
try:
from nrlf.core.dynamodb.model import DocumentPointer
from nrlf.core.logger import logger
from nrlf.producer.fhir.r4.model import DocumentReference
logger.setLevel("ERROR")
except ImportError as e:
print(f"Warning: Failed to import NRLF modules: {e}")
raise
resource = boto3.resource("dynamodb")
SAMPLE_TEMPLATES = {
"1382601000000107": "QUY_RESPECT_FORM_Feb25.json",
"16521000000101": "G3H9E_LLOYD_GEORGE_RECORD_FOLDER_Aug25.json",
"2181441000000107": "11X_PERSONALISED_CARE_AND_SUPPORT_PLAN_Feb25.json",
"735324008": "11X_TREATMENT_ESCALATION_PLAN_Feb25.json",
"736253002": "RAT_MENTAL_HEALTH_PLAN_Feb25.json",
"736366004": "11X_ADVANCE_CARE_PLAN_Feb25.json",
"861421000000109": "VM8W7_EOL_COORDINATION_SUMMARY_Feb25.json",
"887701000000100": "B3H2B_EMERGENCY_HEALTHCARE_PLAN_Feb25.json",
}
# Y05868 is the test custodian required for int-sandbox, since it's the custodian that all sandbox users are represented by
CUSTODIANS = ["Y05868", "Y12345"]
AUTHOR = "X54321"
def _load_sample_template(filename: str) -> dict:
samples_dir = Path(__file__).parent.parent / "tests" / "data" / "samples"
filepath = samples_dir / filename
with open(filepath, "r") as f:
return json.load(f)
def _make_realistic_pointer(
template: dict,
custodian: str,
nhs_number: str,
counter: int,
) -> DocumentPointer:
doc_ref_dict = copy.deepcopy(template)
doc_ref_dict["id"] = f"{custodian}-SANDBOX-{str(counter).zfill(6)}"
doc_ref_dict["subject"]["identifier"]["value"] = nhs_number
doc_ref_dict["custodian"]["identifier"]["value"] = custodian
doc_ref_dict["author"][0]["identifier"]["value"] = AUTHOR
if "masterIdentifier" in doc_ref_dict:
doc_ref_dict["masterIdentifier"]["value"] = f"sandbox-{custodian}-{counter}"
retrieval_mechanism_ext = {
"url": "https://fhir.nhs.uk/England/StructureDefinition/Extension-England-NRLRetrievalMechanism",
"valueCodeableConcept": {
"coding": [
{
"system": "https://fhir.nhs.uk/England/CodeSystem/England-NRLRetrievalMechanism",
"code": "SSP",
"display": "Spine Secure Proxy",
}
]
},
}
if "content" not in doc_ref_dict or not doc_ref_dict["content"]:
doc_ref_dict["content"] = [{"extension": [retrieval_mechanism_ext]}]
else:
extensions = doc_ref_dict["content"][0].get("extension", [])
has_retrieval_mechanism = any(
ext.get("url")
== "https://fhir.nhs.uk/England/StructureDefinition/Extension-England-NRLRetrievalMechanism"
for ext in extensions
)
if not has_retrieval_mechanism:
if "extension" not in doc_ref_dict["content"][0]:
doc_ref_dict["content"][0]["extension"] = []
doc_ref_dict["content"][0]["extension"].append(retrieval_mechanism_ext)
doc_ref = DocumentReference(**doc_ref_dict)
pointer = DocumentPointer.from_document_reference(doc_ref, source="SANDBOX-SEED")
return pointer
def _validate_table_access(table_name: str):
"""Validate that the table exists and can be accessed"""
try:
table = resource.Table(table_name)
table.load()
return table
except ClientError as e:
error_code = e.response["Error"]["Code"]
if error_code == "ResourceNotFoundException":
print(f"Error: Table '{table_name}' does not exist")
sys.exit(1)
elif error_code == "AccessDeniedException":
print(f"Error: No permission to access table '{table_name}'")
sys.exit(1)
else:
print(f"Error accessing table: {e}")
sys.exit(1)
def _check_for_existing_sandbox_pointers(table, force: bool):
if force:
print("⚠️ Force mode enabled - will overwrite existing sandbox pointers")
return
try:
response = table.scan(
FilterExpression="begins_with(#src, :sandbox)",
ExpressionAttributeNames={"#src": "source"},
ExpressionAttributeValues={":sandbox": "SANDBOX"},
Limit=1,
ProjectionExpression="id",
)
if response.get("Items"):
print("\n⚠️ Warning: Sandbox pointers already exist in this table.")
print(
"Running this script will OVERWRITE any existing sandbox pointers that have the same IDs."
)
print("\nOptions:")
print(" 1. Use --force flag to overwrite existing pointers")
print(" 2. Use reset_sandbox_table.py to clear all items first")
print(" 3. Use delete_all_table_items.py to manually clear the table\n")
sys.exit(1)
except ClientError as e:
print(f"Warning: Could not check for existing pointers: {e}")
def _load_pointer_templates() -> dict[str, dict]:
templates = {}
for pointer_type, filename in SAMPLE_TEMPLATES.items():
try:
templates[pointer_type] = _load_sample_template(filename)
print(f"✓ Loaded template for type {pointer_type}")
except FileNotFoundError:
print(f"✗ Template file not found: {filename}")
continue
except json.JSONDecodeError as e:
print(f"✗ Invalid JSON in template {filename}: {e}")
continue
except Exception as e:
print(f"✗ Failed to load template {filename}: {e}")
continue
if not templates:
print("Error: No templates could be loaded. Exiting.")
sys.exit(1)
return templates
def _write_batch_to_dynamodb(table_name: str, batch_items: list[dict]) -> bool:
if not batch_items:
return True
try:
response = resource.batch_write_item(RequestItems={table_name: batch_items})
if response.get("UnprocessedItems"):
unprocessed = len(response["UnprocessedItems"].get(table_name, []))
print(f"\nWarning: {unprocessed} unprocessed items")
print(".", end="", flush=True)
return True
except ClientError as e:
error_code = e.response["Error"]["Code"]
if error_code == "ProvisionedThroughputExceededException":
print("\n✗ Throttled. Retrying batch...")
else:
print(f"\n✗ Error writing batch, batch cancelled: {e}")
return False
def _create_pointer_item(
template: dict,
custodian: str,
nhs_number: str,
counter: int,
pointer_type: str,
) -> tuple[dict | None, list[str] | None]:
"""Create a single pointer and return the put request and CSV data."""
try:
pointer = _make_realistic_pointer(template, custodian, nhs_number, counter)
put_req = {"PutRequest": {"Item": pointer.model_dump()}}
csv_data = [pointer.id, pointer_type, pointer.custodian, pointer.nhs_number]
return put_req, csv_data
except ValueError as e:
print(f"\n✗ Validation error for pointer {counter}: {e}")
return None, None
except Exception as e:
print(f"\n✗ Error creating pointer {counter}: {e}")
return None, None
def _generate_pointers_for_custodian(
template: dict,
pointer_type: str,
custodian: str,
pointers_per_type: int,
counter_start: int,
testnum_iter,
) -> tuple[list[dict], list[list[str]], int]:
batch_items = []
csv_rows = []
counter = counter_start
for _ in range(pointers_per_type):
counter += 1
try:
nhs_number = next(testnum_iter)
except StopIteration:
print(f"\n✗ Error: Ran out of NHS numbers at pointer {counter}")
break
put_req, csv_data = _create_pointer_item(
template, custodian, nhs_number, counter, pointer_type
)
if put_req and csv_data:
batch_items.append(put_req)
csv_rows.append(csv_data)
return batch_items, csv_rows, counter
def _generate_and_write_pointers(
table_name: str, templates: dict[str, dict], pointers_per_type: int, testnum_iter
) -> tuple[list[list[str]], int]:
"""Generate pointers and write them to DynamoDB in batches."""
counter = 0
pointer_data: list[list[str]] = []
batch_upsert_items: list[dict[str, Any]] = []
for (pointer_type, template), custodian in (
(item, cust) for item in templates.items() for cust in CUSTODIANS
):
batch_items, csv_rows, counter = _generate_pointers_for_custodian(
template,
pointer_type,
custodian,
pointers_per_type,
counter,
testnum_iter,
)
pointer_data.extend(csv_rows)
for item in batch_items:
batch_upsert_items.append(item)
if len(batch_upsert_items) >= 25:
_write_batch_to_dynamodb(table_name, batch_upsert_items)
batch_upsert_items = []
_write_batch_to_dynamodb(table_name, batch_upsert_items)
return pointer_data, counter
def seed_sandbox_table(
table_name: str,
pointers_per_type: int = 2,
force: bool = False,
write_csv: bool = True,
):
"""
Seed a sandbox table with realistic pointer data.
Args:
table_name: Name of the DynamoDB table to seed
pointers_per_type: Number of pointers per type per custodian (default: 2)
force: If True, overwrite existing sandbox pointers without prompting (default: False)
write_csv: If True, write pointer data to CSV file (default: True)
"""
print(
f"Seeding table {table_name} with {pointers_per_type} pointers per type per custodian"
)
print(f"Total pointer types: {len(SAMPLE_TEMPLATES)}")
print(f"Total custodians: {len(CUSTODIANS)}")
print(
f"Total pointers to create: {len(SAMPLE_TEMPLATES) * len(CUSTODIANS) * pointers_per_type}"
)
table = _validate_table_access(table_name)
_check_for_existing_sandbox_pointers(table, force)
testnum_cls = TestNhsNumbersIterator()
testnum_iter = iter(testnum_cls)
start_time = datetime.now(tz=timezone.utc)
templates = _load_pointer_templates()
pointer_data, total_attempts = _generate_and_write_pointers(
table_name, templates, pointers_per_type, testnum_iter
)
print("\n✓ Done!")
end_time = datetime.now(tz=timezone.utc)
duration = (end_time - start_time).total_seconds()
total_pointers_created = len(pointer_data)
print(
f"\nAttempted {total_attempts} pointers, successfully created {total_pointers_created}"
)
if total_attempts > total_pointers_created:
failed = total_attempts - total_pointers_created
print(f"⚠️ {failed} pointer(s) failed to create")
print(f"Completed in {duration:.2f} seconds")
if duration > 0:
print(f"Average: {total_pointers_created/duration:.2f} pointers/second")
if write_csv:
try:
_write_pointer_extract(pointer_data)
except Exception as e:
print(f"Warning: Failed to write CSV extract: {e}")
return {
"successful": total_pointers_created,
"attempted": total_attempts,
"failed": total_attempts - total_pointers_created,
}
def _write_pointer_extract(pointer_data: list[list[str]]):
try:
output_dir = Path(__file__).parent.parent / "dist" / "sandbox"
output_dir.mkdir(parents=True, exist_ok=True)
csv_file = (
output_dir
/ f"sandbox-pointers-{datetime.now().strftime('%Y%m%d_%H%M%S')}.csv"
)
with open(csv_file, "w") as f:
writer = csv.writer(f)
writer.writerow(["pointer_id", "pointer_type", "custodian", "nhs_number"])
writer.writerows(pointer_data)
print(f"Pointer data saved to {csv_file}")
except PermissionError:
print(f"Error: Permission denied writing to {output_dir}")
raise
except Exception as e:
print(f"Error writing CSV file: {e}")
raise
if __name__ == "__main__":
if fire is None:
print("Error: fire module not available")
sys.exit(1)
fire.Fire(seed_sandbox_table)