diff --git a/lib/commands/analytics/index.js b/lib/commands/analytics/index.js index 0fc6842a..20e61a8a 100644 --- a/lib/commands/analytics/index.js +++ b/lib/commands/analytics/index.js @@ -1,5 +1,6 @@ /* eslint-disable no-console */ +import chalk from 'chalk' import meow from 'meow' import ora from 'ora' @@ -21,7 +22,9 @@ export const analytics = { if (input.scope === 'org') { await fetchOrgAnalyticsData(input.time, spinner) } else { - // await fetchRepoAnalyticsData(input.time, spinner) + if (input.repo) { + await fetchRepoAnalyticsData(input.repo, input.time, spinner) + } } } } @@ -33,6 +36,7 @@ export const analytics = { * @typedef CommandContext * @property {string} scope * @property {string} time + * @property {string|undefined} repo */ /** @@ -78,7 +82,10 @@ function setupCommand (name, description, argv, importMeta) { if (scope && !['org', 'repo'].includes(scope)) { throw new InputError("The scope must either be 'scope' or 'repo'") } - const time = cli.input[1] + + const repo = scope === 'repo' ? cli.input[1] : undefined + + const time = scope === 'repo' ? cli.input[2] : cli.input[1] if (!time) { throw new InputError('Please provide a time to get analytics data') @@ -88,13 +95,13 @@ function setupCommand (name, description, argv, importMeta) { throw new InputError('The time filter must either be 7, 30 or 60') } - return { - scope, time - } + return { + scope, time, repo + } } /** - * @typedef AnalyticsData + * @typedef OrgAnalyticsData * @property {import('@socketsecurity/sdk').SocketSdkReturnType<'getOrgAnalytics'>["data"]} data */ @@ -111,6 +118,53 @@ async function fetchOrgAnalyticsData (time, spinner) { return handleUnsuccessfulApiResponse('getOrgAnalytics', result, spinner) } + // const formattedData = result.data.map(d => { + // const formattedDate = new Date(d.created_at).toLocaleDateString() + // return { + // ...d, + // created_at: formattedDate, + // } + // }) + // const data = { ...formattedData.flat(1) } + + // const test = result.data.reduce((acc, current) => { + // if (acc[current.created_at]) { + // acc[current.created_at].total_critical_alerts += acc[current.created_at].total_critical_alerts + // } else { + // acc[current.created_at] = current + // } + + // return acc + // }, {}) + + // console.log(test) + + // console.log('\n') + // console.table(data, ['created_at', 'repository_name', 'total_critical_alerts', 'total_high_alerts', 'top_five_alert_types']) + // console.table(data, ['created_at', 'repository_name', 'total_critical_added', 'total_high_added']) + // console.table(data, ['created_at', 'repository_name', 'total_critical_prevented', 'total_high_prevented', 'total_medium_prevented', 'total_low_prevented']) +} + +/** + * @typedef RepoAnalyticsData + * @property {import('@socketsecurity/sdk').SocketSdkReturnType<'getRepoAnalytics'>["data"]} data + */ + +/** + * @param {string} repo + * @param {string} time + * @param {import('ora').Ora} spinner + * @returns {Promise} + */ +async function fetchRepoAnalyticsData (repo, time, spinner) { + const socketSdk = await setupSdk(getDefaultKey()) + const result = await handleApiCall(socketSdk.getRepoAnalytics(repo, time), 'fetching analytics data') + + if (result.success === false) { + return handleUnsuccessfulApiResponse('getRepoAnalytics', result, spinner) + } + spinner.stop() + const formattedData = result.data.map(d => { const formattedDate = new Date(d.created_at).toLocaleDateString() return { @@ -120,5 +174,8 @@ async function fetchOrgAnalyticsData (time, spinner) { }) const data = { ...formattedData.flat(1) } - console.table(data, ['created_at', 'repository_name', 'total_critical_alerts', 'total_high_alerts', 'total_critical_added', 'total_high_added', 'total_critical_prevented', 'total_high_prevented', 'total_medium_prevented', 'total_low_prevented', 'top_five_alert_types']) + console.log(chalk.bgMagenta.white.bold(`\n Analytics data for ${repo} over the last ${time} days: \n`)) + console.table(data, ['created_at', 'total_critical_alerts', 'total_high_alerts', 'top_five_alert_types']) + console.table(data, ['created_at', 'total_critical_added', 'total_high_added']) + console.table(data, ['created_at', 'total_critical_prevented', 'total_high_prevented', 'total_medium_prevented', 'total_low_prevented']) }