Skip to content

Commit

Permalink
add command for repo level analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
charliegerard committed May 8, 2024
1 parent 4aee60f commit cb5faf0
Showing 1 changed file with 64 additions and 7 deletions.
71 changes: 64 additions & 7 deletions lib/commands/analytics/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable no-console */

import chalk from 'chalk'
import meow from 'meow'
import ora from 'ora'

Expand All @@ -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)
}
}
}
}
Expand All @@ -33,6 +36,7 @@ export const analytics = {
* @typedef CommandContext
* @property {string} scope
* @property {string} time
* @property {string|undefined} repo
*/

/**
Expand Down Expand Up @@ -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')
Expand All @@ -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
*/

Expand All @@ -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<void>}
*/
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 {
Expand All @@ -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'])
}

0 comments on commit cb5faf0

Please sign in to comment.