Skip to content

Instantly share code, notes, and snippets.

@coxley
Created August 11, 2015 21:48
Show Gist options
  • Save coxley/0e0f9c7cd31654ed11d1 to your computer and use it in GitHub Desktop.
Save coxley/0e0f9c7cd31654ed11d1 to your computer and use it in GitHub Desktop.
Takes raw ASPATH data and creates coordinate pairs
// The file being loaded by this program looks like:
// ---
// 3356 174 32780
// 6461 174 32780
// 6461 6453 12
// 209 2914 9268 131088
// 209 3257 8495 196624
// 6461 2914 9268 131088
// 6461 3320 8495 196624
// 6461 6939 293 16
// ---
// For few hundred thosand lines
//
// Load modules and set a global unique Set() for adding to
var readline = require('readline');
var zip = require('lodash/array/zip');
var asnLinks = new Set([]);
// Create ReadLine I/O object
var r1 = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function main() {
r1.question("AS paths file? ", function(answer) {
var asPathFile = answer;
// Since this seems to be asynch and need both answers, nest like this?
r1.question("Origin ASN? ", function(answer) {
var originASN = answer;
r1.close();
generatePairs(asPathFile, originASN);
});
});
}
function generatePairs(asPathFile, originASN) {
var fs = require('fs');
fs.readFile(asPathFile, 'utf8', function(err, data) {
if (err) {
return console.log(err);
}
// Split file into array by line
var file_split = data.match(/[^\r\n]+/g);
console.time('generatePairs')
// For each line, split by space, zip into pairs, and add to Set()
file_split.forEach( function(element) {
var path = element;
var path_split = element.split(' ');
path_split.unshift(originASN);
var pairs = zip(path_split, path_split.slice(1)).slice(0, -1);
pairs.forEach( function(x) { asnLinks.add(x); } );
});
console.timeEnd('generatePairs')
});
}
// Run it all
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment