-
Notifications
You must be signed in to change notification settings - Fork 44
/
getstats-deltacompression.js
45 lines (45 loc) · 1.32 KB
/
getstats-deltacompression.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
module.exports = {
decompress: function(baseStats, newStats) {
const timestamp = newStats.timestamp
delete newStats.timestamp;
Object.keys(newStats).forEach(id => {
if (!baseStats[id]) {
if (newStats[id].timestamp === 0) {
newStats[id].timestamp = timestamp;
}
baseStats[id] = newStats[id];
} else {
const report = newStats[id];
if (report.timestamp === 0) {
report.timestamp = timestamp;
} else if (!report.timestamp) {
report.timestamp = new Date(baseStats[id].timestamp).getTime();
}
Object.keys(report).forEach(name => {
baseStats[id][name] = report[name];
});
}
});
return baseStats;
},
compress: function(baseStats, newStats) {
Object.keys(newStats).forEach(id => {
if (!baseStats[id]) {
return;
}
const report = newStats[id];
Object.keys(report).forEach(name => {
if (report[name] === baseStats[id][name]) {
delete newStats[id][name];
}
delete report.timestamp;
if (Object.keys(report).length === 0) {
delete newStats[id];
}
});
});
// TODO: moving the timestamp to the top-level is not compression but...
newStats.timestamp = new Date();
return newStats;
}
};