graph = {
var n_desc = {}
var l_desc = {}
routes.forEach( entry => {
const peer_key = entry.peer_id
n_desc[ peer_key ] = {
'id': peer_key,
'name': peer_key,
'path': entry.path,
'community': entry.community,
'type': 'peer'
}
var path_len = entry.path.length
entry.path.forEach( (asn,idx) => {
const asn_key = asn.toString()
if ( ! n_desc.hasOwnProperty( asn_key ) ) {
n_desc[ asn_key ] = {
'id': asn_key,
'name': "AS" + asn_key,
'type': 'asn',
'min_distance': path_len - idx,
'communities': [entry.community]
}
} else {
n_desc[ asn_key ]['min_distance'] = d3.min([
n_desc[ asn_key ]['min_distance'],
path_len - idx
])
if (! n_desc[ asn_key ]['communities'].includes( entry.community ) ) {
n_desc[ asn_key ]['communities'].push( entry.community )
}
}
if ( idx < entry.path.length - 1 ) {
var link_key = asn + "-" + entry.path[ idx + 1 ]
link_key += '-'
if ( entry.community !== undefined ) {
link_key += entry.community
}
l_desc[ link_key ] = {
'id': link_key,
'source': asn.toString(),
'target': entry.path[ idx + 1 ].toString(),
'community': entry.community
}
}
})
var link_key = peer_key + "-" + entry.path[ 0 ]
l_desc[ link_key ] = {
'id': link_key,
'source': peer_key,
'target': entry.path[ 0 ].toString(),
'community': entry.community
}
})
return {
'nodes': Object.keys( n_desc ).map( x => n_desc[ x ] ),
'links': Object.keys( l_desc ).map( x => l_desc[ x ] )
}
}