From 76f9556bd40378f38ef38513728c1b665fcde2d6 Mon Sep 17 00:00:00 2001 From: Satya Patel Date: Tue, 29 Sep 2026 14:58:16 -0700 Subject: [PATCH 1/2] fix(cli): report each command once per day instead of on every run cli_command_invoked was about 13.3M events a week, roughly 60% of all PostHog events. 24 users sent 80% of them: agents and dashboards that call `terminals list`, `terminals read` and `workspaces list` every few seconds. Each event was also its own analytics.captureEvent call to the API. The CLI now keeps the commands it reported today (UTC) in $SUPERSET_HOME_DIR/reported-commands.json and skips both the event and the API call for repeats. DAU/WAU and users-per-command stay correct; raw call counts go away. --- packages/cli/src/lib/analytics.test.ts | 56 ++++++++++++++++++++++++++ packages/cli/src/lib/analytics.ts | 37 ++++++++++++++++- 2 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 packages/cli/src/lib/analytics.test.ts diff --git a/packages/cli/src/lib/analytics.test.ts b/packages/cli/src/lib/analytics.test.ts new file mode 100644 index 00000000000..57717e9abf8 --- /dev/null +++ b/packages/cli/src/lib/analytics.test.ts @@ -0,0 +1,56 @@ +import { + afterAll, + beforeEach, + describe, + expect, + setSystemTime, + test, +} from "bun:test"; +import { mkdtempSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { isFirstReportToday } from "./analytics"; + +const tempDir = mkdtempSync(join(tmpdir(), "superset-cli-analytics-")); +const reportedPath = join(tempDir, "reported-commands.json"); + +function report(command: string) { + return isFirstReportToday(command, reportedPath); +} + +beforeEach(() => { + rmSync(reportedPath, { force: true }); + setSystemTime(new Date("2026-09-29T12:00:00Z")); +}); + +afterAll(() => { + setSystemTime(); + rmSync(tempDir, { recursive: true, force: true }); +}); + +describe("isFirstReportToday", () => { + test("allows a command once per day", () => { + expect(report("terminals list")).toBe(true); + expect(report("terminals list")).toBe(false); + expect(report("terminals list")).toBe(false); + }); + + test("tracks each command separately", () => { + expect(report("terminals list")).toBe(true); + expect(report("terminals read")).toBe(true); + expect(report("terminals list")).toBe(false); + }); + + test("allows the command again on the next UTC day", () => { + expect(report("status")).toBe(true); + setSystemTime(new Date("2026-09-30T00:00:01Z")); + expect(report("status")).toBe(true); + expect(report("status")).toBe(false); + }); + + test("allows every call when the file cannot be written", () => { + const unwritable = join(tempDir, "missing-dir", "reported-commands.json"); + expect(isFirstReportToday("status", unwritable)).toBe(true); + expect(isFirstReportToday("status", unwritable)).toBe(true); + }); +}); diff --git a/packages/cli/src/lib/analytics.ts b/packages/cli/src/lib/analytics.ts index a98ac28b2b0..08cd5ad2839 100644 --- a/packages/cli/src/lib/analytics.ts +++ b/packages/cli/src/lib/analytics.ts @@ -1,17 +1,52 @@ +import { readFileSync, writeFileSync } from "node:fs"; +import { join } from "node:path"; import type { ApiClient } from "./api-client"; +import { SUPERSET_HOME_DIR } from "./config"; import { env } from "./env"; +const REPORTED_COMMANDS_PATH = join( + SUPERSET_HOME_DIR, + "reported-commands.json", +); + +interface ReportedCommands { + day: string; + commands: string[]; +} + +export function isFirstReportToday( + command: string, + path = REPORTED_COMMANDS_PATH, +): boolean { + const day = new Date().toISOString().slice(0, 10); + let reported: Partial = {}; + try { + reported = JSON.parse(readFileSync(path, "utf-8")); + } catch {} + const commands = reported.day === day ? (reported.commands ?? []) : []; + if (commands.includes(command)) return false; + try { + writeFileSync( + path, + JSON.stringify({ day, commands: [...commands, command] }), + ); + } catch {} + return true; +} + export function trackCommandInvoked(input: { api: ApiClient; commandPath: string[]; flags: string[]; }): void { + const command = input.commandPath.join(" "); + if (!isFirstReportToday(command)) return; void input.api.analytics.captureEvent .mutate({ source: "cli", event: "cli_command_invoked", properties: { - command: input.commandPath.join(" "), + command, flags: input.flags, cli_version: env.VERSION, }, From ac7cb5244d3330274b05f76e23af7c8cc116d7ad Mon Sep 17 00:00:00 2001 From: Satya Patel Date: Tue, 29 Sep 2026 19:12:09 -0700 Subject: [PATCH 2/2] fix(cli): ignore a malformed reported-commands file instead of throwing Valid JSON of the wrong shape (null, a non-array commands) threw in the middleware before the command ran, so one bad file broke every command. --- packages/cli/src/lib/analytics.test.ts | 14 +++++++++++++- packages/cli/src/lib/analytics.ts | 13 +++++-------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/packages/cli/src/lib/analytics.test.ts b/packages/cli/src/lib/analytics.test.ts index 57717e9abf8..41bbec18d75 100644 --- a/packages/cli/src/lib/analytics.test.ts +++ b/packages/cli/src/lib/analytics.test.ts @@ -6,7 +6,7 @@ import { setSystemTime, test, } from "bun:test"; -import { mkdtempSync, rmSync } from "node:fs"; +import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { isFirstReportToday } from "./analytics"; @@ -48,6 +48,18 @@ describe("isFirstReportToday", () => { expect(report("status")).toBe(false); }); + test("starts over when the file holds something unexpected", () => { + for (const contents of [ + "null", + "not json", + '{"day":"2026-09-29","commands":"status"}', + ]) { + writeFileSync(reportedPath, contents); + expect(report("status")).toBe(true); + expect(report("status")).toBe(false); + } + }); + test("allows every call when the file cannot be written", () => { const unwritable = join(tempDir, "missing-dir", "reported-commands.json"); expect(isFirstReportToday("status", unwritable)).toBe(true); diff --git a/packages/cli/src/lib/analytics.ts b/packages/cli/src/lib/analytics.ts index 08cd5ad2839..975b45c4a18 100644 --- a/packages/cli/src/lib/analytics.ts +++ b/packages/cli/src/lib/analytics.ts @@ -9,21 +9,18 @@ const REPORTED_COMMANDS_PATH = join( "reported-commands.json", ); -interface ReportedCommands { - day: string; - commands: string[]; -} - export function isFirstReportToday( command: string, path = REPORTED_COMMANDS_PATH, ): boolean { const day = new Date().toISOString().slice(0, 10); - let reported: Partial = {}; + let commands: string[] = []; try { - reported = JSON.parse(readFileSync(path, "utf-8")); + const reported = JSON.parse(readFileSync(path, "utf-8")); + if (reported?.day === day && Array.isArray(reported.commands)) { + commands = reported.commands; + } } catch {} - const commands = reported.day === day ? (reported.commands ?? []) : []; if (commands.includes(command)) return false; try { writeFileSync(