-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcompute-stats.js
68 lines (63 loc) · 2.02 KB
/
compute-stats.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
66
67
68
const csv = require("csv-parser");
const fs = require("fs");
const records = [];
let recordsPick = [];
fs.createReadStream("scan-logs.csv")
.pipe(csv())
.on("data", data => records.push(data))
.on("end", () => {
console.log(records);
for (let record of records) {
let recordPick = recordsPick.find(
x => x.contactLabel === record.contactLabel
);
if (recordPick) {
if (
recordPick.LastSeenDate === "null" &&
record.LastSeenDate !== "null"
) {
recordsPick[
recordsPick.findIndex(x => x.contactLabel == record.contactLabel)
] = record;
} else if (
recordPick.LastSeenDate !== "null" &&
record.LastSeenDate !== "null" &&
new Date(recordPick.LastSeenDate) < new Date(record.LastSeenDate)
) {
recordsPick[
recordsPick.findIndex(x => x.contactLabel == record.contactLabel)
] = record;
}
} else {
recordsPick.push(record);
}
}
let refDate = new Date("2021-02-15T12:30:57.301Z");
let stats = {
hasWhatsapp: recordsPick.filter(x => x.HasWhatsapp === "true").length,
lastSeenDataAvailable: recordsPick.filter(x => x.LastSeenDate !== "null")
.length,
acticeLastYear: recordsPick.filter(
x =>
new Date(x.LastSeenDate) >
new Date(refDate.getTime() - 365 * 24 * 60 * 60 * 1000)
).length,
acticeLastMonth: recordsPick.filter(
x =>
new Date(x.LastSeenDate) >
new Date(refDate.getTime() - 30 * 24 * 60 * 60 * 1000)
).length,
acticeLastDay: recordsPick.filter(
x =>
new Date(x.LastSeenDate) >
new Date(refDate.getTime() - 1 * 24 * 60 * 60 * 1000)
).length,
acticeLastHour: recordsPick.filter(
x =>
new Date(x.LastSeenDate) >
new Date(refDate.getTime() - 1 * 60 * 60 * 1000)
).length
};
let data = JSON.stringify(stats);
fs.writeFileSync("stats.json", data);
});