Spaces:
Running
Running
Update app_routes.py
Browse files- app_routes.py +20 -22
app_routes.py
CHANGED
|
@@ -633,6 +633,9 @@ def _assign_anchor(category: str) -> str:
|
|
| 633 |
@router.get("/api/live-news")
|
| 634 |
async def api_live_news(hours: int = 24):
|
| 635 |
hours = min(hours, 48)
|
|
|
|
|
|
|
|
|
|
| 636 |
stories = []
|
| 637 |
counters = {}
|
| 638 |
breaking = []
|
|
@@ -672,7 +675,6 @@ async def api_live_news(hours: int = 24):
|
|
| 672 |
'timestamp': r[9], 'data': data,
|
| 673 |
}
|
| 674 |
stories.append(story)
|
| 675 |
-
# Breaking if within last 30 min
|
| 676 |
try:
|
| 677 |
age_min = (_dt.utcnow() - _dt.fromisoformat(closed.replace('Z',''))).total_seconds() / 60
|
| 678 |
if age_min < 30:
|
|
@@ -852,32 +854,28 @@ async def api_live_news(hours: int = 24):
|
|
| 852 |
except Exception as e:
|
| 853 |
logger.warning(f"Live news evolution error: {e}")
|
| 854 |
|
| 855 |
-
# ===== COUNTERS (
|
| 856 |
open_pos=0; long_count=0; active_traders=0; total_risk=0
|
| 857 |
liq_24h=0; liq_gpu_24h=0; trades_24h=0; sec_24h=0; sec_active=0; active_battles=0
|
| 858 |
try:
|
| 859 |
-
c = await db.execute("
|
| 860 |
-
|
| 861 |
-
|
| 862 |
-
|
| 863 |
-
|
| 864 |
-
|
| 865 |
-
|
| 866 |
-
|
| 867 |
-
|
| 868 |
-
|
| 869 |
-
except: pass
|
| 870 |
-
try:
|
| 871 |
-
c = await db.execute("SELECT COALESCE(SUM(gpu_bet),0) FROM npc_positions WHERE status='open'")
|
| 872 |
-
total_risk = (await c.fetchone())[0] or 0
|
| 873 |
-
except: pass
|
| 874 |
-
try:
|
| 875 |
-
c = await db.execute("SELECT COUNT(*) FROM npc_positions WHERE status='liquidated' AND closed_at > datetime('now','-24 hours')")
|
| 876 |
-
liq_24h = (await c.fetchone())[0] or 0
|
| 877 |
except: pass
|
| 878 |
try:
|
| 879 |
-
c = await db.execute("
|
| 880 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 881 |
except: pass
|
| 882 |
try:
|
| 883 |
c = await db.execute("SELECT COUNT(*) FROM npc_positions WHERE opened_at > datetime('now','-24 hours')")
|
|
|
|
| 633 |
@router.get("/api/live-news")
|
| 634 |
async def api_live_news(hours: int = 24):
|
| 635 |
hours = min(hours, 48)
|
| 636 |
+
return await _cache.get(f'live_news_{hours}', 30.0, lambda: _live_news_impl(hours))
|
| 637 |
+
|
| 638 |
+
async def _live_news_impl(hours):
|
| 639 |
stories = []
|
| 640 |
counters = {}
|
| 641 |
breaking = []
|
|
|
|
| 675 |
'timestamp': r[9], 'data': data,
|
| 676 |
}
|
| 677 |
stories.append(story)
|
|
|
|
| 678 |
try:
|
| 679 |
age_min = (_dt.utcnow() - _dt.fromisoformat(closed.replace('Z',''))).total_seconds() / 60
|
| 680 |
if age_min < 30:
|
|
|
|
| 854 |
except Exception as e:
|
| 855 |
logger.warning(f"Live news evolution error: {e}")
|
| 856 |
|
| 857 |
+
# ===== COUNTERS (consolidated into fewer queries) =====
|
| 858 |
open_pos=0; long_count=0; active_traders=0; total_risk=0
|
| 859 |
liq_24h=0; liq_gpu_24h=0; trades_24h=0; sec_24h=0; sec_active=0; active_battles=0
|
| 860 |
try:
|
| 861 |
+
c = await db.execute("""
|
| 862 |
+
SELECT COUNT(*),
|
| 863 |
+
COUNT(CASE WHEN direction='long' THEN 1 END),
|
| 864 |
+
COUNT(DISTINCT agent_id),
|
| 865 |
+
COALESCE(SUM(gpu_bet),0)
|
| 866 |
+
FROM npc_positions WHERE status='open'
|
| 867 |
+
""")
|
| 868 |
+
row = await c.fetchone()
|
| 869 |
+
if row:
|
| 870 |
+
open_pos, long_count, active_traders, total_risk = row[0] or 0, row[1] or 0, row[2] or 0, row[3] or 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 871 |
except: pass
|
| 872 |
try:
|
| 873 |
+
c = await db.execute("""
|
| 874 |
+
SELECT COUNT(*), COALESCE(SUM(ABS(profit_gpu)),0)
|
| 875 |
+
FROM npc_positions WHERE status='liquidated' AND closed_at > datetime('now','-24 hours')
|
| 876 |
+
""")
|
| 877 |
+
row = await c.fetchone()
|
| 878 |
+
if row: liq_24h, liq_gpu_24h = row[0] or 0, row[1] or 0
|
| 879 |
except: pass
|
| 880 |
try:
|
| 881 |
c = await db.execute("SELECT COUNT(*) FROM npc_positions WHERE opened_at > datetime('now','-24 hours')")
|