1
1
/* eslint-disable no-console */
2
2
3
+ import chalk from 'chalk'
3
4
import meow from 'meow'
4
5
import ora from 'ora'
5
6
@@ -21,7 +22,9 @@ export const analytics = {
21
22
if ( input . scope === 'org' ) {
22
23
await fetchOrgAnalyticsData ( input . time , spinner )
23
24
} else {
24
- // await fetchRepoAnalyticsData(input.time, spinner)
25
+ if ( input . repo ) {
26
+ await fetchRepoAnalyticsData ( input . repo , input . time , spinner )
27
+ }
25
28
}
26
29
}
27
30
}
@@ -33,6 +36,7 @@ export const analytics = {
33
36
* @typedef CommandContext
34
37
* @property {string } scope
35
38
* @property {string } time
39
+ * @property {string|undefined } repo
36
40
*/
37
41
38
42
/**
@@ -78,7 +82,10 @@ function setupCommand (name, description, argv, importMeta) {
78
82
if ( scope && ! [ 'org' , 'repo' ] . includes ( scope ) ) {
79
83
throw new InputError ( "The scope must either be 'scope' or 'repo'" )
80
84
}
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 ]
82
89
83
90
if ( ! time ) {
84
91
throw new InputError ( 'Please provide a time to get analytics data' )
@@ -88,13 +95,13 @@ function setupCommand (name, description, argv, importMeta) {
88
95
throw new InputError ( 'The time filter must either be 7, 30 or 60' )
89
96
}
90
97
91
- return {
92
- scope, time
93
- }
98
+ return {
99
+ scope, time, repo
100
+ }
94
101
}
95
102
96
103
/**
97
- * @typedef AnalyticsData
104
+ * @typedef OrgAnalyticsData
98
105
* @property {import('@socketsecurity/sdk').SocketSdkReturnType<'getOrgAnalytics'>["data"] } data
99
106
*/
100
107
@@ -111,6 +118,53 @@ async function fetchOrgAnalyticsData (time, spinner) {
111
118
return handleUnsuccessfulApiResponse ( 'getOrgAnalytics' , result , spinner )
112
119
}
113
120
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
+
114
168
const formattedData = result . data . map ( d => {
115
169
const formattedDate = new Date ( d . created_at ) . toLocaleDateString ( )
116
170
return {
@@ -120,5 +174,8 @@ async function fetchOrgAnalyticsData (time, spinner) {
120
174
} )
121
175
const data = { ...formattedData . flat ( 1 ) }
122
176
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' ] )
124
181
}
0 commit comments