33
44""" Library of code for status logs reused across various calling features """
55import os
6- from datetime import datetime , timedelta
6+ from datetime import datetime , timedelta , timezone
77import base64
88from enum import Enum
99import logging
@@ -169,7 +169,7 @@ def upsert_document(self, document_path, status, status_classification: StatusCl
169169 base_name = os .path .basename (document_path )
170170 document_id = self .encode_document_id (document_path )
171171
172- # add status to standard logger
172+ # Add status to standard logger
173173 logging .info ("%s DocumentID - %s" , status , document_id )
174174
175175 # If this event is the start of an upload, remove any existing status files for this path
@@ -181,26 +181,24 @@ def upsert_document(self, document_path, status, status_classification: StatusCl
181181
182182 json_document = ""
183183 try :
184- # if the document exists and if this is the first call to the function from the parent,
185- # then retrieve the stored document from cosmos, otherwise, use the log stored in self
184+ # If the document exists, retrieve it; otherwise, create a new one
186185 if self ._log_document .get (document_id , "" ) == "" :
187186 json_document = self .container .read_item (item = document_id , partition_key = base_name )
188187 else :
189188 json_document = self ._log_document [document_id ]
190189
191190 if json_document ['state' ] != state .value :
192191 json_document ['state' ] = state .value
193- json_document ['state_timestamp' ] = str (datetime
194- .now ()
195- .strftime ('%Y-%m-%d %H:%M:%S' ))
192+ json_document ['state_timestamp' ] = datetime .now (timezone .utc ).isoformat () # Use UTC
196193
197- # Update state description with latest status
194+ # Update state description with the latest status
198195 json_document ['state_description' ] = status
199- # Append a new item to the array
196+
197+ # Append a new item to the status updates array
200198 status_updates = json_document ["status_updates" ]
201199 new_item = {
202200 "status" : status ,
203- "status_timestamp" : str ( datetime .now (). strftime ( '%Y-%m-%d %H:%M:%S' )),
201+ "status_timestamp" : datetime .now (timezone . utc ). isoformat (), # Use UTC
204202 "status_classification" : str (status_classification .value )
205203 }
206204
@@ -210,49 +208,48 @@ def upsert_document(self, document_path, status, status_classification: StatusCl
210208
211209 except exceptions .CosmosResourceNotFoundError :
212210 if state != State .DELETED :
213- # this is a valid new document
211+ # Create a new document if it doesn't exist
214212 json_document = {
215213 "id" : document_id ,
216214 "file_path" : document_path ,
217215 "file_name" : base_name ,
218216 "state" : str (state .value ),
219- "start_timestamp" : str ( datetime .now (). strftime ( '%Y-%m-%d %H:%M:%S' )),
217+ "start_timestamp" : datetime .now (timezone . utc ). isoformat (), # Use UTC
220218 "state_description" : status ,
221- "state_timestamp" : str ( datetime .now (). strftime ( '%Y-%m-%d %H:%M:%S' )),
219+ "state_timestamp" : datetime .now (timezone . utc ). isoformat (), # Use UTC
222220 "status_updates" : [
223221 {
224222 "status" : status ,
225- "status_timestamp" : str ( datetime .now (). strftime ( '%Y-%m-%d %H:%M:%S' )),
223+ "status_timestamp" : datetime .now (timezone . utc ). isoformat (), # Use UTC
226224 "status_classification" : str (status_classification .value )
227225 }
228226 ]
229227 }
230228 elif state == State .DELETED :
231- # the status file was previously deleted. Do nothing.
232- logging .debug ("No record found for deleted document %s. Nothing to do." ,
233- document_path )
229+ # If the document was previously deleted, do nothing
230+ logging .debug ("No record found for deleted document %s. Nothing to do." , document_path )
234231 except Exception as err :
235- # log the exception with stack trace to the status log
232+ # Log the exception with a stack trace
236233 logging .error ("Unexpected exception upserting document %s" , str (err ))
237234 json_document = {
238235 "id" : document_id ,
239236 "file_path" : document_path ,
240237 "file_name" : base_name ,
241238 "state" : str (state .value ),
242- "start_timestamp" : str ( datetime .now (). strftime ( '%Y-%m-%d %H:%M:%S' )),
239+ "start_timestamp" : datetime .now (timezone . utc ). isoformat (), # Use UTC
243240 "state_description" : status ,
244- "state_timestamp" : str ( datetime .now (). strftime ( '%Y-%m-%d %H:%M:%S' )),
241+ "state_timestamp" : datetime .now (timezone . utc ). isoformat (), # Use UTC
245242 "status_updates" : [
246243 {
247244 "status" : status ,
248- "status_timestamp" : str ( datetime .now (). strftime ( '%Y-%m-%d %H:%M:%S' )),
245+ "status_timestamp" : datetime .now (timezone . utc ). isoformat (), # Use UTC
249246 "status_classification" : str (status_classification .value ),
250247 "stack_trace" : self .get_stack_trace () if not fresh_start else None
251248 }
252249 ]
253250 }
254251
255- #self.container.upsert_item(body=json_document)
252+ # Save the document to the in-memory log and CosmosDB
256253 self ._log_document [document_id ] = json_document
257254
258255 def update_document_state (self , document_path , status , state = State .PROCESSING ):
0 commit comments