Skip to content

Instantly share code, notes, and snippets.

@CelesteComet
Created May 29, 2019 01:27
Show Gist options
  • Save CelesteComet/63de56653251f8820dc99b815a6d24a8 to your computer and use it in GitHub Desktop.
Save CelesteComet/63de56653251f8820dc99b815a6d24a8 to your computer and use it in GitHub Desktop.
scale
const axios = require('axios');
const moment = require('moment');
function Logger(url){
this.uniqueClients = 0;
this.sessions = 0;
this.serverErrors = 0;
this.clicks = [];
this.sessions = [];
var _self = this;
var promise = new Promise((resolve, reject) => {
axios.get(url)
.then(res => {
_self.init(res.data);
resolve(_self);
})
.catch(e => {
reject(e);
});
});
return promise;
}
Logger.prototype.init = function(data) {
this.data = data
this.parseLines();
}
Logger.prototype.parseLines = function() {
var uniqueIPMap = new Map();
var lines = this.data.split("\n");
lines.forEach(line => {
// skip line if it is an error, not the best way to say it's an error but works for this case
if (line.match("error")) {
return;
}
// if guaranteed of the format
var splitLine = line.split(" ");
var IPAddress = splitLine[0];
var method = splitLine[5].slice(1);
var time = splitLine[3].slice(1);
var code = splitLine[8];
var path = splitLine[6].slice(0, splitLine[7].length - 1);
this.clicks.push({
IP: IPAddress,
method: method,
time: time,
code: code,
path: path
})
});
this.uniqueClients = uniqueIPMap.size;
}
Logger.prototype.getUniqueClients = function() {
var set = new Set;
this.clicks.forEach(click => {
set.add(click.IP);
});
return set;
};
Logger.prototype.getUniqueSessions = function() {
let sessionStore = {};
this.uniqueClients = this.getUniqueClients();
for (let entry of this.uniqueClients.entries()) {
var clientIP = entry[0];
var clientClicks = this.clicks.filter(click => {
return click.IP === clientIP;
})
sessionStore[clientIP] = [];
var previousClick = null;
clientClicks.forEach(click => {
if (!previousClick) {
sessionStore[clientIP].push(click);
previousClick = click;
} else {
var previousTime = moment(previousClick.time, 'DD/MMM/YYYY:HH:mm:ss');
var currentTime = moment(click.time, 'DD/MMM/YYYY:HH:mm:ss');
var secondsBetweenClicks = ((currentTime - previousTime) / 1000);
if (secondsBetweenClicks > 30) {
sessionStore[clientIP].push(click);
}
previousTime = currentTime;
}
})
}
var uniqueSessions = 0;
Object.keys(sessionStore).forEach(IP => {
uniqueSessions += sessionStore[IP].length;
});
console.log(sessionStore);
};
Logger.prototype.getServerErrors = function() {
console.log('get 500s');
};
var url = "https://gist.githubusercontent.com/umbrant/6f14684402cae0651bda15fdb9a81476/raw/88fa8063e65542ded2d1d43112f86644b674cf77/apache_access.log"
const logger = new Logger(url);
logger.then(o => {
console.log(o.getUniqueSessions());
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment