Fix empty matches update

parent 2117385c
...@@ -1326,6 +1326,8 @@ def api_reports_sync(): ...@@ -1326,6 +1326,8 @@ def api_reports_sync():
# Create MatchReport records for comprehensive match-level data # Create MatchReport records for comprehensive match-level data
match_reports_count = 0 match_reports_count = 0
# First, process extraction_stats if provided
for stats_data in data['extraction_stats']: for stats_data in data['extraction_stats']:
# Get client token name # Get client token name
client_activity = ClientActivity.query.filter_by(rustdesk_id=client_id).first() client_activity = ClientActivity.query.filter_by(rustdesk_id=client_id).first()
...@@ -1396,6 +1398,99 @@ def api_reports_sync(): ...@@ -1396,6 +1398,99 @@ def api_reports_sync():
db.session.add(match_report) db.session.add(match_report)
match_reports_count += 1 match_reports_count += 1
# If extraction_stats is empty but we have bets, create MatchReport records from bets
# This handles incremental updates where only bets are sent
if not data['extraction_stats'] and data['bets']:
logger.info(f"Creating MatchReport records from bets (extraction_stats is empty)")
# Get client token name
client_activity = ClientActivity.query.filter_by(rustdesk_id=client_id).first()
client_token_name = client_activity.api_token.name if client_activity and client_activity.api_token else 'Unknown'
# Group bets by match_id to create match reports
from sqlalchemy import func
bets_by_match = db.session.query(
Bet.match_id,
Bet.match_number,
Bet.fixture_id,
func.min(Bet.bet_datetime).label('match_datetime'),
func.count(Bet.id).label('total_bets'),
func.sum(Bet.total_amount).label('total_payin')
).filter(
Bet.client_id == client_id,
Bet.sync_id == report_sync.id
).group_by(Bet.match_id, Bet.match_number, Bet.fixture_id).all()
for match_id, match_number, fixture_id, match_datetime, total_bets, total_payin in bets_by_match:
# Calculate winning/losing/pending bets from bet details
winning_bets = 0
losing_bets = 0
pending_bets = 0
total_payout = 0.0
bet_details_query = db.session.query(
BetDetail.result,
func.count(BetDetail.id).label('count'),
func.sum(BetDetail.win_amount).label('total_win')
).join(Bet).filter(
Bet.client_id == client_id,
Bet.match_id == match_id,
Bet.sync_id == report_sync.id
).group_by(BetDetail.result)
for result, count, total_win in bet_details_query.all():
if result == 'won':
winning_bets = count
total_payout += total_win or 0.0
elif result == 'lost':
losing_bets = count
elif result == 'pending':
pending_bets = count
balance = total_payin - total_payout
# Check if MatchReport already exists for this match and client
existing_match_report = MatchReport.query.filter_by(
client_id=client_id,
match_id=match_id
).first()
if existing_match_report:
# Update existing MatchReport
existing_match_report.sync_id = report_sync.id
existing_match_report.total_bets = total_bets
existing_match_report.winning_bets = winning_bets
existing_match_report.losing_bets = losing_bets
existing_match_report.pending_bets = pending_bets
existing_match_report.total_payin = total_payin
existing_match_report.total_payout = total_payout
existing_match_report.balance = balance
existing_match_report.cap_compensation_balance = cap_compensation_balance
logger.info(f"Updated existing MatchReport for match {match_id}")
else:
# Create new MatchReport
match_report = MatchReport(
sync_id=report_sync.id,
client_id=client_id,
client_token_name=client_token_name,
match_id=match_id,
match_number=match_number,
fixture_id=fixture_id,
match_datetime=match_datetime,
total_bets=total_bets,
winning_bets=winning_bets,
losing_bets=losing_bets,
pending_bets=pending_bets,
total_payin=total_payin,
total_payout=total_payout,
balance=balance,
cap_compensation_balance=cap_compensation_balance
)
db.session.add(match_report)
logger.info(f"Created new MatchReport for match {match_id}")
match_reports_count += 1
# Commit all changes # Commit all changes
db.session.commit() db.session.commit()
......
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