Increment map views on static preview images

This commit is contained in:
Raul Ochoa 2015-09-29 12:21:11 +02:00
parent f33c3ce21a
commit f2278d47a5
5 changed files with 132 additions and 5 deletions

View File

@ -11,7 +11,7 @@ var cors = require('../middleware/cors');
var TablesCacheEntry = require('../cache/model/database_tables_entry'); var TablesCacheEntry = require('../cache/model/database_tables_entry');
function NamedMapsController(app, authApi, pgConnection, namedMapProviderCache, tileBackend, previewBackend, function NamedMapsController(app, authApi, pgConnection, namedMapProviderCache, tileBackend, previewBackend,
surrogateKeysCache, tablesExtentApi) { surrogateKeysCache, tablesExtentApi, metadataBackend) {
BaseController.call(this, authApi, pgConnection); BaseController.call(this, authApi, pgConnection);
this.app = app; this.app = app;
@ -20,6 +20,7 @@ function NamedMapsController(app, authApi, pgConnection, namedMapProviderCache,
this.previewBackend = previewBackend; this.previewBackend = previewBackend;
this.surrogateKeysCache = surrogateKeysCache; this.surrogateKeysCache = surrogateKeysCache;
this.tablesExtentApi = tablesExtentApi; this.tablesExtentApi = tablesExtentApi;
this.metadataBackend = metadataBackend;
} }
util.inherits(NamedMapsController, BaseController); util.inherits(NamedMapsController, BaseController);
@ -156,6 +157,19 @@ NamedMapsController.prototype.staticMap = function(req, res) {
namedMapProvider, format, width, height, imageOpts.bounds, this); namedMapProvider, format, width, height, imageOpts.bounds, this);
} }
}, },
function incrementMapViews(err, image, headers, stats) {
assert.ifError(err);
var next = this;
namedMapProvider.getMapConfig(function(mapConfigErr, mapConfig) {
self.metadataBackend.incMapviewCount(cdbUser, mapConfig.obj().stat_tag, function(sErr) {
if (err) {
global.logger.log("ERROR: failed to increment mapview count for user '%s': %s", cdbUser, sErr);
}
next(err, image, headers, stats);
});
});
},
function handleImage(err, image, headers, stats) { function handleImage(err, image, headers, stats) {
if (req.profiler) { if (req.profiler) {
req.profiler.done('render-' + format); req.profiler.done('render-' + format);

View File

@ -197,7 +197,8 @@ module.exports = function(serverOptions) {
tileBackend, tileBackend,
previewBackend, previewBackend,
surrogateKeysCache, surrogateKeysCache,
tablesExtentApi tablesExtentApi,
metadataBackend
).register(app); ).register(app);
new controller.NamedMapsAdmin(templateMaps, authApi, pgConnection).register(app); new controller.NamedMapsAdmin(templateMaps, authApi, pgConnection).register(app);

View File

@ -261,7 +261,7 @@ describe('named maps authentication', function() {
assert.equal(img.height(), 480); assert.equal(img.height(), 480);
test_helper.checkSurrogateKey(res, new NamedMapsCacheEntry(username, tokenAuthTemplateName).key()); test_helper.checkSurrogateKey(res, new NamedMapsCacheEntry(username, tokenAuthTemplateName).key());
done(); test_helper.deleteRedisKeys({'user:localhost:mapviews:global': 5}, done);
}); });
}); });
}); });

View File

@ -1,4 +1,4 @@
require('../support/test_helper'); var testHelper = require('../support/test_helper');
var RedisPool = require('redis-mpool'); var RedisPool = require('redis-mpool');
var assert = require('../support/assert'); var assert = require('../support/assert');
@ -77,8 +77,10 @@ describe('named maps static view', function() {
var server = new CartodbWindshaft(serverOptions); var server = new CartodbWindshaft(serverOptions);
assert.response(server, requestOptions, expectedResponse, function (res, err) { assert.response(server, requestOptions, expectedResponse, function (res, err) {
testHelper.deleteRedisKeys({'user:localhost:mapviews:global': 5}, function() {
return callback(err, mapnik.Image.fromBytes(new Buffer(res.body, 'binary'))); return callback(err, mapnik.Image.fromBytes(new Buffer(res.body, 'binary')));
}); });
});
} }
function previewFixture(version) { function previewFixture(version) {

View File

@ -0,0 +1,110 @@
var test_helper = require('../support/test_helper');
var RedisPool = require('redis-mpool');
var querystring = require('querystring');
var assert = require('../support/assert');
var mapnik = require('windshaft').mapnik;
var CartodbWindshaft = require(__dirname + '/../../lib/cartodb/server');
var serverOptions = require(__dirname + '/../../lib/cartodb/server_options');
var server = new CartodbWindshaft(serverOptions);
var TemplateMaps = require('../../lib/cartodb/backends/template_maps.js');
var NamedMapsCacheEntry = require('../../lib/cartodb/cache/model/named_maps_entry');
describe('named maps preview stats', function() {
var redisPool = new RedisPool(global.environment.redis);
var templateMaps = new TemplateMaps(redisPool, {
max_user_templates: global.environment.maxUserTemplates
});
var username = 'localhost';
var statTag = 'wadus_viz';
var templateName = 'with_stats';
var template = {
version: '0.0.1',
name: templateName,
auth: {
method: 'open'
},
"placeholders": {
"color": {
"type": "css_color",
"default": "#cc3300"
}
},
layergroup: {
stat_tag: statTag,
layers: [
{
type: 'cartodb',
options: {
sql: 'select 1 cartodb_id, null::geometry the_geom_webmercator',
cartocss: '#layer { marker-fill: <%= color %>; }',
cartocss_version: '2.3.0'
}
}
]
}
};
beforeEach(function (done) {
templateMaps.addTemplate(username, template, done);
});
afterEach(function (done) {
templateMaps.delTemplate(username, templateName, done);
});
function getStaticMap(name, options, callback) {
var url = '/api/v1/map/static/named/' + name + '/640/480.png';
if (options.params) {
url = url + '?' + querystring.stringify(options.params);
}
var requestOptions = {
url: url,
method: 'GET',
headers: {
host: username
},
encoding: 'binary'
};
var statusCode = options.status || 200;
var expectedResponse = {
status: statusCode,
headers: {
'Content-Type': statusCode === 200 ? 'image/png' : 'application/json; charset=utf-8'
}
};
assert.response(server,
requestOptions,
expectedResponse,
function (res, err) {
var img;
if (!err && res.headers['content-type'] === 'image/png') {
img = mapnik.Image.fromBytes(new Buffer(res.body, 'binary'));
}
return callback(err, res, img);
}
);
}
it('should return 200 if properly authorized', function(done) {
getStaticMap(templateName, { params: { auth_token: 'valid1' } }, function(err, res, img) {
assert.ok(!err);
assert.equal(img.width(), 640);
assert.equal(img.height(), 480);
test_helper.checkSurrogateKey(res, new NamedMapsCacheEntry(username, templateName).key());
var redisKeysToDelete = { 'user:localhost:mapviews:global': 5 };
redisKeysToDelete['user:localhost:mapviews:stat_tag:' + statTag] = 5;
test_helper.deleteRedisKeys(redisKeysToDelete, done);
});
});
});