repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

Add ui telemetry aggregator

d32302dda6534cfb83ac109677bf0080c8c6c93d

Oliver Krylow <o.krylow@isp-insoft.de> ·

unsigned

Browse filesCompare with HEAD

Changed files (2)

extensions/ui/__tests__/telemetry.test.ts

Added
new file mode 100644
1 +import { describe, expect, it } from "vitest";
2 +import { redactText, TelemetryAggregator } from "../telemetry";
4 +describe("ui telemetry aggregator", () => {
5 + it("redacts selected and input text", () => {
6 + expect(redactText("secret text")).toBe("[redacted 11 chars]");
7 + expect(redactText("")).toBe("");
8 + });
10 + it("summarizes scroll click focus pagehide widget and form events", () => {
11 + const telemetry = new TelemetryAggregator({ maxEvents: 8 });
12 + telemetry.add({
13 + type: "viewport",
14 + device: "phone",
15 + width: 390,
16 + height: 760,
17 + });
18 + telemetry.add({ type: "scroll", y: 320, dwellMs: 1200 });
19 + telemetry.add({ type: "click", target: "SEND" });
20 + telemetry.add({ type: "focus", target: "composer" });
21 + telemetry.add({ type: "selection", text: "private selection" });
22 + telemetry.add({
23 + type: "widget_action",
24 + widgetId: "widget-1",
25 + label: "Approve",
26 + });
27 + telemetry.add({ type: "form", formId: "f1", state: "abandon" });
28 + telemetry.add({ type: "pagehide" });
30 + expect(telemetry.digest()).toBe(
31 + [
32 + "Browser digest:",
33 + "- viewport phone 390x760",
34 + "- scroll y=320 dwell=1200ms",
35 + "- click SEND",
36 + "- focus composer",
37 + "- selection [redacted 17 chars]",
38 + "- widget widget-1 action Approve",
39 + "- form f1 abandon",
40 + "- pagehide",
41 + ].join("\n"),
42 + );
43 + });
45 + it("caps events and clears digest", () => {
46 + const telemetry = new TelemetryAggregator({ maxEvents: 2 });
47 + telemetry.add({ type: "click", target: "one" });
48 + telemetry.add({ type: "click", target: "two" });
49 + telemetry.add({ type: "click", target: "three" });
50 + expect(telemetry.digest()).toContain("three");
51 + expect(telemetry.digest()).not.toContain("one");
52 + telemetry.clear();
53 + expect(telemetry.digest()).toBe("");
54 + });
55 +});

extensions/ui/telemetry.ts

Added
new file mode 100644
1 +export type TelemetryEvent =
2 + | { type: "viewport"; device: string; width: number; height: number }
3 + | { type: "scroll"; y: number; dwellMs: number }
4 + | { type: "click"; target: string }
5 + | { type: "focus" | "blur"; target: string }
6 + | { type: "selection"; text: string }
7 + | { type: "widget_action"; widgetId: string; label: string }
8 + | { type: "form"; formId: string; state: "submit" | "abandon" }
9 + | { type: "error"; message: string }
10 + | { type: "pagehide" };
12 +export type TelemetryAggregatorOptions = {
13 + maxEvents?: number;
14 +};
16 +export class TelemetryAggregator {
17 + readonly #maxEvents: number;
18 + #events: TelemetryEvent[] = [];
20 + constructor(options: TelemetryAggregatorOptions = {}) {
21 + this.#maxEvents = options.maxEvents ?? 24;
22 + }
24 + add(event: TelemetryEvent): void {
25 + this.#events.push(redactEvent(event));
26 + while (this.#events.length > this.#maxEvents) this.#events.shift();
27 + }
29 + digest(): string {
30 + if (this.#events.length === 0) return "";
31 + return ["Browser digest:", ...this.#events.map(formatEvent)].join("\n");
32 + }
34 + clear(): void {
35 + this.#events = [];
36 + }
39 +export function redactText(text: string): string {
40 + return text ? `[redacted ${text.length} chars]` : "";
43 +function redactEvent(event: TelemetryEvent): TelemetryEvent {
44 + if (event.type === "selection")
45 + return { ...event, text: redactText(event.text) };
46 + if (event.type === "error")
47 + return { ...event, message: redactText(event.message) };
48 + return event;
51 +function formatEvent(event: TelemetryEvent): string {
52 + if (event.type === "viewport")
53 + return `- viewport ${event.device} ${event.width}x${event.height}`;
54 + if (event.type === "scroll")
55 + return `- scroll y=${event.y} dwell=${event.dwellMs}ms`;
56 + if (event.type === "click") return `- click ${event.target}`;
57 + if (event.type === "focus") return `- focus ${event.target}`;
58 + if (event.type === "blur") return `- blur ${event.target}`;
59 + if (event.type === "selection") return `- selection ${event.text}`;
60 + if (event.type === "widget_action")
61 + return `- widget ${event.widgetId} action ${event.label}`;
62 + if (event.type === "form") return `- form ${event.formId} ${event.state}`;
63 + if (event.type === "error") return `- error ${event.message}`;
64 + return "- pagehide";