-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(bright-cli): track command usage (#564)
- Loading branch information
Showing
3 changed files
with
84 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters