community增加事件详情

This commit is contained in:
2026-01-08 18:50:10 +08:00
parent 86158d1dd5
commit 08b4d67e12
2 changed files with 279 additions and 128 deletions

44
app.py
View File

@@ -11139,20 +11139,53 @@ def get_events_effectiveness_stats():
# 计算汇总统计
total_events = len(events_query)
event_ids = [e.id for e in events_query]
avg_chg_list = [e.related_avg_chg for e in events_query if e.related_avg_chg is not None]
max_chg_list = [e.related_max_chg for e in events_query if e.related_max_chg is not None]
invest_scores = [e.invest_score for e in events_query if e.invest_score is not None]
surprise_scores = [e.expectation_surprise_score for e in events_query if e.expectation_surprise_score is not None]
positive_count = sum(1 for chg in avg_chg_list if chg > 0)
# 查询关联股票数据
stock_stats = []
total_stocks = 0
if event_ids:
# 查询所有关联股票
related_stocks = db.session.query(EventStock).filter(
EventStock.event_id.in_(event_ids)
).all()
# 统计股票数量(去重)
unique_stocks = {}
for rs in related_stocks:
stock_key = rs.stock_code
if stock_key not in unique_stocks:
unique_stocks[stock_key] = {
'stockCode': rs.stock_code,
'stockName': rs.stock_name,
'maxChg': rs.chg_pct or 0,
'eventId': rs.event_id,
}
else:
# 保留最大涨幅的记录
if (rs.chg_pct or 0) > unique_stocks[stock_key]['maxChg']:
unique_stocks[stock_key]['maxChg'] = rs.chg_pct or 0
unique_stocks[stock_key]['eventId'] = rs.event_id
total_stocks = len(unique_stocks)
# 按涨幅排序,取 TOP10
stock_stats = sorted(
unique_stocks.values(),
key=lambda x: x['maxChg'],
reverse=True
)[:10]
summary = {
'totalEvents': total_events,
'totalStocks': total_stocks,
'avgChg': round(sum(avg_chg_list) / len(avg_chg_list), 2) if avg_chg_list else 0,
'maxChg': round(max(max_chg_list), 2) if max_chg_list else 0,
'positiveRate': round(positive_count / len(avg_chg_list) * 100, 1) if avg_chg_list else 0,
'avgInvestScore': round(sum(invest_scores) / len(invest_scores)) if invest_scores else 0,
'avgSurpriseScore': round(sum(surprise_scores) / len(surprise_scores)) if surprise_scores else 0
}
# 按日期分组统计
@@ -11225,7 +11258,8 @@ def get_events_effectiveness_stats():
'currentDate': current_trading_day.strftime('%Y-%m-%d') if hasattr(current_trading_day, 'strftime') else str(current_trading_day),
'summary': summary,
'dailyStats': daily_stats,
'topPerformers': top_performers_list
'topPerformers': top_performers_list,
'topStocks': stock_stats
}
})