1
- // @ts -nocheck
2
1
// @ts -ignore
3
2
import blessed from 'blessed'
4
3
import contrib from 'blessed-contrib'
@@ -112,26 +111,26 @@ async function fetchOrgAnalyticsData (time: string, spinner: Ora, apiKey: string
112
111
113
112
spinner . stop ( )
114
113
115
- // const data = result.data.reduce((acc: { [key: string]: any }, current) => {
116
- // const formattedDate = new Date(current.created_at).toLocaleDateString()
117
-
118
- // if (acc[formattedDate]) {
119
- // acc[formattedDate].total_critical_alerts += current.total_critical_alerts
120
- // acc[formattedDate].total_high_alerts += current.total_high_alerts
121
- // acc[formattedDate].total_critical_added += current.total_critical_added
122
- // acc[formattedDate].total_high_added += current.total_high_added
123
- // acc[formattedDate].total_critical_prevented += current.total_critical_prevented
124
- // acc[formattedDate].total_high_prevented += current.total_high_prevented
125
- // acc[formattedDate].total_medium_prevented += current.total_medium_prevented
126
- // acc[formattedDate].total_low_prevented += current.total_low_prevented
127
- // // acc[formattedDate].top_five_alert_types += current.top_five_alert_types
128
- // } else {
129
- // acc[formattedDate] = current
130
- // acc[formattedDate].created_at = formattedDate
131
- // }
132
-
133
- // return acc
134
- // }, {})
114
+ const data = result . data . reduce ( ( acc : { [ key : string ] : any } , current ) => {
115
+ const formattedDate = new Date ( current . created_at ) . toLocaleDateString ( )
116
+
117
+ if ( acc [ formattedDate ] ) {
118
+ acc [ formattedDate ] . total_critical_alerts += current . total_critical_alerts
119
+ acc [ formattedDate ] . total_high_alerts += current . total_high_alerts
120
+ acc [ formattedDate ] . total_critical_added += current . total_critical_added
121
+ acc [ formattedDate ] . total_high_added += current . total_high_added
122
+ acc [ formattedDate ] . total_critical_prevented += current . total_critical_prevented
123
+ acc [ formattedDate ] . total_high_prevented += current . total_high_prevented
124
+ acc [ formattedDate ] . total_medium_prevented += current . total_medium_prevented
125
+ acc [ formattedDate ] . total_low_prevented += current . total_low_prevented
126
+ // acc[formattedDate].top_five_alert_types += current.top_five_alert_types
127
+ } else {
128
+ acc [ formattedDate ] = current
129
+ acc [ formattedDate ] . created_at = formattedDate
130
+ }
131
+
132
+ return acc
133
+ } , { } )
135
134
136
135
// console.log(data)
137
136
@@ -155,29 +154,31 @@ async function fetchOrgAnalyticsData (time: string, spinner: Ora, apiKey: string
155
154
// eslint-disable-next-line
156
155
const grid = new contrib . grid ( { rows : 4 , cols : 4 , screen} )
157
156
158
- renderLineCharts ( grid , screen , 'Critical alerts' , [ 0 , 0 , 1 , 1.5 ] )
159
- renderLineCharts ( grid , screen , 'High alerts' , [ 0 , 1.5 , 1 , 1.5 ] )
160
- renderLineCharts ( grid , screen , 'Critical alerts added to main' , [ 1 , 0 , 1 , 2 ] )
161
- renderLineCharts ( grid , screen , 'High alerts added to main' , [ 1 , 2 , 1 , 2 ] )
162
- renderLineCharts ( grid , screen , 'Critical alerts prevented from main' , [ 2 , 0 , 1 , 2 ] )
163
- renderLineCharts ( grid , screen , 'High alerts prevented to main' , [ 2 , 2 , 1 , 2 ] )
157
+ renderLineCharts ( grid , screen , 'Critical alerts' , [ 0 , 0 , 1 , 1.5 ] , data , 'total_critical_alerts' )
158
+ renderLineCharts ( grid , screen , 'High alerts' , [ 0 , 1.5 , 1 , 1.5 ] , data , 'total_high_alerts' )
159
+ renderLineCharts ( grid , screen , 'Critical alerts added to main' , [ 1 , 0 , 1 , 2 ] , data , 'total_critical_added' )
160
+ renderLineCharts ( grid , screen , 'High alerts added to main' , [ 1 , 2 , 1 , 2 ] , data , 'total_high_added' )
161
+ renderLineCharts ( grid , screen , 'Critical alerts prevented from main' , [ 2 , 0 , 1 , 2 ] , data , 'total_critical_prevented' )
162
+ renderLineCharts ( grid , screen , 'High alerts prevented to main' , [ 2 , 2 , 1 , 2 ] , data , 'total_high_prevented' )
164
163
165
- const bar = grid . set ( 3 , 0 , 1 , 1 , contrib . bar ,
164
+ const bar = grid . set ( 3 , 0 , 1 , 2 , contrib . bar ,
166
165
{ label : 'Top 5 alert types'
167
- , barWidth : 4
168
- , barSpacing : 6
166
+ , barWidth : 10
167
+ , barSpacing : 20
169
168
, xOffset : 0
170
- , maxHeight : 9 } )
169
+ , maxHeight : 9 , barBgColor : 'magenta' } )
171
170
172
171
screen . append ( bar ) //must append before setting data
173
172
173
+ const top5AlertTypes = Object . values ( data ) [ 0 ] . top_five_alert_types
174
+
174
175
bar . setData (
175
- { titles : [ 'Env vars' , 'stuff' ]
176
- , data : [ 5 , 10 ] } )
176
+ { titles : Object . keys ( top5AlertTypes )
177
+ , data : Object . values ( top5AlertTypes ) } )
177
178
178
179
screen . render ( )
179
180
180
- screen . key ( [ 'escape' , 'q' , 'C-c' ] , function ( ch , key ) {
181
+ screen . key ( [ 'escape' , 'q' , 'C-c' ] , function ( ) {
181
182
return process . exit ( 0 ) ;
182
183
} )
183
184
}
@@ -218,7 +219,11 @@ async function fetchRepoAnalyticsData (repo: string, time: string, spinner: Ora,
218
219
}
219
220
220
221
221
- const renderLineCharts = ( grid , screen , title , coords ) => {
222
+ const renderLineCharts = ( grid : any , screen : any , title : string , coords : number [ ] , data : { [ key : string ] : { [ key : string ] : number | { } } } , label : string ) => {
223
+ const formattedDates = Object . keys ( data ) . map ( d => `${ new Date ( d ) . getMonth ( ) + 1 } /${ new Date ( d ) . getDate ( ) } ` )
224
+
225
+ const alertsCounts = Object . values ( data ) . map ( d => d [ label ] )
226
+
222
227
const line = grid . set ( ...coords , contrib . line ,
223
228
{ style :
224
229
{ line : "cyan"
@@ -233,9 +238,8 @@ const renderLineCharts = (grid, screen, title, coords) => {
233
238
screen . append ( line )
234
239
235
240
const lineData = {
236
- // x: Object.keys(data),
237
- x : [ '8/22' , '8/21' , '8/20' , '8/19' , '8/18' , '8/17' , '8/16' ] . reverse ( ) ,
238
- y : [ 0 , 0 , 0 , 0 , 0 , 12 , 50 ]
241
+ x : formattedDates . reverse ( ) ,
242
+ y : alertsCounts
239
243
}
240
244
241
245
line . setData ( [ lineData ] )
0 commit comments