-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (57 loc) · 1.65 KB
/
index.js
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
const arrayDuplicates = require('./arrayDuplicates');
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
const getReport = (path) => {
var fs = require('fs')
, filename = path;
fs.readFile(filename, 'utf8', function(err, data) {
if (err) throw err;
console.log('File OK: ' + filename);
summarizeData(data);
});
}
function groupArray(arr, property) {
return arr.reduce(function(memo, x) {
if (!memo[x[property]]) { memo[x[property]] = []; }
memo[x[property]].push(x);
return memo;
}, {});
}
const summarizeData = (data) => {
const result = data.split('\n');
const url = [];
const items = [];
const pageViews = [];
result.slice(1).map(line => {
var obj = line.split('|');
if(obj.length > 0) {
var item = {
url: obj[2],
views: obj[2] + ' ' + obj[3]
}
url.push(item.url);
pageViews.push(item.views);
items.push(item);
}
});
var urls = arrayDuplicates.getDuplicates(url);
var sorted = {};
for( var i = 0, max = items.length; i < max ; i++ ){
if( sorted[items[i].url] == undefined ){
sorted[items[i].url] = [];
}
sorted[items[i].url].push(items[i]);
}
console.log('|URL |Page views |Unique');
urls.map(url => {
url.pageViews = items.reduce((acc, cur) => cur.url === url.value ? ++acc : acc, 0);
url.unique = [...new Set(sorted[url.value].map(item => item.views.split(' ')[2]))];
console.log(url.value + ' ' + url.pageViews + ' ' + url.unique.length);
});
}
readline.question(`Enter path to report: `, (path) => {
getReport(path);
readline.close();
});