diff --git a/packages/cli/src/lib/analytics.test.ts b/packages/cli/src/lib/analytics.test.ts new file mode 100644 index 00000000000..41bbec18d75 --- /dev/null +++ b/packages/cli/src/lib/analytics.test.ts @@ -0,0 +1,68 @@ +import { + afterAll, + beforeEach, + describe, + expect, + setSystemTime, + test, +} from "bun:test"; +import { mkdtempSync, rmSync, writeFileSync } 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("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); + expect(isFirstReportToday("status", unwritable)).toBe(true); + }); +}); diff --git a/packages/cli/src/lib/analytics.ts b/packages/cli/src/lib/analytics.ts index a98ac28b2b0..975b45c4a18 100644 --- a/packages/cli/src/lib/analytics.ts +++ b/packages/cli/src/lib/analytics.ts @@ -1,17 +1,49 @@ +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", +); + +export function isFirstReportToday( + command: string, + path = REPORTED_COMMANDS_PATH, +): boolean { + const day = new Date().toISOString().slice(0, 10); + let commands: string[] = []; + try { + const reported = JSON.parse(readFileSync(path, "utf-8")); + if (reported?.day === day && Array.isArray(reported.commands)) { + commands = reported.commands; + } + } catch {} + 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, },