Add debug logging to track_client_activity

- Log API token, rustdesk_id, IP, and user agent received
- Log client creation and updates with user agent changes
- Log database commit success
- Add rollback on exception for better error handling
parent 2130b77e
......@@ -809,6 +809,7 @@ def track_client_activity(api_token, rustdesk_id=None):
"""Track client activity for online status"""
try:
if not api_token or not api_token.is_valid():
logger.debug(f"track_client_activity: Invalid API token")
return
from app.models import ClientActivity
......@@ -818,6 +819,8 @@ def track_client_activity(api_token, rustdesk_id=None):
ip_address = request.headers.get('X-Forwarded-For', request.remote_addr)
user_agent = request.headers.get('User-Agent')
logger.info(f"track_client_activity: api_token_id={api_token.id}, rustdesk_id={rustdesk_id}, ip={ip_address}, user_agent='{user_agent}'")
if rustdesk_id:
# If rustdesk_id is provided, use specific client
client = ClientActivity.query.filter_by(
......@@ -827,9 +830,11 @@ def track_client_activity(api_token, rustdesk_id=None):
if client:
# Update existing client
old_user_agent = client.user_agent
client.last_seen = datetime.utcnow()
client.ip_address = ip_address
client.user_agent = user_agent
logger.info(f"track_client_activity: Updated existing client {client.id}, user_agent changed from '{old_user_agent}' to '{user_agent}'")
else:
# Create new client
client = ClientActivity(
......@@ -839,6 +844,7 @@ def track_client_activity(api_token, rustdesk_id=None):
user_agent=user_agent
)
db.session.add(client)
logger.info(f"track_client_activity: Created new client with rustdesk_id {rustdesk_id}")
else:
# If no rustdesk_id provided, update the most recent client for this API token
client = ClientActivity.query.filter_by(
......@@ -847,9 +853,11 @@ def track_client_activity(api_token, rustdesk_id=None):
if client:
# Update most recent client
old_user_agent = client.user_agent
client.last_seen = datetime.utcnow()
client.ip_address = ip_address
client.user_agent = user_agent
logger.info(f"track_client_activity: Updated most recent client {client.id} (rustdesk_id: {client.rustdesk_id}), user_agent changed from '{old_user_agent}' to '{user_agent}'")
else:
# Create new client with unknown rustdesk_id
client = ClientActivity(
......@@ -859,11 +867,14 @@ def track_client_activity(api_token, rustdesk_id=None):
user_agent=user_agent
)
db.session.add(client)
logger.info(f"track_client_activity: Created new client with unknown rustdesk_id")
db.session.commit()
logger.info(f"track_client_activity: Database commit successful")
except Exception as e:
logger.error(f"Failed to track client activity: {str(e)}")
db.session.rollback()
@bp.route('/track', methods=['POST'])
@csrf.exempt
......
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