Fix: Include extraction stats for all matches with bets in incremental sync

- When sending incremental report sync updates, extraction_stats array was empty
- This happened because extraction stats were only included if they were new/updated
- But when new bets are placed on matches with existing extraction stats, those stats weren't included
- Now extraction stats are included for ALL matches referenced by bets being synced
- This ensures server receives match information for all matches that have bets placed
- Matches without extraction stats (not completed) get minimal entries with default values
parent 8b3213e5
...@@ -1164,42 +1164,6 @@ class ReportsSyncResponseHandler(ResponseHandler): ...@@ -1164,42 +1164,6 @@ class ReportsSyncResponseHandler(ResponseHandler):
for detail in bet_details: for detail in bet_details:
unique_match_ids.add(detail.match_id) unique_match_ids.add(detail.match_id)
# Collect match data for all matches referenced by bets
matches_to_sync = []
if unique_match_ids:
matches = session.query(MatchModel).filter(
MatchModel.id.in_(unique_match_ids)
).all()
for match in matches:
match_data = {
'id': match.id,
'match_number': match.match_number,
'fixture_id': match.fixture_id,
'fighter1_township': match.fighter1_township,
'fighter2_township': match.fighter2_township,
'venue_kampala_township': match.venue_kampala_township,
'start_time': match.start_time.isoformat() if match.start_time else None,
'end_time': match.end_time.isoformat() if match.end_time else None,
'result': match.result,
'winning_outcomes': match.winning_outcomes,
'under_over_result': match.under_over_result,
'done': match.done,
'running': match.running,
'status': match.status,
'filename': match.filename,
'file_sha1sum': match.file_sha1sum,
'active_status': match.active_status,
'zip_filename': match.zip_filename,
'zip_sha1sum': match.zip_sha1sum,
'zip_upload_status': match.zip_upload_status,
'zip_upload_progress': match.zip_upload_progress,
'zip_validation_status': match.zip_validation_status,
'fixture_active_time': match.fixture_active_time,
'accumulated_shortfall': float(match.accumulated_shortfall) if match.accumulated_shortfall else 0.0,
'cap_percent': float(match.cap_percent) if match.cap_percent else 70.0
}
matches_to_sync.append(match_data)
# Build report data payload (incremental - only new/changed data) # Build report data payload (incremental - only new/changed data)
report_data = { report_data = {
'sync_id': self._generate_sync_id(), 'sync_id': self._generate_sync_id(),
...@@ -1209,7 +1173,6 @@ class ReportsSyncResponseHandler(ResponseHandler): ...@@ -1209,7 +1173,6 @@ class ReportsSyncResponseHandler(ResponseHandler):
'start_date': start_date.isoformat(), 'start_date': start_date.isoformat(),
'end_date': end_date.isoformat(), 'end_date': end_date.isoformat(),
'bets': [], 'bets': [],
'matches': matches_to_sync, # Include matches associated with bets
'extraction_stats': [], 'extraction_stats': [],
'cap_compensation_balance': cap_compensation_balance, 'cap_compensation_balance': cap_compensation_balance,
'summary': self._calculate_summary(bets_to_sync, stats), # Use bets_to_sync which have bet_details loaded 'summary': self._calculate_summary(bets_to_sync, stats), # Use bets_to_sync which have bet_details loaded
...@@ -1249,8 +1212,14 @@ class ReportsSyncResponseHandler(ResponseHandler): ...@@ -1249,8 +1212,14 @@ class ReportsSyncResponseHandler(ResponseHandler):
report_data['bets'].append(bet_data) report_data['bets'].append(bet_data)
# Add extraction stats (only new/updated stats) # Add extraction stats for all matches referenced by bets being synced
for stat in stats_to_sync: # This ensures the server receives match information for all matches with new/updated bets
for match_id in unique_match_ids:
# Check if this match has extraction stats
stat = session.query(ExtractionStatsModel).filter_by(match_id=match_id).first()
if stat:
# Match has extraction stats - include them
# Get the match to retrieve the stored accumulated shortfall, cap_percent, and match_number at completion time # Get the match to retrieve the stored accumulated shortfall, cap_percent, and match_number at completion time
match = session.query(MatchModel).filter_by(id=stat.match_id).first() match = session.query(MatchModel).filter_by(id=stat.match_id).first()
accumulated_shortfall = float(match.accumulated_shortfall) if match and match.accumulated_shortfall is not None else 0.0 accumulated_shortfall = float(match.accumulated_shortfall) if match and match.accumulated_shortfall is not None else 0.0
...@@ -1278,6 +1247,34 @@ class ReportsSyncResponseHandler(ResponseHandler): ...@@ -1278,6 +1247,34 @@ class ReportsSyncResponseHandler(ResponseHandler):
'cap_percent': cap_percent # CAP percentage configured at match completion time 'cap_percent': cap_percent # CAP percentage configured at match completion time
} }
report_data['extraction_stats'].append(stat_data) report_data['extraction_stats'].append(stat_data)
logger.debug(f"Added match {match_id} with extraction stats to sync")
else:
# Match doesn't have extraction stats yet (match not completed)
# Create minimal extraction stat entry with default values
match = session.query(MatchModel).filter_by(id=match_id).first()
if match:
stat_data = {
'match_id': match.id,
'match_number': match.match_number,
'fixture_id': match.fixture_id,
'match_datetime': match.start_time.isoformat() if match.start_time else None,
'total_bets': 0,
'total_amount_collected': 0.0,
'total_redistributed': 0.0,
'actual_result': None,
'extraction_result': None,
'cap_applied': False,
'cap_percentage': None,
'under_bets': 0,
'under_amount': 0.0,
'over_bets': 0,
'over_amount': 0.0,
'result_breakdown': None,
'accumulated_shortfall': 0.0,
'cap_percent': 70.0
}
report_data['extraction_stats'].append(stat_data)
logger.debug(f"Added match {match_id} without extraction stats to sync")
logger.info(f"Collected report data: {len(report_data['bets'])} bets, {len(report_data['extraction_stats'])} stats") logger.info(f"Collected report data: {len(report_data['bets'])} bets, {len(report_data['extraction_stats'])} stats")
return report_data return report_data
......
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