-
Notifications
You must be signed in to change notification settings - Fork 4
[NRL-1606] Add report to get masterids for pointers #1043
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mattdean3-nhs
merged 5 commits into
develop
from
feature/made14-NRL-1606-masterids-report
Sep 23, 2025
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d8b0a0e
[NRL-1606] Add report to get masterids for pointers
mattdean3-nhs 07ed678
[NRL-1606] Fix case where custodians param is not a tuple
mattdean3-nhs 8910f69
[NRL-1606] Fix sonarcloud warning in new report
mattdean3-nhs 857d7be
[NRL-1606] Switch report attributes list from tenery to if
mattdean3-nhs 63e5d45
[NRL-1606] Set patient_id in report to non-included if flag is not set
mattdean3-nhs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| import json | ||
| import os | ||
| from datetime import datetime, timedelta, timezone | ||
| from typing import Any | ||
|
|
||
| import boto3 | ||
| import fire | ||
|
|
||
| INCLUDE_PATIENT_IDS = os.environ.get("INCLUDE_PATIENT_IDS", "false").lower() == "true" | ||
|
|
||
| dynamodb = boto3.client("dynamodb") | ||
| paginator = dynamodb.get_paginator("scan") | ||
|
|
||
|
|
||
| def _get_masterids_for_custodians(table_name: str, custodians: str | tuple[str]) -> Any: | ||
| """ | ||
| Get masterids for pointers in the given table for a list of custodians. | ||
| Parameters: | ||
| - table_name: The name of the pointers table to use. | ||
| """ | ||
| custodian_list = ( | ||
| custodians.split(",") if isinstance(custodians, str) else list(custodians) | ||
| ) | ||
|
|
||
| print( # noqa | ||
| f"Getting masterids for custodians {custodian_list} in table {table_name}...." | ||
| ) | ||
|
|
||
| required_attributes = ["id", "type_id", "master_identifier", "custodian"] | ||
| if INCLUDE_PATIENT_IDS: | ||
| required_attributes.append("nhs_number") | ||
|
|
||
| expression_names_str = ",".join( | ||
| [f":param{custodian}" for custodian in custodian_list] | ||
| ) | ||
| expression_values_list = { | ||
| f":param{custodian}": {"S": custodian} for custodian in custodian_list | ||
| } | ||
|
|
||
| params: dict[str, Any] = { | ||
| "TableName": table_name, | ||
| "PaginationConfig": {"PageSize": 50}, | ||
| "FilterExpression": f"custodian IN ({expression_names_str})", | ||
| "ExpressionAttributeValues": expression_values_list, | ||
| "ProjectionExpression": ",".join(required_attributes), | ||
| } | ||
|
|
||
| pointers_info: list[dict[str, str]] = [] | ||
| total_scanned_count = 0 | ||
|
|
||
| start_time = datetime.now(tz=timezone.utc) | ||
|
|
||
| for page in paginator.paginate(**params): | ||
| for item in page["Items"]: | ||
| pointer_id = item.get("id", {}).get("S", "no-id") | ||
| pointer_type = item.get("type_id", {}).get("S", "no-type") | ||
| master_id = item.get("master_identifier", {}).get("S", "no-master-id") | ||
| custodian = item.get("custodian", {}).get("S", "no-custodian") | ||
| patient_id = item.get("nhs_number", {}).get("S", "no-patient-id") | ||
|
|
||
| pointers_info.append( | ||
| { | ||
| "nrl-id": pointer_id, | ||
| "pointer-type": pointer_type, | ||
| "master_identifier": master_id, | ||
| "custodian": custodian, | ||
| "patient_id": patient_id if INCLUDE_PATIENT_IDS else "not-included", | ||
| } | ||
| ) | ||
|
|
||
| total_scanned_count += page["ScannedCount"] | ||
|
|
||
| if total_scanned_count % 1000 == 0: | ||
| print(".", end="", flush=True) # noqa | ||
|
|
||
| if total_scanned_count % 100000 == 0: | ||
| print(f"scanned={total_scanned_count} found={len(pointers_info)} ") # noqa | ||
|
|
||
| end_time = datetime.now(tz=timezone.utc) | ||
|
|
||
| print(" Done") # noqa | ||
|
|
||
| print(f"Writing pointers to file ./pointer-masterids.txt ...") # noqa | ||
| with open("pointer-masterids.txt", "w") as f: | ||
| f.write(json.dumps(pointers_info, indent=2)) | ||
|
|
||
| return { | ||
| "output-file": "pointer-masterids.txt", | ||
| "pointers-found": len(pointers_info), | ||
| "scanned-count": total_scanned_count, | ||
| "took-secs": timedelta.total_seconds(end_time - start_time), | ||
| } | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| fire.Fire(_get_masterids_for_custodians) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.