256 lines
11 KiB
Python
256 lines
11 KiB
Python
#!/usr/bin/env python3
|
|
"""读取 me/lang.db,渲染 me/dashboard.html(自包含仪表盘)。
|
|
|
|
用法:
|
|
python actions/build_dashboard.py
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import datetime as _dt
|
|
import json
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parent.parent
|
|
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
|
import langdb # noqa: E402
|
|
|
|
OUT = langdb.ME / "dashboard.html"
|
|
|
|
|
|
HTML = r"""<!doctype html>
|
|
<html lang="zh">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
<title>Lang · 我的学习状态</title>
|
|
<style>
|
|
:root{
|
|
--bg:#0f1115; --panel:#171a21; --panel2:#1e222b; --line:#2a2f3a;
|
|
--fg:#e7e9ee; --muted:#9aa3b2; --accent:#5b8def;
|
|
--known:#2ecc71; --learning:#f1c40f; --fix:#e74c3c;
|
|
}
|
|
*{box-sizing:border-box}
|
|
body{margin:0;background:var(--bg);color:var(--fg);
|
|
font:15px/1.55 -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"PingFang SC","Microsoft YaHei",sans-serif}
|
|
header{padding:22px 26px;border-bottom:1px solid var(--line);display:flex;
|
|
justify-content:space-between;align-items:baseline;flex-wrap:wrap;gap:8px}
|
|
h1{margin:0;font-size:20px;letter-spacing:.3px}
|
|
.gen{color:var(--muted);font-size:13px}
|
|
main{padding:22px 26px;max-width:1200px;margin:0 auto}
|
|
h2{font-size:15px;text-transform:uppercase;letter-spacing:.8px;color:var(--muted);
|
|
margin:30px 0 12px;font-weight:600}
|
|
.cards{display:grid;grid-template-columns:repeat(auto-fit,minmax(160px,1fr));gap:12px}
|
|
.card{background:var(--panel);border:1px solid var(--line);border-radius:12px;padding:16px}
|
|
.card .k{color:var(--muted);font-size:13px}
|
|
.card .v{font-size:28px;font-weight:700;margin-top:4px}
|
|
.card .c{color:var(--muted);font-size:12px;margin-top:6px}
|
|
.chart{background:var(--panel);border:1px solid var(--line);border-radius:12px;padding:14px 12px 6px}
|
|
.legend{display:flex;gap:16px;flex-wrap:wrap;font-size:13px;margin:0 6px 6px;color:var(--muted)}
|
|
.legend i{display:inline-block;width:10px;height:10px;border-radius:2px;margin-right:5px;vertical-align:middle}
|
|
table{width:100%;border-collapse:collapse;background:var(--panel);
|
|
border:1px solid var(--line);border-radius:10px;overflow:hidden;font-size:13.5px}
|
|
th,td{padding:9px 11px;text-align:left;border-bottom:1px solid var(--line);vertical-align:top}
|
|
th{background:var(--panel2);color:var(--muted);font-weight:600;position:sticky;top:0}
|
|
tr:last-child td{border-bottom:none}
|
|
code,.mono{font-family:ui-monospace,SFMono-Regular,Consolas,monospace;font-size:12.5px;color:#c9d1e2}
|
|
.badge{display:inline-block;padding:1px 8px;border-radius:999px;font-size:12px;font-weight:600}
|
|
.b-known{background:rgba(46,204,113,.15);color:var(--known)}
|
|
.b-learning{background:rgba(241,196,15,.15);color:var(--learning)}
|
|
.b-fix{background:rgba(231,76,60,.15);color:var(--fix)}
|
|
.fx{color:#ffb4a8}
|
|
.toolbar{display:flex;gap:8px;flex-wrap:wrap;align-items:center;margin-bottom:10px}
|
|
.toolbar button,.toolbar input{background:var(--panel2);color:var(--fg);
|
|
border:1px solid var(--line);border-radius:8px;padding:6px 11px;font-size:13px;cursor:pointer}
|
|
.toolbar button.on{border-color:var(--accent);color:#fff;background:#24314d}
|
|
.toolbar input{cursor:text;min-width:200px}
|
|
.muted{color:var(--muted)}
|
|
.wrap{overflow:auto;max-height:70vh;border-radius:10px}
|
|
footer{color:var(--muted);padding:30px 26px;font-size:12px;text-align:center}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<header>
|
|
<h1>Lang · 我的学习状态</h1>
|
|
<span class="gen">生成于 __GENERATED__ · 数据源 me/lang.db</span>
|
|
</header>
|
|
<main>
|
|
<h2>四维等级</h2>
|
|
<div class="cards" id="skills"></div>
|
|
|
|
<h2>等级趋势</h2>
|
|
<div class="chart" id="chart"></div>
|
|
|
|
<h2>掌握概览</h2>
|
|
<div class="cards" id="overview"></div>
|
|
|
|
<h2>单词 / 词组 / 表达</h2>
|
|
<div class="toolbar">
|
|
<button data-f="all" class="on">全部</button>
|
|
<button data-f="known">已掌握</button>
|
|
<button data-f="learning">学习中</button>
|
|
<button data-f="fix">待修正</button>
|
|
<input id="q" placeholder="搜索 item / 释义 / 上下文…">
|
|
</div>
|
|
<div class="wrap"><table id="items"></table></div>
|
|
|
|
<h2>语法修正</h2>
|
|
<div class="wrap"><table id="grammar"></table></div>
|
|
|
|
<h2>训练日志</h2>
|
|
<div class="wrap"><table id="log"></table></div>
|
|
|
|
<h2>等级变更历史</h2>
|
|
<div class="wrap"><table id="history"></table></div>
|
|
</main>
|
|
<footer>由 actions/build_dashboard.py 生成 · SQLite: me/lang.db</footer>
|
|
<script>
|
|
const DATA = __DATA__;
|
|
const esc = s => String(s ?? "").replace(/[&<>"]/g, c => ({"&":"&","<":"<",">":">",'"':"""}[c]));
|
|
const badge = s => s ? `<span class="badge b-${s}">${s}</span>` : "";
|
|
const fx = s => s ? `<span class="fx mono">${esc(s)}</span>` : "";
|
|
|
|
// 四维等级
|
|
document.getElementById("skills").innerHTML = DATA.skills.map(s => `
|
|
<div class="card"><div class="k">${esc(s.name)}</div>
|
|
<div class="v">${esc(s.level)||"—"}</div>
|
|
<div class="c">置信度 ${esc(s.confidence)||"—"}</div></div>`).join("")
|
|
|| `<div class="muted">无数据</div>`;
|
|
|
|
// 概览
|
|
const ov = DATA.overview;
|
|
const glyph = {word:"单词", phrase:"词组", expression:"表达", grammar:"语法"};
|
|
document.getElementById("overview").innerHTML = Object.keys(glyph).map(k => {
|
|
const o = ov[k];
|
|
const parts = k === "grammar"
|
|
? `待修正 ${o.fix} · 已克服 ${o.known||0}`
|
|
: `已掌握 ${o.known} · 学习中 ${o.learning} · 待修正 ${o.fix}`;
|
|
return `<div class="card"><div class="k">${glyph[k]}</div>
|
|
<div class="v">${o.total}</div><div class="c">${parts}</div></div>`;
|
|
}).join("");
|
|
|
|
// 词条表 + 过滤
|
|
const KIND = {word:"单词", phrase:"词组", expression:"表达"};
|
|
function renderItems() {
|
|
const f = currentFilter, q = document.getElementById("q").value.trim().toLowerCase();
|
|
const rows = DATA.items.filter(i =>
|
|
(f === "all" || i.status === f) &&
|
|
(!q || (i.item + i.meaning + i.context + i.tags).toLowerCase().includes(q)));
|
|
document.getElementById("items").innerHTML =
|
|
`<thead><tr><th>id</th><th>类型</th><th>条目</th><th>等级</th><th>释义</th>
|
|
<th>上下文</th><th>状态</th><th>first</th><th>修正</th><th>标签</th></tr></thead><tbody>` +
|
|
rows.map(i => `<tr>
|
|
<td class="mono">${esc(i.id)}</td><td>${KIND[i.kind]||i.kind}</td>
|
|
<td><b>${esc(i.item)}</b></td><td>${esc(i.level)}</td>
|
|
<td>${esc(i.meaning)}</td><td class="muted">${esc(i.context)}</td>
|
|
<td>${badge(i.status)}</td><td class="mono">${esc(i.first)}</td>
|
|
<td>${fx(i.fix)}</td><td class="muted">${esc(i.tags)}</td></tr>`).join("") +
|
|
`</tbody>`;
|
|
}
|
|
let currentFilter = "all";
|
|
document.querySelectorAll(".toolbar button[data-f]").forEach(b => b.onclick = () => {
|
|
currentFilter = b.dataset.f;
|
|
document.querySelectorAll(".toolbar button[data-f]").forEach(x => x.classList.toggle("on", x === b));
|
|
renderItems();
|
|
});
|
|
document.getElementById("q").oninput = renderItems;
|
|
|
|
// 语法
|
|
document.getElementById("grammar").innerHTML =
|
|
`<thead><tr><th>id</th><th>类别</th><th>问题</th><th>正确规则</th><th>例子</th>
|
|
<th>状态</th><th>次数</th><th>最近</th><th>下次复习</th></tr></thead><tbody>` +
|
|
(DATA.grammar.map(g => `<tr>
|
|
<td class="mono">${esc(g.id)}</td><td>${esc(g.category)}</td>
|
|
<td>${esc(g.issue)}</td><td class="muted">${esc(g.rule)}</td>
|
|
<td>${fx(g.examples)}</td><td>${badge(g.status === "known" ? "known" : g.status)}</td>
|
|
<td>${g.count}</td><td class="mono">${esc(g.last)}</td>
|
|
<td class="mono">${esc(g.next_review)}</td></tr>`).join("") || "") + `</tbody>`;
|
|
|
|
// 训练日志
|
|
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>
|
|
<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>`;
|
|
|
|
// 等级历史
|
|
document.getElementById("history").innerHTML =
|
|
`<thead><tr><th>日期</th><th>变更</th></tr></thead><tbody>` +
|
|
(DATA.history.map(h => `<tr><td class="mono">${esc(h.date)}</td>
|
|
<td>${esc(h.text)}</td></tr>`).join("") || "") + `</tbody>`;
|
|
|
|
// 等级趋势图(从训练日志的每次等级画线)
|
|
(function () {
|
|
const LV = {A1:1, A2:2, B1:3, B2:4, C1:5, C2:6};
|
|
const val = s => {
|
|
const m = String(s || "").trim().toUpperCase().match(/^([ABC])([12])(\+?)/);
|
|
return m ? LV[m[1] + m[2]] + (m[3] ? 0.5 : 0) : null;
|
|
};
|
|
const SK = [
|
|
{k:"listening", l:"听", c:"#4da3ff"}, {k:"speaking", l:"说", c:"#f1c40f"},
|
|
{k:"reading", l:"读", c:"#2ecc71"}, {k:"writing", l:"写", c:"#e879f9"}];
|
|
const el = document.getElementById("chart");
|
|
const pts = DATA.log.slice().sort((a, b) => a.date < b.date ? -1 : 1)
|
|
.map(r => ({date: r.date, v: Object.fromEntries(SK.map(s => [s.k, val(r[s.k])]))}));
|
|
if (!pts.some(p => SK.some(s => p.v[s.k] != null))) {
|
|
el.innerHTML = '<div class="muted">暂无趋势数据</div>'; return;
|
|
}
|
|
const W = 760, H = 320, L = 46, R = 18, T = 16, B = 40, ymin = 0.7, ymax = 6.3;
|
|
const xs = i => L + (pts.length === 1 ? (W - L - R) / 2 : i * (W - L - R) / (pts.length - 1));
|
|
const ys = v => T + (ymax - v) * (H - T - B) / (ymax - ymin);
|
|
let g = "";
|
|
["A1","A2","B1","B2","C1","C2"].forEach((lab, i) => {
|
|
const y = ys(i + 1);
|
|
g += `<line x1="${L}" y1="${y}" x2="${W - R}" y2="${y}" stroke="var(--line)"/>`;
|
|
g += `<text x="${L - 8}" y="${y + 4}" fill="var(--muted)" font-size="11" text-anchor="end">${lab}</text>`;
|
|
});
|
|
pts.forEach((p, i) => {
|
|
g += `<text x="${xs(i)}" y="${H - 14}" fill="var(--muted)" font-size="11" text-anchor="middle">${p.date.slice(5)}</text>`;
|
|
});
|
|
SK.forEach(s => {
|
|
const seq = pts.map((p, i) => ({i, v: p.v[s.k]})).filter(o => o.v != null);
|
|
if (!seq.length) return;
|
|
if (seq.length > 1)
|
|
g += `<polyline fill="none" stroke="${s.c}" stroke-width="2" points="${seq.map(o => xs(o.i) + "," + ys(o.v)).join(" ")}"/>`;
|
|
seq.forEach(o => {
|
|
g += `<circle cx="${xs(o.i)}" cy="${ys(o.v)}" r="4" fill="${s.c}"><title>${s.l} ${o.v} · ${pts[o.i].date}</title></circle>`;
|
|
});
|
|
});
|
|
el.innerHTML = `<div class="legend">` + SK.map(s => `<span><i style="background:${s.c}"></i>${s.l}</span>`).join("") +
|
|
`</div><svg viewBox="0 0 ${W} ${H}" width="100%" preserveAspectRatio="xMidYMid meet">${g}</svg>`;
|
|
})();
|
|
|
|
renderItems();
|
|
</script>
|
|
</body>
|
|
</html>
|
|
"""
|
|
|
|
|
|
def main() -> int:
|
|
conn = langdb.connect()
|
|
data = langdb.export(conn)
|
|
payload = {
|
|
"generated": _dt.datetime.now().strftime("%Y-%m-%d %H:%M"),
|
|
"skills": data["skills"],
|
|
"overview": langdb.overview(conn),
|
|
"items": data["items"],
|
|
"grammar": data["grammar"],
|
|
"log": data["training_log"],
|
|
"history": data["level_history"],
|
|
}
|
|
conn.close()
|
|
|
|
html = (HTML
|
|
.replace("__GENERATED__", payload["generated"])
|
|
.replace("__DATA__", json.dumps(payload, ensure_ascii=False)))
|
|
OUT.write_text(html, encoding="utf-8")
|
|
print(f"HTML : {OUT.relative_to(ROOT)} "
|
|
f"({len(payload['items'])} items, {len(payload['grammar'])} grammar)")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|