11/* eslint-disable no-console */
22
3+ import chalk from 'chalk'
34import meow from 'meow'
45import ora from 'ora'
56
@@ -21,7 +22,9 @@ export const analytics = {
2122 if ( input . scope === 'org' ) {
2223 await fetchOrgAnalyticsData ( input . time , spinner )
2324 } else {
24- // await fetchRepoAnalyticsData(input.time, spinner)
25+ if ( input . repo ) {
26+ await fetchRepoAnalyticsData ( input . repo , input . time , spinner )
27+ }
2528 }
2629 }
2730 }
@@ -33,6 +36,7 @@ export const analytics = {
3336 * @typedef CommandContext
3437 * @property {string } scope
3538 * @property {string } time
39+ * @property {string|undefined } repo
3640 */
3741
3842/**
@@ -78,7 +82,10 @@ function setupCommand (name, description, argv, importMeta) {
7882 if ( scope && ! [ 'org' , 'repo' ] . includes ( scope ) ) {
7983 throw new InputError ( "The scope must either be 'scope' or 'repo'" )
8084 }
81- const time = cli . input [ 1 ]
85+
86+ const repo = scope === 'repo' ? cli . input [ 1 ] : undefined
87+
88+ const time = scope === 'repo' ? cli . input [ 2 ] : cli . input [ 1 ]
8289
8390 if ( ! time ) {
8491 throw new InputError ( 'Please provide a time to get analytics data' )
@@ -88,13 +95,13 @@ function setupCommand (name, description, argv, importMeta) {
8895 throw new InputError ( 'The time filter must either be 7, 30 or 60' )
8996 }
9097
91- return {
92- scope, time
93- }
98+ return {
99+ scope, time, repo
100+ }
94101}
95102
96103/**
97- * @typedef AnalyticsData
104+ * @typedef OrgAnalyticsData
98105 * @property {import('@socketsecurity/sdk').SocketSdkReturnType<'getOrgAnalytics'>["data"] } data
99106 */
100107
@@ -111,6 +118,53 @@ async function fetchOrgAnalyticsData (time, spinner) {
111118 return handleUnsuccessfulApiResponse ( 'getOrgAnalytics' , result , spinner )
112119 }
113120
121+ // const formattedData = result.data.map(d => {
122+ // const formattedDate = new Date(d.created_at).toLocaleDateString()
123+ // return {
124+ // ...d,
125+ // created_at: formattedDate,
126+ // }
127+ // })
128+ // const data = { ...formattedData.flat(1) }
129+
130+ // const test = result.data.reduce((acc, current) => {
131+ // if (acc[current.created_at]) {
132+ // acc[current.created_at].total_critical_alerts += acc[current.created_at].total_critical_alerts
133+ // } else {
134+ // acc[current.created_at] = current
135+ // }
136+
137+ // return acc
138+ // }, {})
139+
140+ // console.log(test)
141+
142+ // console.log('\n')
143+ // console.table(data, ['created_at', 'repository_name', 'total_critical_alerts', 'total_high_alerts', 'top_five_alert_types'])
144+ // console.table(data, ['created_at', 'repository_name', 'total_critical_added', 'total_high_added'])
145+ // console.table(data, ['created_at', 'repository_name', 'total_critical_prevented', 'total_high_prevented', 'total_medium_prevented', 'total_low_prevented'])
146+ }
147+
148+ /**
149+ * @typedef RepoAnalyticsData
150+ * @property {import('@socketsecurity/sdk').SocketSdkReturnType<'getRepoAnalytics'>["data"] } data
151+ */
152+
153+ /**
154+ * @param {string } repo
155+ * @param {string } time
156+ * @param {import('ora').Ora } spinner
157+ * @returns {Promise<void> }
158+ */
159+ async function fetchRepoAnalyticsData ( repo , time , spinner ) {
160+ const socketSdk = await setupSdk ( getDefaultKey ( ) )
161+ const result = await handleApiCall ( socketSdk . getRepoAnalytics ( repo , time ) , 'fetching analytics data' )
162+
163+ if ( result . success === false ) {
164+ return handleUnsuccessfulApiResponse ( 'getRepoAnalytics' , result , spinner )
165+ }
166+ spinner . stop ( )
167+
114168 const formattedData = result . data . map ( d => {
115169 const formattedDate = new Date ( d . created_at ) . toLocaleDateString ( )
116170 return {
@@ -120,5 +174,8 @@ async function fetchOrgAnalyticsData (time, spinner) {
120174 } )
121175 const data = { ...formattedData . flat ( 1 ) }
122176
123- 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' ] )
177+ console . log ( chalk . bgMagenta . white . bold ( `\n Analytics data for ${ repo } over the last ${ time } days: \n` ) )
178+ console . table ( data , [ 'created_at' , 'total_critical_alerts' , 'total_high_alerts' , 'top_five_alert_types' ] )
179+ console . table ( data , [ 'created_at' , 'total_critical_added' , 'total_high_added' ] )
180+ console . table ( data , [ 'created_at' , 'total_critical_prevented' , 'total_high_prevented' , 'total_medium_prevented' , 'total_low_prevented' ] )
124181}
0 commit comments