Skip to content

Commit 744d600

Browse files
committed
[WIP] analytics
1 parent d065cdc commit 744d600

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed

lib/commands/analytics/index.js

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/* eslint-disable no-console */
2+
3+
import meow from 'meow'
4+
import ora from 'ora'
5+
6+
import { outputFlags, validationFlags } from '../../flags/index.js'
7+
// import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api-helpers.js'
8+
import { InputError } from '../../utils/errors.js'
9+
import { printFlagList } from '../../utils/formatting.js'
10+
import { FREE_API_KEY, getDefaultKey, setupSdk } from '../../utils/sdk.js'
11+
12+
/** @type {import('../../utils/meow-with-subcommands').CliSubcommand} */
13+
export const analytics = {
14+
description: 'Look up analytics data',
15+
async run (argv, importMeta, { parentName }) {
16+
const name = parentName + ' analytics'
17+
18+
const input = setupCommand(name, analytics.description, argv, importMeta)
19+
if (input) {
20+
const spinner = ora("Fetching analytics data").start()
21+
console.log(input)
22+
if(input.scope === 'org'){
23+
await fetchOrgAnalyticsData(input.time, spinner)
24+
} else {
25+
// await fetchRepoAnalyticsData(input.time, spinner)
26+
}
27+
}
28+
}
29+
}
30+
31+
// Internal functions
32+
33+
/**
34+
* @typedef CommandContext
35+
* @property {string} scope
36+
* @property {string} time
37+
*/
38+
39+
/**
40+
* @param {string} name
41+
* @param {string} description
42+
* @param {readonly string[]} argv
43+
* @param {ImportMeta} importMeta
44+
* @returns {void|CommandContext}
45+
*/
46+
function setupCommand (name, description, argv, importMeta) {
47+
const flags = {
48+
...outputFlags,
49+
...validationFlags,
50+
}
51+
52+
const cli = meow(`
53+
Usage
54+
$ ${name} <scope> <time>
55+
56+
Options
57+
${printFlagList(flags, 6)}
58+
59+
Examples
60+
$ ${name} org 7
61+
$ ${name} org 30
62+
`, {
63+
argv,
64+
description,
65+
importMeta,
66+
flags
67+
})
68+
69+
const scope = cli.input[0]
70+
71+
if(!scope){
72+
throw new InputError("Please provide a scope to get analytics data")
73+
}
74+
75+
if(!cli.input.length){
76+
throw new InputError("Please provide a scope and a time to get analytics data")
77+
}
78+
79+
if(scope && !['org', 'repo'].includes(scope)){
80+
throw new InputError("The scope must either be 'scope' or 'repo'")
81+
}
82+
const time = cli.input[1]
83+
84+
if(!time){
85+
throw new InputError("Please provide a time to get analytics data")
86+
}
87+
88+
if (time && !['7','30','60'].includes(time)) {
89+
throw new InputError('The time filter must either be 7, 30 or 60')
90+
}
91+
92+
return {
93+
scope, time
94+
}
95+
}
96+
97+
// /**
98+
// * @typedef AnalyticsData
99+
// * @property {import('@socketsecurity/sdk').SocketSdkReturnType<'getOrgAnalytics'>["data"]} data
100+
// */
101+
102+
/**
103+
* @param {string} time
104+
* @param {import('ora').Ora} spinner
105+
* @returns {Promise<void>}
106+
*/
107+
// * @returns {Promise<void|AnalyticsData>}
108+
async function fetchOrgAnalyticsData (time, spinner) {
109+
const socketSdk = await setupSdk(getDefaultKey() || FREE_API_KEY)
110+
console.table(time)
111+
112+
// const result = await handleApiCall(socketSdk.getOrgAnalytics(time), 'fetching analytics data')
113+
114+
// if (result.success === false) {
115+
// return handleUnsuccessfulApiResponse('getOrgAnalytics', result, spinner)
116+
// }
117+
118+
// return {
119+
120+
// }
121+
}

0 commit comments

Comments
 (0)