# -*- coding: utf-8 -*- """Upload timeline helpers — hour buckets for Shorts / TikTok schedule boards.""" from __future__ import annotations from typing import Any from .timeutil import beijing_from_ts def build_timeline( jobs: list[dict[str, Any]], *, now: float, day_start: float, day_end: float, ) -> dict[str, Any]: """Build 24 Beijing-hour slots with job markers for the current push day.""" hours: list[dict[str, Any]] = [] for h in range(24): hours.append( { "hour": h, "label": f"{h:02d}:00", "jobs": [], "count": 0, "is_past": False, "is_current": False, } ) now_h = beijing_from_ts(now).hour for bucket in hours: bucket["is_past"] = int(bucket["hour"]) < now_h bucket["is_current"] = int(bucket["hour"]) == now_h for j in jobs: ts = float(j.get("upload_at") or j.get("finished_at") or j.get("updated_at") or 0) if ts <= 0: continue if ts < day_start - 60 or ts > day_end + 60: continue h = beijing_from_ts(ts).hour item = { "id": j.get("id"), "upload_at": ts, "status": j.get("status"), "anchor_name": j.get("anchor_name") or "", "channel_name": j.get("channel_name") or "", "channel_pk": j.get("channel_pk") or "", "clip_ready": bool(j.get("clip_ready")), "can_upload_now": bool(j.get("can_upload_now")), } hours[h]["jobs"].append(item) hours[h]["count"] = len(hours[h]["jobs"]) return { "hours": hours, "now_hour": now_h, "total": sum(int(x["count"]) for x in hours), }