pi-evals 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -0
- package/README.md +29 -9
- package/dist/build.js +4 -3
- package/dist/cli.js +2 -2
- package/dist/coverage.js +8 -1
- package/dist/findings.js +50 -0
- package/dist/index.js +22 -10
- package/dist/model.js +21 -10
- package/dist/ops.js +52 -22
- package/dist/query.js +32 -13
- package/dist/ranges.js +14 -0
- package/dist/render.js +38 -170
- package/dist/report/concepts.js +37 -0
- package/dist/report/findings.js +84 -0
- package/dist/report/format.js +16 -0
- package/dist/report/sections.js +61 -0
- package/dist/report/strip.js +42 -0
- package/dist/report/style.js +85 -0
- package/dist/report/timeline.js +82 -0
- package/dist/report/week.js +44 -0
- package/dist/trends.js +58 -0
- package/docs/design.md +26 -0
- package/docs/learn.zh-TW.md +33 -0
- package/package.json +6 -3
- package/scripts/change-impact.mjs +18 -0
- package/scripts/package-errors.mjs +10 -0
- package/scripts/query-lib.mjs +36 -0
- package/scripts/skill-days.mjs +9 -0
- package/skills/analyzing-evals/SKILL.md +21 -0
- package/src/aggregate.ts +3 -0
- package/src/build.ts +4 -3
- package/src/cli.ts +2 -2
- package/src/coverage.ts +8 -1
- package/src/findings.ts +49 -0
- package/src/index.ts +22 -12
- package/src/model.ts +22 -10
- package/src/ops.ts +49 -22
- package/src/query.ts +29 -13
- package/src/ranges.ts +15 -0
- package/src/render.ts +70 -187
- package/src/report/concepts.ts +94 -0
- package/src/report/findings.ts +102 -0
- package/src/report/format.ts +24 -0
- package/src/report/sections.ts +186 -0
- package/src/report/strip.ts +40 -0
- package/src/report/style.ts +85 -0
- package/src/report/timeline.ts +127 -0
- package/src/report/week.ts +59 -0
- package/src/trends.ts +61 -0
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import type { ReportModel } from "../model.ts";
|
|
2
|
+
import { type concepts, lesson, selfCheck } from "./concepts.ts";
|
|
3
|
+
import { duration, esc, type Lang, num, pct, table, usd, words } from "./format.ts";
|
|
4
|
+
|
|
5
|
+
export function sections(m: ReportModel, lang: Lang, cards: ReturnType<typeof concepts>) {
|
|
6
|
+
const L = words(lang), meta = m.meta.metrics;
|
|
7
|
+
const label = (id: string) => meta[id].label[lang];
|
|
8
|
+
const T = (head: string[], rows: (number | string)[][]) => table(head, rows, L);
|
|
9
|
+
const section = (id: string, title: string, intro: string, body: string) =>
|
|
10
|
+
`<section id="${id}" class="topic"><p class="eyebrow">PI-EVALS / ${
|
|
11
|
+
L("Field notes", "工作紀錄")
|
|
12
|
+
}</p><h2>${title}</h2><p class="intro">${esc(intro)}</p>${body}</section>`;
|
|
13
|
+
const more = (title: string, html: string) => `<details><summary>${esc(title)}</summary>${html}</details>`;
|
|
14
|
+
const daily = (values: number[]) => T([L("Date (UTC)", "日期(UTC)"), L("Value", "數值")], m.series.days.map((d, i) => [d, values[i]]));
|
|
15
|
+
const entries = (map: Record<string, number>) => Object.entries(map);
|
|
16
|
+
const sk = m.skills, a = m.askUser, mon = m.monitors, s = m.subagents;
|
|
17
|
+
const skills = section(
|
|
18
|
+
"skills",
|
|
19
|
+
L("Which skills got opened?", "哪些技能真的有人打開?"),
|
|
20
|
+
meta.skill_loads.meaning[lang],
|
|
21
|
+
`<div class="pair"><article class="panel"><h3>${L("Most used", "最常載入")}</h3>${
|
|
22
|
+
T([L("Skill", "技能"), L("Loads", "次數"), L("Sessions", "工作段數")], sk.top.slice(0, 8).map((r) => [r.name, r.loads, r.sessions]))
|
|
23
|
+
}</article><article class="panel tint"><h3>${L("Offered but never opened", "有提供,但沒人載入")}</h3><p class="big">${
|
|
24
|
+
num(sk.neverLoaded.length)
|
|
25
|
+
} / ${num(sk.offered)}</p><p>${esc(meta.never_loaded.howComputed[lang])}</p>${
|
|
26
|
+
more(L("Skill names", "看技能名稱"), `<p>${sk.neverLoaded.map(esc).join("、") || "—"}</p>`)
|
|
27
|
+
}</article></div>` +
|
|
28
|
+
more(
|
|
29
|
+
L("Loads by day, project and trigger", "查看每日載入、各專案與載入方式"),
|
|
30
|
+
daily(m.series.skillLoads) +
|
|
31
|
+
T([L("Project", "專案"), L("Skill", "技能"), L("Loads", "次數")], sk.byProject.map((r) => [r.project, r.skill, r.loads])) +
|
|
32
|
+
T([L("Trigger", "方式"), L("Loads", "次數")], entries(sk.triggers)),
|
|
33
|
+
),
|
|
34
|
+
);
|
|
35
|
+
const packages = section(
|
|
36
|
+
"packages",
|
|
37
|
+
L("Packages and tools", "套件與工具"),
|
|
38
|
+
meta.package_calls.meaning[lang],
|
|
39
|
+
T(
|
|
40
|
+
[L("Package", "套件"), L("Version", "版本"), L("Calls", "呼叫次數"), label("tool_error_rate"), label("avg_duration_ms")],
|
|
41
|
+
m.packages.map((r) => [r.owner, r.version ?? "—", r.calls, pct(r.errorRate), duration(r.avgMs)]),
|
|
42
|
+
) +
|
|
43
|
+
more(
|
|
44
|
+
L("Tools by calls", "依呼叫次數查看工具"),
|
|
45
|
+
T(
|
|
46
|
+
[L("Tool", "工具"), L("Package", "套件"), L("Calls", "次數"), label("tool_error_rate")],
|
|
47
|
+
m.tools.map((r) => [r.name, r.owner, r.calls, pct(r.errorRate)]),
|
|
48
|
+
),
|
|
49
|
+
),
|
|
50
|
+
);
|
|
51
|
+
const ask = section(
|
|
52
|
+
"ask",
|
|
53
|
+
L("When the agent stopped to ask", "代理人何時停下來問你"),
|
|
54
|
+
L(
|
|
55
|
+
"See how often you were asked, how long an answer took and how often you dismissed the question.",
|
|
56
|
+
"這裡可以看到代理人問了幾次、答一題平均要等多久,以及你取消了多少提問。",
|
|
57
|
+
),
|
|
58
|
+
T([L("Measure", "指標"), L("Reading", "數值"), L("Counted", "計算次數")], [[label("ask_calls"), a.calls, a.calls], [
|
|
59
|
+
label("ask_wait_ms"),
|
|
60
|
+
duration(a.avgWaitMs),
|
|
61
|
+
a.waitN,
|
|
62
|
+
], [label("ask_cancel_rate"), pct(a.cancelRate), a.cancelRate.n]]) +
|
|
63
|
+
more(
|
|
64
|
+
L("Wait times and answer sources", "查看等待時間分布與回答來源"),
|
|
65
|
+
T(
|
|
66
|
+
[L("Wait", "等待時間"), L("Count", "次數")],
|
|
67
|
+
a.buckets.map((b, i) => [
|
|
68
|
+
b.upToMs == null
|
|
69
|
+
? `≥ ${duration(a.buckets[i - 1]?.upToMs ?? 0)}`
|
|
70
|
+
: `${duration(i ? a.buckets[i - 1].upToMs : 0)} – < ${duration(b.upToMs)}`,
|
|
71
|
+
b.n,
|
|
72
|
+
]),
|
|
73
|
+
) + T([L("Source", "來源"), L("Count", "次數")], entries(a.sources)),
|
|
74
|
+
),
|
|
75
|
+
);
|
|
76
|
+
const monitors = section(
|
|
77
|
+
"monitors",
|
|
78
|
+
L("Background jobs", "背景工作"),
|
|
79
|
+
meta.monitor_outcomes.meaning[lang],
|
|
80
|
+
`<p>${L("Started", "啟動")} ${num(mon.started)} / ${L("Ending recorded", "有結束紀錄")} ${num(mon.finished)} / ${
|
|
81
|
+
label("monitor_duration_ms")
|
|
82
|
+
} ${duration(mon.avgMs)}(${num(mon.durationN)})</p>${
|
|
83
|
+
T([L("Outcome", "結果"), L("Count", "次數")], entries(mon.statuses))
|
|
84
|
+
}<p class="muted">${esc(meta.monitor_outcomes.howComputed[lang])}</p>`,
|
|
85
|
+
);
|
|
86
|
+
const sub = section(
|
|
87
|
+
"subagents",
|
|
88
|
+
L("Work handed to other agents", "交給子代理的工作"),
|
|
89
|
+
meta.subagent_launches.meaning[lang],
|
|
90
|
+
T([L("Role", "角色"), L("Model", "模型"), L("Launches", "啟動次數")], s.launches.slice(0, 40).map((r) => [r.role, r.model, r.n])) +
|
|
91
|
+
more(label("subagent_cost"), T([L("Role", "角色"), L("USD", "美元")], s.costByRole.map((r) => [r.role, usd(r.cost)]))) +
|
|
92
|
+
`<p>${L("Linked to a parent in this period", "這段期間能對回母工作階段的子代理")}:${s.linked === null ? L("Run /evals extract for this breakdown.", "請執行 /evals extract 更新分期統計。") : `${num(s.linked)} / ${num(s.subagentTotal)}`} </p>`,
|
|
93
|
+
);
|
|
94
|
+
const process = section(
|
|
95
|
+
"process",
|
|
96
|
+
L("Did the agent follow the steps?", "工作有沒有照說好的步驟走?"),
|
|
97
|
+
L(
|
|
98
|
+
`Only sessions starting from ${m.process.eraStart} are judged. Earlier work is not a failure.`,
|
|
99
|
+
`只檢查 ${m.process.eraStart} 起的工作;更早的工作不算失敗。`,
|
|
100
|
+
),
|
|
101
|
+
lesson(cards.check, L) + `<div class="pair checks">${
|
|
102
|
+
m.process.detectors.map((d) => {
|
|
103
|
+
const md = m.meta.detectors[d.name];
|
|
104
|
+
return `<article class="panel"><p class="eyebrow">${L("Code check", "程式檢查")} / ${L("higher is better", "越高越好")}</p><h3>${
|
|
105
|
+
esc(md.label[lang])
|
|
106
|
+
}</h3><p class="big">${pct(d.passRate)} <small>${num(d.pass)} / ${num(d.passRate.n)} ${L("sessions", "段")}</small></p><p>${
|
|
107
|
+
esc(md.howComputed[lang])
|
|
108
|
+
}</p><p><b>${L("Pass", "通過")}</b> ${esc(md.example.pass[lang])}<br><b>${L("Fail", "未通過")}</b> ${
|
|
109
|
+
esc(md.example.fail[lang])
|
|
110
|
+
}</p>${
|
|
111
|
+
more(
|
|
112
|
+
L("By day and version", "查看每日與各版本的通過數"),
|
|
113
|
+
T([L("Day", "日期"), L("Pass", "通過"), L("Fail", "未通過")], d.perDay.map((r) => [r.day, r.pass, r.fail])) +
|
|
114
|
+
T([L("Version", "版本"), L("Pass", "通過"), L("Fail", "未通過")], d.byVersion.map((r) => [r.version, r.pass, r.fail])),
|
|
115
|
+
)
|
|
116
|
+
}</article>`;
|
|
117
|
+
}).join("")
|
|
118
|
+
}</div>` +
|
|
119
|
+
more(
|
|
120
|
+
L("Close-guard reminders", "收尾提醒"),
|
|
121
|
+
T([L("Kind", "種類"), L("Count", "次數")], entries(m.process.notices).map(([k, n]) => [m.meta.notices[k]?.[lang] ?? k, n])),
|
|
122
|
+
),
|
|
123
|
+
);
|
|
124
|
+
const cost = section(
|
|
125
|
+
"cost",
|
|
126
|
+
L("What the work cost", "工作花了多少錢"),
|
|
127
|
+
meta.cost.meaning[lang],
|
|
128
|
+
`<p class="big">${usd(m.cost.total)} <small>USD</small></p><p>${label("cost_per_session")} ${usd(m.cost.perSession.value)}(${
|
|
129
|
+
num(m.cost.perSession.n)
|
|
130
|
+
} ${L("sessions", "段")})</p><p>${esc(meta.skill_cost.meaning[lang])}</p>` +
|
|
131
|
+
T([L("Skill", "技能"), L("USD", "美元"), L("Sessions", "工作段數")], m.cost.bySkill.map((r) => [r.skill, usd(r.cost), r.sessions])) +
|
|
132
|
+
more(
|
|
133
|
+
L("Daily spend and model replies", "查看每日費用與各模型回覆數"),
|
|
134
|
+
daily(m.series.cost) + T([L("Model", "模型"), L("Replies", "回覆數")], m.cost.byModel.map((r) => [r.model, r.replies])),
|
|
135
|
+
),
|
|
136
|
+
);
|
|
137
|
+
const cov = m.coverage;
|
|
138
|
+
const coverage = section(
|
|
139
|
+
"coverage",
|
|
140
|
+
L("When is cleanup safe?", "何時可以開啟清理?"),
|
|
141
|
+
L(
|
|
142
|
+
"Only consider cleanup when all old files match the index, by both file count and bytes. This report never deletes a file.",
|
|
143
|
+
"所有舊檔的大小和修改時間都要和索引相符,未建索引的檔案數和位元組數都歸零,才考慮開啟清理。報表不會刪檔。",
|
|
144
|
+
),
|
|
145
|
+
cov
|
|
146
|
+
? `<p>${L("Files modified in the selected dates, indexed (all projects)", "選定日期內有修改的檔案,已建索引(全部專案)")}:${num(cov.inRange?.indexed ?? 0)} / ${num(cov.inRange?.files ?? 0)}</p><p>${L("Cleanup gate across all dates", "清理門檻不分日期")} · ${L("Inactive days", "閒置天數")} > ${cov.cutDays} / ${L("Files indexed", "已建索引")} ${num(cov.indexed.files)} / ${
|
|
147
|
+
num(cov.old.files)
|
|
148
|
+
}</p><p>${L("Not indexed", "未建索引")}:${num(cov.notIndexed.files)} ${L("files", "個檔案")} · ${
|
|
149
|
+
num(cov.notIndexed.bytes)
|
|
150
|
+
} bytes</p>`
|
|
151
|
+
: `<p>${L("Coverage not measured; keep cleanup off.", "沒有索引覆蓋資料,請勿開啟清理。")}</p>`,
|
|
152
|
+
);
|
|
153
|
+
const labels = section(
|
|
154
|
+
"labels",
|
|
155
|
+
L("Your labels", "你的標記"),
|
|
156
|
+
meta.agreement.meaning[lang],
|
|
157
|
+
m.labels.needsIndex ? `<p>${L("Run /evals extract to count labels in this period.", "請執行 /evals extract 以計算這段期間的標記。")}</p>` : m.labels.present && m.labels.n
|
|
158
|
+
? T(
|
|
159
|
+
[L("Check", "檢查"), L("Labelled", "已標記"), L("Agreement", "一致比例")],
|
|
160
|
+
m.labels.agreement.map((r) => [m.meta.detectors[r.detector]?.label[lang] ?? r.detector, r.n, pct(r.rate)]),
|
|
161
|
+
)
|
|
162
|
+
: `<p>${
|
|
163
|
+
L(
|
|
164
|
+
"No labels yet. In phase 2 you will judge sessions yourself and compare your answers with the code checks.",
|
|
165
|
+
"還沒有標記。第二階段會讓你親自判讀工作紀錄,再對照程式與你的判斷是否一致。",
|
|
166
|
+
)
|
|
167
|
+
}</p>`,
|
|
168
|
+
);
|
|
169
|
+
return [
|
|
170
|
+
skills,
|
|
171
|
+
packages,
|
|
172
|
+
ask,
|
|
173
|
+
monitors,
|
|
174
|
+
sub,
|
|
175
|
+
process,
|
|
176
|
+
cost,
|
|
177
|
+
coverage,
|
|
178
|
+
labels,
|
|
179
|
+
section(
|
|
180
|
+
"concepts",
|
|
181
|
+
L("Concepts", "概念"),
|
|
182
|
+
L("Use these five ideas when you read the next report.", "下次讀報表時,可以用這五個概念判讀數字。"),
|
|
183
|
+
`<div class="notes-grid">${Object.values(cards).map((c) => lesson(c, L)).join("")}</div>${selfCheck(L)}`,
|
|
184
|
+
),
|
|
185
|
+
].join("");
|
|
186
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { ReportModel } from "../model.ts";
|
|
2
|
+
import type { Preset } from "../ranges.ts";
|
|
3
|
+
import { type Change, esc, type Lang, num, words } from "./format.ts";
|
|
4
|
+
|
|
5
|
+
const KINDS = ["pin", "model-routing", "rule", "agent-role", "skill", "manual"] as const;
|
|
6
|
+
|
|
7
|
+
/** Dense time slots get a count; every change still has its own mark and title. */
|
|
8
|
+
export function changeStrip(m: ReportModel, all: Change[], picks: Change[], ids: Map<Change, string>, lang: Lang, range: Preset | "custom") {
|
|
9
|
+
const L = words(lang), label = (k: Change["kind"]) => m.meta.changeKinds[k][lang];
|
|
10
|
+
const period = typeof range === "number" ? L(`Last ${range} days`, `近 ${range} 天`) : range === "all" ? L("All time", "全部期間") : L("Selected dates", "選定期間");
|
|
11
|
+
const since = m.window.since ?? "", until = m.window.until ?? "", start = Date.parse(since), span = Date.parse(until) + 86400000 - start;
|
|
12
|
+
const x = (c: Change) => 16 + Math.min(1, Math.max(0, (Date.parse(c.at) - start) / span)) * 968;
|
|
13
|
+
const bins = new Map<number, Change[]>(), counts = new Map<string, number>();
|
|
14
|
+
for (const c of all) {
|
|
15
|
+
const slot = Math.floor((x(c) - 16) / 30), cells = bins.get(slot) ?? [];
|
|
16
|
+
cells.push(c); bins.set(slot, cells); counts.set(c.kind, (counts.get(c.kind) ?? 0) + 1);
|
|
17
|
+
}
|
|
18
|
+
const short = { pin: "版本", "model-routing": "模型路由", rule: "規則", "agent-role": "角色", skill: "skill", manual: "手動" };
|
|
19
|
+
const breakdown = KINDS.map(k => `${L(label(k), short[k])} ${num(counts.get(k) ?? 0)}`).join(L(", ", "、"));
|
|
20
|
+
const total = L(`${period}: ${num(all.length)} changes: ${breakdown}; ${picks.length} worth a closer look.`, `${period}共 ${num(all.length)} 筆調整:${breakdown};其中 ${picks.length} 筆值得回看。`);
|
|
21
|
+
const legend = KINDS.map(k => `<span><i class="key kind-${k}"></i>${esc(label(k))}: ${num(counts.get(k) ?? 0)}</span>`).join("");
|
|
22
|
+
const base = 64, marks: string[] = [], labeled = new Map<Change, { px: number; py: number; dot: string }>();
|
|
23
|
+
for (const [slot, cells] of bins) {
|
|
24
|
+
cells.reverse(); // Earliest mark at the left of a dense slot.
|
|
25
|
+
const left = 16 + slot * 30, columns = Math.ceil(cells.length / 5);
|
|
26
|
+
cells.forEach((c, i) => {
|
|
27
|
+
const px = cells.length <= 5 ? x(c) : left + (Math.floor(i / 5) + .5) * 28 / columns;
|
|
28
|
+
const py = base - 6 - i % 5 * 5;
|
|
29
|
+
const dot = `<circle class="change-mark kind-${c.kind}" cx="${px.toFixed(1)}" cy="${py}" r="${Math.min(2.4, 13 / columns)}"><title>${esc(c.at)} · ${esc(label(c.kind))}</title></circle>`;
|
|
30
|
+
if (picks.includes(c)) labeled.set(c, { px, py, dot }); else marks.push(dot);
|
|
31
|
+
});
|
|
32
|
+
if (cells.length > 5) marks.push(`<text class="slot-count" x="${left + 14}" y="28" text-anchor="middle">×${cells.length}</text>`);
|
|
33
|
+
}
|
|
34
|
+
const links = picks.map((c,i) => {
|
|
35
|
+
const {px, py, dot} = labeled.get(c)!, end = px > 700, caption = c.package ? `${c.package} ${c.to ?? ""}` : `${label(c.kind)} · ${c.at.slice(5,16).replace("T", " ")}`;
|
|
36
|
+
const y = 87 + i * 23;
|
|
37
|
+
return `<a href="#${ids.get(c)}"><title>${esc(caption)}</title><path class="guide" d="M${px} ${py}V${y - 6}h${end ? -7 : 7}"/>${dot}<text x="${px + (end ? -10 : 10)}" y="${y}" text-anchor="${end ? "end" : "start"}">${esc(Array.from(caption).length > 48 ? Array.from(caption).slice(0, 47).join("") + "…" : caption)}</text></a>`;
|
|
38
|
+
}).join("");
|
|
39
|
+
return `<section class="change-strip"><h2>${total}</h2><div class="kind-key">${legend}</div><svg viewBox="0 0 1000 ${base + 14 + picks.length * 23}" aria-label="${esc(total)}"><path class="axis" d="M16 ${base}H984"/>${marks.join("")}${links}</svg><div class="ends"><span>${esc(since)}</span><span>${esc(until)} UTC</span></div></section>`;
|
|
40
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/** Embedded in report.html so the page also works when copied or opened offline. */
|
|
2
|
+
export const css = `
|
|
3
|
+
:root {
|
|
4
|
+
color-scheme:light dark; --bg:#f9faf6; --paper:#fff; --ink:#172e36; --sub:#4f6366;
|
|
5
|
+
--green:#16665c; --line:#cdd8d3; --tint:#e4f1eb; --area:#dceae3; --warm:#855126; --accent:#16665c;
|
|
6
|
+
}
|
|
7
|
+
@media(prefers-color-scheme:dark) { :root {
|
|
8
|
+
--bg:#152320; --paper:#1e302d; --ink:#ebf5ef; --sub:#bdcfc8; --green:#98dcca;
|
|
9
|
+
--line:#465d56; --tint:#243e36; --area:#355348; --warm:#eabc90;
|
|
10
|
+
} }
|
|
11
|
+
* {box-sizing:border-box}
|
|
12
|
+
body {margin:0 auto;max-width:1280px;padding:0 clamp(18px,4.375vw,56px);background:var(--bg);color:var(--ink);
|
|
13
|
+
font:17px/1.58 -apple-system,BlinkMacSystemFont,"PingFang TC","Noto Sans TC","Segoe UI",sans-serif;
|
|
14
|
+
font-variant-numeric:tabular-nums;overflow-wrap:anywhere}
|
|
15
|
+
a,summary {color:var(--green)}
|
|
16
|
+
a:focus-visible,summary:focus-visible {outline:3px solid var(--warm);outline-offset:3px}
|
|
17
|
+
header,.hero,.topic {border-bottom:1px solid var(--line)}
|
|
18
|
+
header,footer,.ends,.ruler-head,.reading {display:flex;align-items:center;justify-content:space-between;gap:12px}
|
|
19
|
+
header {min-height:75px;flex-wrap:wrap}
|
|
20
|
+
.brand {display:flex;align-items:center;gap:12px;text-decoration:none;color:var(--ink);font-size:21px;font-weight:700}
|
|
21
|
+
.brand span {font-size:17px;font-weight:400;padding-left:20px;margin-left:16px;border-left:1px solid var(--line);color:var(--sub)}
|
|
22
|
+
small,.muted,.intro,.deck,.ends {color:var(--sub)}
|
|
23
|
+
small,.muted,.ends,summary,.footline,.button {font-size:15px}
|
|
24
|
+
.ranges,.ranges nav {display:flex;align-items:center;gap:6px;flex-wrap:wrap}.ranges {justify-content:space-between;margin-top:18px}
|
|
25
|
+
.ranges a {padding:5px 12px;text-decoration:none;border-radius:6px}.ranges a[aria-current] {background:var(--tint);font-weight:700}
|
|
26
|
+
.hero {padding:36px 0 20px}
|
|
27
|
+
.hero h1,.hero-title {font-size:clamp(29px,3.125vw,40px);line-height:1.25;margin:18px 0}
|
|
28
|
+
.deck,.intro {font-size:19px;margin:0}.hero .deck+.deck {margin-top:24px}
|
|
29
|
+
.eyebrow {font-size:14px;font-weight:650;letter-spacing:1px;color:var(--green);margin:0}
|
|
30
|
+
.stale,.verdict {color:var(--warm)}
|
|
31
|
+
h2 {font-size:25px;line-height:1.35;margin:0 0 14px}
|
|
32
|
+
h3,.big {font-size:29px;line-height:1.35;margin:14px 0 18px}
|
|
33
|
+
.spine {padding-top:25px}
|
|
34
|
+
.layout {display:grid;grid-template-columns:minmax(0,709fr) minmax(0,419fr);gap:40px;align-items:start}
|
|
35
|
+
.rail {position:relative;padding-left:54px}
|
|
36
|
+
.rail:before {content:"";position:absolute;left:22px;top:20px;bottom:20px;border-left:2px solid var(--line)}
|
|
37
|
+
.card,.panel,.lesson {background:var(--paper);border:1px solid var(--line);border-radius:12px;padding:22px 25px}
|
|
38
|
+
.card {position:relative;padding-block:19px 17px}.card+.card {margin-top:30px}
|
|
39
|
+
.card:before,.ruler i {width:10px;height:10px;background:var(--green);border:3px solid var(--bg);outline:2px solid var(--green);border-radius:50%}
|
|
40
|
+
.card:before {content:"";position:absolute;left:-37px;top:31px}
|
|
41
|
+
.card h3 {margin:9px 0 14px}.reading {line-height:1.45;margin:12px 0}.card .reading {margin:9px 0}
|
|
42
|
+
.reading b {font-size:18px}.reading small {flex-shrink:0}.card .muted {margin:9px 0}
|
|
43
|
+
.button {display:inline-block;padding:9px 22px;border-radius:8px;background:var(--accent);color:white;text-decoration:none;margin-top:3px;min-width:156px;text-align:center}
|
|
44
|
+
.workload h2 {margin-bottom:0}.workload h3 {margin:25px 0 12px}.workload .muted {margin:5px 0}
|
|
45
|
+
.workload svg {width:100%;height:auto;display:block;margin-top:8px}
|
|
46
|
+
.area {fill:var(--area)}.trend {stroke:var(--green);stroke-width:2.5;stroke-linejoin:round;fill:none}
|
|
47
|
+
.ends {margin:4px 0}
|
|
48
|
+
.change-strip {margin:18px 0 0;padding:12px 0;border-block:1px solid var(--line)}.change-strip h2 {font-size:19px;margin:0 0 6px}
|
|
49
|
+
.kind-key {display:flex;flex-wrap:wrap;gap:4px 16px;font-size:14px;color:var(--sub)}.key {display:inline-block;width:.7em;height:.7em;border-radius:100%;background:var(--kind);margin-right:5px}
|
|
50
|
+
.kind-pin {--kind:#3b9090}.kind-model-routing {--kind:#736cc9}.kind-rule {--kind:#c78231}.kind-agent-role {--kind:#c16c89}.kind-skill {--kind:#718c40}.kind-manual {--kind:#477dac}
|
|
51
|
+
.change-mark,.work-mark {fill:var(--kind)}.change-strip svg {width:100%;display:block}.change-strip .axis,.change-strip .guide {stroke:var(--line);fill:none}
|
|
52
|
+
.change-strip .slot-count {fill:var(--sub);font:12px sans-serif}.change-strip a text {fill:var(--green);font-size:14px;text-decoration:underline}
|
|
53
|
+
@media(max-width:640px){.change-strip {overflow-x:auto}.change-strip svg {min-width:600px}}
|
|
54
|
+
.footline {border-top:1px solid var(--line);padding-top:13px;margin:12px 0 0}
|
|
55
|
+
.lesson,.tint,.after {background:var(--tint)}.lesson {padding:16px 19px}.lesson p {margin:5px 0 0}.side>.lesson {margin-top:16px}
|
|
56
|
+
.topic {padding:38px 0}.topic h2 {margin:9px 0}.intro {margin:0 0 18px}
|
|
57
|
+
.jump {display:flex;flex-wrap:wrap;gap:8px 18px;padding:20px 0;border-block:1px solid var(--line);margin:32px 0 0}
|
|
58
|
+
.day li {margin:.45rem 0}.day time {color:var(--sub)}details {margin:12px 0}summary {cursor:pointer;font-weight:600}
|
|
59
|
+
.scroll {overflow-x:auto}table {width:100%;border-collapse:collapse;margin:.4rem 0;background:var(--paper)}
|
|
60
|
+
th,td {text-align:start;padding:7px 12px;border-bottom:1px solid var(--line)}th {font-size:15px;color:var(--green)}
|
|
61
|
+
.pair,.notes-grid {display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:20px}
|
|
62
|
+
.checks {margin-top:18px}.checks h3,.topic .panel h3 {font-size:22px}
|
|
63
|
+
.detail {display:none;padding-bottom:24px;scroll-margin-top:75px}.detail:target {display:block;outline:none}
|
|
64
|
+
body:has(.detail:target) main {display:none}.back {float:right;font-size:15px}
|
|
65
|
+
.ruler-head {margin:30px 0 24px}.ruler-head h2 {margin:0}
|
|
66
|
+
.ruler {position:relative;list-style:none;margin:0 0 30px;padding:0;height:86px}
|
|
67
|
+
.ruler:before,.ruler.partial:after {content:"";position:absolute;top:39px;left:6%;right:6%;height:2px;background:var(--green)}
|
|
68
|
+
.ruler.partial:after {left:65.5%;right:6.5%;background:var(--bg);border-top:2px dashed var(--line)}
|
|
69
|
+
.ruler li {position:absolute;width:118px;transform:translateX(-50%);text-align:center;font-size:15px;color:var(--sub)}
|
|
70
|
+
.ruler li:first-child {left:6%}.ruler li:nth-child(2) {left:50%}.ruler li:nth-child(3) {left:65%}.ruler li:last-child {left:94%}
|
|
71
|
+
.ruler time {color:var(--green);white-space:nowrap}.ruler i {display:block;position:relative;margin:11px auto}
|
|
72
|
+
.compare h3 {border-bottom:1px solid var(--line);padding-bottom:12px}.compare .reading {min-height:32px}.detail>.eyebrow {margin-bottom:8px}
|
|
73
|
+
.notes-grid {margin:16px 0}.notes-grid .lesson {font-size:16px}
|
|
74
|
+
footer {font-size:14px;margin-top:24px;padding:18px 0 25px;color:var(--sub);border-top:1px solid var(--line)}
|
|
75
|
+
@media(max-width:850px) {
|
|
76
|
+
.layout,.pair,.notes-grid {grid-template-columns:1fr}.layout {gap:22px}.rail {padding-left:27px}.rail:before {left:7px}.card:before {left:-26px}
|
|
77
|
+
.reading {flex-wrap:wrap}.ruler-head,footer {align-items:flex-start;flex-direction:column}
|
|
78
|
+
}
|
|
79
|
+
@media(max-width:640px) {
|
|
80
|
+
header {padding:12px 0;gap:5px}.hero {padding:24px 0}.brand span {margin-left:4px;padding-left:10px}
|
|
81
|
+
.card,.panel {padding:16px}.ruler {height:auto}.ruler:before,.ruler:after,.ruler li i {display:none}
|
|
82
|
+
.ruler li {position:static;width:auto;transform:none;display:flex;align-items:center;gap:8px;text-align:left}
|
|
83
|
+
.card h3 {font-size:23px}.back {float:none;display:block;margin-bottom:8px}
|
|
84
|
+
}
|
|
85
|
+
`;
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import type { ReportModel } from "../model.ts";
|
|
2
|
+
import { concepts, lesson } from "./concepts.ts";
|
|
3
|
+
import { overview, type Reading, readings, value, verdict } from "./findings.ts";
|
|
4
|
+
import { type Change, esc, type Lang, num, stamp, title, type Words, words } from "./format.ts";
|
|
5
|
+
import type { Preset } from "../ranges.ts";
|
|
6
|
+
import { changeStrip } from "./strip.ts";
|
|
7
|
+
import type { Week } from "./week.ts";
|
|
8
|
+
|
|
9
|
+
function card(c: Change, id: string, rs: Reading[], L: Words) {
|
|
10
|
+
const i = c.impact;
|
|
11
|
+
return `<article class="card"><p class="eyebrow"><time datetime="${esc(c.at)}">${esc(stamp(c.at))} UTC</time></p><h3>${
|
|
12
|
+
esc(title(c))
|
|
13
|
+
}</h3>${
|
|
14
|
+
rs.map((r) =>
|
|
15
|
+
`<div class="reading"><b>${esc(r.label)} ${value(r, "before")} → ${value(r, "after")}</b><small>${num(r.before?.n ?? 0)} → ${
|
|
16
|
+
num(r.after?.n ?? 0)
|
|
17
|
+
} ${r.unit}</small></div>`
|
|
18
|
+
).join("")
|
|
19
|
+
}<p class="muted">${
|
|
20
|
+
rs[0].direction === rs[1].direction
|
|
21
|
+
? L(`Both: ${rs[0].direction}.`, `兩項都${rs[0].direction}。`)
|
|
22
|
+
: rs.map((r) => `${r.label}:${r.direction}`).join(" · ")
|
|
23
|
+
}${i ? ` ${num(i.before.sessions)} → ${num(i.after.sessions)} ${L("sessions.", "段工作。")}` : ""}<br><span class="verdict">${
|
|
24
|
+
esc(verdict(c, rs, L))
|
|
25
|
+
}</span></p><a class="button" href="#${id}">${L("Change details", "看調整明細")} →</a></article>`;
|
|
26
|
+
}
|
|
27
|
+
function ruler(c: Change, last: string | null, L: Words) {
|
|
28
|
+
const i = c.impact!;
|
|
29
|
+
const cutoff = last && last > c.at && last < i.window.after.until ? last : null;
|
|
30
|
+
const points = [
|
|
31
|
+
[i.window.before.since, L("Before starts", "開始算調整前")],
|
|
32
|
+
[c.at, L("Change", "做了調整")],
|
|
33
|
+
...(cutoff ? [[cutoff, L("Last extract", "資料截至這裡")]] : []),
|
|
34
|
+
[i.window.after.until, L("After window ends", "調整後期間結束")],
|
|
35
|
+
];
|
|
36
|
+
return `<div class="ruler-head"><h2>${
|
|
37
|
+
L(`Compare ${i.window.days} days on each side`, `同一條規則,前後各看 ${i.window.days} 天`)
|
|
38
|
+
}</h2><small>${L("Grouped by start time · not to scale", "依工作開始時間分組 / 時間線不按比例繪製")}</small></div><ol class="ruler${
|
|
39
|
+
cutoff ? " partial" : ""
|
|
40
|
+
}">${points.map(([at, text]) => `<li><time>${esc(stamp(at))}</time><i></i><span>${text}</span></li>`).join("")}</ol>`;
|
|
41
|
+
}
|
|
42
|
+
function panel(c: Change, side: "before" | "after", rs: Reading[], period: string, L: Words) {
|
|
43
|
+
const i = c.impact!;
|
|
44
|
+
return `<article class="panel compare ${side}"><p class="eyebrow">${
|
|
45
|
+
side === "before" ? L("Before", "調整前") : L("After", "調整後")
|
|
46
|
+
} / ${esc(period)}</p><h3>${num(i[side].sessions)} ${L("sessions", "段工作")}</h3>${
|
|
47
|
+
rs.map((r) => {
|
|
48
|
+
const x = r[side];
|
|
49
|
+
const sample = x?.value != null
|
|
50
|
+
? (r.key === "cost_per_session" ? `${num(x.n)} ${r.unit}` : `${num(Math.round(x.value * x.n))} / ${num(x.n)} ${r.unit}`)
|
|
51
|
+
: L("no eligible records", "沒有適用紀錄");
|
|
52
|
+
return `<div class="reading"><span>${esc(r.label)}</span><b>${value(r, side)} <small>(${sample})</small></b></div>`;
|
|
53
|
+
}).join("")
|
|
54
|
+
}</article>`;
|
|
55
|
+
}
|
|
56
|
+
function detail(c: Change, id: string, rs: Reading[], m: ReportModel, w: Week, lang: Lang, siblings: Change[]) {
|
|
57
|
+
const L = words(lang), i = c.impact, lessons = concepts(m, w, c, L);
|
|
58
|
+
const peers = siblings.filter((s) => s !== c);
|
|
59
|
+
const together = peers.length
|
|
60
|
+
? L(
|
|
61
|
+
`Same commit: ${peers.map((s) => s.package ?? m.meta.changeKinds[s.kind][lang]).join(", ")}.`,
|
|
62
|
+
`同一次提交也調整了 ${peers.map((s) => s.package ?? m.meta.changeKinds[s.kind][lang]).join("、")}。`,
|
|
63
|
+
)
|
|
64
|
+
: "";
|
|
65
|
+
const primary = rs[0];
|
|
66
|
+
const age = (Date.parse(m.freshness.lastExtract ?? "") - Date.parse(c.at)) / 3600000;
|
|
67
|
+
const partial = i?.incomplete && age >= 0 && age < i.window.days * 24;
|
|
68
|
+
const ageText = age < 1 ? L("less than an hour", "不到一小時") : L(`about ${Math.floor(age)} hours`, `約 ${Math.floor(age)} 小時`);
|
|
69
|
+
const heading = partial ? L(`Only ${ageText} since the change: too early to tell`, `調整後${ageText},還看不出效果`) : verdict(c, rs, L);
|
|
70
|
+
const fullPeriod = L(`${i?.window.days} days`, `${i?.window.days} 天`);
|
|
71
|
+
const beforePeriod = i?.incomplete ? L(`Planned: ${fullPeriod}`, `原定 ${fullPeriod}`) : fullPeriod;
|
|
72
|
+
return `<section class="detail" id="${id}" tabindex="-1"><div class="hero"><a class="back" href="#timeline">← ${
|
|
73
|
+
L("Timeline", "回時間軸")
|
|
74
|
+
}</a><p class="eyebrow">${L("Architecture change", "架構調整明細")} / ${esc(stamp(c.at))} UTC</p><h2 class="hero-title">${
|
|
75
|
+
esc(heading)
|
|
76
|
+
}</h2><p class="deck">${esc(title(c))} ${esc(together)}</p>${
|
|
77
|
+
i
|
|
78
|
+
? `<p class="deck">${esc(primary.label)}:${value(primary, "before")} → ${value(primary, "after")}。 ${esc(verdict(c, rs, L))}</p>`
|
|
79
|
+
: ""
|
|
80
|
+
}</div>
|
|
81
|
+
${
|
|
82
|
+
i
|
|
83
|
+
? `${ruler(c, m.freshness.lastExtract, L)}<p class="eyebrow">${L("Records side by side", "紀錄對照")}</p><h2>${
|
|
84
|
+
L("Compare the two groups", "比較兩個時段的工作")
|
|
85
|
+
}</h2><div class="pair">${panel(c, "before", rs, beforePeriod, L)}${
|
|
86
|
+
panel(c, "after", rs, partial ? ageText : fullPeriod, L)
|
|
87
|
+
}</div><p>${rs.map((r) => `${esc(r.label)}:${r.direction}`).join(" / ")}</p>`
|
|
88
|
+
: ""
|
|
89
|
+
}
|
|
90
|
+
<div class="notes-grid">${lesson(lessons.sample, L)}${lesson(lessons.confounding, L)}${
|
|
91
|
+
c.package === "pi-anchorloop" ? lesson(lessons.check, L) : ""
|
|
92
|
+
}</div><p class="muted">${esc(c.repo)} ${esc(c.sha)} ${esc(c.subject)}</p><a href="#timeline">← ${
|
|
93
|
+
L("Timeline", "回時間軸")
|
|
94
|
+
}</a></section>`;
|
|
95
|
+
}
|
|
96
|
+
export function timeline(m: ReportModel, w: Week, lang: Lang, range: Preset | "custom") {
|
|
97
|
+
const L = words(lang), { all, lead, highlighted, folded, headline, trust } = overview(m, lang);
|
|
98
|
+
const ids = new Map(all.map((c, i) => [c, `change-${i + 1}`]));
|
|
99
|
+
const metrics = new Map(all.map((c) => [c, readings(c, m, lang)]));
|
|
100
|
+
const byDay = new Map<string, Change[]>(), byCommit = new Map<string, Change[]>();
|
|
101
|
+
for (const c of folded) {
|
|
102
|
+
const d = c.at.slice(0, 10);
|
|
103
|
+
if (!byDay.has(d)) byDay.set(d, []);
|
|
104
|
+
byDay.get(d)!.push(c);
|
|
105
|
+
}
|
|
106
|
+
for (const c of all) {
|
|
107
|
+
if (c.sha) {
|
|
108
|
+
const key = `${c.repo}/${c.sha}`;
|
|
109
|
+
if (!byCommit.has(key)) byCommit.set(key, []);
|
|
110
|
+
byCommit.get(key)!.push(c);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return {
|
|
114
|
+
lead,
|
|
115
|
+
headline,
|
|
116
|
+
trust,
|
|
117
|
+
strip: changeStrip(m, all, highlighted, ids, lang, range),
|
|
118
|
+
count: highlighted.length,
|
|
119
|
+
cards: highlighted.map((c) => card(c, ids.get(c)!, metrics.get(c)!, L)).join(""),
|
|
120
|
+
archive: [...byDay].map(([day, list]) =>
|
|
121
|
+
`<details class="day"><summary>${esc(day)} / ${num(list.length)} ${L("changes", "筆調整")}</summary><ul>${
|
|
122
|
+
list.map((c) => `<li><time>${esc(c.at.slice(11, 16))}</time> <a href="#${ids.get(c)}">${esc(title(c))}</a></li>`).join("")
|
|
123
|
+
}</ul></details>`
|
|
124
|
+
).join(""),
|
|
125
|
+
details: all.map((c) => detail(c, ids.get(c)!, metrics.get(c)!, m, w, lang, byCommit.get(`${c.repo}/${c.sha}`) ?? [])).join(""),
|
|
126
|
+
};
|
|
127
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { ReportModel } from "../model.ts";
|
|
2
|
+
import { esc, num, type Words } from "./format.ts";
|
|
3
|
+
|
|
4
|
+
/** Workload over the selected dates with records; the extraction day is partial. */
|
|
5
|
+
export function periodRows(m: ReportModel) {
|
|
6
|
+
if (!m.series.days.length || !m.window.until || !m.window.since) return [];
|
|
7
|
+
const end = Math.min(Date.parse(m.freshness.lastExtract?.slice(0, 10) ?? m.window.until), Date.parse(m.window.until));
|
|
8
|
+
const positions = new Map(m.series.days.map((d, i) => [d, i]));
|
|
9
|
+
const changes = new Map<string, Set<string>>(), totals = new Map<string, number>();
|
|
10
|
+
for (const c of m.changes) {
|
|
11
|
+
const day = c.at.slice(0, 10), kinds = changes.get(day) ?? new Set<string>();
|
|
12
|
+
kinds.add(c.kind); changes.set(day, kinds); totals.set(day, (totals.get(day) ?? 0) + 1);
|
|
13
|
+
}
|
|
14
|
+
const rows = [];
|
|
15
|
+
for (let t = Math.max(Date.parse(m.series.days[0]), Date.parse(m.window.since)); t <= end; t += 86400000) {
|
|
16
|
+
const day = new Date(t).toISOString().slice(0, 10), i = positions.get(day) ?? -1;
|
|
17
|
+
rows.push({
|
|
18
|
+
day,
|
|
19
|
+
sessions: m.series.sessions[i] ?? 0,
|
|
20
|
+
calls: m.series.toolCalls[i] ?? 0,
|
|
21
|
+
loads: m.series.skillLoads[i] ?? 0,
|
|
22
|
+
changes: totals.get(day) ?? 0,
|
|
23
|
+
kinds: [...(changes.get(day) ?? [])],
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
return rows;
|
|
27
|
+
}
|
|
28
|
+
export type Week = ReturnType<typeof periodRows>;
|
|
29
|
+
export function workload(rows: Week, L: Words) {
|
|
30
|
+
const heading = `<h2>${L("Workload in this period", "這段期間的工作量")}</h2>`;
|
|
31
|
+
if (!rows.length) return `<div class="panel">${heading}<p>${L("No records in this period.", "這段期間沒有紀錄。")}</p></div>`;
|
|
32
|
+
const total = (key: "sessions" | "calls" | "loads") => num(rows.reduce((n, r) => n + r[key], 0));
|
|
33
|
+
const max = Math.max(1, ...rows.map((r) => r.sessions));
|
|
34
|
+
const points = rows.map((r, i) => [5 + i * 348 / Math.max(1, rows.length - 1), 89 - 64 * r.sessions / max]);
|
|
35
|
+
const line = points.map((p) => p.join(",")).join(" ");
|
|
36
|
+
const md = (d: string) => d.slice(5).replace("-", ".");
|
|
37
|
+
const desc = rows.map((r) => `${r.day}: ${num(r.sessions)} ${L("sessions", "段工作")} · ${r.changes} ${L("changes", "筆調整")}`).join(
|
|
38
|
+
"; ",
|
|
39
|
+
);
|
|
40
|
+
return `<div class="panel workload">${heading}<p class="muted">${md(rows[0].day)}–${md(rows.at(-1)!.day)} / ${
|
|
41
|
+
L("Through the last extract", "統計至上次抽取")
|
|
42
|
+
}</p><h3>${total("sessions")} ${L("sessions", "段工作")}</h3><p class="muted">${
|
|
43
|
+
L("Sessions per day · colored dots mark changes", "每日工作段數 · 彩點表示當日調整類別")
|
|
44
|
+
}</p>
|
|
45
|
+
<svg role="img" aria-label="${esc(desc)}" viewBox="0 0 358 96"><polygon points="5,89 ${line} ${
|
|
46
|
+
points.at(-1)![0]
|
|
47
|
+
},89" class="area"/><polyline points="${line}" class="trend"/>${
|
|
48
|
+
points.map(([x], i) =>
|
|
49
|
+
rows[i].changes
|
|
50
|
+
? `<g><title>${esc(rows[i].day)}: ${num(rows[i].changes)} ${L("changes", "筆調整")}</title>${rows[i].kinds.map((k,j) => `<circle class="work-mark kind-${esc(k)}" cx="${x}" cy="${5 + 4*j}" r="1.9"/>`).join("")}</g>`
|
|
51
|
+
: ""
|
|
52
|
+
).join("")
|
|
53
|
+
}</svg>
|
|
54
|
+
<div class="ends"><span>${md(rows[0].day)} ${num(rows[0].sessions)}</span><span>${md(rows.at(-1)!.day)} ${
|
|
55
|
+
num(rows.at(-1)!.sessions)
|
|
56
|
+
}</span></div><p class="footline">${L("Tool calls", "工具呼叫")} ${total("calls")} / ${L("Skill loads", "讀取技能")} ${
|
|
57
|
+
total("loads")
|
|
58
|
+
}</p></div>`;
|
|
59
|
+
}
|
package/src/trends.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { DayAgg } from "./aggregate.ts";
|
|
2
|
+
import type { DaysIndex } from "./build.ts";
|
|
3
|
+
import { ownerFor, sumDays } from "./model.ts";
|
|
4
|
+
|
|
5
|
+
const DAY = 86_400_000;
|
|
6
|
+
|
|
7
|
+
/** Counts by event date; session counts by skill in the index use session start date. */
|
|
8
|
+
export function recentDays(index: DaysIndex, project?: string) {
|
|
9
|
+
if (!index.lastExtract || !index.range.start) return [];
|
|
10
|
+
const end = Date.parse(index.lastExtract.slice(0, 10));
|
|
11
|
+
const dates = Array.from({ length: 14 }, (_, i) => new Date(end - (14 - i) * DAY).toISOString().slice(0, 10));
|
|
12
|
+
const rows = new Map(sumDays(index, { project, since: dates[0], until: dates[13] }).perDay);
|
|
13
|
+
return dates.filter(d => d >= index.range.start.slice(0, 10)).map(day => ({ day, agg: rows.get(day) }));
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
interface Count { errors: number; calls: number }
|
|
17
|
+
export function trendPair(r: { before: Count; after: Count }) {
|
|
18
|
+
const side = (c: Count) => `${c.errors}/${c.calls} (${(100 * c.errors / c.calls).toFixed(1)}%)`;
|
|
19
|
+
return `${side(r.before)} → ${side(r.after)}`;
|
|
20
|
+
}
|
|
21
|
+
const zero = (): Count => ({ errors: 0, calls: 0 });
|
|
22
|
+
function add(into: Count, from: Count) { into.errors += from.errors; into.calls += from.calls; }
|
|
23
|
+
export function packageCounts(index: DaysIndex, day?: DayAgg) {
|
|
24
|
+
const by = new Map<string, Count>();
|
|
25
|
+
for (const [tool, count] of Object.entries(day?.tools ?? {})) {
|
|
26
|
+
const owner = ownerFor(index, tool), total = by.get(owner) ?? zero();
|
|
27
|
+
add(total, count);
|
|
28
|
+
by.set(owner, total);
|
|
29
|
+
}
|
|
30
|
+
return by;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function week(index: DaysIndex, rows: ReturnType<typeof recentDays>) {
|
|
34
|
+
const packages = new Map<string, Count>(), monitors = zero();
|
|
35
|
+
for (const { agg } of rows) {
|
|
36
|
+
for (const [owner, count] of packageCounts(index, agg)) {
|
|
37
|
+
const total = packages.get(owner) ?? zero();
|
|
38
|
+
add(total, count);
|
|
39
|
+
packages.set(owner, total);
|
|
40
|
+
}
|
|
41
|
+
if (agg) add(monitors, { errors: agg.monitors.statuses.error ?? 0, calls: agg.monitors.finished });
|
|
42
|
+
}
|
|
43
|
+
return { packages, monitors };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function rise(before: Count | undefined, after: Count) {
|
|
47
|
+
if (!before || Math.min(before.calls, after.calls) < 20) return;
|
|
48
|
+
// Compare integer counts before rounding the displayed rates (threshold: five percentage points).
|
|
49
|
+
const diff = after.errors * before.calls - before.errors * after.calls;
|
|
50
|
+
if (20 * diff >= before.calls * after.calls) return { before, after, delta: diff / before.calls / after.calls };
|
|
51
|
+
}
|
|
52
|
+
export function weeklyRises(index: DaysIndex, since?: string, project?: string) {
|
|
53
|
+
const rows = recentDays(index, project);
|
|
54
|
+
if (rows.length !== 14 || (since && Date.parse(rows[0].day) < Date.parse(since) - 86400000)) return;
|
|
55
|
+
const prev = week(index, rows.slice(0, 7)), curr = week(index, rows.slice(7));
|
|
56
|
+
const packages = [...curr.packages].flatMap(([name, count]) => {
|
|
57
|
+
const r = rise(prev.packages.get(name), count);
|
|
58
|
+
return r ? [{ name, ...r }] : [];
|
|
59
|
+
}).sort((a, b) => b.delta - a.delta || b.after.calls - a.after.calls || a.name.localeCompare(b.name));
|
|
60
|
+
return { start: rows[0].day, end: rows[13].day, packages, monitors: rise(prev.monitors, curr.monitors) };
|
|
61
|
+
}
|