Last active
December 17, 2015 16:49
-
-
Save jrgm/5641287 to your computer and use it in GitHub Desktop.
verifier-metrics.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// usage: cat file | verifier-metrics.js | |
const ONE_HOUR_MS = 60*60*1000; | |
function handleData(data, struct) { | |
var message, epoch = Date.parse(data.timestamp); | |
try { | |
message = JSON.parse(data.message); | |
} catch(e) { | |
return console.log('bad message', data.message); | |
} | |
message.epoch = epoch; | |
epoch -= epoch % ONE_HOUR_MS; | |
if (!struct[epoch]) { | |
struct[epoch] = {}; | |
} | |
if (!struct[epoch][message.rp]) { | |
struct[epoch][message.rp] = []; | |
} | |
struct[epoch][message.rp].push(message); | |
} | |
function handleStruct(struct) { | |
function lpad(num) { | |
num = num.toString(); | |
return new Array(9 - num.length).join(' ') + num; | |
} | |
function countMatch(rp, match) { | |
function matcher(elt) { | |
return elt.result === match; | |
} | |
return rp.filter(matcher).length; | |
} | |
Object.keys(struct).forEach(function(block) { | |
var section = struct[block]; | |
block = new Date(parseInt(block, 10)).toISOString(); | |
var sectionKeys = Object.keys(section); | |
if (sectionKeys.length === 0) return; | |
console.log('\n\nDuring %s', block); | |
var rps = sectionKeys.sort(function(a, b) { | |
return section[b].length - section[a].length; | |
}); | |
rps.forEach(function(rp) { | |
var fail = countMatch(section[rp], 'failure'); | |
var pass = countMatch(section[rp], 'success'); | |
console.log([ block, lpad(pass), lpad(fail), ' ', rp ].join(' ')); | |
}); | |
}); | |
} | |
function consumeData(data) { | |
var struct = {}; | |
var lines = data.split('\n'); | |
lines.forEach(function(line) { | |
if (!line) return; | |
var parsed; | |
try { | |
parsed = JSON.parse(line); | |
} catch(e) { | |
return console.log('bad line', line); | |
} | |
handleData(parsed, struct); | |
}); | |
handleStruct(struct); | |
} | |
(function() { | |
process.stdin.resume(); | |
process.stdin.setEncoding('utf8'); | |
var data = ''; | |
process.stdin.on('data', function (chunk) { | |
data += chunk; | |
}); | |
process.stdin.on('end', function () { | |
consumeData(data); | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment