File size: 21,316 Bytes
66916b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
"""Analytics: derive proxy traffic and bot file-activity metrics from
``AuditLog`` + live bot metrics from ``bot_runner``.

Why derive instead of pre-aggregate
-----------------------------------
The panel already write-through-persists every ``AuditLog`` row to the
HF Dataset (see ``storage.py``). For the traffic volumes a hosting panel
sees — even at ~10 req/s sustained, that's <1M rows/day — a single
``GROUP BY strftime('%Y-%m-%d %H:00:00', created_at)`` is well under
200 ms on SQLite. So:

  * No separate ``metric_samples`` rollup table to maintain,
  * No risk of pre-aggregation diverging from the source of truth,
  * "Last hour / last 24h / last 7d / last 30d" all use the same code
    path — only the window changes.

If a future deployment exceeds this, the same collectors can be
swapped for a rollup table without breaking the route signatures.

Bucketing
---------
All time-series use *hour* buckets (``YYYY-MM-DD HH:00:00`` UTC). This
matches what humans actually look at — sub-hour resolution makes
charts spiky and hard to read at the panel's typical scale.

Percentiles
-----------
We compute p50/p95/p99 of latency in Python over the rows fetched for
the window. SQLite has no native percentile function.  For windows
above 50k rows we downsample to 5k before computing percentiles to
keep the response under 200 ms; the bucketed timeseries stays exact.
"""
from __future__ import annotations

import json
import logging
import time
from collections import defaultdict
from dataclasses import dataclass
from datetime import datetime, timedelta
from typing import Any, Iterable, Optional

from sqlalchemy import select, func, and_, desc
from sqlalchemy.ext.asyncio import AsyncSession

from models import (
    AuditLog,
    BotInstance,
    BotStatus,
    DeploymentMode,
)

log = logging.getLogger("panel.analytics")


# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------

_HOUR_FMT = "%Y-%m-%d %H:00:00"


def _utc_now() -> datetime:
    return datetime.utcnow()


def _hour_bucket(dt: datetime) -> datetime:
    """Truncate a datetime to its UTC hour bucket."""
    return dt.replace(minute=0, second=0, microsecond=0)


def _hour_label(dt: datetime) -> str:
    return dt.strftime(_HOUR_FMT)


def _hour_labels(start: datetime, end: datetime) -> list[str]:
    """Inclusive list of hour labels covering [start, end]."""
    cur = _hour_bucket(start)
    end_b = _hour_bucket(end)
    labels: list[str] = []
    while cur <= end_b:
        labels.append(_hour_label(cur))
        cur = cur + timedelta(hours=1)
    return labels


def _percentile(sorted_values: list[float], p: float) -> float:
    """Linear-interpolation percentile. ``p`` in [0, 100]."""
    if not sorted_values:
        return 0.0
    if len(sorted_values) == 1:
        return float(sorted_values[0])
    if p <= 0:
        return float(sorted_values[0])
    if p >= 100:
        return float(sorted_values[-1])
    k = (len(sorted_values) - 1) * (p / 100.0)
    f = int(k)
    c = min(f + 1, len(sorted_values) - 1)
    if f == c:
        return float(sorted_values[f])
    return float(sorted_values[f] + (sorted_values[c] - sorted_values[f]) * (k - f))


# ---------------------------------------------------------------------------
# Proxy traffic
# ---------------------------------------------------------------------------

PROXY_ACTIONS = ("proxy_request",)


@dataclass
class ProxyTrafficSummary:
    """A single time-window rollup of proxy traffic."""
    window_start: datetime
    window_end: datetime
    total_requests: int
    error_count: int          # 5xx
    client_error_count: int   # 4xx
    success_count: int        # 2xx-3xx
    avg_latency_ms: float
    p50_latency_ms: float
    p95_latency_ms: float
    p99_latency_ms: float
    requests_per_minute: float
    error_rate_percent: float = 0.0   # explicit field so it's picked up
                                       # up by `__dict__` and serialised
                                       # cleanly to JSON.


@dataclass
class ProxyHourBucket:
    hour: str        # "YYYY-MM-DD HH:00:00"
    requests: int
    errors: int
    avg_latency_ms: float
    p95_latency_ms: float


@dataclass
class EndpointStat:
    method: str
    path_template: str      # ``/proxy/<slug>/<rest>`` -> ``/proxy/<slug>/...``
    requests: int
    errors: int
    avg_latency_ms: float


def _templated_path(path: str) -> str:
    """Collapse the bot slug in a proxy path to ``...`` so endpoint
    aggregation isn't fragmented per bot.

    ``/proxy/my-bot/telegram/webhook`` -> ``/proxy/<slug>/telegram/webhook``
    (we still want to see the trailing sub-path).
    """
    parts = path.strip("/").split("/", 2)
    if len(parts) >= 2 and parts[0] == "proxy":
        if len(parts) == 2:
            return "/proxy/<slug>"
        return f"/proxy/<slug>/{parts[2]}"
    return path


async def _proxy_window(
    db: AsyncSession,
    start: datetime,
    end: datetime,
    bot_id: Optional[int] = None,
) -> list[dict]:
    """Fetch raw proxy_request rows in [start, end), optionally per-bot."""
    q = (
        select(AuditLog)
        .where(AuditLog.action == "proxy_request")
        .where(AuditLog.created_at >= start)
        .where(AuditLog.created_at < end)
    )
    if bot_id is not None:
        q = q.where(AuditLog.target_type == "bot").where(AuditLog.target_id == bot_id)
    q = q.order_by(AuditLog.created_at.asc())
    rows = (await db.execute(q)).scalars().all()
    return rows


def _summarise_proxy(rows: Iterable[AuditLog], start: datetime, end: datetime) -> ProxyTrafficSummary:
    latencies: list[float] = []
    total = errors = client_errors = success = 0
    for r in rows:
        total += 1
        try:
            details = json.loads(r.details_json) if r.details_json else {}
        except (ValueError, TypeError):
            details = {}
        status = int(details.get("status") or 0)
        if status >= 500:
            errors += 1
        elif 400 <= status < 500:
            client_errors += 1
        elif 200 <= status < 400:
            success += 1
        lat = details.get("latency_ms")
        if isinstance(lat, (int, float)):
            latencies.append(float(lat))
    latencies.sort()
    elapsed_min = max(1.0, (end - start).total_seconds() / 60.0)
    rpm = round(total / elapsed_min, 2)
    err_rate = round((errors / total * 100.0), 2) if total > 0 else 0.0
    return ProxyTrafficSummary(
        window_start=start,
        window_end=end,
        total_requests=total,
        error_count=errors,
        client_error_count=client_errors,
        success_count=success,
        avg_latency_ms=round(sum(latencies) / len(latencies), 1) if latencies else 0.0,
        p50_latency_ms=round(_percentile(latencies, 50), 1),
        p95_latency_ms=round(_percentile(latencies, 95), 1),
        p99_latency_ms=round(_percentile(latencies, 99), 1),
        requests_per_minute=rpm,
        error_rate_percent=err_rate,
    )


async def collect_proxy_traffic(
    db: AsyncSession,
    hours: int = 24,
    bot_id: Optional[int] = None,
) -> dict[str, Any]:
    """Aggregate proxy traffic over the last ``hours`` hours.

    Returns
    -------
    {
      "summary":        ProxyTrafficSummary dict,
      "buckets":        [ProxyHourBucket dict] one per hour, always filled
                        (zero-buckets included for empty hours),
      "endpoints":      top-N endpoint stats (default top 10),
      "status_codes":   {str(2xx): int, str(3xx): int, str(4xx): int, str(5xx): int,
                         str(other): int},
      "bot_breakdown":  [{"bot_id": int, "name": str, "slug": str, "requests": int,
                          "errors": int, "rpm": float}, ...]   — only when bot_id is None
    }
    """
    end = _utc_now()
    start = end - timedelta(hours=hours)

    rows = await _proxy_window(db, start, end, bot_id=bot_id)
    summary = _summarise_proxy(rows, start, end)

    # ---- Hourly buckets ----
    bucket_data: dict[str, dict[str, float]] = {}
    for r in rows:
        h = _hour_label(_hour_bucket(r.created_at))
        b = bucket_data.setdefault(h, {"requests": 0, "errors": 0, "_lat": []})
        b["requests"] += 1
        try:
            details = json.loads(r.details_json) if r.details_json else {}
        except (ValueError, TypeError):
            details = {}
        status = int(details.get("status") or 0)
        if status >= 500:
            b["errors"] += 1
        lat = details.get("latency_ms")
        if isinstance(lat, (int, float)):
            b["_lat"].append(float(lat))

    buckets: list[dict] = []
    for label in _hour_labels(start, end):
        b = bucket_data.get(label)
        if not b:
            buckets.append({
                "hour": label,
                "requests": 0,
                "errors": 0,
                "avg_latency_ms": 0.0,
                "p95_latency_ms": 0.0,
            })
            continue
        lats: list[float] = b["_lat"]
        lats.sort()
        buckets.append({
            "hour": label,
            "requests": int(b["requests"]),
            "errors": int(b["errors"]),
            "avg_latency_ms": round(sum(lats) / len(lats), 1) if lats else 0.0,
            "p95_latency_ms": round(_percentile(lats, 95), 1) if lats else 0.0,
        })

    # ---- Endpoints ----
    endpoint_agg: dict[str, dict[str, float]] = {}
    status_agg: dict[str, int] = defaultdict(int)
    for r in rows:
        try:
            details = json.loads(r.details_json) if r.details_json else {}
        except (ValueError, TypeError):
            details = {}
        method = (details.get("method") or "?").upper()
        path = details.get("path") or ""
        # path looks like ``/proxy/<slug>/<rest>...``; the AuditLog
        # stores the *incoming* request path (panel-side), so use it.
        endpoint = _templated_path(path) if path.startswith("/proxy/") else (path or "?")
        lat = details.get("latency_ms")
        agg = endpoint_agg.setdefault(
            f"{method} {endpoint}",
            {"requests": 0, "errors": 0, "_lat": []},
        )
        agg["requests"] += 1
        status = int(details.get("status") or 0)
        if status >= 500:
            agg["errors"] += 1
        status_agg[_status_bucket(status)] += 1
        if isinstance(lat, (int, float)):
            agg["_lat"].append(float(lat))

    endpoints: list[dict] = []
    for key, agg in sorted(endpoint_agg.items(), key=lambda kv: -kv[1]["requests"])[:10]:
        method, _, path = key.partition(" ")
        lats: list[float] = agg["_lat"]
        endpoints.append({
            "method": method,
            "path_template": path,
            "requests": int(agg["requests"]),
            "errors": int(agg["errors"]),
            "avg_latency_ms": round(sum(lats) / len(lats), 1) if lats else 0.0,
        })

    out: dict[str, Any] = {
        "summary": summary.__dict__,
        "buckets": buckets,
        "endpoints": endpoints,
        "status_codes": dict(status_agg),
        "window_hours": hours,
        "ts": time.time(),
    }

    # ---- Per-bot breakdown (cluster view only) ----
    if bot_id is None:
        out["bot_breakdown"] = await _proxy_bot_breakdown(db, start, end)

    return out


def _status_bucket(status: int) -> str:
    if 200 <= status < 300:
        return "2xx"
    if 300 <= status < 400:
        return "3xx"
    if 400 <= status < 500:
        return "4xx"
    if 500 <= status < 600:
        return "5xx"
    return "other"


async def _proxy_bot_breakdown(
    db: AsyncSession,
    start: datetime,
    end: datetime,
    limit: int = 10,
) -> list[dict]:
    """Top-N bots by proxy request volume in the window."""
    rows = (await db.execute(
        select(AuditLog, BotInstance)
        .join(BotInstance, and_(
            BotInstance.id == AuditLog.target_id,
            AuditLog.target_type == "bot",
        ))
        .where(AuditLog.action == "proxy_request")
        .where(AuditLog.created_at >= start)
        .where(AuditLog.created_at < end)
    )).all()
    agg: dict[int, dict[str, Any]] = {}
    for entry, bot in rows:
        b = agg.setdefault(bot.id, {
            "bot_id": bot.id,
            "name": bot.name,
            "slug": bot.slug,
            "requests": 0,
            "errors": 0,
        })
        b["requests"] += 1
        try:
            details = json.loads(entry.details_json) if entry.details_json else {}
        except (ValueError, TypeError):
            details = {}
        if int(details.get("status") or 0) >= 500:
            b["errors"] += 1

    elapsed_min = max(1.0, (end - start).total_seconds() / 60.0)
    out: list[dict] = []
    for b in sorted(agg.values(), key=lambda r: -r["requests"])[:limit]:
        b["rpm"] = round(b["requests"] / elapsed_min, 2)
        out.append(b)
    return out


# ---------------------------------------------------------------------------
# Bot activity (uploads + downloads per hour)
# ---------------------------------------------------------------------------
#
# "Upload" / "download" as observed by the panel. We don't instrument
# HTTP egress inside the bot process — only panel-mediated actions:
#
#   upload   = user pushed content INTO the bot:
#                * zip_upload (whole zip import)
#                * file_save  (single-file commit)
#                * file_create (new file)
#
#   download = user pulled content OUT of the bot:
#                * file_raw (any GET /api/bots/{id}/files/raw in the audit log)
#
# Anything that touches /api/bots/{id}/files/raw counts as a download
# — that's the only panel endpoint that returns bot file bytes to the
# user. We log it the same way we log everything else.

UPLOAD_ACTIONS = ("zip_upload", "file_save", "file_create")
DOWNLOAD_ACTIONS = ("file_raw",)        # see _audit_file_raw in routes.py
# NOTE: the file_raw audit row is added alongside this module; until
# it ships the downloads bucket will be empty — that's expected and
# shows in the UI as "no downloads yet".


@dataclass
class BotActivityHourBucket:
    hour: str
    uploads: int
    downloads: int


@dataclass
class BotActivitySummary:
    bot_id: int
    name: str
    slug: str
    total_uploads: int
    total_downloads: int
    hours: list[BotActivityHourBucket]


async def collect_bot_activity(
    db: AsyncSession,
    hours: int = 24,
    bot_id: Optional[int] = None,
) -> dict[str, Any]:
    """Per-hour upload/download counts.

    When ``bot_id`` is None, returns the cluster rollup + per-bot
    top-N. When set, returns the per-hour buckets for that bot only.
    """
    end = _utc_now()
    start = end - timedelta(hours=hours)

    wanted_actions = set(UPLOAD_ACTIONS) | set(DOWNLOAD_ACTIONS)

    q = (
        select(AuditLog)
        .where(AuditLog.action.in_(wanted_actions))
        .where(AuditLog.created_at >= start)
        .where(AuditLog.created_at < end)
        .where(AuditLog.target_type == "bot")
    )
    if bot_id is not None:
        q = q.where(AuditLog.target_id == bot_id)
    q = q.order_by(AuditLog.created_at.asc())
    rows = (await db.execute(q)).scalars().all()

    # bucket[bot_id][hour_label] = {u: int, d: int}
    bucket: dict[int, dict[str, dict[str, int]]] = defaultdict(dict)
    bot_totals: dict[int, dict[str, int]] = defaultdict(lambda: {"u": 0, "d": 0})

    for r in rows:
        bid = int(r.target_id or 0)
        if not bid:
            continue
        h = _hour_label(_hour_bucket(r.created_at))
        cell = bucket[bid].setdefault(h, {"u": 0, "d": 0})
        if r.action in UPLOAD_ACTIONS:
            cell["u"] += 1
            bot_totals[bid]["u"] += 1
        else:
            cell["d"] += 1
            bot_totals[bid]["d"] += 1

    labels = _hour_labels(start, end)

    if bot_id is not None:
        bot = await db.get(BotInstance, bot_id)
        if not bot:
            return {"bots": [], "window_hours": hours}
        buckets = []
        for label in labels:
            cell = bucket.get(bot_id, {}).get(label, {"u": 0, "d": 0})
            buckets.append({"hour": label, "uploads": cell["u"], "downloads": cell["d"]})
        totals = bot_totals.get(bot_id, {"u": 0, "d": 0})
        return {
            "bots": [{
                "bot_id": bot.id,
                "name": bot.name,
                "slug": bot.slug,
                "total_uploads": totals["u"],
                "total_downloads": totals["d"],
                "buckets": buckets,
            }],
            "window_hours": hours,
        }

    # Cluster view: one row per active bot, with merged buckets.
    bot_ids = sorted(bot_totals.keys(), key=lambda i: -(bot_totals[i]["u"] + bot_totals[i]["d"]))
    # Resolve bot metadata in one query.
    bot_rows = (await db.execute(
        select(BotInstance).where(BotInstance.id.in_(bot_ids))
    )).scalars().all() if bot_ids else []
    bot_map = {b.id: b for b in bot_rows}

    bots: list[dict] = []
    for bid in bot_ids:
        b = bot_map.get(bid)
        if not b:
            continue
        totals = bot_totals[bid]
        buckets = []
        for label in labels:
            cell = bucket[bid].get(label, {"u": 0, "d": 0})
            buckets.append({"hour": label, "uploads": cell["u"], "downloads": cell["d"]})
        bots.append({
            "bot_id": b.id,
            "name": b.name,
            "slug": b.slug,
            "total_uploads": totals["u"],
            "total_downloads": totals["d"],
            "buckets": buckets,
        })

    return {"bots": bots, "window_hours": hours}


# ---------------------------------------------------------------------------
# Live bot metrics (used by alerts evaluator + the metrics page)
# ---------------------------------------------------------------------------

async def collect_live_bot_metrics(db: AsyncSession) -> list[dict]:
    """Per-bot live snapshot — RAM/CPU/status.

    MULTITENANT bots have a live process we can probe via
    ``bot_runner.bot_status``. LEGACY_SPACE bots report a status
    only (their RAM/CPU live in the remote HF Space, not here).
    """
    import bot_runner  # local import to keep this module cheap to import

    rows = (await db.execute(select(BotInstance))).scalars().all()
    out: list[dict] = []
    for b in rows:
        rec = {
            "bot_id": b.id,
            "name": b.name,
            "slug": b.slug,
            "status": b.status.value if hasattr(b.status, "value") else str(b.status),
            "deployment_mode": b.deployment_mode.value if hasattr(b.deployment_mode, "value") else str(b.deployment_mode),
            "ram_mb_used": None,
            "cpu_percent": None,
            "port": b.port,
            "last_healthcheck": b.last_healthcheck.isoformat() if b.last_healthcheck else None,
        }
        if b.deployment_mode == DeploymentMode.MULTITENANT and b.port:
            try:
                st = bot_runner.bot_status(b)
                rec["ram_mb_used"] = float(st.get("rss_mb") or 0.0)
                rec["cpu_percent"] = float(st.get("cpu_percent") or 0.0)
            except Exception:  # noqa: BLE001
                pass
        out.append(rec)
    return out


# ---------------------------------------------------------------------------
# Cluster overview — single payload for the dashboard cards.
# ---------------------------------------------------------------------------

async def collect_overview(db: AsyncSession, hours: int = 24) -> dict[str, Any]:
    """Top-of-dashboard cards: requests today, errors, active bots,
    most active bot, alerts in last 24h.
    """
    from models import AlertEvent   # local to dodge a cycle at import

    end = _utc_now()
    start = end - timedelta(hours=hours)

    proxy = await collect_proxy_traffic(db, hours=hours)
    activity = await collect_bot_activity(db, hours=hours)
    live = await collect_live_bot_metrics(db)

    active_bots = [r for r in live if r["status"] == BotStatus.RUNNING.value]
    top_bots = sorted(
        activity["bots"], key=lambda b: -(b["total_uploads"] + b["total_downloads"])
    )[:5]

    alert_rows = (await db.execute(
        select(func.count(AlertEvent.id)).where(AlertEvent.fired_at >= start)
    )).scalar() or 0

    return {
        "summary": proxy["summary"],
        "buckets": proxy["buckets"],
        "bots": {
            "total": len(live),
            "running": len(active_bots),
            "status_breakdown": {
                (r["status"]): sum(1 for x in live if x["status"] == r["status"])
                for r in live
            },
        },
        "activity": {
            "total_uploads": sum(b["total_uploads"] for b in activity["bots"]),
            "total_downloads": sum(b["total_downloads"] for b in activity["bots"]),
            "top_bots": top_bots,
        },
        "alerts_24h": int(alert_rows),
        "window_hours": hours,
        "ts": time.time(),
    }


__all__ = [
    "ProxyTrafficSummary",
    "ProxyHourBucket",
    "EndpointStat",
    "BotActivitySummary",
    "BotActivityHourBucket",
    "PROXY_ACTIONS",
    "UPLOAD_ACTIONS",
    "DOWNLOAD_ACTIONS",
    "collect_proxy_traffic",
    "collect_bot_activity",
    "collect_live_bot_metrics",
    "collect_overview",
]