Fix timezone issue in cluster client uptime calculation

- Store connected_at as proper UTC timestamp in database using FROM_UNIXTIME/datetime
- Update web interface to handle datetime objects and timestamps correctly
- Ensure uptime starts from actual connection time, not offset by timezone
parent 3abb4c58
......@@ -1656,15 +1656,14 @@ def save_cluster_client(client_id: str, token: str, hostname: str, ip_address: s
if connected_at is None:
connected_at = time.time()
# Convert to datetime string for database storage
from datetime import datetime
connected_at_str = datetime.utcfromtimestamp(connected_at).strftime('%Y-%m-%d %H:%M:%S')
# Store as Unix timestamp (float) to avoid timezone issues
connected_at_value = connected_at
config = get_db_config()
if config['type'] == 'mysql':
cursor.execute('''
INSERT INTO cluster_clients (client_id, token, hostname, ip_address, weight, gpu_info, available_backends, connected, connected_at, last_seen)
VALUES (?, ?, ?, ?, ?, ?, ?, 1, ?, CURRENT_TIMESTAMP)
VALUES (?, ?, ?, ?, ?, ?, ?, 1, FROM_UNIXTIME(?), CURRENT_TIMESTAMP)
ON DUPLICATE KEY UPDATE
hostname = VALUES(hostname),
ip_address = VALUES(ip_address),
......@@ -1672,16 +1671,16 @@ def save_cluster_client(client_id: str, token: str, hostname: str, ip_address: s
gpu_info = VALUES(gpu_info),
available_backends = VALUES(available_backends),
connected = 1,
connected_at = VALUES(connected_at),
connected_at = FROM_UNIXTIME(?),
last_seen = CURRENT_TIMESTAMP
''', (client_id, token, hostname, ip_address, weight, gpu_info_json, available_backends_json, connected_at_str))
''', (client_id, token, hostname, ip_address, weight, gpu_info_json, available_backends_json, connected_at_value, connected_at_value))
else:
# For SQLite
cursor.execute('''
INSERT OR REPLACE INTO cluster_clients
(client_id, token, hostname, ip_address, weight, gpu_info, available_backends, connected, connected_at, last_seen)
VALUES (?, ?, ?, ?, ?, ?, ?, 1, ?, CURRENT_TIMESTAMP)
''', (client_id, token, hostname, ip_address, weight, gpu_info_json, available_backends_json, connected_at_str))
VALUES (?, ?, ?, ?, ?, ?, ?, 1, datetime(?, 'unixepoch'), CURRENT_TIMESTAMP)
''', (client_id, token, hostname, ip_address, weight, gpu_info_json, available_backends_json, connected_at_value))
conn.commit()
success = cursor.rowcount > 0
......
......@@ -447,6 +447,10 @@ def api_cluster_nodes():
# Parse timestamp string
import datetime
connected_at = datetime.datetime.fromisoformat(connected_at.replace('Z', '+00:00')).timestamp()
elif hasattr(connected_at, 'timestamp'):
# datetime object
connected_at = connected_at.timestamp()
# If it's already a float/int timestamp, use it directly
uptime_seconds = current_time - connected_at
else:
uptime_seconds = 0
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment