@@ -31,6 +31,10 @@ def _find_invalid_pointers(table_name: str) -> dict[str, Any]:
3131 params : dict [str , Any ] = {
3232 "TableName" : table_name ,
3333 "PaginationConfig" : {"PageSize" : 50 },
34+ "FilterExpression" : "created_on < :date" ,
35+ "ExpressionAttributeValues" : {
36+ ":date" : {"S" : "2025-01-20T00:00:00.000000+0000" }
37+ }
3438 }
3539
3640 invalid_pointers = []
@@ -41,6 +45,11 @@ def _find_invalid_pointers(table_name: str) -> dict[str, Any]:
4145 for page in paginator .paginate (** params ):
4246 for item in page ["Items" ]:
4347 pointer_id = item .get ("id" , {}).get ("S" )
48+ created_on = item .get ("created_on" , {}).get ("S" )
49+ # parse datetime from created_on string
50+ created_on = datetime .strptime (created_on , "%Y-%m-%dT%H:%M:%S.%f%z" )
51+ if created_on > datetime (2025 , 1 , 20 , tzinfo = timezone .utc ):
52+ continue
4453 document = item .get ("document" , {}).get ("S" , "" )
4554 try :
4655 _validate_document (document )
@@ -146,5 +155,82 @@ def _find_and_delete_invalid_pointers(table_name: str) -> dict[str, float | int]
146155 return {** find_result , ** delete_result }
147156
148157
158+ def _fix_invalid_pointers (table_name : str ) -> dict [str , Any ]:
159+ print (f"Finding and fixing invalid pointers in table { table_name } ...." )
160+
161+ params : dict [str , Any ] = {
162+ "TableName" : table_name ,
163+ "PaginationConfig" : {"PageSize" : 50 },
164+ "FilterExpression" : "created_on < :date" ,
165+ "ExpressionAttributeValues" : {
166+ ":date" : {"S" : "2025-01-20T00:00:00.000000+0000" }
167+ }
168+ }
169+
170+ fixed_pointers = []
171+ total_scanned_count = 0
172+ total_fixed_count = 0
173+
174+ start_time = datetime .now (tz = timezone .utc )
175+
176+ for page in paginator .paginate (** params ):
177+ for item in page ["Items" ]:
178+ pointer_id = item .get ("id" , {}).get ("S" )
179+ created_on = item .get ("created_on" , {}).get ("S" )
180+ # parse datetime from created_on string
181+ created_on = datetime .strptime (created_on , "%Y-%m-%dT%H:%M:%S.%f%z" )
182+ if created_on > datetime (2025 , 1 , 20 , tzinfo = timezone .utc ):
183+ continue
184+ document = item .get ("document" , {}).get ("S" , "" )
185+ try :
186+ docref = DocumentReference .model_validate_json (document )
187+ if (
188+ docref .type .coding [0 ].display == "Mental Health Crisis plan"
189+ ):
190+ #print(f"Fixing document {pointer_id}")
191+ docref .type .coding [0 ].display = "Mental health crisis plan"
192+ resource .Table (table_name ).update_item (
193+ Key = {"pk" : f"D#{ pointer_id } " , "sk" : f"D#{ pointer_id } " },
194+ UpdateExpression = "SET document = :d" ,
195+ ExpressionAttributeValues = {":d" : docref .json ()},
196+ )
197+ fixed_pointers .append (pointer_id )
198+ total_fixed_count += 1
199+ except Exception as exc :
200+ print (f"Failed to fix document { pointer_id } : { exc } " )
201+
202+ total_scanned_count += page ["ScannedCount" ]
203+
204+ if total_fixed_count % 100 == 0 :
205+ print ("x" , end = "" , flush = True )
206+
207+ if total_scanned_count % 1000 == 0 :
208+ print ("." , end = "" , flush = True )
209+
210+ if total_scanned_count % 100000 == 0 :
211+ print (f"scanned={ total_scanned_count } fixed={ len (fixed_pointers )} " )
212+
213+ end_time = datetime .now (tz = timezone .utc )
214+
215+ print (f" Done. Fixed { len (fixed_pointers )} invalid pointers" )
216+
217+ #save fixed pointers to file
218+ if len (fixed_pointers ) > 0 :
219+ print ("Writing fixed pointers IDs to file ./fixed_pointers.txt ..." )
220+ with open ("fixed_pointers.txt" , "w" ) as f :
221+ for _id in fixed_pointers :
222+ f .write (f"{ _id } \n " )
223+
224+ return {
225+ "fixed_pointers" : fixed_pointers ,
226+ "scanned_count" : total_scanned_count ,
227+ "fix-took-secs" : timedelta .total_seconds (end_time - start_time ),
228+ }
229+
230+
149231if __name__ == "__main__" :
150- fire .Fire (_find_and_delete_invalid_pointers )
232+ fire .Fire ({
233+ "find_and_delete_invalid_pointers" : _find_and_delete_invalid_pointers ,
234+ "fix_invalid_pointers" : _fix_invalid_pointers ,
235+ "find_invalid_pointers" : _find_invalid_pointers ,
236+ })
0 commit comments