Windshaft-cartodb/lib/cartodb/controllers/layergroup.js

694 lines
22 KiB
JavaScript
Raw Normal View History

2018-03-07 22:01:04 +08:00
const cors = require('../middleware/cors');
const userMiddleware = require('../middleware/user');
const vectorError = require('../middleware/vector-error');
const locals = require('../middleware/locals');
const cleanUpQueryParams = require('../middleware/clean-up-query-params');
const layergroupToken = require('../middleware/layergroup-token');
const credentials = require('../middleware/credentials');
const dbConnSetup = require('../middleware/db-conn-setup');
const authorize = require('../middleware/authorize');
2018-03-07 22:01:04 +08:00
const DataviewBackend = require('../backends/dataview');
const AnalysisStatusBackend = require('../backends/analysis-status');
const MapStoreMapConfigProvider = require('../models/mapconfig/provider/map-store-provider');
const QueryTables = require('cartodb-query-tables');
2018-03-08 19:23:00 +08:00
const SUPPORTED_FORMATS = {
grid_json: true,
json_torque: true,
torque_json: true,
png: true,
png32: true,
mvt: true
};
/**
2018-03-07 21:54:09 +08:00
* @param {prepareContext} prepareContext
* @param {PgConnection} pgConnection
* @param {MapStore} mapStore
* @param {TileBackend} tileBackend
* @param {PreviewBackend} previewBackend
* @param {AttributesBackend} attributesBackend
* @param {SurrogateKeysCache} surrogateKeysCache
* @param {UserLimitsApi} userLimitsApi
* @param {LayergroupAffectedTables} layergroupAffectedTables
* @param {AnalysisBackend} analysisBackend
* @constructor
*/
function LayergroupController(pgConnection, mapStore, tileBackend, previewBackend, attributesBackend,
surrogateKeysCache, userLimitsApi, layergroupAffectedTables, analysisBackend, authApi) {
2016-02-22 18:40:25 +08:00
this.pgConnection = pgConnection;
this.mapStore = mapStore;
this.tileBackend = tileBackend;
this.previewBackend = previewBackend;
this.attributesBackend = attributesBackend;
this.surrogateKeysCache = surrogateKeysCache;
2015-07-11 01:10:55 +08:00
this.userLimitsApi = userLimitsApi;
this.layergroupAffectedTables = layergroupAffectedTables;
this.dataviewBackend = new DataviewBackend(analysisBackend);
this.analysisStatusBackend = new AnalysisStatusBackend();
this.authApi = authApi;
}
module.exports = LayergroupController;
2017-10-05 18:12:21 +08:00
LayergroupController.prototype.register = function(app) {
2018-03-16 20:04:42 +08:00
const { base_url_mapconfig: mapconfigBasePath } = app;
2018-03-07 19:05:53 +08:00
2017-10-05 18:12:21 +08:00
app.get(
2018-03-16 20:04:42 +08:00
`${mapconfigBasePath}/:token/:z/:x/:y@:scale_factor?x.:format`,
cors(),
userMiddleware(),
locals(),
cleanUpQueryParams(),
layergroupToken(),
credentials(),
authorize(this.authApi),
dbConnSetup(this.pgConnection),
createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi),
2018-03-07 22:39:59 +08:00
getTile(this.tileBackend, 'map_tile'),
setCacheControlHeader(),
setLastModifiedHeader(),
2018-03-12 20:29:40 +08:00
getAffectedTables(this.layergroupAffectedTables, this.pgConnection, this.mapStore),
setCacheChannelHeader(),
setSurrogateKeyHeader(this.surrogateKeysCache),
incrementSuccessMetrics(global.statsClient),
sendResponse(),
incrementErrorMetrics(global.statsClient),
tileError(),
2017-11-07 18:07:38 +08:00
vectorError()
);
2017-10-05 18:12:21 +08:00
app.get(
2018-03-16 20:04:42 +08:00
`${mapconfigBasePath}/:token/:z/:x/:y.:format`,
2017-10-05 18:12:21 +08:00
cors(),
userMiddleware(),
locals(),
cleanUpQueryParams(),
layergroupToken(),
credentials(),
authorize(this.authApi),
dbConnSetup(this.pgConnection),
createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi),
2018-03-07 22:39:59 +08:00
getTile(this.tileBackend, 'map_tile'),
setCacheControlHeader(),
setLastModifiedHeader(),
2018-03-12 20:29:40 +08:00
getAffectedTables(this.layergroupAffectedTables, this.pgConnection, this.mapStore),
setCacheChannelHeader(),
setSurrogateKeyHeader(this.surrogateKeysCache),
incrementSuccessMetrics(global.statsClient),
sendResponse(),
incrementErrorMetrics(global.statsClient),
tileError(),
2017-11-07 18:07:38 +08:00
vectorError()
);
2017-10-05 18:12:21 +08:00
app.get(
2018-03-16 20:04:42 +08:00
`${mapconfigBasePath}/:token/:layer/:z/:x/:y.(:format)`,
2018-03-07 22:56:16 +08:00
distinguishLayergroupFromStaticRoute(),
2017-10-05 18:12:21 +08:00
cors(),
userMiddleware(),
locals(),
cleanUpQueryParams(),
layergroupToken(),
credentials(),
authorize(this.authApi),
dbConnSetup(this.pgConnection),
createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi),
2018-03-07 22:39:59 +08:00
getTile(this.tileBackend, 'maplayer_tile'),
setCacheControlHeader(),
setLastModifiedHeader(),
2018-03-12 20:29:40 +08:00
getAffectedTables(this.layergroupAffectedTables, this.pgConnection, this.mapStore),
setCacheChannelHeader(),
setSurrogateKeyHeader(this.surrogateKeysCache),
incrementSuccessMetrics(global.statsClient),
sendResponse(),
incrementErrorMetrics(global.statsClient),
tileError(),
2017-11-07 18:07:38 +08:00
vectorError()
);
2017-10-05 18:12:21 +08:00
app.get(
2018-03-16 20:04:42 +08:00
`${mapconfigBasePath}/:token/:layer/attributes/:fid`,
2017-10-05 18:12:21 +08:00
cors(),
userMiddleware(),
locals(),
cleanUpQueryParams(),
layergroupToken(),
credentials(),
authorize(this.authApi),
dbConnSetup(this.pgConnection),
createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi),
2018-03-08 01:41:41 +08:00
getFeatureAttributes(this.attributesBackend),
setCacheControlHeader(),
setLastModifiedHeader(),
2018-03-12 20:29:40 +08:00
getAffectedTables(this.layergroupAffectedTables, this.pgConnection, this.mapStore),
setCacheChannelHeader(),
setSurrogateKeyHeader(this.surrogateKeysCache),
sendResponse()
);
const forcedFormat = 'png';
2017-10-05 18:12:21 +08:00
app.get(
2018-03-16 20:04:42 +08:00
`${mapconfigBasePath}/static/center/:token/:z/:lat/:lng/:width/:height.:format`,
2017-10-05 18:12:21 +08:00
cors(),
userMiddleware(),
locals(),
cleanUpQueryParams(['layer']),
layergroupToken(),
credentials(),
authorize(this.authApi),
dbConnSetup(this.pgConnection),
createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi, forcedFormat),
2018-03-08 01:51:43 +08:00
getPreviewImageByCenter(this.previewBackend),
setCacheControlHeader(),
setLastModifiedHeader(),
2018-03-12 20:29:40 +08:00
getAffectedTables(this.layergroupAffectedTables, this.pgConnection, this.mapStore),
setCacheChannelHeader(),
setSurrogateKeyHeader(this.surrogateKeysCache),
sendResponse()
);
2017-10-05 18:12:21 +08:00
app.get(
2018-03-16 20:04:42 +08:00
`${mapconfigBasePath}/static/bbox/:token/:west,:south,:east,:north/:width/:height.:format`,
2017-10-05 18:12:21 +08:00
cors(),
userMiddleware(),
locals(),
cleanUpQueryParams(['layer']),
layergroupToken(),
credentials(),
authorize(this.authApi),
dbConnSetup(this.pgConnection),
createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi, forcedFormat),
2018-03-08 01:51:43 +08:00
getPreviewImageByBoundingBox(this.previewBackend),
setCacheControlHeader(),
setLastModifiedHeader(),
2018-03-12 20:29:40 +08:00
getAffectedTables(this.layergroupAffectedTables, this.pgConnection, this.mapStore),
setCacheChannelHeader(),
setSurrogateKeyHeader(this.surrogateKeysCache),
sendResponse()
);
2015-10-07 01:47:44 +08:00
// Undocumented/non-supported API endpoint methods.
// Use at your own peril.
2018-03-07 19:09:41 +08:00
const allowedDataviewQueryParams = [
'filters', // json
'own_filter', // 0, 1
2017-12-12 18:54:09 +08:00
'no_filters', // 0, 1
'bbox', // w,s,e,n
'start', // number
'end', // number
'column_type', // string
'bins', // number
'aggregation', //string
2017-07-15 00:30:36 +08:00
'offset', // number
'q', // widgets search
'categories', // number
];
2017-10-05 18:12:21 +08:00
app.get(
2018-03-16 20:04:42 +08:00
`${mapconfigBasePath}/:token/dataview/:dataviewName`,
2017-10-05 18:12:21 +08:00
cors(),
userMiddleware(),
locals(),
cleanUpQueryParams(allowedDataviewQueryParams),
layergroupToken(),
credentials(),
authorize(this.authApi),
dbConnSetup(this.pgConnection),
createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi),
getDataview(this.dataviewBackend),
setCacheControlHeader(),
setLastModifiedHeader(),
2018-03-12 20:29:40 +08:00
getAffectedTables(this.layergroupAffectedTables, this.pgConnection, this.mapStore),
setCacheChannelHeader(),
setSurrogateKeyHeader(this.surrogateKeysCache),
sendResponse()
);
2017-10-05 18:12:21 +08:00
app.get(
2018-03-16 20:04:42 +08:00
`${mapconfigBasePath}/:token/:layer/widget/:dataviewName`,
2017-10-05 18:12:21 +08:00
cors(),
userMiddleware(),
locals(),
cleanUpQueryParams(allowedDataviewQueryParams),
layergroupToken(),
credentials(),
authorize(this.authApi),
dbConnSetup(this.pgConnection),
createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi),
getDataview(this.dataviewBackend),
setCacheControlHeader(),
setLastModifiedHeader(),
2018-03-12 20:29:40 +08:00
getAffectedTables(this.layergroupAffectedTables, this.pgConnection, this.mapStore),
setCacheChannelHeader(),
setSurrogateKeyHeader(this.surrogateKeysCache),
sendResponse()
);
2017-10-05 18:12:21 +08:00
app.get(
2018-03-16 20:04:42 +08:00
`${mapconfigBasePath}/:token/dataview/:dataviewName/search`,
2017-10-05 18:12:21 +08:00
cors(),
userMiddleware(),
locals(),
cleanUpQueryParams(allowedDataviewQueryParams),
layergroupToken(),
credentials(),
authorize(this.authApi),
dbConnSetup(this.pgConnection),
createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi),
dataviewSearch(this.dataviewBackend),
setCacheControlHeader(),
setLastModifiedHeader(),
2018-03-12 20:29:40 +08:00
getAffectedTables(this.layergroupAffectedTables, this.pgConnection, this.mapStore),
setCacheChannelHeader(),
setSurrogateKeyHeader(this.surrogateKeysCache),
sendResponse()
);
2017-10-05 18:12:21 +08:00
app.get(
2018-03-16 20:04:42 +08:00
`${mapconfigBasePath}/:token/:layer/widget/:dataviewName/search`,
2017-10-05 18:12:21 +08:00
cors(),
userMiddleware(),
locals(),
cleanUpQueryParams(allowedDataviewQueryParams),
layergroupToken(),
credentials(),
authorize(this.authApi),
dbConnSetup(this.pgConnection),
createMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi),
dataviewSearch(this.dataviewBackend),
setCacheControlHeader(),
setLastModifiedHeader(),
2018-03-12 20:29:40 +08:00
getAffectedTables(this.layergroupAffectedTables, this.pgConnection, this.mapStore),
setCacheChannelHeader(),
setSurrogateKeyHeader(this.surrogateKeysCache),
sendResponse()
);
2016-04-12 00:49:56 +08:00
2017-10-05 18:12:21 +08:00
app.get(
2018-03-16 20:04:42 +08:00
`${mapconfigBasePath}/:token/analysis/node/:nodeId`,
2017-10-05 18:12:21 +08:00
cors(),
userMiddleware(),
locals(),
cleanUpQueryParams(),
layergroupToken(),
credentials(),
authorize(this.authApi),
dbConnSetup(this.pgConnection),
analysisNodeStatus(this.analysisStatusBackend),
sendResponse()
);
2016-04-12 00:49:56 +08:00
};
2018-03-07 22:56:16 +08:00
function distinguishLayergroupFromStaticRoute () {
return function distinguishLayergroupFromStaticRouteMiddleware(req, res, next) {
2018-03-07 21:48:21 +08:00
if (req.params.token === 'static') {
return next('route');
}
2018-03-07 21:48:21 +08:00
next();
};
}
function analysisNodeStatus (analysisStatusBackend) {
return function analysisNodeStatusMiddleware(req, res, next) {
2018-03-07 19:46:18 +08:00
analysisStatusBackend.getNodeStatus(res.locals, (err, nodeStatus, stats = {}) => {
req.profiler.add(stats);
if (err) {
err.label = 'GET NODE STATUS';
return next(err);
}
res.set({
'Cache-Control': 'public,max-age=5',
'Last-Modified': new Date().toUTCString()
});
res.body = nodeStatus;
next();
});
2018-03-07 03:05:55 +08:00
};
}
function getRequestParams(locals) {
const params = Object.assign({}, locals);
delete params.mapConfigProvider;
delete params.allowedQueryParams;
return params;
}
function createMapStoreMapConfigProvider (mapStore, userLimitsApi, forcedFormat = null) {
return function createMapStoreMapConfigProviderMiddleware (req, res, next) {
2018-03-06 00:44:04 +08:00
const { user } = res.locals;
const params = getRequestParams(res.locals);
if (forcedFormat) {
params.format = forcedFormat;
params.layer = params.layer || 'all';
}
2018-03-12 20:29:40 +08:00
res.locals.mapConfigProvider = new MapStoreMapConfigProvider(mapStore, user, userLimitsApi, params);
2018-03-06 00:44:04 +08:00
2018-03-12 20:29:40 +08:00
next();
2018-03-06 00:44:04 +08:00
};
}
2018-03-06 00:44:04 +08:00
function getDataview (dataviewBackend) {
2018-03-06 00:44:04 +08:00
return function getDataviewMiddleware (req, res, next) {
const { user, mapConfigProvider } = res.locals;
const params = getRequestParams(res.locals);
2018-03-06 00:44:04 +08:00
dataviewBackend.getDataview(mapConfigProvider, user, params, (err, dataview, stats = {}) => {
2018-03-07 19:46:18 +08:00
req.profiler.add(stats);
if (err) {
err.label = 'GET DATAVIEW';
2018-03-06 00:44:04 +08:00
return next(err);
}
2018-03-06 00:44:04 +08:00
res.body = dataview;
next();
2018-03-06 00:44:04 +08:00
});
2018-03-07 03:05:55 +08:00
};
}
function dataviewSearch (dataviewBackend) {
2018-03-07 22:05:36 +08:00
return function dataviewSearchMiddleware (req, res, next) {
2018-03-06 01:05:42 +08:00
const { user, dataviewName, mapConfigProvider } = res.locals;
const params = getRequestParams(res.locals);
dataviewBackend.search(mapConfigProvider, user, dataviewName, params, (err, searchResult, stats = {}) => {
2018-03-07 19:46:18 +08:00
req.profiler.add(stats);
if (err) {
err.label = 'GET DATAVIEW SEARCH';
2018-03-06 01:05:42 +08:00
return next(err);
}
res.body = searchResult;
next();
2018-03-06 01:05:42 +08:00
});
2018-03-07 03:05:55 +08:00
};
}
2018-03-08 01:41:41 +08:00
function getFeatureAttributes (attributesBackend) {
return function getFeatureAttributesMiddleware (req, res, next) {
2018-03-06 01:28:52 +08:00
req.profiler.start('windshaft.maplayer_attribute');
2018-03-06 01:28:52 +08:00
const { mapConfigProvider } = res.locals;
const params = getRequestParams(res.locals);
attributesBackend.getFeatureAttributes(mapConfigProvider, params, false, (err, tile, stats = {}) => {
2018-03-07 19:46:18 +08:00
req.profiler.add(stats);
if (err) {
err.label = 'GET ATTRIBUTES';
2018-03-06 01:28:52 +08:00
return next(err);
}
res.body = tile;
next();
2018-03-06 01:28:52 +08:00
});
2018-03-07 03:05:55 +08:00
};
}
2018-03-06 23:19:53 +08:00
function getStatusCode(tile, format){
return tile.length === 0 && format === 'mvt'? 204 : 200;
2018-03-06 23:19:53 +08:00
}
function parseFormat (format = '') {
2018-03-06 23:19:53 +08:00
const prettyFormat = format.replace('.', '_');
2018-03-08 19:27:49 +08:00
return SUPPORTED_FORMATS[prettyFormat] ? prettyFormat : 'invalid';
}
2018-03-07 22:39:59 +08:00
function getTile (tileBackend, profileLabel = 'tile') {
return function getTileMiddleware (req, res, next) {
req.profiler.start(`windshaft.${profileLabel}`);
2018-03-06 23:19:53 +08:00
const { mapConfigProvider } = res.locals;
const params = getRequestParams(res.locals);
2018-03-07 19:46:18 +08:00
tileBackend.getTile(mapConfigProvider, params, (err, tile, headers, stats = {}) => {
2018-03-06 23:19:53 +08:00
req.profiler.add(stats);
if (err) {
return next(err);
2018-03-06 23:19:53 +08:00
}
if (headers) {
res.set(headers);
}
const formatStat = parseFormat(req.params.format);
res.statusCode = getStatusCode(tile, formatStat);
res.body = tile;
next();
2018-03-06 23:19:53 +08:00
});
2018-03-07 03:05:55 +08:00
};
}
2018-03-08 01:51:43 +08:00
function getPreviewImageByCenter (previewBackend) {
return function getPreviewImageByCenterMiddleware (req, res, next) {
const width = +req.params.width;
const height = +req.params.height;
const zoom = +req.params.z;
const center = {
lng: +req.params.lng,
lat: +req.params.lat
};
const format = req.params.format === 'jpg' ? 'jpeg' : 'png';
2018-03-07 19:46:18 +08:00
const { mapConfigProvider: provider } = res.locals;
2018-03-07 19:46:18 +08:00
previewBackend.getImage(provider, format, width, height, zoom, center, (err, image, headers, stats = {}) => {
2018-03-08 19:23:43 +08:00
req.profiler.done(`render-${format}`);
2018-03-07 19:46:18 +08:00
req.profiler.add(stats);
if (err) {
err.label = 'STATIC_MAP';
return next(err);
}
if (headers) {
res.set(headers);
}
2018-03-08 19:35:54 +08:00
res.set('Content-Type', headers['Content-Type'] || `image/${format}`);
res.body = image;
next();
});
2018-03-07 03:05:55 +08:00
};
}
2018-03-08 01:51:43 +08:00
function getPreviewImageByBoundingBox (previewBackend) {
return function getPreviewImageByBoundingBoxMiddleware (req, res, next) {
const width = +req.params.width;
const height = +req.params.height;
const bounds = {
west: +req.params.west,
north: +req.params.north,
east: +req.params.east,
south: +req.params.south
};
const format = req.params.format === 'jpg' ? 'jpeg' : 'png';
2018-03-07 19:46:18 +08:00
const { mapConfigProvider: provider } = res.locals;
2018-03-07 19:46:18 +08:00
previewBackend.getImage(provider, format, width, height, bounds, (err, image, headers, stats = {}) => {
2018-03-08 19:35:54 +08:00
req.profiler.done(`render-${format}`);
2018-03-07 19:46:18 +08:00
req.profiler.add(stats);
if (err) {
err.label = 'STATIC_MAP';
return next(err);
}
if (headers) {
res.set(headers);
}
2018-03-08 19:35:54 +08:00
res.set('Content-Type', headers['Content-Type'] || `image/${format}`);
res.body = image;
next();
});
2018-03-07 03:05:55 +08:00
};
}
function setLastModifiedHeader () {
return function setLastModifiedHeaderMiddleware (req, res, next) {
let { cache_buster: cacheBuster } = res.locals;
cacheBuster = parseInt(cacheBuster, 10);
const lastUpdated = res.locals.cache_buster ? new Date(cacheBuster) : new Date();
res.set('Last-Modified', lastUpdated.toUTCString());
next();
};
}
function setCacheControlHeader () {
return function setCacheControlHeaderMiddleware (req, res, next) {
res.set('Cache-Control', 'public,max-age=31536000');
2018-03-06 23:55:27 +08:00
next();
};
}
2018-03-06 23:55:27 +08:00
2018-03-12 20:29:40 +08:00
function getAffectedTables (layergroupAffectedTables, pgConnection, mapStore) {
2018-03-07 03:01:43 +08:00
return function getAffectedTablesMiddleware (req, res, next) {
2018-03-12 20:29:40 +08:00
const { user, dbname, token } = res.locals;
2018-03-07 03:01:43 +08:00
if (layergroupAffectedTables.hasAffectedTables(dbname, token)) {
res.locals.affectedTables = layergroupAffectedTables.get(dbname, token);
return next();
}
2018-03-12 20:29:40 +08:00
mapStore.load(token, (err, mapconfig) => {
if (err) {
global.logger.warn('ERROR generating cache channel:', err);
return next();
}
2018-03-12 20:29:40 +08:00
const queries = [];
mapconfig.getLayers().forEach(function(layer) {
queries.push(layer.options.sql);
if (layer.options.affected_tables) {
layer.options.affected_tables.map(function(table) {
queries.push(`SELECT * FROM ${table} LIMIT 0`);
});
}
});
2018-03-12 20:29:40 +08:00
const sql = queries.length ? queries.join(';') : null;
2018-03-12 20:29:40 +08:00
if (!sql) {
2018-03-12 21:18:07 +08:00
global.logger.warn('ERROR generating cache channel:' +
' this request doesn\'t need an X-Cache-Channel generated');
2018-03-07 03:01:43 +08:00
return next();
}
2018-03-12 20:29:40 +08:00
pgConnection.getConnection(user, (err, connection) => {
2018-03-07 03:01:43 +08:00
if (err) {
2018-03-12 20:29:40 +08:00
global.logger.warn('ERROR generating cache channel:', err);
2018-03-07 03:01:43 +08:00
return next();
}
2018-03-12 20:29:40 +08:00
QueryTables.getAffectedTablesFromQuery(connection, sql, (err, affectedTables) => {
req.profiler.done('getAffectedTablesFromQuery');
if (err) {
global.logger.warn('ERROR generating cache channel: ', err);
return next();
}
2018-03-12 20:29:40 +08:00
// feed affected tables cache so it can be reused from, for instance, map controller
layergroupAffectedTables.set(dbname, token, affectedTables);
2018-03-12 20:29:40 +08:00
res.locals.affectedTables = affectedTables;
next();
});
2018-03-07 03:01:43 +08:00
});
2018-03-07 01:43:23 +08:00
});
2018-03-07 03:01:43 +08:00
};
}
2018-03-07 01:43:23 +08:00
function setCacheChannelHeader () {
2018-03-07 03:01:43 +08:00
return function setCacheChannelHeaderMiddleware (req, res, next) {
const { affectedTables } = res.locals;
2018-03-07 03:01:43 +08:00
if (affectedTables) {
res.set('X-Cache-Channel', affectedTables.getCacheChannel());
2018-03-07 01:43:23 +08:00
}
2018-03-07 03:01:43 +08:00
next();
};
}
2018-03-07 00:08:39 +08:00
function setSurrogateKeyHeader (surrogateKeysCache) {
2018-03-07 03:01:43 +08:00
return function setSurrogateKeyHeaderMiddleware (req, res, next) {
const { affectedTables } = res.locals;
2018-03-07 00:08:39 +08:00
2018-03-07 03:01:43 +08:00
if (affectedTables) {
surrogateKeysCache.tag(res, affectedTables);
}
2018-03-07 01:43:23 +08:00
2018-03-07 03:01:43 +08:00
next();
};
}
function incrementSuccessMetrics (statsClient) {
2018-03-07 21:53:13 +08:00
return function incrementSuccessMetricsMiddleware (req, res, next) {
const formatStat = parseFormat(req.params.format);
statsClient.increment('windshaft.tiles.success');
statsClient.increment(`windshaft.tiles.${formatStat}.success`);
next();
};
}
2018-03-07 21:53:13 +08:00
function sendResponse () {
return function sendResponseMiddleware (req, res) {
req.profiler.done('res');
res.status(res.statusCode || 200);
if (!Buffer.isBuffer(res.body) && typeof res.body === 'object') {
if (req.query && req.query.callback) {
res.jsonp(res.body);
} else {
res.json(res.body);
}
} else {
res.send(res.body);
}
};
}
2018-03-07 21:53:13 +08:00
function incrementErrorMetrics (statsClient) {
2018-03-07 21:53:13 +08:00
return function incrementErrorMetricsMiddleware (err, req, res, next) {
const formatStat = parseFormat(req.params.format);
statsClient.increment('windshaft.tiles.error');
statsClient.increment(`windshaft.tiles.${formatStat}.error`);
next(err);
};
2018-03-07 22:39:59 +08:00
}
2018-03-07 21:53:13 +08:00
function tileError () {
2018-03-07 21:53:13 +08:00
return function tileErrorMiddleware (err, req, res, next) {
// See https://github.com/Vizzuality/Windshaft-cartodb/issues/68
let errMsg = err.message ? ( '' + err.message ) : ( '' + err );
// Rewrite mapnik parsing errors to start with layer number
const matches = errMsg.match("(.*) in style 'layer([0-9]+)'");
if (matches) {
2018-03-08 19:35:54 +08:00
errMsg = `style${matches[2]}: ${matches[1]}`;
2018-03-07 21:53:13 +08:00
}
err.message = errMsg;
err.label = 'TILE RENDER';
next(err);
};
}