fix dashboard mapping table coverage
This commit is contained in:
1 parent
17068c7928
commit
23abd486af
3 files changed
+110
-22
No files matched your search
+94
-18
@@ -65,28 +65,104 @@ def _normalize_rows(raw: Any, keys: tuple[str, ...]) -> list[dict[str, Any]]:
|
||||
return []
|
||||
|
||||
|
||||
def _text(value: Any, default: str = "-") -> str:
|
||||
if value is None:
|
||||
return default
|
||||
text = str(value).strip()
|
||||
return text if text else default
|
||||
|
||||
|
||||
def _first_text(source: dict[str, Any], *keys: str, default: str = "-") -> str:
|
||||
for key in keys:
|
||||
value = source.get(key)
|
||||
if value not in (None, ""):
|
||||
return _text(value, default)
|
||||
return default
|
||||
|
||||
|
||||
def _format_countdown(detail: dict[str, Any]) -> str:
|
||||
time_text = _first_text(detail, "time_text", "countdown", "countdown_text")
|
||||
if time_text != "-":
|
||||
return time_text
|
||||
mm = _text(detail.get("countdown_mm"), "")
|
||||
ss = _text(detail.get("countdown_ss"), "")
|
||||
if mm and ss:
|
||||
return f"{mm.zfill(2)}:{ss.zfill(2)}"
|
||||
return mm or ss or "-"
|
||||
|
||||
|
||||
def _mapping_rows() -> list[dict[str, Any]]:
|
||||
raw = _read_json(Path(path_config.get_project_root()) / "douyin" / "core" / "state" / "room_pos_map.json", {})
|
||||
reservations = _read_json(path_config.get_reservations_path(), {})
|
||||
reserved_positions = set()
|
||||
for item in _normalize_rows(reservations, ("queue", "reservations", "items")):
|
||||
pos = item.get("pos_seen") or item.get("pos") or item.get("room_pos")
|
||||
if pos not in (None, ""):
|
||||
reserved_positions.add(str(pos))
|
||||
|
||||
rows: list[dict[str, Any]] = []
|
||||
if isinstance(raw, dict) and "_meta" in raw:
|
||||
raw = {key: value for key, value in raw.items() if key != "_meta"}
|
||||
|
||||
if isinstance(raw, dict):
|
||||
source = raw.get("items") if isinstance(raw.get("items"), list) else raw
|
||||
if isinstance(source, dict):
|
||||
iterable = source.items()
|
||||
else:
|
||||
iterable = enumerate(source if isinstance(source, list) else [])
|
||||
for key, value in iterable:
|
||||
if str(key).startswith("_"):
|
||||
continue
|
||||
if isinstance(value, dict):
|
||||
rows.append({
|
||||
"pos": value.get("pos") or value.get("room_pos") or key,
|
||||
"title": value.get("title") or value.get("room_title") or value.get("raw_title") or "-",
|
||||
"status": value.get("status") or value.get("state") or "已记录",
|
||||
"updated": value.get("updated_at") or value.get("ts") or value.get("time") or "-",
|
||||
})
|
||||
else:
|
||||
rows.append({"pos": key, "title": str(value), "status": "已记录", "updated": "-"})
|
||||
return rows[:80]
|
||||
source = raw.get("items") or raw.get("records") or raw.get("rooms") or raw
|
||||
iterable = source.items() if isinstance(source, dict) else enumerate(source if isinstance(source, list) else [])
|
||||
elif isinstance(raw, list):
|
||||
iterable = enumerate(raw)
|
||||
else:
|
||||
iterable = []
|
||||
|
||||
for key, value in iterable:
|
||||
if str(key).startswith("_"):
|
||||
continue
|
||||
if not isinstance(value, dict):
|
||||
rows.append({
|
||||
"pos": key,
|
||||
"title": _text(value),
|
||||
"raw_title": "-",
|
||||
"reserved": "是" if str(key) in reserved_positions else "-",
|
||||
"has_fudai": "-",
|
||||
"value": "-",
|
||||
"countdown": "-",
|
||||
"participants": "-",
|
||||
"copies": "-",
|
||||
"button": "-",
|
||||
"task": "-",
|
||||
"content": "-",
|
||||
"updated": "-",
|
||||
})
|
||||
continue
|
||||
|
||||
detail = value.get("fudai_detail") if isinstance(value.get("fudai_detail"), dict) else {}
|
||||
pos = _first_text(value, "pos", "room_pos", "pos_seen", default=str(key))
|
||||
updated = (
|
||||
value.get("timestamp")
|
||||
or value.get("updated_at")
|
||||
or value.get("ts")
|
||||
or detail.get("timestamp")
|
||||
or value.get("time")
|
||||
)
|
||||
rows.append({
|
||||
"pos": pos,
|
||||
"title": _first_text(value, "title", "room_title", "live_room_title", "title_raw"),
|
||||
"raw_title": _first_text(value, "title_raw", "raw_title", "live_room_title_raw"),
|
||||
"reserved": "是" if pos in reserved_positions or str(key) in reserved_positions else "-",
|
||||
"has_fudai": "是" if value.get("has_fudai") else "-",
|
||||
"value": _first_text(detail, "prize_value_yuan", "value", "value_raw"),
|
||||
"countdown": _format_countdown(detail),
|
||||
"participants": _first_text(detail, "participants"),
|
||||
"copies": _first_text(detail, "copies"),
|
||||
"button": _first_text(detail, "button_text", "button_text_raw"),
|
||||
"task": _first_text(detail, "task_list"),
|
||||
"content": _first_text(value, "fudai_content", "content", default=_first_text(detail, "content")),
|
||||
"updated": fmt_ts(updated) if updated else "-",
|
||||
})
|
||||
|
||||
def sort_key(row: dict[str, Any]) -> tuple[int, str]:
|
||||
pos = str(row.get("pos", ""))
|
||||
return (int(pos), "") if pos.isdigit() else (999999, pos)
|
||||
|
||||
return sorted(rows, key=sort_key)
|
||||
|
||||
|
||||
def load_dashboard_data() -> DashboardData:
|
||||
|
||||
@@ -270,7 +270,10 @@ def build_mapping(parent: tk.Misc, data: DashboardData, ctx: PageContext) -> Non
|
||||
section(parent, "映射表", "显示原始标题 / 刷新映射表 / 搜索 / 复制条目")
|
||||
toolbar(parent, [("自动刷新(5s)", None, "soft"), ("刷新映射表", ctx.action("refresh"), "soft"), "显示原始标题", "清除", "复制条目"])
|
||||
table_panel(parent, "运行时映射表", (
|
||||
("pos", "位置", 80), ("title", "标题", 360), ("status", "状态", 100), ("updated", "更新时间", 180),
|
||||
("pos", "位置", 70), ("title", "标题", 230), ("raw_title", "原始标题", 250),
|
||||
("reserved", "预约", 64), ("has_fudai", "福袋", 64), ("value", "价值", 90),
|
||||
("countdown", "倒计时", 90), ("participants", "人数", 74), ("copies", "份数", 70),
|
||||
("button", "按钮", 120), ("task", "任务", 180), ("content", "内容", 220), ("updated", "更新时间", 160),
|
||||
), data.mapping_rows, height=22).pack(fill="both", expand=True)
|
||||
|
||||
|
||||
|
||||
@@ -375,15 +375,24 @@ def log_panel(parent, title: str, lines: Iterable[str], height: int = 24) -> tup
|
||||
|
||||
def table_panel(parent, title: str, columns: Sequence[tuple[str, str, int]], rows: Sequence[dict], height: int = 8) -> tk.Frame:
|
||||
box = card(parent, title)
|
||||
tree = ttk.Treeview(box, columns=[c[0] for c in columns], show="headings", height=height)
|
||||
table_wrap = tk.Frame(box, bg=theme.SURFACE)
|
||||
table_wrap.pack(fill="both", expand=True, padx=14, pady=(4, 14))
|
||||
table_wrap.grid_rowconfigure(0, weight=1)
|
||||
table_wrap.grid_columnconfigure(0, weight=1)
|
||||
tree = ttk.Treeview(table_wrap, columns=[c[0] for c in columns], show="headings", height=height)
|
||||
ybar = ttk.Scrollbar(table_wrap, orient="vertical", command=tree.yview)
|
||||
xbar = ttk.Scrollbar(table_wrap, orient="horizontal", command=tree.xview)
|
||||
tree.configure(yscrollcommand=ybar.set, xscrollcommand=xbar.set)
|
||||
for key, label, width in columns:
|
||||
tree.heading(key, text=label)
|
||||
tree.column(key, width=width, anchor="w")
|
||||
tree.column(key, width=width, minwidth=min(width, 90), anchor="w", stretch=False)
|
||||
for row in rows:
|
||||
tree.insert("", "end", values=[str(row.get(key, "-")) for key, _, _ in columns])
|
||||
if not rows:
|
||||
tree.insert("", "end", values=["暂无数据" if idx == 0 else "" for idx, _c in enumerate(columns)])
|
||||
tree.pack(fill="both", expand=True, padx=14, pady=(4, 14))
|
||||
tree.grid(row=0, column=0, sticky="nsew")
|
||||
ybar.grid(row=0, column=1, sticky="ns")
|
||||
xbar.grid(row=1, column=0, sticky="ew")
|
||||
return box
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user