Adds histogram support, a bit of code duplication

This commit is contained in:
Raul Ochoa 2015-10-26 11:10:59 +01:00
parent 36a6af3266
commit 4b0ecb1251
2 changed files with 148 additions and 0 deletions

View File

@ -73,6 +73,9 @@ LayergroupController.prototype.register = function(app) {
'/:token/:layer/list/:listName', cors(), userMiddleware,
this.list.bind(this));
app.get(app.base_url_mapconfig +
'/:token/:layer/histogram/:histogramName', cors(), userMiddleware,
this.histogram.bind(this));
};
LayergroupController.prototype.list = function(req, res) {
@ -103,6 +106,34 @@ LayergroupController.prototype.list = function(req, res) {
};
LayergroupController.prototype.histogram = function(req, res) {
var self = this;
step(
function setupParams() {
self.req2params(req, this);
},
function retrieveList(err) {
assert.ifError(err);
var mapConfigProvider = new MapStoreMapConfigProvider(
self.mapStore, req.context.user, self.userLimitsApi, req.params
);
self.widgetBackend.getHistogram(mapConfigProvider, req.params, this);
},
function finish(err, tile, stats) {
req.profiler.add(stats || {});
if (err) {
self.sendError(req, res, err, 'GET LIST');
} else {
self.sendResponse(req, res, tile, 200);
}
}
);
};
LayergroupController.prototype.attributes = function(req, res) {
var self = this;

View File

@ -0,0 +1,117 @@
var assert = require('../support/assert');
var step = require('step');
var helper = require(__dirname + '/../support/test_helper');
var LayergroupToken = require('../../lib/cartodb/models/layergroup_token');
var CartodbWindshaft = require('../../lib/cartodb/server');
var serverOptions = require('../../lib/cartodb/server_options');
var server = new CartodbWindshaft(serverOptions);
server.setMaxListeners(0);
describe('lists', function() {
var keysToDelete;
beforeEach(function() {
keysToDelete = {};
});
afterEach(function(done) {
helper.deleteRedisKeys(keysToDelete, done);
});
it("should expose layer histogram", function(done) {
var layergroup = {
version: '1.5.0',
layers: [
{
type: 'mapnik',
options: {
sql: 'select * from populated_places_simple_reduced',
cartocss: '#layer { marker-fill: red; marker-width: 32; marker-allow-overlap: true; }',
cartocss_version: '2.3.0'
},
widgets: {
pop_max: {
type: 'histogram',
options: {
column: 'pop_max'
}
}
}
}
]
};
var layergroupId;
step(
function createLayergroup() {
var next = this;
assert.response(server,
{
url: '/api/v1/map',
method: 'POST',
headers: {
host: 'localhost',
'Content-Type': 'application/json'
},
data: JSON.stringify(layergroup)
},
{
status: 200,
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
},
function(res, err) {
if (err) {
return next(err);
}
return next(null, JSON.parse(res.body).layergroupid);
}
);
},
function getList(err, _layergroupId) {
assert.ifError(err);
var next = this;
layergroupId = _layergroupId;
assert.response(server,
{
url: '/api/v1/map/' + layergroupId + '/0/histogram/pop_max',
method: 'GET',
headers: {
host: 'localhost'
}
},
{
status: 200,
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
},
function(res, err) {
if (err) {
return next(err);
}
console.log(JSON.parse(res.body));
next(null);
}
);
},
function finish(err) {
keysToDelete['map_cfg|' + LayergroupToken.parse(layergroupId).token] = 0;
keysToDelete['user:localhost:mapviews:global'] = 5;
done(err);
}
);
});
});