-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreducer.js
92 lines (72 loc) · 2.13 KB
/
reducer.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
91
92
const fs = require("fs");
const path = require("path");
const USAGE = `
usage: node reducer.js <source dir>
Consumes a directory populated using recorder.js and creates a reduced set of
responses with only the interesting changes. Reduced response set will be saved
in a new directory <source>-reduced.
`;
const sourceDir = process.argv[2];
if (!sourceDir) {
console.error("Missing argument");
console.error(USAGE);
process.exit(1);
}
const targetDir = path.join(
path.dirname(sourceDir),
path.basename(sourceDir) + "-reduced"
);
if (fs.existsSync(targetDir)) {
console.error(`Target directory ${targetDir} already exists`);
process.exit(1);
} else {
fs.mkdirSync(targetDir);
}
const log = (message) => {
console.log(message);
};
const isJsonFile = (file) => path.extname(file).toLowerCase() === ".json";
const readJsonFile = (filePath) => {
const content = fs.readFileSync(filePath, "utf-8");
return JSON.parse(content);
};
const isInteresting = (key) => {
return (
key !== "timestamp" &&
key !== "version_hash" &&
key !== "live_time" &&
key !== "live_time_mmss" &&
key !== "last_modified"
);
};
const compareJsonObjects = (obj1, obj2) => {
const keys1 = Object.keys(obj1.score).filter(isInteresting);
const keys2 = Object.keys(obj2.score).filter(isInteresting);
let returnValue = false;
for (const key of keys1) {
if (obj1.score[key] !== obj2.score[key]) {
log(`Diff at ${key}: ${obj1.score[key]} vs ${obj2.score[key]}`);
returnValue = true;
}
}
return returnValue;
};
const copyFile = (source, target) => {
log(`Copying ${source} to ${target}`);
fs.copyFileSync(source, target);
};
const main = () => {
const files = fs.readdirSync(sourceDir).filter(isJsonFile).sort();
let previousJson = null;
for (const file of files) {
const filePath = path.join(sourceDir, file);
log(`Reading ${filePath}`);
const currentJson = readJsonFile(filePath);
if (previousJson && compareJsonObjects(previousJson, currentJson)) {
const targetPath = path.join(targetDir, file);
copyFile(filePath, targetPath);
}
previousJson = currentJson;
}
};
main();