Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions packages/cli/src/lib/analytics.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
34 changes: 33 additions & 1 deletion packages/cli/src/lib/analytics.ts
Original file line number Diff line number Diff line change
@@ -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,
},
Expand Down
Loading