seawolf2357 commited on
Commit
78e94ff
·
verified ·
1 Parent(s): 11fa5c3

Update app_routes.py

Browse files
Files changed (1) hide show
  1. app_routes.py +42 -24
app_routes.py CHANGED
@@ -805,41 +805,59 @@ async def api_live_news(hours: int = 24):
805
  except Exception as e:
806
  logger.warning(f"Live news evolution error: {e}")
807
 
808
- # ===== COUNTERS =====
 
 
809
  try:
810
  c = await db.execute("SELECT COUNT(*) FROM npc_positions WHERE status='open'")
811
- open_pos = (await c.fetchone())[0]
 
 
812
  c = await db.execute("SELECT COUNT(*) FROM npc_positions WHERE status='open' AND direction='long'")
813
- long_count = (await c.fetchone())[0]
 
 
814
  c = await db.execute("SELECT COUNT(DISTINCT agent_id) FROM npc_positions WHERE status='open'")
815
- active_traders = (await c.fetchone())[0]
 
 
816
  c = await db.execute("SELECT COALESCE(SUM(gpu_bet),0) FROM npc_positions WHERE status='open'")
817
- total_risk = (await c.fetchone())[0]
 
 
818
  c = await db.execute("SELECT COUNT(*) FROM npc_positions WHERE status='liquidated' AND closed_at > datetime('now','-24 hours')")
819
- liq_24h = (await c.fetchone())[0]
 
 
820
  c = await db.execute("SELECT COALESCE(SUM(ABS(profit_gpu)),0) FROM npc_positions WHERE status='liquidated' AND closed_at > datetime('now','-24 hours')")
821
- liq_gpu_24h = (await c.fetchone())[0]
 
 
822
  c = await db.execute("SELECT COUNT(*) FROM npc_positions WHERE opened_at > datetime('now','-24 hours')")
823
- trades_24h = (await c.fetchone())[0]
 
 
824
  c = await db.execute("SELECT COUNT(*) FROM sec_violations WHERE created_at > datetime('now','-24 hours')")
825
- sec_24h = (await c.fetchone())[0]
 
 
826
  c = await db.execute("SELECT COUNT(*) FROM sec_suspensions WHERE until > datetime('now')")
827
- sec_active = (await c.fetchone())[0]
 
 
828
  c = await db.execute("SELECT COUNT(*) FROM battle_rooms WHERE status='active'")
829
- active_battles = (await c.fetchone())[0]
830
- short_count = open_pos - long_count
831
- counters = {
832
- 'active_positions': open_pos, 'active_traders': active_traders,
833
- 'long_count': long_count, 'short_count': short_count,
834
- 'long_pct': round(long_count / open_pos * 100) if open_pos > 0 else 50,
835
- 'total_risk_gpu': round(total_risk),
836
- 'liquidations_24h': liq_24h, 'liquidated_gpu_24h': round(liq_gpu_24h),
837
- 'trades_24h': trades_24h, 'sec_violations_24h': sec_24h,
838
- 'sec_active_suspensions': sec_active, 'active_battles': active_battles,
839
- }
840
- except Exception as e:
841
- logger.warning(f"Live news counters error: {e}")
842
- counters = {}
843
 
844
  # ===== MVP & VILLAIN =====
845
  mvp = None; villain = None
 
805
  except Exception as e:
806
  logger.warning(f"Live news evolution error: {e}")
807
 
808
+ # ===== COUNTERS (each query individually wrapped) =====
809
+ open_pos=0; long_count=0; active_traders=0; total_risk=0
810
+ liq_24h=0; liq_gpu_24h=0; trades_24h=0; sec_24h=0; sec_active=0; active_battles=0
811
  try:
812
  c = await db.execute("SELECT COUNT(*) FROM npc_positions WHERE status='open'")
813
+ open_pos = (await c.fetchone())[0] or 0
814
+ except: pass
815
+ try:
816
  c = await db.execute("SELECT COUNT(*) FROM npc_positions WHERE status='open' AND direction='long'")
817
+ long_count = (await c.fetchone())[0] or 0
818
+ except: pass
819
+ try:
820
  c = await db.execute("SELECT COUNT(DISTINCT agent_id) FROM npc_positions WHERE status='open'")
821
+ active_traders = (await c.fetchone())[0] or 0
822
+ except: pass
823
+ try:
824
  c = await db.execute("SELECT COALESCE(SUM(gpu_bet),0) FROM npc_positions WHERE status='open'")
825
+ total_risk = (await c.fetchone())[0] or 0
826
+ except: pass
827
+ try:
828
  c = await db.execute("SELECT COUNT(*) FROM npc_positions WHERE status='liquidated' AND closed_at > datetime('now','-24 hours')")
829
+ liq_24h = (await c.fetchone())[0] or 0
830
+ except: pass
831
+ try:
832
  c = await db.execute("SELECT COALESCE(SUM(ABS(profit_gpu)),0) FROM npc_positions WHERE status='liquidated' AND closed_at > datetime('now','-24 hours')")
833
+ liq_gpu_24h = (await c.fetchone())[0] or 0
834
+ except: pass
835
+ try:
836
  c = await db.execute("SELECT COUNT(*) FROM npc_positions WHERE opened_at > datetime('now','-24 hours')")
837
+ trades_24h = (await c.fetchone())[0] or 0
838
+ except: pass
839
+ try:
840
  c = await db.execute("SELECT COUNT(*) FROM sec_violations WHERE created_at > datetime('now','-24 hours')")
841
+ sec_24h = (await c.fetchone())[0] or 0
842
+ except: pass
843
+ try:
844
  c = await db.execute("SELECT COUNT(*) FROM sec_suspensions WHERE until > datetime('now')")
845
+ sec_active = (await c.fetchone())[0] or 0
846
+ except: pass
847
+ try:
848
  c = await db.execute("SELECT COUNT(*) FROM battle_rooms WHERE status='active'")
849
+ active_battles = (await c.fetchone())[0] or 0
850
+ except: pass
851
+ short_count = open_pos - long_count
852
+ counters = {
853
+ 'active_positions': open_pos, 'active_traders': active_traders,
854
+ 'long_count': long_count, 'short_count': short_count,
855
+ 'long_pct': round(long_count / open_pos * 100) if open_pos > 0 else 50,
856
+ 'total_risk_gpu': round(total_risk),
857
+ 'liquidations_24h': liq_24h, 'liquidated_gpu_24h': round(liq_gpu_24h),
858
+ 'trades_24h': trades_24h, 'sec_violations_24h': sec_24h,
859
+ 'sec_active_suspensions': sec_active, 'active_battles': active_battles,
860
+ }
 
 
861
 
862
  # ===== MVP & VILLAIN =====
863
  mvp = None; villain = None