Skip to content

Commit

Permalink
feat(bright-cli): track command usage (#564)
Browse files Browse the repository at this point in the history
  • Loading branch information
bramkor authored Sep 2, 2024
1 parent 16d295f commit 320cce8
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Config/CliBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CliConfig, ConfigReader } from './ConfigReader';
import { ClusterArgs, Helpers, logger, LogLevel } from '../Utils';
import { ClusterArgs, Helpers, logger, LogLevel, Tracker } from '../Utils';
import { SystemConfigManager } from './SystemConfigManager';
import { CliInfo } from './CliInfo';
import { Arguments, Argv, CommandModule } from 'yargs';
Expand Down Expand Up @@ -91,6 +91,7 @@ export class CliBuilder {
? (+args.logLevel as unknown as LogLevel)
: LogLevel[args.logLevel.toString().toUpperCase()])
)
.middleware(async (args: Arguments) => Tracker.trackCommandUsage(args))
.usage('Usage: $0 <command> [options] [<file | scan>]')
.pkgConf('bright', info.cwd)
.example(
Expand Down
81 changes: 81 additions & 0 deletions src/Utils/Tracker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import axios from 'axios';
import { Arguments } from 'yargs';

export interface Event {
subject: SubjectType;
event: EventType;
properties: Record<string, unknown>;
}

enum SubjectType {
CLI = 'cli'
}

export enum EventType {
CLI_COMMAND = 'CLI command'
}

export class Tracker {
// eslint-disable-next-line @typescript-eslint/require-await
public static async trackCommandUsage(args: Arguments) {
const baseUrl = args.api as string;
const url = `${baseUrl}/api/v1/analytics/events`;
const command = args._.join(' ');
const event: Event = {
subject: SubjectType.CLI,
event: EventType.CLI_COMMAND,
properties: {
command,
hostname: args.cluster || args.hostname,
arguments: this.filterArguments({ ...args })
}
};

// fire-and-forget, so command execution won't be delayed
axios
.post(url, event, {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${args.token}`
}
})
.catch((e) => {
// eslint-disable-next-line no-console
console.warn('Error tracking CLI-command event:', e.message);
});
}

private static filterArguments(
args: Record<string, any>
): Record<string, any> {
const whitelist = [
'b',
'breakpoint',
'bucket',
'cluster',
'hostname',
'insecure',
'interval',
'log-level',
'module',
'param',
'smart',
'template',
'test',
'timeout',
'tp',
'type',
'verbose'
];

const filteredArgs: Record<string, any> = {};

for (const key of Object.keys(args)) {
if (whitelist.includes(key)) {
filteredArgs[key] = args[key];
}
}

return filteredArgs;
}
}
1 change: 1 addition & 0 deletions src/Utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from './Helpers';
export * from './Logger';
export * from './ProxyFactory';
export * from './Traceroute';
export * from './Tracker';

0 comments on commit 320cce8

Please sign in to comment.