Fix: Add comprehensive debug logging to /api/reports/last-sync endpoint

- Add detailed logging for client_id, user, and API token information
- Log counts of bets, extraction_stats, and match_reports for each client
- Log requires_full_sync flag value for each client
- Log response being sent to client
- Add requires_full_sync: False to responses when sync records exist
- This will help diagnose why client keeps sending incremental sync requests instead of full sync
parent e7abf6b6
......@@ -1460,16 +1460,22 @@ def api_get_last_sync():
# Get client_id from query parameter (optional)
client_id = request.args.get('client_id')
logger.info(f"API get last sync: user={user.username}, client_id={client_id}, api_token={api_token.name if api_token else 'None'}")
# If client_id is not provided, get all clients for this user
if not client_id:
# Get all clients associated with this user's API tokens
user_token_ids = [t.id for t in APIToken.query.filter_by(user_id=user.id).all()]
logger.info(f"API get last sync: Found {len(user_token_ids)} API tokens for user {user.username}")
if user_token_ids:
client_ids = [c.rustdesk_id for c in ClientActivity.query.filter(
ClientActivity.api_token_id.in_(user_token_ids)
).all()]
logger.info(f"API get last sync: Found {len(client_ids)} clients for user {user.username}: {client_ids}")
if not client_ids:
logger.warning(f"API get last sync: No clients found for user {user.username}")
return jsonify({
'success': True,
'message': 'No clients found for this user',
......@@ -1480,18 +1486,22 @@ def api_get_last_sync():
# Return last sync info for all clients
clients_data = []
for cid in client_ids:
logger.info(f"API get last sync: Processing client {cid}")
last_sync = ReportSync.query.filter_by(client_id=cid)\
.order_by(desc(ReportSync.sync_timestamp))\
.first()
if last_sync:
total_syncs = ReportSync.query.filter_by(client_id=cid).count()
logger.info(f"API get last sync: Client {cid} has {total_syncs} sync(s), last_sync_id={last_sync.sync_id}")
clients_data.append({
'client_id': cid,
'last_sync_id': last_sync.sync_id,
'last_sync_timestamp': last_sync.sync_timestamp.isoformat() if last_sync.sync_timestamp else None,
'last_sync_type': last_sync.sync_type if hasattr(last_sync, 'sync_type') else 'unknown',
'total_syncs': total_syncs,
'requires_full_sync': False,
'last_sync_summary': {
'total_payin': float(last_sync.total_payin) if last_sync.total_payin else 0.0,
'total_payout': float(last_sync.total_payout) if last_sync.total_payout else 0.0,
......@@ -1508,6 +1518,8 @@ def api_get_last_sync():
total_match_reports_for_client = MatchReport.query.filter_by(client_id=cid).count()
requires_full_sync = (total_bets_for_client == 0 and total_stats_for_client == 0 and total_match_reports_for_client == 0)
logger.info(f"API get last sync: Client {cid} - bets={total_bets_for_client}, stats={total_stats_for_client}, match_reports={total_match_reports_for_client}, requires_full_sync={requires_full_sync}")
clients_data.append({
'client_id': cid,
'last_sync_id': None,
......@@ -1518,6 +1530,7 @@ def api_get_last_sync():
'last_sync_summary': None
})
logger.info(f"API get last sync: Returning {len(clients_data)} client(s) for user {user.username}")
return jsonify({
'success': True,
'message': f'Found {len(clients_data)} client(s) for this user',
......@@ -1553,6 +1566,8 @@ def api_get_last_sync():
'details': 'You do not have access to this client'
}), 403
logger.info(f"API get last sync: Querying specific client {client_id}")
# Get most recent sync for this client
last_sync = ReportSync.query.filter_by(client_id=client_id)\
.order_by(desc(ReportSync.sync_timestamp))\
......@@ -1567,6 +1582,8 @@ def api_get_last_sync():
# If no records exist in any table, signal that full sync is required
requires_full_sync = (total_bets_for_client == 0 and total_stats_for_client == 0 and total_match_reports_for_client == 0)
logger.info(f"API get last sync: Client {client_id} - NO sync records found, bets={total_bets_for_client}, stats={total_stats_for_client}, match_reports={total_match_reports_for_client}, requires_full_sync={requires_full_sync}")
return jsonify({
'success': True,
'message': 'No sync records found for this client',
......@@ -1582,6 +1599,8 @@ def api_get_last_sync():
# Get total sync count for this client
total_syncs = ReportSync.query.filter_by(client_id=client_id).count()
logger.info(f"API get last sync: Client {client_id} - Found {total_syncs} sync(s), last_sync_id={last_sync.sync_id}")
# Get most recent sync log for additional details
last_sync_log = ReportSyncLog.query.filter_by(client_id=client_id)\
.order_by(desc(ReportSyncLog.created_at))\
......@@ -1598,6 +1617,7 @@ def api_get_last_sync():
'last_start_date': last_sync.start_date.isoformat() if last_sync.start_date else None,
'last_end_date': last_sync.end_date.isoformat() if last_sync.end_date else None,
'total_syncs': total_syncs,
'requires_full_sync': False,
'last_sync_summary': {
'total_payin': float(last_sync.total_payin) if last_sync.total_payin else 0.0,
'total_payout': float(last_sync.total_payout) if last_sync.total_payout else 0.0,
......@@ -1609,6 +1629,8 @@ def api_get_last_sync():
'server_timestamp': datetime.utcnow().isoformat()
}
logger.info(f"API get last sync: Client {client_id} - Returning response with requires_full_sync=False (has sync records)")
# Add sync log details if available
if last_sync_log:
response_data['last_sync_log'] = {
......
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