1
1
import chalk from 'chalk'
2
2
import meow from 'meow'
3
3
import ora from 'ora'
4
+ // @ts -ignore
5
+ import chalkTable from 'chalk-table'
4
6
5
7
import { outputFlags , validationFlags } from '../flags'
6
8
import { handleApiCall , handleUnsuccessfulApiResponse } from '../utils/api-helpers'
7
- import { InputError } from '../utils/errors'
9
+ import { AuthError , InputError } from '../utils/errors'
8
10
import { printFlagList } from '../utils/formatting'
9
11
import { getDefaultKey , setupSdk } from '../utils/sdk'
10
12
@@ -18,12 +20,16 @@ export const analytics: CliSubcommand = {
18
20
19
21
const input = setupCommand ( name , analytics . description , argv , importMeta )
20
22
if ( input ) {
23
+ const apiKey = getDefaultKey ( )
24
+ if ( ! apiKey ) {
25
+ throw new AuthError ( "User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key." )
26
+ }
21
27
const spinner = ora ( 'Fetching analytics data' ) . start ( )
22
28
if ( input . scope === 'org' ) {
23
- await fetchOrgAnalyticsData ( input . time , spinner )
29
+ await fetchOrgAnalyticsData ( input . time , spinner , apiKey )
24
30
} else {
25
31
if ( input . repo ) {
26
- await fetchRepoAnalyticsData ( input . repo , input . time , spinner )
32
+ await fetchRepoAnalyticsData ( input . repo , input . time , spinner , apiKey )
27
33
}
28
34
}
29
35
}
@@ -92,8 +98,8 @@ function setupCommand (name: string, description: string, argv: readonly string[
92
98
}
93
99
}
94
100
95
- async function fetchOrgAnalyticsData ( time : string , spinner : Ora ) : Promise < void > {
96
- const socketSdk = await setupSdk ( getDefaultKey ( ) )
101
+ async function fetchOrgAnalyticsData ( time : string , spinner : Ora , apiKey : string ) : Promise < void > {
102
+ const socketSdk = await setupSdk ( apiKey )
97
103
const result = await handleApiCall ( socketSdk . getOrgAnalytics ( time ) , 'fetching analytics data' )
98
104
99
105
if ( result . success === false ) {
@@ -123,14 +129,26 @@ async function fetchOrgAnalyticsData (time: string, spinner: Ora): Promise<void>
123
129
return acc
124
130
} , { } )
125
131
126
- console . log ( chalk . bgMagenta . white . bold ( `\n Analytics data for the organization over the last ${ time } days: \n` ) )
127
- console . table ( data , [ 'repository_name' , 'total_critical_alerts' , 'total_high_alerts' , 'top_five_alert_types' ] )
128
- console . table ( data , [ 'repository_name' , 'total_critical_added' , 'total_high_added' ] )
129
- console . table ( data , [ 'repository_name' , 'total_critical_prevented' , 'total_high_prevented' , 'total_medium_prevented' , 'total_low_prevented' ] )
132
+
133
+ const options = {
134
+ columns : [
135
+ { field : 'created_at' , name : chalk . cyan ( 'Date' ) } ,
136
+ { field : 'total_critical_alerts' , name : chalk . cyan ( 'Critical alerts' ) } ,
137
+ { field : 'total_high_alerts' , name : chalk . cyan ( 'High alerts' ) } ,
138
+ { field : 'total_critical_added' , name : chalk . cyan ( 'Critical alerts added' ) } ,
139
+ { field : 'total_high_added' , name : chalk . cyan ( 'High alerts added' ) } ,
140
+ { field : 'total_critical_prevented' , name : chalk . cyan ( 'Critical alerts prevented' ) } ,
141
+ { field : 'total_medium_prevented' , name : chalk . cyan ( 'Medium alerts prevented' ) } ,
142
+ { field : 'total_low_prevented' , name : chalk . cyan ( 'Low alerts prevented' ) } ,
143
+ ]
144
+ }
145
+
146
+ console . log ( chalk . bgMagenta . white . bold ( `\n Analytics data at the organization level over the last ${ time } days (indicated in total amount): \n` ) )
147
+ console . log ( `${ chalkTable ( options , Object . values ( data ) ) } \n` )
130
148
}
131
149
132
- async function fetchRepoAnalyticsData ( repo : string , time : string , spinner : Ora ) : Promise < void > {
133
- const socketSdk = await setupSdk ( getDefaultKey ( ) )
150
+ async function fetchRepoAnalyticsData ( repo : string , time : string , spinner : Ora , apiKey : string ) : Promise < void > {
151
+ const socketSdk = await setupSdk ( apiKey )
134
152
const result = await handleApiCall ( socketSdk . getRepoAnalytics ( repo , time ) , 'fetching analytics data' )
135
153
136
154
if ( result . success === false ) {
@@ -147,8 +165,19 @@ async function fetchRepoAnalyticsData (repo: string, time: string, spinner: Ora)
147
165
} )
148
166
const data = { ...formattedData . flat ( 1 ) }
149
167
168
+ const options = {
169
+ columns : [
170
+ { field : 'created_at' , name : chalk . cyan ( 'Date' ) } ,
171
+ { field : 'total_critical_alerts' , name : chalk . cyan ( 'Critical alerts' ) } ,
172
+ { field : 'total_high_alerts' , name : chalk . cyan ( 'High alerts' ) } ,
173
+ { field : 'total_critical_added' , name : chalk . cyan ( 'Critical alerts added' ) } ,
174
+ { field : 'total_high_added' , name : chalk . cyan ( 'High alerts added' ) } ,
175
+ { field : 'total_critical_prevented' , name : chalk . cyan ( 'Critical alerts prevented' ) } ,
176
+ { field : 'total_medium_prevented' , name : chalk . cyan ( 'Medium alerts prevented' ) } ,
177
+ { field : 'total_low_prevented' , name : chalk . cyan ( 'Low alerts prevented' ) } ,
178
+ ]
179
+ }
180
+
150
181
console . log ( chalk . bgMagenta . white . bold ( `\n Analytics data for ${ repo } over the last ${ time } days: \n` ) )
151
- console . table ( data , [ 'created_at' , 'total_critical_alerts' , 'total_high_alerts' , 'top_five_alert_types' ] )
152
- console . table ( data , [ 'created_at' , 'total_critical_added' , 'total_high_added' ] )
153
- console . table ( data , [ 'created_at' , 'total_critical_prevented' , 'total_high_prevented' , 'total_medium_prevented' , 'total_low_prevented' ] )
182
+ console . log ( `${ chalkTable ( options , Object . values ( data ) ) } \n` )
154
183
}
0 commit comments