Skip to content

Instantly share code, notes, and snippets.

/irc-stats.js
Created Oct 21, 2016

Embed
What would you like to do?
"use strict";
var readline = require("readline");
var fs = require("fs");
// https://echelog.com/logs/browse/angularjs/1476828000
var filename = "irc-angularjs-log-2016-10-20.txt";
var stats = {};
var lineReader = readline.createInterface({
input: fs.createReadStream(filename)
});
lineReader.on("line", function (line) {
processLine(line);
});
lineReader.on("close", function () {
displayStats();
});
function processLine(line) {
var timeAndNickJoinRegex = new RegExp(/\[([0-9]+):([0-9]+):([0-9]+)\][\s]*\*\*\*[\s]*([^\s]+)([\s]+[^\s]+[\s]*has joined #angularjs)/g);
var timeAndNickQuitRegex = new RegExp(/\[([0-9]+):([0-9]+):([0-9]+)\][\s]*\*\*\*[\s]*([^\s]+)([\s]+[^\s]+[\s]*has quit IRC)/g);
var matchesJoin = timeAndNickJoinRegex.exec(line);
var matchesQuit = timeAndNickQuitRegex.exec(line);
var matches,
nick,
time,
endOfDay;
matches = matchesJoin || matchesQuit;
if (matches) {
// year, month and day doesn't matter: log is daily
time = new Date(2016, 0, 1, matches[1], matches[2], matches[3]);
endOfDay = new Date(2016, 0, 1, 23, 59, 59);
nick = matches[4];
if (!stats.hasOwnProperty(nick)) {
stats[nick] = {};
}
if (matchesJoin) {
stats[nick].in = time;
stats[nick].out = endOfDay;
stats[nick].ttl = calcTTL(stats[nick].in, stats[nick].out);
}
if (matchesQuit) {
stats[nick].out = time;
stats[nick].ttl = calcTTL(stats[nick].in, stats[nick].out);
}
}
}
function displayStats() {
var nicks = Object.keys(stats),
myStats = [],
myStatsOrdered;
nicks.forEach(function (nick) {
var ttl = stats[nick].ttl;
myStats.push({
nick: nick,
ttl: ttl
});
});
myStatsOrdered = myStats.sort(function (a, b) {
return b.ttl - a.ttl; // descendent sort
});
myStatsOrdered.forEach(function (s) {
if (s.ttl) {
console.log(s.nick, s.ttl);
}
});
}
function calcTTL(start, end) {
// time to live in minutes from unix milliseconds
return Math.round((end - start) / 1000 / 60);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.