mirror of
https://github.com/bitbaum/heidi.git
synced 2026-12-04 13:17:06 +01:00
* Owner: one page that always says the next step; counts come from the pack /owner never answers 404: it says whether you are signed out, signed in with another address, unconfirmed, or the owner, and what to do. The check asks whether the address is the owner's before asking if it is confirmed. The investor room's question and review counts and the warm-up's promised length are tied to the content. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtmtfseicWGK2CMC2U5r * Team QR poster, the visitor's path counted, care page follows the pack The team invite shows a QR code and opens a printable poster for the staff room. Anonymous totals now count the visitor's path from home page to answer, and from organisations page to pilot request; /owner lists them. The care page lists every care scene the pack has. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtmtfseicWGK2CMC2U5r --------- Co-authored-by: Claude <noreply@anthropic.com>
90 lines
3.6 KiB
TypeScript
90 lines
3.6 KiB
TypeScript
import type { Run } from "../../db/run.ts";
|
|
|
|
/**
|
|
* Write one count, and read the totals back.
|
|
*
|
|
* `countUsage` is one statement: the day's row for the key, made or raised
|
|
* by one. The day is the server's UTC date — never a time, so a row cannot
|
|
* say when in a day somebody practised.
|
|
*
|
|
* `usageTotals` is what the owner's numbers show: per key, over the last
|
|
* `days`. It reads counts and returns counts.
|
|
*/
|
|
export async function countUsage(run: Run, key: string, now: Date): Promise<void> {
|
|
await run(
|
|
`insert into usage_counts (day, key, count) values ($1::date, $2, 1)
|
|
on conflict (day, key) do update set count = usage_counts.count + 1`,
|
|
[now.toISOString().slice(0, 10), key],
|
|
);
|
|
}
|
|
|
|
export type UsageTotals = {
|
|
days: number;
|
|
/** Guided runs per situation: started, finished. */
|
|
runs: { scene: string; started: number; finished: number }[];
|
|
sittings: { started: number; finished: number };
|
|
/** «Selber bauen» in the chat. */
|
|
ownSentence: { opened: number; right: number; wrong: number };
|
|
turnsWritten: number;
|
|
/**
|
|
* The questions answered wrong most often, among those answered at least
|
|
* `MIN_ANSWERS` times: where the material, or the question, is failing.
|
|
*/
|
|
hardest: { id: string; right: number; wrong: number; skipped: number }[];
|
|
/** The visitor's path (`GO` in keys.ts): step → how many times it was reached. */
|
|
path: Record<string, number>;
|
|
};
|
|
|
|
export const MIN_ANSWERS = 5;
|
|
export const HARDEST = 20;
|
|
|
|
export async function usageTotals(run: Run, now: Date, days = 28): Promise<UsageTotals> {
|
|
const rows = await run(
|
|
`select key, sum(count)::int as count from usage_counts
|
|
where day > ($1::date - $2::int) and day <= $1::date group by key`,
|
|
[now.toISOString().slice(0, 10), days],
|
|
);
|
|
const runs = new Map<string, { started: number; finished: number }>();
|
|
const steps = new Map<string, { right: number; wrong: number; skipped: number }>();
|
|
const sittings = { started: 0, finished: 0 };
|
|
const ownSentence = { opened: 0, right: 0, wrong: 0 };
|
|
let turnsWritten = 0;
|
|
const path: Record<string, number> = {};
|
|
for (const row of rows) {
|
|
const key = String(row.key);
|
|
const count = Number(row.count);
|
|
const parts = key.split(":");
|
|
if (parts[0] === "run") {
|
|
const scene = runs.get(parts[2]!) ?? { started: 0, finished: 0 };
|
|
if (parts[1] === "start") scene.started += count;
|
|
else scene.finished += count;
|
|
runs.set(parts[2]!, scene);
|
|
} else if (parts[0] === "sit") {
|
|
if (parts[1] === "start") sittings.started += count;
|
|
else sittings.finished += count;
|
|
} else if (parts[0] === "step") {
|
|
const id = parts.slice(1, -1).join(":");
|
|
const step = steps.get(id) ?? { right: 0, wrong: 0, skipped: 0 };
|
|
step[parts.at(-1) as "right" | "wrong" | "skipped"] += count;
|
|
steps.set(id, step);
|
|
} else if (key === "own:open") ownSentence.opened += count;
|
|
else if (key === "own:right") ownSentence.right += count;
|
|
else if (key === "own:wrong") ownSentence.wrong += count;
|
|
else if (key === "effort:write") turnsWritten += count;
|
|
else if (parts[0] === "go") path[key.slice(3)] = (path[key.slice(3)] ?? 0) + count;
|
|
}
|
|
return {
|
|
days,
|
|
runs: [...runs.entries()].map(([scene, r]) => ({ scene, ...r })).sort((a, b) => b.started - a.started || a.scene.localeCompare(b.scene)),
|
|
sittings,
|
|
ownSentence,
|
|
turnsWritten,
|
|
hardest: [...steps.entries()]
|
|
.map(([id, s]) => ({ id, ...s }))
|
|
.filter((s) => s.right + s.wrong >= MIN_ANSWERS)
|
|
.sort((a, b) => b.wrong / (b.right + b.wrong) - a.wrong / (a.right + a.wrong) || b.wrong - a.wrong || a.id.localeCompare(b.id))
|
|
.slice(0, HARDEST),
|
|
path,
|
|
};
|
|
}
|