forked from mapbox/mapbox-studio-classic
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split drawtime/srcbytes stat handling + cookie encoding into its own …
…lib with tests.
- Loading branch information
Showing
5 changed files
with
84 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
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('.'); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
var test = require('tape'); | ||
var stats = require('../lib/stats'); | ||
|
||
test('stats.set/get', function(t) { | ||
var source = {}; | ||
t.equal(stats.set(source, 'drawtime', 0, 100), undefined, 'set'); | ||
t.deepEqual(stats.get(source, 'drawtime'), { | ||
0: { count:1, avg:100, min:100, max:100 } | ||
}, 'sets stats'); | ||
|
||
t.equal(stats.set(source, 'drawtime', 0, 200), undefined, 'set'); | ||
t.deepEqual(stats.get(source, 'drawtime'), { | ||
0: { count:2, avg:150, min:100, max:200 } | ||
}, 'calculates avg/min/max'); | ||
|
||
t.equal(stats.set(source, 'drawtime', 1, 200), undefined, 'set'); | ||
t.deepEqual(stats.get(source, 'drawtime'), { | ||
0: { count:2, avg:150, min:100, max:200 }, | ||
1: { count:1, avg:200, min:200, max:200 } | ||
}, 'segments by zoom'); | ||
|
||
t.equal(stats.set(source, 'srcbytes', 0, 200), undefined, 'set'); | ||
t.deepEqual(stats.get(source, 'drawtime'), { | ||
0: { count:2, avg:150, min:100, max:200 }, | ||
1: { count:1, avg:200, min:200, max:200 } | ||
}, 'segments by key'); | ||
t.deepEqual(stats.get(source, 'srcbytes'), { | ||
0: { count:1, avg:200, min:200, max:200 } | ||
}, 'segments by key'); | ||
t.end(); | ||
}); | ||
|
||
test('stats.cookie', function(t) { | ||
var source = {}; | ||
t.equal(stats.set(source, 'drawtime', 0, 100), undefined, 'set'); | ||
t.equal(stats.set(source, 'drawtime', 1, 200), undefined, 'set'); | ||
t.equal(stats.cookie(source, 'drawtime'), '0-100-100-100.1-200-200-200', 'serializes cookie'); | ||
t.end(); | ||
}); | ||
|