export default function makeTree ( events ) { const processMap = new Map ( ) ; const tree = { pid : 'root' , children : { } , index : - 1 , level : 0 , } ; events . forEach ( ( s , index ) => { if ( ! processMap . has ( s . pid ) ) { processMap . set ( s . pid , { files : [ ] , network : [ ] , ppid : s . ppid , index , } ) ; } const process = processMap . get ( s . pid ) ; if ( s . event === 'session.connect' ) { process . network . push ( index ) ; } if ( s . event === 'session.open' ) { process . files . push ( index ) ; } if ( s . event === 'session.exec' ) { process . ppid = s . ppid ; process . index = index ; } } ) ; processMap . forEach ( ( process , keyPid ) => { const ppath = [ keyPid ] ; let pid = process . ppid ; while ( processMap . has ( pid ) ) { ppath . unshift ( pid ) ; pid = processMap . get ( pid ) . ppid ; } let current = tree . children ; const level = ppath . length - 1 ; for ( var i = 0 ; i < ppath . length ; i ++ ) { const pid = ppath [ i ] ; current [ pid ] = current [ pid ] || { index : processMap . get ( pid ) . index , pid , level , files : processMap . get ( pid ) . files , network : processMap . get ( pid ) . network , children : { } , } ; current = current [ pid ] . children ; } } ) ; return tree ; }