feat(dashboard): log date links to per-session detail page (session-<id>.html)

This commit is contained in:
Lang
2026-10-14 20:44:36 +08:00
parent 05d8abab7a
commit 7c7e3bf242
2 changed files with 77 additions and 4 deletions
+75 -2
View File
@@ -15,6 +15,7 @@
from __future__ import annotations
import datetime as _dt
import html as _html
import json
import sys
from pathlib import Path
@@ -125,6 +126,64 @@ CHART_JS = r"""
"""
# ---------- 服务端渲染小工具(会话详情页用) ----------
KIND_CN = {"word": "单词", "phrase": "词组", "expression": "表达"}
def esc(v) -> str:
return _html.escape("" if v is None else str(v))
def badge(s: str) -> str:
return f'<span class="badge b-{s}">{s}</span>' if s else ""
def items_table(items: list[dict]) -> str:
if not items:
return '<p class="muted">无</p>'
head = ("<thead><tr><th>id</th><th>类型</th><th>条目</th><th>等级</th>"
"<th>释义</th><th>上下文</th><th>状态</th><th>修正</th></tr></thead>")
rows = "".join(
f'<tr><td class="mono">{esc(i["id"])}</td><td>{KIND_CN.get(i["kind"], esc(i["kind"]))}</td>'
f'<td><b>{esc(i["item"])}</b></td><td>{esc(i["level"])}</td><td>{esc(i["meaning"])}</td>'
f'<td class="muted">{esc(i["context"])}</td><td>{badge(i["status"])}</td>'
f'<td class="fx mono">{esc(i["fix"])}</td></tr>' for i in items)
return f"<table>{head}<tbody>{rows}</tbody></table>"
def grammar_table(grs: list[dict]) -> str:
if not grs:
return '<p class="muted">无</p>'
head = ("<thead><tr><th>id</th><th>类别</th><th>问题</th><th>规则</th>"
"<th>例子</th><th>状态</th></tr></thead>")
rows = "".join(
f'<tr><td class="mono">{esc(g["id"])}</td><td>{esc(g["category"])}</td>'
f'<td>{esc(g["issue"])}</td><td class="muted">{esc(g["rule"])}</td>'
f'<td class="fx mono">{esc(g["examples"])}</td><td>{badge(g["status"])}</td></tr>'
for g in grs)
return f"<table>{head}<tbody>{rows}</tbody></table>"
def session_body(s: dict, its: list[dict], grs: list[dict], files: list[str]) -> str:
kv = [("日期", s["date"]), ("类型", s["type"]), ("材料", s["material_level"]),
("听", s["listening"]), ("说", s["speaking"]), ("读", s["reading"]), ("写", s["writing"])]
cards = "".join(
f'<div class="card"><div class="k">{k}</div><div class="v">{esc(v) or "—"}</div></div>'
for k, v in kv)
note = esc(s["note"]) or '<span class="muted">—</span>'
links = "".join(f'<li><a href="../../logs/{esc(f)}">{esc(f)}</a></li>' for f in files) \
or '<li class="muted">无</li>'
return (
f'<p><a href="log.html">← 返回日志</a></p>'
f'<h1>{esc(s["date"])} · {esc(s["type"]) or "session"}</h1>'
f'<div class="cards">{cards}</div>'
f'<h2>备注</h2><p>{note}</p>'
f'<h2>相关词条</h2>{items_table(its)}'
f'<h2>相关语法</h2>{grammar_table(grs)}'
f'<h2>记录文件</h2><ul>{links}</ul>')
def html_page(title: str, active: str, body: str, script: str, generated: str) -> str:
nav = "".join(
f'<a href="{href}"{" class=\"on\"" if key == active else ""}>{label}</a>'
@@ -241,7 +300,7 @@ document.getElementById("grammar").innerHTML =
SCRIPT_LOG = r"""
document.getElementById("log").innerHTML =
`<thead><tr><th>日期</th><th>类型</th><th>材料</th><th>听</th><th>说</th><th>读</th><th>写</th><th>备注</th></tr></thead><tbody>` +
(DATA.log.map(l => `<tr><td class="mono">${esc(l.date)}</td><td>${esc(l.type)}</td>
(DATA.log.map(l => `<tr><td class="mono"><a href="session-${l.id}.html">${esc(l.date)}</a></td><td>${esc(l.type)}</td>
<td>${esc(l.material_level)}</td><td>${esc(l.listening)}</td><td>${esc(l.speaking)}</td>
<td>${esc(l.reading)}</td><td>${esc(l.writing)}</td>
<td class="muted">${esc(l.note)}</td></tr>`).join("") || "") + `</tbody>`;
@@ -280,7 +339,21 @@ def main() -> int:
for fname, (title, active, body, script) in pages.items():
(OUT / fname).write_text(html_page(title, active, body, script, gen), encoding="utf-8")
print(f"HTML : {OUT.relative_to(ROOT)}/ ({len(pages)} 页 + data.js, "
# 每个训练日志条目生成一个会话详情页
logs_dir = ROOT / "logs"
n = 0
for s in data["training_log"]:
date = s.get("date", "")
its = [i for i in payload["items"] if (i.get("added") or "") == date]
grs = [g for g in payload["grammar"]
if date and date in (g.get("first", ""), g.get("last", ""))]
files = sorted(p.name for p in logs_dir.glob(f"{date}*.md")) if logs_dir.exists() else []
body = session_body(s, its, grs, files)
(OUT / f"session-{s['id']}.html").write_text(
html_page(f"{date} {s.get('type','')}", "log", body, "", gen), encoding="utf-8")
n += 1
print(f"HTML : {OUT.relative_to(ROOT)}/ ({len(pages)} 页 + {n} 会话详情 + data.js, "
f"{len(payload['items'])} items, {len(payload['grammar'])} grammar)")
return 0
+2 -2
View File
@@ -152,8 +152,8 @@ def export(conn: sqlite3.Connection) -> dict:
return {
"skills": rows("SELECT id,name,level,confidence,note FROM skills"),
"training_log": rows(
"SELECT date,type,material_level,listening,speaking,reading,writing,note"
" FROM training_log ORDER BY date"),
"SELECT id,date,type,material_level,listening,speaking,reading,writing,note"
" FROM training_log ORDER BY date,id"),
"level_history": rows("SELECT date,text FROM level_history ORDER BY date,id"),
"items": rows(
"SELECT id,kind,item,level,meaning,context,status,first,fix,function,tags,"