Add tabbed period stats with lightweight charts on instance pages.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-07-10 13:27:02 +08:00
parent 0ce14c8822
commit 6aaa328a24
8 changed files with 826 additions and 535 deletions
+26 -24
View File
@@ -7,39 +7,40 @@ ROOT = Path(__file__).resolve().parent.parent
SRC = ROOT / "lib" / "instance" / "templates" / "index.html"
OUT = ROOT / "lib" / "instance" / "templates" / "embed_page_fragment.html"
GRID_START = ' <div class="grid">'
STATS_START = ' <div class="card full stats-card'
GRID_START = " <div class=\"grid\">"
def _slice_between(lines: list[str], start: str, end: str | None) -> list[str]:
try:
i = next(idx for idx, line in enumerate(lines) if line == start)
except StopIteration:
raise SystemExit(f"marker not found: {start!r}")
if end is None:
return lines[i:]
try:
j = next(idx for idx, line in enumerate(lines[i + 1 :], i + 1) if line.startswith(end))
except StopIteration:
raise SystemExit(f"end marker not found: {end!r}")
return lines[i:j]
def _find_line(lines: list[str], predicate, *, start: int = 0) -> int:
for idx in range(start, len(lines)):
if predicate(lines[idx]):
return idx
raise SystemExit("marker not found")
def main() -> None:
lines = SRC.read_text(encoding="utf-8").splitlines()
macro_start = next(i for i, l in enumerate(lines) if l.startswith("{% macro period_stats"))
macro_end = next(i for i, l in enumerate(lines) if l.strip() == "{% endmacro %}")
macro_start = _find_line(lines, lambda l: "macro period_stats_pane" in l)
macro_end = _find_line(lines, lambda l: l.strip() == "{% endmacro %}", start=macro_start)
macro_body = lines[macro_start : macro_end + 1]
grid_block = _slice_between(lines, GRID_START, STATS_START)
# strip outer .grid wrapper; fragment adds its own
if grid_block and grid_block[0] == GRID_START:
grid_block = grid_block[1:]
if grid_block and grid_block[-1].strip() == "</div>":
# only remove closing div if it closes .grid (heuristic: last line before stats)
pass
grid_start = _find_line(lines, lambda l: l == GRID_START)
panel_start = _find_line(lines, lambda l: "{% if page == 'env_config' %}" in l)
stats_card_line = _find_line(lines, lambda l: 'id="stats-card"' in l)
stats_start = stats_card_line
while stats_start > 0 and lines[stats_start].strip() != "{% if page == 'stats' %}":
stats_start -= 1
if lines[stats_start].strip() != "{% if page == 'stats' %}":
raise SystemExit("stats if-block not found")
stats_end = _find_line(lines, lambda l: l.strip() == "{% endif %}", start=stats_start + 1)
stats_block = _slice_between(lines, STATS_START, " </div>")
grid_block = lines[grid_start + 1 : panel_start]
while grid_block and not grid_block[-1].strip():
grid_block.pop()
if grid_block and grid_block[-1].strip() == "</div>":
grid_block.pop()
panel_block = lines[panel_start:stats_start]
stats_block = lines[stats_start : stats_end + 1]
out_lines = [
"{# Hub iframe tab fragment — shared via embed_templates #}",
@@ -47,6 +48,7 @@ def main() -> None:
'<div class="grid">',
*grid_block,
"</div>",
*panel_block,
*stats_block,
]
text = "\n".join(out_lines).rstrip() + "\n"