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

519 lines
17 KiB
JavaScript
Raw Normal View History

var assert = require('assert');
var step = require('step');
var cors = require('../middleware/cors');
var userMiddleware = require('../middleware/user');
var allowQueryParams = require('../middleware/allow-query-params');
2017-11-07 18:07:38 +08:00
var vectorError = require('../middleware/vector-error');
var DataviewBackend = require('../backends/dataview');
var AnalysisStatusBackend = require('../backends/analysis-status');
2016-05-23 19:29:34 +08:00
var MapStoreMapConfigProvider = require('../models/mapconfig/provider/map-store-provider');
2016-02-22 18:40:25 +08:00
var QueryTables = require('cartodb-query-tables');
/**
* @param {AuthApi} authApi
* @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(prepareContext, pgConnection, mapStore, tileBackend, previewBackend, attributesBackend,
surrogateKeysCache, userLimitsApi, layergroupAffectedTables, analysisBackend) {
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.prepareContext = prepareContext;
}
module.exports = LayergroupController;
2017-10-05 18:12:21 +08:00
LayergroupController.prototype.register = function(app) {
app.get(
app.base_url_mapconfig + '/:token/:z/:x/:y@:scale_factor?x.:format',
cors(),
2017-10-05 18:12:21 +08:00
userMiddleware,
2017-09-22 23:56:47 +08:00
this.prepareContext,
2017-11-07 18:07:38 +08:00
this.tile.bind(this),
vectorError()
);
2017-10-05 18:12:21 +08:00
app.get(
app.base_url_mapconfig + '/:token/:z/:x/:y.:format',
cors(),
userMiddleware,
2017-09-22 23:56:47 +08:00
this.prepareContext,
2017-11-07 18:07:38 +08:00
this.tile.bind(this),
vectorError()
);
2017-10-05 18:12:21 +08:00
app.get(
app.base_url_mapconfig + '/:token/:layer/:z/:x/:y.(:format)',
cors(),
userMiddleware,
validateLayerRouteMiddleware,
2017-09-22 23:56:47 +08:00
this.prepareContext,
2017-11-07 18:07:38 +08:00
this.layer.bind(this),
vectorError()
);
2017-10-05 18:12:21 +08:00
app.get(
app.base_url_mapconfig + '/:token/:layer/attributes/:fid',
cors(),
userMiddleware,
2017-09-22 23:56:47 +08:00
this.prepareContext,
2018-03-06 01:28:52 +08:00
this.getMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi),
this.attributes(this.attributesBackend)
);
2017-10-05 18:12:21 +08:00
app.get(
app.base_url_mapconfig + '/static/center/:token/:z/:lat/:lng/:width/:height.:format',
cors(),
userMiddleware,
allowQueryParams(['layer']),
2017-09-22 23:56:47 +08:00
this.prepareContext,
this.getMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi),
this.center(this.previewBackend)
);
2017-10-05 18:12:21 +08:00
app.get(
app.base_url_mapconfig + '/static/bbox/:token/:west,:south,:east,:north/:width/:height.:format',
cors(),
userMiddleware,
allowQueryParams(['layer']),
2017-09-22 23:56:47 +08:00
this.prepareContext,
this.getMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi),
this.bbox(this.previewBackend)
);
2015-10-07 01:47:44 +08:00
// Undocumented/non-supported API endpoint methods.
// Use at your own peril.
var 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(
app.base_url_mapconfig + '/:token/dataview/:dataviewName',
cors(),
userMiddleware,
allowQueryParams(allowedDataviewQueryParams),
2017-09-22 23:56:47 +08:00
this.prepareContext,
2018-03-06 00:44:04 +08:00
this.getMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi),
this.getDataview(this.dataviewBackend)
);
2017-10-05 18:12:21 +08:00
app.get(
app.base_url_mapconfig + '/:token/:layer/widget/:dataviewName',
cors(),
userMiddleware,
allowQueryParams(allowedDataviewQueryParams),
2017-09-22 23:56:47 +08:00
this.prepareContext,
2018-03-06 00:44:04 +08:00
this.getMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi),
this.getDataview(this.dataviewBackend)
);
2017-10-05 18:12:21 +08:00
app.get(
app.base_url_mapconfig + '/:token/dataview/:dataviewName/search',
cors(),
userMiddleware,
allowQueryParams(allowedDataviewQueryParams),
2017-09-22 23:56:47 +08:00
this.prepareContext,
2018-03-06 01:05:42 +08:00
this.getMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi),
this.dataviewSearch(this.dataviewBackend)
);
2017-10-05 18:12:21 +08:00
app.get(
app.base_url_mapconfig + '/:token/:layer/widget/:dataviewName/search',
cors(),
userMiddleware,
allowQueryParams(allowedDataviewQueryParams),
2017-09-22 23:56:47 +08:00
this.prepareContext,
2018-03-06 01:05:42 +08:00
this.getMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi),
this.dataviewSearch(this.dataviewBackend)
);
2016-04-12 00:49:56 +08:00
2017-10-05 18:12:21 +08:00
app.get(
app.base_url_mapconfig + '/:token/analysis/node/:nodeId',
cors(),
userMiddleware,
2017-09-22 23:56:47 +08:00
this.prepareContext,
this.analysisNodeStatus(this.analysisStatusBackend)
);
2016-04-12 00:49:56 +08:00
};
LayergroupController.prototype.analysisNodeStatus = function (analysisStatusBackend) {
return function analysisNodeStatusMiddleware(req, res, next) {
analysisStatusBackend.getNodeStatus(res.locals, (err, nodeStatus, stats) => {
req.profiler.add(stats || {});
if (err) {
err.label = 'GET NODE STATUS';
return next(err);
}
this.sendResponse(req, res, nodeStatus, 200, {
'Cache-Control': 'public,max-age=5',
'Last-Modified': new Date().toUTCString()
});
});
}.bind(this);
};
2018-03-06 00:44:04 +08:00
LayergroupController.prototype.getMapStoreMapConfigProvider = function (mapStore, userLimitsApi) {
return function getMapStoreMapConfigProviderMiddleware (req, res, next) {
const { user } = res.locals;
2018-03-06 00:44:04 +08:00
res.locals.mapConfigProvider = new MapStoreMapConfigProvider(mapStore, user, userLimitsApi, res.locals);
next();
};
};
LayergroupController.prototype.getDataview = function (dataviewBackend) {
2018-03-06 00:44:04 +08:00
return function getDataviewMiddleware (req, res, next) {
const { user, mapConfigProvider } = res.locals;
dataviewBackend.getDataview(mapConfigProvider, user, res.locals, (err, dataview, stats) => {
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
this.sendResponse(req, res, dataview, 200);
});
}.bind(this);
};
2018-03-06 01:05:42 +08:00
LayergroupController.prototype.dataviewSearch = function (dataviewBackend) {
return function dataviewSearchMiddlewarify (req, res, next) {
const { user, dataviewName, mapConfigProvider } = res.locals;
2018-03-06 01:05:42 +08:00
dataviewBackend.search(mapConfigProvider, user, dataviewName, res.locals, (err, searchResult, stats) => {
req.profiler.add(stats || {});
if (err) {
err.label = 'GET DATAVIEW SEARCH';
2018-03-06 01:05:42 +08:00
return next(err);
}
2018-03-06 01:05:42 +08:00
this.sendResponse(req, res, searchResult, 200);
});
}.bind(this);
};
2018-03-06 01:28:52 +08:00
LayergroupController.prototype.attributes = function (attributesBackend) {
return function attributesMiddleware (req, res, next) {
req.profiler.start('windshaft.maplayer_attribute');
2018-03-06 01:28:52 +08:00
const { mapConfigProvider } = res.locals;
2018-03-06 01:28:52 +08:00
attributesBackend.getFeatureAttributes(mapConfigProvider, res.locals, false, (err, tile, stats) => {
req.profiler.add(stats || {});
if (err) {
err.label = 'GET ATTRIBUTES';
2018-03-06 01:28:52 +08:00
return next(err);
}
2018-03-06 01:28:52 +08:00
this.sendResponse(req, res, tile, 200);
});
}.bind(this);
};
// Gets a tile for a given token and set of tile ZXY coords. (OSM style)
LayergroupController.prototype.tile = function(req, res, next) {
req.profiler.start('windshaft.map_tile');
this.tileOrLayer(req, res, next);
};
// Gets a tile for a given token, layer set of tile ZXY coords. (OSM style)
LayergroupController.prototype.layer = function(req, res, next) {
req.profiler.start('windshaft.maplayer_tile');
this.tileOrLayer(req, res, next);
};
LayergroupController.prototype.tileOrLayer = function (req, res, next) {
var self = this;
step(
function mapController$getTileOrGrid() {
2015-07-11 01:10:55 +08:00
self.tileBackend.getTile(
2017-10-03 23:47:57 +08:00
new MapStoreMapConfigProvider(self.mapStore, res.locals.user, self.userLimitsApi, res.locals),
2018-02-07 23:02:13 +08:00
res.locals, this
2015-07-11 01:10:55 +08:00
);
},
function mapController$finalize(err, tile, headers, stats) {
req.profiler.add(stats);
self.finalizeGetTileOrGrid(err, req, res, tile, headers, next);
}
);
};
function getStatusCode(tile, format){
return tile.length===0 && format==='mvt'? 204:200;
}
// This function is meant for being called as the very last
// step by all endpoints serving tiles or grids
LayergroupController.prototype.finalizeGetTileOrGrid = function(err, req, res, tile, headers, next) {
var supportedFormats = {
grid_json: true,
json_torque: true,
torque_json: true,
2017-05-17 18:04:11 +08:00
png: true,
2017-05-17 18:16:16 +08:00
png32: true,
2017-05-17 18:04:11 +08:00
mvt: true
};
var formatStat = 'invalid';
if (req.params.format) {
var format = req.params.format.replace('.', '_');
if (supportedFormats[format]) {
formatStat = format;
}
}
if (err) {
// See https://github.com/Vizzuality/Windshaft-cartodb/issues/68
var errMsg = err.message ? ( '' + err.message ) : ( '' + err );
// Rewrite mapnik parsing errors to start with layer number
var matches = errMsg.match("(.*) in style 'layer([0-9]+)'");
if (matches) {
errMsg = 'style'+matches[2]+': ' + matches[1];
}
err.message = errMsg;
err.label = 'TILE RENDER';
next(err);
global.statsClient.increment('windshaft.tiles.error');
global.statsClient.increment('windshaft.tiles.' + formatStat + '.error');
} else {
this.sendResponse(req, res, tile, getStatusCode(tile, formatStat), headers);
global.statsClient.increment('windshaft.tiles.success');
global.statsClient.increment('windshaft.tiles.' + formatStat + '.success');
}
};
LayergroupController.prototype.center = function (previewBackend) {
return function centerMiddleware (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';
// We force always the tile to be generated using PNG because
// is the only format we support by now
res.locals.format = 'png';
res.locals.layer = res.locals.layer || 'all';
const { mapConfigProvider } = res.locals;
2018-03-06 02:33:46 +08:00
previewBackend.getImage(mapConfigProvider, format, width, height, zoom, center,(err, image, headers, stats) => {
req.profiler.done('render-' + format);
req.profiler.add(stats || {});
if (err) {
err.label = 'STATIC_MAP';
return next(err);
}
res.set('Content-Type', headers['Content-Type'] || 'image/' + format);
this.sendResponse(req, res, image, 200);
});
}.bind(this);
};
LayergroupController.prototype.bbox = function (previewBackend) {
return function bboxMiddleware (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';
// We force always the tile to be generated using PNG because
// is the only format we support by now
res.locals.format = 'png';
res.locals.layer = res.locals.layer || 'all';
const { mapConfigProvider } = res.locals;
previewBackend.getImage(mapConfigProvider, format, width, height, bounds, (err, image, headers, stats) => {
req.profiler.done('render-' + format);
req.profiler.add(stats || {});
if (err) {
err.label = 'STATIC_MAP';
return next(err);
}
res.set('Content-Type', headers['Content-Type'] || 'image/' + format);
this.sendResponse(req, res, image, 200);
});
}.bind(this);
};
2015-09-17 08:05:25 +08:00
LayergroupController.prototype.sendResponse = function(req, res, body, status, headers) {
var self = this;
req.profiler.done('res');
2015-09-17 08:03:09 +08:00
res.set('Cache-Control', 'public,max-age=31536000');
// Set Last-Modified header
var lastUpdated;
2017-09-29 18:32:46 +08:00
if (res.locals.cache_buster) {
// Assuming cache_buster is a timestamp
2017-09-29 18:32:46 +08:00
lastUpdated = new Date(parseInt(res.locals.cache_buster));
} else {
lastUpdated = new Date();
}
2015-09-17 08:03:09 +08:00
res.set('Last-Modified', lastUpdated.toUTCString());
2017-09-29 18:32:46 +08:00
var dbName = res.locals.dbname;
step(
function getAffectedTables() {
2017-10-03 23:47:57 +08:00
self.getAffectedTables(res.locals.user, dbName, res.locals.token, this);
},
function sendResponse(err, affectedTables) {
req.profiler.done('affectedTables');
if (err) {
2015-09-18 23:23:37 +08:00
global.logger.warn('ERROR generating cache channel: ' + err);
}
if (!!affectedTables) {
2016-02-22 18:40:25 +08:00
res.set('X-Cache-Channel', affectedTables.getCacheChannel());
self.surrogateKeysCache.tag(res, affectedTables);
}
if (headers) {
res.set(headers);
}
res.status(status);
if (!Buffer.isBuffer(body) && typeof body === 'object') {
if (req.query && req.query.callback) {
res.jsonp(body);
} else {
res.json(body);
}
} else {
res.send(body);
}
}
);
};
LayergroupController.prototype.getAffectedTables = function(user, dbName, layergroupId, callback) {
if (this.layergroupAffectedTables.hasAffectedTables(dbName, layergroupId)) {
return callback(null, this.layergroupAffectedTables.get(dbName, layergroupId));
}
var self = this;
step(
function extractSQL() {
step(
function loadFromStore() {
self.mapStore.load(layergroupId, this);
},
function getSQL(err, mapConfig) {
assert.ifError(err);
var 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');
});
}
});
return queries.length ? queries.join(';') : null;
},
this
);
},
function findAffectedTables(err, sql) {
assert.ifError(err);
if ( ! sql ) {
throw new Error("this request doesn't need an X-Cache-Channel generated");
}
2016-02-22 18:40:25 +08:00
step(
function getConnection() {
self.pgConnection.getConnection(user, this);
},
function getAffectedTables(err, connection) {
assert.ifError(err);
QueryTables.getAffectedTablesFromQuery(connection, sql, this);
},
this
);
},
function buildCacheChannel(err, tables) {
assert.ifError(err);
2016-02-22 18:40:25 +08:00
self.layergroupAffectedTables.set(dbName, layergroupId, tables);
return tables;
},
callback
);
};
function validateLayerRouteMiddleware(req, res, next) {
if (req.params.token === 'static') {
return next('route');
}
next();
}