22import time
33from datetime import datetime
44from pathlib import Path
5- from typing import Dict , List , Optional
5+ from typing import Dict , List , Optional , TypedDict , NotRequired
66import logging
77
88logger = logging .getLogger (__name__ )
99
10+ class OperationData (TypedDict ):
11+ """Type definition for operation data structure."""
12+ timestamp : str
13+ operation_type : str
14+ path : str
15+ environment : NotRequired [Optional [str ]]
16+ successful_files : List [str ]
17+ failed_files : List [str ]
18+ error : NotRequired [Optional [str ]]
19+ duration_ms : NotRequired [Optional [float ]]
20+
21+ class Metadata (TypedDict ):
22+ """Type definition for the metadata structure."""
23+ last_operation : Optional [OperationData ]
24+ history : List [OperationData ]
25+
1026class MetadataHandler :
1127 """Handles metadata storage and retrieval for sync operations.
1228
1329 This class manages a JSON file that stores the last 5 sync operations
1430 and maintains a record of the most recent operation with detailed information.
1531 """
1632
17- def __init__ (self , base_dir : Path , max_history : int = 5 ):
33+ def __init__ (self , base_dir : Path , max_history : int = 5 ) -> None :
1834 """Initialize the metadata handler.
1935
2036 Args:
@@ -29,13 +45,13 @@ def __init__(self, base_dir: Path, max_history: int = 5):
2945 def _ensure_metadata_file (self ) -> None :
3046 """Ensure the metadata file exists with proper structure."""
3147 if not self .metadata_file .exists ():
32- initial_data = {
48+ initial_data : Metadata = {
3349 "last_operation" : None ,
3450 "history" : []
3551 }
3652 self ._write_metadata (initial_data )
3753
38- def _read_metadata (self ) -> Dict :
54+ def _read_metadata (self ) -> Metadata :
3955 """Read the current metadata from file."""
4056 try :
4157 with open (self .metadata_file , 'r' ) as f :
@@ -44,7 +60,7 @@ def _read_metadata(self) -> Dict:
4460 logger .error (f"Error reading metadata file: { e } " )
4561 return {"last_operation" : None , "history" : []}
4662
47- def _write_metadata (self , data : Dict ) -> None :
63+ def _write_metadata (self , data : Metadata ) -> None :
4864 """Write metadata to file."""
4965 try :
5066 self .metadata_file .parent .mkdir (parents = True , exist_ok = True )
@@ -72,11 +88,11 @@ def log_operation(
7288 successful_files: List of successfully processed files
7389 failed_files: List of files that failed to process
7490 error: Any error message if the operation failed
75- start_time : Optional timestamp when the operation started (from time.time())
91+ duration_ms : Optional duration of the operation in milliseconds
7692 """
7793 current_time = datetime .now ().isoformat ()
7894
79- operation_data = {
95+ operation_data : OperationData = {
8096 "timestamp" : current_time ,
8197 "operation_type" : operation_type ,
8298 "path" : path ,
@@ -98,12 +114,12 @@ def log_operation(
98114
99115 self ._write_metadata (metadata )
100116
101- def get_last_operation (self ) -> Optional [Dict ]:
117+ def get_last_operation (self ) -> Optional [OperationData ]:
102118 """Get the most recent operation details."""
103119 metadata = self ._read_metadata ()
104120 return metadata .get ("last_operation" )
105121
106- def get_history (self ) -> List [Dict ]:
122+ def get_history (self ) -> List [OperationData ]:
107123 """Get the operation history."""
108124 metadata = self ._read_metadata ()
109125 return metadata .get ("history" , [])
0 commit comments