Skip to content

Commit

Permalink
Split drawtime/srcbytes stat handling + cookie encoding into its own …
Browse files Browse the repository at this point in the history
…lib with tests.
  • Loading branch information
yhahn committed Sep 7, 2014
1 parent 39c344b commit 55765b8
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 31 deletions.
1 change: 1 addition & 0 deletions app/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ var statHandler = function(key) {
return _(function() {
if (document.cookie.indexOf(key) === -1) return;
var max = 300;

var stats = _(document.cookie
.split(key + '=').pop()
.split(';').shift()
Expand Down
21 changes: 6 additions & 15 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var fs = require('fs');
var url = require('url');
var source = require('./source');
var style = require('./style');
var stats = require('./stats');
var middleware = require('./middleware');
var express = require('express');
var cors = require('cors');
Expand Down Expand Up @@ -175,21 +176,11 @@ function tile(req, res, next) {
return next(err);
}

// Set drawtime cookie for a given style.
style.stats(id, 'drawtime', z, data._drawtime);
res.cookie('drawtime', _(style.stats(id, 'drawtime'))
.reduce(function(memo, stat, z) {
memo.push([z,stat.min,stat.avg|0,stat.max].join('-'));
return memo;
}, []).join('.'));

// Set srcbytes cookie for a given style.
style.stats(id, 'srcbytes', z, data._srcbytes);
res.cookie('srcbytes', _(style.stats(id, 'srcbytes')).
reduce(function(memo, stat, z) {
memo.push([z,stat.min,stat.avg|0,stat.max].join('-'));
return memo;
}, []).join('.'));
// Set drawtime/srcbytes cookies.
stats.set(source, 'drawtime', z, data._drawtime);
stats.set(source, 'srcbytes', z, data._srcbytes);
res.cookie('drawtime', stats.cookie(source, 'drawtime'));
res.cookie('srcbytes', stats.cookie(source, 'srcbytes'));

// Clear out tile errors.
res.cookie('errors', '');
Expand Down
37 changes: 37 additions & 0 deletions lib/stats.js
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('.');
}

16 changes: 0 additions & 16 deletions lib/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -442,22 +442,6 @@ style.toPackage = function(id, dest, callback) {
}
};

// Set or get stats for a given zoom level.
style.stats = function(id, key, z, val) {
if (!cache[id]) return false;
if ('number' === typeof z && val) {
cache[id].stats = cache[id].stats || {};
cache[id].stats[key] = cache[id].stats[key] || {};
cache[id].stats[key][z] = cache[id].stats[key][z] || { count:0 };
var stats = cache[id].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 cache[id].stats[key];
};

// Set or get tile serving errors.
style.error = function(id, err) {
if (!cache[id]) return false;
Expand Down
40 changes: 40 additions & 0 deletions test/stats.test.js
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();
});

0 comments on commit 55765b8

Please sign in to comment.