Files
MoFin/archive/scripts/fix_summary.py
T
知微 ccea9fcf09 cleanup: 去冗余+归档一次性脚本
- 移除重复的系统健康检查-每日(no_agent版,与LLM版重叠)
- 归档42个一次性脚本(移出MoFin/scripts→archive/scripts):
  - fix_*: 历史数据修复(11个)
  - migrate_*/rollback_*: 数据迁移(2个)
  - 一次性数据修复: bulk/close/data_freshness等(16个)
  - check_*/verify_*/diagnose_*: 历史诊断工具(12个)
  - test_*: 测试脚本(3个)
- 84个活跃脚本, 0架构违规, 31张healthy数据表
2026-07-09 00:19:00 +08:00

31 lines
1.3 KiB
Python

"""Fix portfolio_summary directly from holdings table"""
import sqlite3, sys
sys.path.insert(0, '/home/hmo/MoFin')
from mo_models import calc_total_mv, calc_total_assets, calc_position_pct
from datetime import datetime
db = sqlite3.connect('/home/hmo/web-dashboard/data/mofin.db')
# Get holdings
rows = db.execute("SELECT code, name, shares, cost, price, market_value, change_pct, currency, position_pct FROM holdings WHERE is_active=1").fetchall()
holdings = [dict(zip(['code','name','shares','cost','price','market_value','change_pct','currency','position_pct'], r)) for r in rows]
# Get cash/summary
sr = db.execute("SELECT cash, frozen_cash FROM portfolio_summary WHERE id=1").fetchone()
cash = sr[0] or 0
frozen = sr[1] or 0
pf = {'holdings': holdings, 'cash': cash, 'frozen_cash': frozen}
mv = calc_total_mv(holdings)
ta = calc_total_assets(pf)
pp = calc_position_pct(pf)
pnl = sum((h['price'] or 0) * (h['shares'] or 0) - (h['cost'] or 0) * (h['shares'] or 0) for h in holdings)
print(f"mv={mv} ta={ta} pnl={pnl} pp={pp}%")
db.execute("UPDATE portfolio_summary SET total_mv=?, total_assets=?, total_pnl=?, position_pct=?, updated_at=? WHERE id=1",
(mv, ta, pnl, pp, datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
db.commit()
print("portfolio_summary updated")
db.close()