-
Notifications
You must be signed in to change notification settings - Fork 4
[NRL-1311] Add script to set pointer supersede info. Add script to put pointers from JSON files #856
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 4 commits into
develop
from
feature/made14-NRL-1311-supersede-fixup-script
Mar 12, 2025
Merged
[NRL-1311] Add script to set pointer supersede info. Add script to put pointers from JSON files #856
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1836d7a
[NRL-1311] Add script to set pointer supersede info. Add script to pu…
mattdean3-nhs a576ed4
Merge branch 'develop' into feature/made14-NRL-1311-supersede-fixup-s…
mattdean3-nhs 18aab26
[NRL-1311] Add prod env warn to new scripts. Fix supersede typo
mattdean3-nhs ca426b5
[NRL-1311] Fix typo dev->prod
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,53 @@ | ||
| #!/usr/bin/env python | ||
| # Put pointers from the provided files into the pointers table | ||
| # This will overwrite the pointer if it already exists in the table | ||
| import json | ||
| import os | ||
|
|
||
| import fire | ||
| from aws_session_assume import get_boto_session | ||
|
|
||
| from nrlf.core.dynamodb.model import DocumentPointer | ||
| from nrlf.core.logger import logger | ||
| from nrlf.producer.fhir.r4.model import DocumentReference | ||
|
|
||
| logger.setLevel("ERROR") | ||
|
|
||
| SKIP_PROD_WARNING = os.getenv("SKIP_PROD_WARNING", "false") | ||
|
|
||
|
|
||
| def _put_pointers_from_files( | ||
| *filenames, env: str = "dev", table_name: str | None = None | ||
| ): | ||
| if env == "prod" and SKIP_PROD_WARNING != "true": | ||
| confirmation = input( | ||
| "\nWARNING - This command will modify the PROD environment. Continue? [y/n] " | ||
| ) | ||
| if confirmation != "y": | ||
| return "Exiting at user request" | ||
|
|
||
| docrefs: list[DocumentReference] = [] | ||
| print("Reading docrefs from files...") | ||
| for filename in filenames: | ||
| with open(filename) as f: | ||
| docref_json = json.load(f) | ||
| docref = DocumentReference.model_validate(docref_json) | ||
| docrefs.append(docref) | ||
|
|
||
| session = get_boto_session(env) | ||
| dynamodb = session.resource("dynamodb") | ||
| if not table_name: | ||
| table_name = f"nhsd-nrlf--{env}-pointers-table" | ||
| table = dynamodb.Table(table_name) | ||
|
|
||
| for docref in docrefs: | ||
| try: | ||
| print(f"Putting {docref.id}....") | ||
| pointer = DocumentPointer.from_document_reference(docref) | ||
| table.put_item(Item=pointer.model_dump()) | ||
| except Exception as e: | ||
| print(f"Unable to put pointer for {docref.id}. Error: {e}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| fire.Fire(_put_pointers_from_files) | ||
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,107 @@ | ||
| #!/usr/bin/env python | ||
| # Set supersede info on a pointer | ||
| import json | ||
| import os | ||
|
|
||
| import aws_session_assume | ||
| import fire | ||
|
|
||
| from nrlf.core.dynamodb.model import DocumentPointer | ||
| from nrlf.core.logger import logger | ||
| from nrlf.producer.fhir.r4.model import ( | ||
| DocumentReference, | ||
| DocumentReferenceRelatesTo, | ||
| Identifier, | ||
| Reference, | ||
| ) | ||
|
|
||
| logger.setLevel("ERROR") | ||
|
|
||
| SKIP_PROD_WARNING = os.getenv("SKIP_PROD_WARNING", "false") | ||
|
|
||
|
|
||
| def _set_pointer_supersede_info( | ||
| pointer_id: str, | ||
| supersede_pointer_id: str, | ||
| delete_superseded: bool = False, | ||
| env: str = "dev", | ||
| table_name: str | None = None, | ||
| ): | ||
| if env == "prod" and SKIP_PROD_WARNING != "true": | ||
| confirmation = input( | ||
| "\nWARNING - This command will modify the PROD environment. Continue? [y/n] " | ||
| ) | ||
| if confirmation != "y": | ||
| return "Exiting at user request" | ||
|
|
||
| session = aws_session_assume.get_boto_session(env) | ||
| dynamodb = session.resource("dynamodb") | ||
|
|
||
| if not table_name: | ||
| table_name = f"nhsd-nrlf--{env}-pointers-table" | ||
mattdean3-nhs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| table = dynamodb.Table(table_name) | ||
|
|
||
| print( | ||
| f"Setting pointer {pointer_id} in {table_name} to supersede {supersede_pointer_id}...." | ||
| ) | ||
|
|
||
| try: | ||
| doc_key = f"D#{pointer_id}" | ||
| print(f"Getting {pointer_id}...") | ||
| result = table.get_item( | ||
| Key={"pk": doc_key, "sk": doc_key}, | ||
| ) | ||
| except Exception as e: | ||
| print(f"Unable to get pointer. Error: {e}") | ||
| return | ||
|
|
||
| if "Item" not in result: | ||
| print(f"Unable to set superseded info. Pointer {pointer_id} not found.") | ||
| return | ||
|
|
||
| item = result["Item"] | ||
|
|
||
| try: | ||
| pointer = DocumentPointer.model_validate({"_from_dynamo": True, **item}) | ||
| doc_ref = DocumentReference.model_validate_json(pointer.document) | ||
| except Exception as e: | ||
| print(f"Could not validate pointer from table. Error: {e}") | ||
| return | ||
|
|
||
| if not doc_ref.relatesTo: | ||
| doc_ref.relatesTo = [] | ||
|
|
||
| existing_supersedes = [ | ||
| relates_to for relates_to in doc_ref.relatesTo if relates_to.code == "replaces" | ||
| ] | ||
| if existing_supersedes: | ||
| print( | ||
| f"Unable to add supersede info as pointer is already superseding a pointer: {existing_supersedes}" | ||
| ) | ||
| return | ||
|
|
||
| doc_ref.relatesTo.append( | ||
| DocumentReferenceRelatesTo( | ||
| code="replaces", | ||
| target=Reference( | ||
| type="DocumentReference", | ||
| identifier=Identifier(value=supersede_pointer_id), | ||
| ), | ||
| ) | ||
| ) | ||
|
|
||
| print(f"Adding supersede info to {pointer_id}...") | ||
| updated_pointer = DocumentPointer.from_document_reference(doc_ref) | ||
| table.put_item( | ||
| Item=updated_pointer.dict(exclude_none=True, exclude={"_from_dynamo"}) | ||
| ) | ||
|
|
||
| if delete_superseded: | ||
| print(f"Deleting superseded {supersede_pointer_id}...") | ||
| table.delete_item( | ||
| Key={"pk": f"D#{supersede_pointer_id}", "sk": f"D#{supersede_pointer_id}"} | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| fire.Fire(_set_pointer_supersede_info) | ||
Oops, something went wrong.
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.