-
Notifications
You must be signed in to change notification settings - Fork 1
/
logEachCheckout.js
90 lines (85 loc) · 2.58 KB
/
logEachCheckout.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
goes through all the logs in logs/dailyStats and creates a new log in
logs/eachCheckout that lists each checkout just once, and contains no
personal identifying information
*/
const { readdirSync } = require('fs');
const path = require('path');
const fs = require('fs');
const dayjs = require('dayjs');
// get list of folders in logs/dailyStats - each is one set of stats
const getDirectories = (source) =>
readdirSync(source, { withFileTypes: true })
.filter((dirent) => dirent.isDirectory())
.map((dirent) => dirent.name);
let logfolder = path.resolve(__dirname, './logs/dailyStats/');
let pkgs = getDirectories(logfolder);
let allPkgData = [];
const logCheckoutDates = function (data, filename) {
let allBookings = [];
data = data.filter((d) => d.status === 'Confirmed');
bookIds = [...new Set(data.map((item) => item.bookId))];
bookIds.forEach((currBookId) => {
// get first entry only for each checkout
let { bookId, cid, fromDate, category_name, item_name } = data.find(
(item) => item.bookId === currBookId
);
// gather relevant data to log
fromDate = dayjs(fromDate).format('YYYY-MM-DD');
allBookings.push({
bookId,
cid,
fromDate,
category_name,
item_name,
});
});
allBookings.sort((a, b) => (a.fromDate > b.fromDate ? 1 : -1));
let dir = './logs/eachCheckout/';
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, 0744);
}
fs.writeFile(
dir + '/' + filename + '.json',
JSON.stringify(allBookings, null, 2),
(err) => {
if (err) {
console.error(err);
return;
}
//file written successfully
}
);
};
pkgs.forEach((pkg) => {
let thisdata = [];
let thisFolder = logfolder + '/' + pkg + '/';
let anonFolder = thisFolder + 'anon/';
if (!fs.existsSync(anonFolder)) {
fs.mkdirSync(anonFolder, 0744);
}
let files = readdirSync(thisFolder, { withFileTypes: true })
.filter((dirent) => dirent.isFile())
.map((dirent) => dirent.name);
let anonFiles = readdirSync(anonFolder, { withFileTypes: true })
.filter((dirent) => dirent.isFile())
.map((dirent) => dirent.name);
files.push(...anonFiles);
files.forEach((file) => {
let data;
try {
data = require(thisFolder + file);
} catch {
data = require(anonFolder + file);
}
thisdata = thisdata.concat(data);
allPkgData = allPkgData.concat(data);
});
if (files.length === 0) {
return;
}
let first = files[0].replace('.json', '');
let last = files[files.length - 1].replace('.json', '');
logCheckoutDates(thisdata, pkg);
});
logCheckoutDates(allPkgData, 'all');