-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
76 lines (69 loc) · 3.55 KB
/
app.js
File metadata and controls
76 lines (69 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
var http = require('http');
var fs = require('fs');
var readline = require('readline');
console.log("Fetching log file...");
http.get("http://dev.inspiringapps.com/Files/IAChallenge/30E02AAA-B947-4D4B-8FB6-9C57C43872A9/Apache.log", function (response) {
let fileContent = "";
const filePath = "downloads/downloaded.log";
const regEx = new RegExp('([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+).*GET\\s*([\\w/]+).*');
let userActivityMap = {};
let sequenceTotals = {};
response.on('data', (chunk) => { fileContent += chunk } )
response.on('end', function () {
if ( !fs.existsSync("downloads") ) { fs.mkdirSync("downloads"); }
fs.writeFile(filePath, fileContent, function () {
let lineReaderInterface = readline.createInterface({
input: fs.createReadStream(filePath)
});
console.log("Analyzing log file...");
// Iterate file line-by-line, build a map of IP addresses (users) and all the resource paths they ever requested
lineReaderInterface.on("line", function (line) {
let result = regEx.exec(line);
let userIpAddress = result[1];
let resourcePath = result[2];
if ( !userActivityMap[userIpAddress] ) {
userActivityMap[userIpAddress] = { rawPathList: [] };
}
userActivityMap[userIpAddress].rawPathList.push(resourcePath);
});
// Done iterating lines in file, now iterate through raw lists of path requests for each user
lineReaderInterface.on("close", function () {
for (let i in userActivityMap ) {
let tmpRawPathList = userActivityMap[i].rawPathList;
// We are at the list of paths for this user, now build sequences of 3
for ( let j=0; j<tmpRawPathList.length; j++ ) {
let nextIndex = j+1;
let thirdIndex = j+2;
// Ensure we are not creating a sequence that will go off the end of the array
if ( tmpRawPathList[nextIndex] && tmpRawPathList[thirdIndex] ) {
// Store the path sequence and increment the count for it
let pathSequence = tmpRawPathList[j]+","+tmpRawPathList[nextIndex]+","+tmpRawPathList[thirdIndex];
if ( !sequenceTotals[pathSequence] ) {
sequenceTotals[pathSequence] = 0;
}
sequenceTotals[pathSequence]++
}
else {
// No more 3-page sequences are possible.
break;
}
}
}
// Sort sequence totals based on counts
let sortableTotals = [];
for ( let sequence in sequenceTotals ) {
sortableTotals.push(
{ "sequence": sequence, "count": sequenceTotals[sequence] }
)
}
sortableTotals.sort( (a, b) => b.count - a.count );
// Output results
console.log("\nCOUNT\t3-PAGE SEQUENCE");
sortableTotals.forEach( (sequenceTotal) => console.log(sequenceTotal.count+"\t"+sequenceTotal.sequence) );
})
});
})
})
.on('error', function(e) {
console.log("Error during download of log file: " + e.message);
});