1- // @ts -nocheck
21// @ts -ignore
32import blessed from 'blessed'
43import contrib from 'blessed-contrib'
@@ -112,26 +111,26 @@ async function fetchOrgAnalyticsData (time: string, spinner: Ora, apiKey: string
112111
113112 spinner . stop ( )
114113
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+ } , { } )
135134
136135 // console.log(data)
137136
@@ -155,29 +154,31 @@ async function fetchOrgAnalyticsData (time: string, spinner: Ora, apiKey: string
155154 // eslint-disable-next-line
156155 const grid = new contrib . grid ( { rows : 4 , cols : 4 , screen} )
157156
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' )
164163
165- const bar = grid . set ( 3 , 0 , 1 , 1 , contrib . bar ,
164+ const bar = grid . set ( 3 , 0 , 1 , 2 , contrib . bar ,
166165 { label : 'Top 5 alert types'
167- , barWidth : 4
168- , barSpacing : 6
166+ , barWidth : 10
167+ , barSpacing : 20
169168 , xOffset : 0
170- , maxHeight : 9 } )
169+ , maxHeight : 9 , barBgColor : 'magenta' } )
171170
172171 screen . append ( bar ) //must append before setting data
173172
173+ const top5AlertTypes = Object . values ( data ) [ 0 ] . top_five_alert_types
174+
174175 bar . setData (
175- { titles : [ 'Env vars' , 'stuff' ]
176- , data : [ 5 , 10 ] } )
176+ { titles : Object . keys ( top5AlertTypes )
177+ , data : Object . values ( top5AlertTypes ) } )
177178
178179 screen . render ( )
179180
180- screen . key ( [ 'escape' , 'q' , 'C-c' ] , function ( ch , key ) {
181+ screen . key ( [ 'escape' , 'q' , 'C-c' ] , function ( ) {
181182 return process . exit ( 0 ) ;
182183 } )
183184}
@@ -218,7 +219,11 @@ async function fetchRepoAnalyticsData (repo: string, time: string, spinner: Ora,
218219}
219220
220221
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+
222227 const line = grid . set ( ...coords , contrib . line ,
223228 { style :
224229 { line : "cyan"
@@ -233,9 +238,8 @@ const renderLineCharts = (grid, screen, title, coords) => {
233238 screen . append ( line )
234239
235240 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
239243 }
240244
241245 line . setData ( [ lineData ] )
0 commit comments