function print_sync_info
Reads and displays synchronization log information from a JSON file in a replica directory, including last sync time, root hash, and number of nodes synced.
/tf/active/vicechatdev/e-ink-llm/cloudtest/analyze_replica.py
239 - 257
simple
Purpose
This function provides a user-friendly display of synchronization status for a replica directory. It reads a 'sync_log.json' file and prints formatted information about the last synchronization operation, including timestamp, root hash (truncated for readability), and the count of synced nodes. It handles missing files and read errors gracefully with warning messages.
Source Code
def print_sync_info(replica_dir: Path):
"""Print sync log information"""
sync_log_file = replica_dir / "sync_log.json"
if not sync_log_file.exists():
print(f"ā ļø No sync log found")
return
try:
with open(sync_log_file, 'r', encoding='utf-8') as f:
sync_log = json.load(f)
print(f"\nš SYNC INFORMATION:")
print(f" š Last sync: {sync_log.get('last_sync', 'unknown')}")
print(f" š Root hash: {sync_log.get('root_hash', 'unknown')[:16]}...")
print(f" š Nodes synced: {sync_log.get('nodes_synced', 0)}")
except Exception as e:
print(f"ā ļø Error reading sync log: {e}")
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
replica_dir |
Path | - | positional_or_keyword |
Parameter Details
replica_dir: A Path object pointing to the replica directory that contains the 'sync_log.json' file. This directory should be the root of a replica structure where synchronization logs are stored. The function will look for a file named 'sync_log.json' directly within this directory.
Return Value
This function returns None. It performs side effects by printing formatted synchronization information to stdout using print() statements. Output includes emoji-prefixed status messages showing last sync time, root hash (first 16 characters), and number of nodes synced, or warning messages if the log file is missing or cannot be read.
Dependencies
jsonpathlib
Required Imports
import json
from pathlib import Path
Usage Example
from pathlib import Path
import json
# Setup: Create a sample replica directory with sync log
replica_path = Path('./my_replica')
replica_path.mkdir(exist_ok=True)
# Create a sample sync_log.json file
sync_data = {
'last_sync': '2024-01-15 14:30:00',
'root_hash': 'abc123def456ghi789jkl012mno345pqr678',
'nodes_synced': 42
}
with open(replica_path / 'sync_log.json', 'w', encoding='utf-8') as f:
json.dump(sync_data, f)
# Use the function
print_sync_info(replica_path)
# Output:
# š SYNC INFORMATION:
# š Last sync: 2024-01-15 14:30:00
# š Root hash: abc123def456ghi7...
# š Nodes synced: 42
Best Practices
- Ensure the replica_dir Path object points to a valid directory before calling this function
- The sync_log.json file should follow the expected schema with 'last_sync', 'root_hash', and 'nodes_synced' keys
- This function is designed for console output and uses emoji characters - ensure your terminal supports UTF-8 encoding
- The function gracefully handles missing files and JSON parsing errors, making it safe to call even if the sync log doesn't exist
- The root hash is truncated to 16 characters for display purposes - if you need the full hash, consider modifying the function or reading the file directly
- This is a display-only function with no return value - use it for logging/monitoring purposes, not for programmatic access to sync data
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function print_status 63.9% similar
-
function analyze_logs 56.1% similar
-
function main_v28 53.8% similar
-
function sync_directory 53.0% similar
-
function print_database_analysis 52.5% similar