douyinWEB/backend/app/platform_dashboard.py
eric d409e416da Ship physical dual-edge stack, Studio CDP live metrics, and ops tooling.
Capture Neko/WG/network hubs, Studio viewer charts, physical deploy docs, and one-shot scripts so VPS vs physical can be toggled later without losing WIP.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-04 22:42:40 -05:00

108 lines
4.4 KiB
Python

# -*- coding: utf-8 -*-
"""Cross-platform recent jobs + OAuth health board (read-mostly)."""
from __future__ import annotations
import time
from typing import Any
def list_recent_jobs(*, limit: int = 60, status: str = "") -> dict[str, Any]:
from . import db
from . import tiktok_db
from . import x_clips_db
from . import yt_long_db
lim = max(1, min(limit, 200))
items: list[dict[str, Any]] = []
def _push(platform: str, row: dict[str, Any], *, url_key: str, id_key: str) -> None:
items.append(
{
"platform": platform,
"id": str(row.get("id") or ""),
"status": str(row.get("status") or ""),
"anchor_name": str(row.get("anchor_name") or ""),
"room_id": str(row.get("room_id") or ""),
"channel_pk": str(row.get("channel_pk") or ""),
"error": str(row.get("error") or "")[:240],
"external_url": str(row.get(url_key) or ""),
"external_id": str(row.get(id_key) or ""),
"finished_at": float(row.get("finished_at") or row.get("updated_at") or 0),
"updated_at": float(row.get("updated_at") or 0),
}
)
st = str(status or "").strip()
shorts = db.list_shorts_jobs(status=st or None, limit=lim, offset=0)
for row in shorts.get("jobs") or []:
_push("shorts", row, url_key="youtube_url", id_key="youtube_video_id")
tt = tiktok_db.list_tiktok_jobs(status=st or None, limit=lim, offset=0)
for row in tt.get("jobs") or []:
vid = str(row.get("tiktok_video_id") or row.get("tiktok_publish_id") or "")
_push("tiktok", row, url_key="tiktok_video_id", id_key="tiktok_video_id")
if items:
items[-1]["external_url"] = vid
xc = x_clips_db.list_x_clip_jobs(status=st or None, limit=lim, offset=0)
for row in xc.get("jobs") or []:
_push("x_clips", row, url_key="media_url", id_key="x2_job_id")
yl = yt_long_db.list_yt_long_jobs_page(status=st or None, limit=lim, offset=0)
for row in yl.get("jobs") or []:
_push("yt_long", row, url_key="youtube_url", id_key="youtube_video_id")
items.sort(key=lambda x: float(x.get("finished_at") or x.get("updated_at") or 0), reverse=True)
return {"items": items[:lim], "count": len(items[:lim])}
def oauth_health_board(*, probe: bool = False) -> dict[str, Any]:
from . import db
from . import youtube_manage
from . import yt_long_db
from .yt_channel_meta import oauth_meta_from_channel, brand_hint_for_handle
rows = db.list_shorts_channels(enabled=None)
ypp_map = yt_long_db.ypp_ok_by_shorts_pk()
profiles = youtube_manage.list_yt_profile_caches()
out: list[dict[str, Any]] = []
probed = 0
for ch in rows:
pk = str(ch.get("id") or "")
meta = oauth_meta_from_channel(ch)
prof = profiles.get(pk) or {}
handle = prof.get("custom_url") or prof.get("handle") or ""
brand = brand_hint_for_handle(handle, str(ch.get("name") or ""), str(ch.get("note") or ""))
row = {
"id": pk,
"name": ch.get("name"),
"youtube_channel_id": ch.get("youtube_channel_id"),
"handle": handle,
"thumb_url": (prof.get("thumbnails") or {}).get("default", {}).get("url") if isinstance(prof.get("thumbnails"), dict) else "",
"oauth_ok": int(ch.get("oauth_ok") if ch.get("oauth_ok") is not None else -1),
"oauth_error": str(ch.get("oauth_error") or "")[:160],
"oauth_profile_id": meta.get("oauth_profile_id") or str(ch.get("oauth_profile_id") or ""),
"brand_hint": brand,
"authorized": bool(meta.get("authorized")),
"enabled": bool(ch.get("enabled")),
"ypp_ok": (
int(ch.get("ypp_ok"))
if ch.get("ypp_ok") is not None and int(ch.get("ypp_ok")) in (0, 1)
else ypp_map.get(pk, -1)
),
}
out.append(row)
return {"channels": out, "total": len(out)}
async def oauth_health_probe_all() -> dict[str, Any]:
from .shorts_oauth import probe_all_enabled_channels
from .yt_long_monetize import refresh_all_ypp_from_shorts
result = await probe_all_enabled_channels()
ypp = await refresh_all_ypp_from_shorts(limit=100)
board = oauth_health_board(probe=False)
board["probe"] = result
board["ypp_probe"] = ypp
return board