fix(админка): графики не растягиваются при одном месяце

Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
anor
2026-11-10 01:12:51 +10:00
co-authored by Claude Code
parent c343f7bf46
commit 642db51ab3
2 changed files with 42 additions and 12 deletions
+22 -11
View File
@@ -3,30 +3,41 @@ import htm from '/static/admin2/vendor/htm.module.js';
const html = htm.bind(h);
// SVG-атрибуты, не style: CSP v2 запрещает inline-стили.
// Ширина полотна ПОСТОЯННАЯ: раньше она считалась от числа точек, и при одном месяце
// viewBox сжимался до единиц, а SVG с width:100% растягивался в многоэкранную полосу.
const W = 600;
export const Bars = ({ values, label, labels = [] }) => {
const max = Math.max(1, ...values.map((v) => Math.max(0, v)));
const w = 12, gap = 6, h0 = 90;
const total = Math.max(1, values.length) * (w + gap);
const h0 = 90;
const n = Math.max(1, values.length);
const slot = W / n;
const bw = Math.min(48, slot * 0.6);
return html`<figure class="chart">
<figcaption>${label}</figcaption>
<svg viewBox=${`0 0 ${total} ${h0 + 18}`} role="img" aria-label=${label}>
${values.map((v, i) => html`<rect key=${i} x=${i * (w + gap)} y=${h0 - Math.max(0, v) / max * h0}
width=${w} height=${Math.max(1, v) / max * h0} rx="2" fill="#2457d6"></rect>`)}
${labels.map((m, i) => html`<text key=${'l' + i} x=${i * (w + gap)} y=${h0 + 14}
font-size="9" fill="#667085">${String(m).slice(5)}</text>`)}
<svg viewBox=${`0 0 ${W} ${h0 + 18}`} role="img" aria-label=${label}>
${values.map((v, i) => html`<rect key=${i} x=${i * slot + (slot - bw) / 2}
y=${h0 - Math.max(0, v) / max * h0}
width=${bw} height=${Math.max(1, v) / max * h0} rx="2" fill="#2457d6"></rect>`)}
${labels.map((m, i) => html`<text key=${'l' + i} x=${i * slot + slot / 2} y=${h0 + 14}
font-size="11" text-anchor="middle" fill="#667085">${String(m).slice(5)}</text>`)}
</svg>
</figure>`;
};
export const Line = ({ values, label }) => {
const max = Math.max(1, ...values.map((v) => Math.max(0, v)));
const pts = values.map((v, i) => `${i * 20},${90 - Math.max(0, v) / max * 80}`).join(' ');
const n = values.length;
const step = n > 1 ? W / (n - 1) : 0;
const x = (i) => n > 1 ? i * step : W / 2;
const y = (v) => 90 - Math.max(0, v) / max * 80;
const pts = values.map((v, i) => `${x(i)},${y(v)}`).join(' ');
return html`<figure class="chart">
<figcaption>${label}</figcaption>
<svg viewBox=${`0 0 ${Math.max(1, (values.length - 1) * 20)} 94`} role="img" aria-label=${label}>
${values.length > 1
<svg viewBox=${`0 0 ${W} 94`} role="img" aria-label=${label}>
${n > 1
? html`<polyline points=${pts} fill="none" stroke="#067647" stroke-width="3"></polyline>`
: html`<circle cx="0" cy=${90 - Math.max(0, values[0] || 0) / max * 80} r="3" fill="#067647"></circle>`}
: html`<circle cx=${x(0)} cy=${y(values[0] || 0)} r="4" fill="#067647"></circle>`}
</svg>
</figure>`;
};
+20 -1
View File
@@ -24,6 +24,9 @@ DASH = {"user": "boss@x", "generated_at": 1794000000, "last_visit": None,
MONEY = {"period_days": 30, "revenue_rub": 100, "refunded_rub": 0, "cost_rub": 50,
"flow_rub": 50, "charged_rub": 80, "accounts_total_rub": 200,
"accounts_count": 1, "unpriced": 0}
# Один закрытый месяц — тот случай, на котором графики разъезжались: ширина viewBox
# зависела от числа точек, и при одной точке SVG растягивался в многоэкранную полосу.
FIN = {"months": [{"month": "2026-11", "revenue_rub": 0, "cost_rub": 714.31}], "days": 30}
SYS = {"channels": [{"name": "database", "ok": True, "detail": "ок", "level": "fail"}],
"settings": [{"key": "agent_killswitch", "value": "off"}],
"history": [], "errors": {"items": [], "total": 0}, "generated_at": 1793000000}
@@ -42,7 +45,7 @@ class H(http.server.SimpleHTTPRequestHandler):
"/api/admin/v2/customers": {"items": [], "total": 0, "page": 1, "per_page": 50},
"/api/admin/v2/customer-emails": {"emails": []},
"/api/admin/v2/money": MONEY,
"/api/admin/v2/finance": {"months": [], "days": 30},
"/api/admin/v2/finance": FIN,
"/api/admin/v2/payments": {"items": [], "total": 0, "page": 1, "per_page": 50},
"/api/admin/v2/accounts": {"items": [], "total": 0, "total_rub": 0},
"/api/admin/v2/feedback": {"items": [], "total": 0, "page": 1, "per_page": 50},
@@ -97,3 +100,19 @@ def test_sections_render(browser, site, path, title):
assert page.evaluate("document.documentElement.scrollWidth <= innerWidth")
assert errors == []
page.close()
def test_money_charts_stay_inside_card(browser, site):
"""Один закрытый месяц не должен растягивать графики на несколько экранов.
Ловит именно случай «точек меньше двух»: ширина viewBox считалась от числа точек,
и при одной точке полотно становилось огромным. Мутация: вернуть формулу ширины от
числа точек — высота снова вылезет за 300."""
page = browser.new_page(viewport={"width": 1280, "height": 800})
page.add_init_script("localStorage.setItem('token','t')")
page.goto(site + "/admin/v2/money")
page.wait_for_selector(".chart svg")
heights = page.eval_on_selector_all(
".chart svg", "els => els.map(e => Math.round(e.getBoundingClientRect().height))")
assert heights and max(heights) <= 300, heights
page.close()