Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(bright-cli): track command usage #564

Merged
merged 11 commits into from
Sep 2, 2024
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
maksadbek marked this conversation as resolved.
Show resolved Hide resolved
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';
Loading