57 lines
1.9 KiB
Python
57 lines
1.9 KiB
Python
"""在途调拨运营指标。
|
|
|
|
用途:从数据库现状投影成功率、失败类型、待处理老化与通知失败等指标,
|
|
供运营看板与告警使用。
|
|
依赖:models、backlog_service。
|
|
逻辑:指标只读,不触发任何外部调用。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import datetime
|
|
from typing import Any
|
|
|
|
from app import db
|
|
from app.models import TransferInvoiceItem, TransferRun
|
|
from app.services.in_transit_transfer.backlog_service import backlog_summary
|
|
from app.services.in_transit_transfer.status import TransferNotificationStatus
|
|
|
|
|
|
def collect_metrics(*, now: datetime | None = None) -> dict[str, Any]:
|
|
"""采集调拨域指标。
|
|
|
|
Args:
|
|
now: 判定时刻(老化计算用)。
|
|
Returns:
|
|
{"runs": {...}, "items": {...}, "backlog": {...}}。
|
|
"""
|
|
runs_by_status: dict[str, int] = {}
|
|
for (status,) in db.session.query(TransferRun.status).all():
|
|
runs_by_status[str(status)] = runs_by_status.get(str(status), 0) + 1
|
|
notification_failed_runs = TransferRun.query.filter_by(
|
|
notification_status=TransferNotificationStatus.FAILED.value,
|
|
).count()
|
|
|
|
items_by_status: dict[str, int] = {}
|
|
for (status,) in db.session.query(TransferInvoiceItem.status).all():
|
|
items_by_status[str(status)] = items_by_status.get(str(status), 0) + 1
|
|
|
|
backlog = backlog_summary(now=now)
|
|
return {
|
|
"runs": {
|
|
**runs_by_status,
|
|
"notification_failed_runs": notification_failed_runs,
|
|
},
|
|
"items": items_by_status,
|
|
"backlog": {
|
|
"recoverable": backlog.recoverable_count,
|
|
"closed": backlog.closed_count,
|
|
"unknown": backlog.unknown_count,
|
|
"aging": {
|
|
"warn_3d": backlog.warn_3d,
|
|
"escalate_5d": backlog.escalate_5d,
|
|
"critical_7d": backlog.critical_7d,
|
|
},
|
|
},
|
|
}
|