Move incrementMapViews to a middlewares

This commit is contained in:
Daniel García Aubert 2017-12-30 16:04:24 +01:00
parent 731fe4c00f
commit f13b45862d

View File

@ -42,7 +42,8 @@ NamedMapsController.prototype.register = function(app) {
this.prepareLayerFilterFromPreviewLayers(),
this.getStaticImageOptions(),
this.getImage(),
this.staticMap.bind(this)
this.incrementMapViews(),
this.handleImage()
);
};
@ -163,39 +164,6 @@ NamedMapsController.prototype.tile = function(req, res, next) {
});
};
NamedMapsController.prototype.staticMap = function(req, res, next) {
var self = this;
var cdbUser = res.locals.user;
const { namedMapProvider, image, headers, stats } = res.locals;
step(
function incrementMapViews() {
var next = this;
namedMapProvider.getMapConfig(function(mapConfigErr, mapConfig) {
self.metadataBackend.incMapviewCount(cdbUser, mapConfig.obj().stat_tag, function(sErr) {
if (sErr) {
global.logger.log("ERROR: failed to increment mapview count for user '%s': %s", cdbUser, sErr);
}
next(null, image, headers, stats);
});
});
},
function handleImage(err, image, headers, stats) {
req.profiler.done('render-' + res.locals.format);
req.profiler.add(stats || {});
if (err) {
err.label = 'STATIC_VIZ_MAP';
next(err);
} else {
self.sendResponse(req, res, image, headers, namedMapProvider);
}
}
);
};
var DEFAULT_ZOOM_CENTER = {
zoom: 1,
@ -343,6 +311,42 @@ NamedMapsController.prototype.getImage = function () {
}.bind(this);
};
const incrementMapViewsError = ctx => `ERROR: failed to increment mapview count for user '${ctx.user}': ${ctx.err}`;
NamedMapsController.prototype.incrementMapViews = function () {
return function incrementMapViewsMiddleware(req, res, next) {
const { user, namedMapProvider } = res.locals;
namedMapProvider.getMapConfig((err, mapConfig) => {
if (err) {
global.logger.log(incrementMapViewsError({ user, err }));
return next();
}
const statTag = mapConfig.obj().stat_tag;
this.metadataBackend.incMapviewCount(user, statTag, (err) => {
if (err) {
global.logger.log(incrementMapViewsError({ user, err }));
}
next();
});
});
}.bind(this);
};
NamedMapsController.prototype.handleImage = function () {
return function handleImageMiddleware (req, res) {
const { namedMapProvider, image, headers, stats = {}, format } = res.locals;
req.profiler.done('render-' + format);
req.profiler.add(stats);
this.sendResponse(req, res, image, headers, namedMapProvider);
}.bind(this);
};
function templateZoomCenter(view) {
if (!_.isUndefined(view.zoom) && view.center) {
return {