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):
for detail in bet_details:
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)
report_data = {
'sync_id': self._generate_sync_id(),
......@@ -1209,7 +1173,6 @@ class ReportsSyncResponseHandler(ResponseHandler):
'start_date': start_date.isoformat(),
'end_date': end_date.isoformat(),
'bets': [],
'matches': matches_to_sync, # Include matches associated with bets
'extraction_stats': [],
'cap_compensation_balance': cap_compensation_balance,
'summary': self._calculate_summary(bets_to_sync, stats), # Use bets_to_sync which have bet_details loaded
......@@ -1249,35 +1212,69 @@ class ReportsSyncResponseHandler(ResponseHandler):
report_data['bets'].append(bet_data)
# Add extraction stats (only new/updated stats)
for stat in stats_to_sync:
# 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()
accumulated_shortfall = float(match.accumulated_shortfall) if match and match.accumulated_shortfall is not None else 0.0
cap_percent = float(match.cap_percent) if match and match.cap_percent is not None else 70.0
match_number = match.match_number if match else None
# Add extraction stats for all matches referenced by bets being synced
# 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()
stat_data = {
'match_id': stat.match_id,
'match_number': match_number, # Include match_number for correlation with results
'fixture_id': stat.fixture_id,
'match_datetime': stat.match_datetime.isoformat(),
'total_bets': stat.total_bets,
'total_amount_collected': float(stat.total_amount_collected),
'total_redistributed': float(stat.total_redistributed),
'actual_result': stat.actual_result,
'extraction_result': stat.extraction_result,
'cap_applied': stat.cap_applied,
'cap_percentage': float(stat.cap_percentage) if stat.cap_percentage else None,
'under_bets': stat.under_bets,
'under_amount': float(stat.under_amount),
'over_bets': stat.over_bets,
'over_amount': float(stat.over_amount),
'result_breakdown': stat.result_breakdown,
'accumulated_shortfall': accumulated_shortfall, # Historical value at match completion
'cap_percent': cap_percent # CAP percentage configured at match completion time
}
report_data['extraction_stats'].append(stat_data)
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
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
cap_percent = float(match.cap_percent) if match and match.cap_percent is not None else 70.0
match_number = match.match_number if match else None
stat_data = {
'match_id': stat.match_id,
'match_number': match_number, # Include match_number for correlation with results
'fixture_id': stat.fixture_id,
'match_datetime': stat.match_datetime.isoformat(),
'total_bets': stat.total_bets,
'total_amount_collected': float(stat.total_amount_collected),
'total_redistributed': float(stat.total_redistributed),
'actual_result': stat.actual_result,
'extraction_result': stat.extraction_result,
'cap_applied': stat.cap_applied,
'cap_percentage': float(stat.cap_percentage) if stat.cap_percentage else None,
'under_bets': stat.under_bets,
'under_amount': float(stat.under_amount),
'over_bets': stat.over_bets,
'over_amount': float(stat.over_amount),
'result_breakdown': stat.result_breakdown,
'accumulated_shortfall': accumulated_shortfall, # Historical value at match completion
'cap_percent': cap_percent # CAP percentage configured at match completion time
}
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")
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