pi-evals 0.2.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.
@@ -1,28 +1,34 @@
1
1
  import type { ReportModel } from "../model.ts";
2
2
  import { esc, num, type Words } from "./format.ts";
3
3
 
4
- /** Last seven calendar days ending on the last complete UTC day in the selected period. */
5
- export function week(m: ReportModel) {
6
- const last = m.freshness.lastExtract?.slice(0, 10);
7
- if (!last || !m.window.until || !m.window.since) return [];
8
- const end = Math.min(Date.parse(last) - 86400000, Date.parse(m.window.until));
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
+ }
9
14
  const rows = [];
10
- for (let t = Math.max(end - 6 * 86400000, Date.parse(m.window.since)); t <= end; t += 86400000) {
11
- const day = new Date(t).toISOString().slice(0, 10), i = m.series.days.indexOf(day);
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;
12
17
  rows.push({
13
18
  day,
14
19
  sessions: m.series.sessions[i] ?? 0,
15
20
  calls: m.series.toolCalls[i] ?? 0,
16
21
  loads: m.series.skillLoads[i] ?? 0,
17
- changes: m.changes.filter((c) => c.at.startsWith(day)).length,
22
+ changes: totals.get(day) ?? 0,
23
+ kinds: [...(changes.get(day) ?? [])],
18
24
  });
19
25
  }
20
26
  return rows;
21
27
  }
22
- export type Week = ReturnType<typeof week>;
28
+ export type Week = ReturnType<typeof periodRows>;
23
29
  export function workload(rows: Week, L: Words) {
24
- const heading = `<h2>${L("This week’s workload", "這週的工作量")}</h2>`;
25
- if (!rows.length) return `<div class="panel">${heading}<p>${L("No complete recorded day yet.", "尚無完整一天的紀錄。")}</p></div>`;
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>`;
26
32
  const total = (key: "sessions" | "calls" | "loads") => num(rows.reduce((n, r) => n + r[key], 0));
27
33
  const max = Math.max(1, ...rows.map((r) => r.sessions));
28
34
  const points = rows.map((r, i) => [5 + i * 348 / Math.max(1, rows.length - 1), 89 - 64 * r.sessions / max]);
@@ -32,18 +38,16 @@ export function workload(rows: Week, L: Words) {
32
38
  "; ",
33
39
  );
34
40
  return `<div class="panel workload">${heading}<p class="muted">${md(rows[0].day)}–${md(rows.at(-1)!.day)} / ${
35
- L("Volume is not quality", "多寡不代表好壞")
41
+ L("Through the last extract", "統計至上次抽取")
36
42
  }</p><h3>${total("sessions")} ${L("sessions", "段工作")}</h3><p class="muted">${
37
- L("Sessions per day · dashes mark changes", "每日工作段數 · 虛線表示當天有調整")
43
+ L("Sessions per day · colored dots mark changes", "每日工作段數 · 彩點表示當日調整類別")
38
44
  }</p>
39
45
  <svg role="img" aria-label="${esc(desc)}" viewBox="0 0 358 96"><polygon points="5,89 ${line} ${
40
46
  points.at(-1)![0]
41
47
  },89" class="area"/><polyline points="${line}" class="trend"/>${
42
- points.map(([x, y], i) =>
48
+ points.map(([x], i) =>
43
49
  rows[i].changes
44
- ? `<path d="M${x} 12V89" class="marker"/><circle cx="${x}" cy="${y}" r="3.5" class="dot"><title>${esc(rows[i].day)}: ${
45
- rows[i].changes
46
- } ${L("changes", "筆調整")}</title></circle>`
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>`
47
51
  : ""
48
52
  ).join("")
49
53
  }</svg>
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
+ }