forked from mapbox/mapbox-studio-classic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.js
37 lines (31 loc) · 1.01 KB
/
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
module.exports = {};
module.exports.get = get;
module.exports.set = set;
module.exports.cookie = cookie;
// Get stats.
function get(source, key) {
source.stats = source.stats || {};
source.stats[key] = source.stats[key] || {};
return source.stats[key];
}
// Set stats for a given zoom level.
function set(source, key, z, val) {
source.stats = source.stats || {};
source.stats[key] = source.stats[key] || {};
source.stats[key][z] = source.stats[key][z] || { count:0 };
var stats = source.stats[key][z];
stats.min = Math.min(val, stats.min||Infinity);
stats.max = Math.max(val, stats.max||0);
stats.avg = stats.count ? ((stats.avg * stats.count) + val) / (stats.count + 1) : val;
stats.count++;
return;
}
// Serialize stats into a cookie.
function cookie(source, key) {
var stats = get(source, key);
var serialized = [];
for (var z in stats) {
serialized.push([z,stats[z].min,stats[z].avg|0,stats[z].max].join('-'));
}
return serialized.join('.');
}