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

645 lines
21 KiB
JavaScript
Raw Normal View History

var _ = require('underscore');
var windshaft = require('windshaft');
var QueryTables = require('cartodb-query-tables');
var ResourceLocator = require('../models/resource-locator');
2015-07-08 19:27:56 +08:00
var cors = require('../middleware/cors');
var userMiddleware = require('../middleware/user');
2015-07-08 19:27:56 +08:00
var MapConfig = windshaft.model.MapConfig;
var Datasource = windshaft.model.Datasource;
var NamedMapsCacheEntry = require('../cache/model/named_maps_entry');
var NamedMapMapConfigProvider = require('../models/mapconfig/provider/named-map-provider');
var CreateLayergroupMapConfigProvider = require('../models/mapconfig/provider/create-layergroup-provider');
/**
* @param {AuthApi} authApi
* @param {PgConnection} pgConnection
* @param {TemplateMaps} templateMaps
* @param {MapBackend} mapBackend
* @param metadataBackend
* @param {SurrogateKeysCache} surrogateKeysCache
* @param {UserLimitsApi} userLimitsApi
* @param {LayergroupAffectedTables} layergroupAffectedTables
2016-05-24 05:35:42 +08:00
* @param {MapConfigAdapter} mapConfigAdapter
* @param {StatsBackend} statsBackend
* @constructor
*/
function MapController(prepareContext, pgConnection, templateMaps, mapBackend, metadataBackend,
2017-05-09 00:42:40 +08:00
surrogateKeysCache, userLimitsApi, layergroupAffectedTables, mapConfigAdapter,
statsBackend) {
this.pgConnection = pgConnection;
this.templateMaps = templateMaps;
this.mapBackend = mapBackend;
this.metadataBackend = metadataBackend;
this.surrogateKeysCache = surrogateKeysCache;
2015-07-11 01:10:55 +08:00
this.userLimitsApi = userLimitsApi;
this.layergroupAffectedTables = layergroupAffectedTables;
2016-05-24 05:35:42 +08:00
this.mapConfigAdapter = mapConfigAdapter;
this.resourceLocator = new ResourceLocator(global.environment);
2017-05-09 00:42:40 +08:00
this.statsBackend = statsBackend;
this.prepareContext = prepareContext;
}
module.exports = MapController;
MapController.prototype.register = function(app) {
2017-11-03 02:03:20 +08:00
app.get(app.base_url_mapconfig, this.composeCreateLayergroupMiddleware({
parseConfigQueryParam: true,
2017-11-03 02:03:20 +08:00
includeQuery: true,
label: 'ANONYMOUS LAYERGROUP',
addContext: true
2017-11-03 02:03:20 +08:00
}));
app.post(app.base_url_mapconfig, this.composeCreateLayergroupMiddleware({
includeQuery: true,
label: 'ANONYMOUS LAYERGROUP',
addContext: true
}));
app.get(app.base_url_templated + '/:template_id/jsonp', this.composeInstantiateLayergroupMiddleware({
parseConfigQueryParam: true,
useTemplateHash: true,
label: 'NAMED MAP LAYERGROUP'
}));
app.post(app.base_url_templated + '/:template_id', this.composeInstantiateLayergroupMiddleware({
useTemplateHash: true,
label: 'NAMED MAP LAYERGROUP'
2017-11-03 02:03:20 +08:00
}));
app.options(app.base_url_mapconfig, cors('Content-Type'));
};
2017-11-03 02:03:20 +08:00
MapController.prototype.composeCreateLayergroupMiddleware = function (options) {
const {
parseConfigQueryParam = false,
useTemplateHash = false,
includeQuery = false,
label,
addContext = false
2017-11-03 02:03:20 +08:00
} = options;
return [
cors(),
userMiddleware,
this.prepareContext,
parseConfigQueryParam ? createGetPrepareConfig : createPostPrepareConfig,
this.prepareAdapterMapConfig.bind(this),
this.createLayergroup.bind(this),
this.incrementMapViewCount.bind(this),
this.augmentLayergroupData.bind(this),
this.getAffectedTables.bind(this),
this.setCacheChannel.bind(this),
this.setLastUpdatedTimeToLayergroup.bind(this),
this.setCacheControl.bind(this),
this.setLayerStats.bind(this),
this.setLayergroupIdHeaderBuilder(useTemplateHash),
this.setDataviewsAndWidgetsUrlsToLayergroupMetadata.bind(this),
this.setAnalysesMetadataToLayergroupBuilder(includeQuery),
this.setTurboCartoMetadataToLayergroup.bind(this),
this.setSurrogateKeyHeader.bind(this),
sendResponse,
augmentError({ label, addContext })
];
};
MapController.prototype.composeInstantiateLayergroupMiddleware = function (options) {
const {
parseConfigQueryParam = false,
useTemplateHash = false,
includeQuery = false,
label,
addContext = false
} = options;
return [
cors(),
userMiddleware,
this.prepareContext,
parseConfigQueryParam ? prepareJsonTemplateParams : prepareTemplateParams,
this.getTemplate.bind(this),
this.instantiateLayergroup.bind(this),
this.incrementMapViewCount.bind(this),
this.augmentLayergroupData.bind(this),
this.getAffectedTables.bind(this),
this.setCacheChannel.bind(this),
this.setLastUpdatedTimeToLayergroup.bind(this),
this.setCacheControl.bind(this),
this.setLayerStats.bind(this),
this.setLayergroupIdHeaderBuilder(useTemplateHash),
this.setDataviewsAndWidgetsUrlsToLayergroupMetadata.bind(this),
this.setAnalysesMetadataToLayergroupBuilder(includeQuery),
this.setTurboCartoMetadataToLayergroup.bind(this),
this.setSurrogateKeyHeader.bind(this),
sendResponse,
augmentError({ label, addContext })
2017-11-03 02:03:20 +08:00
];
};
2017-11-02 00:57:35 +08:00
function createGetPrepareConfig (req, res, next) {
req.profiler.start(`windshaft.createmap_get`);
2017-11-02 17:22:30 +08:00
2017-11-02 00:57:35 +08:00
const { config } = res.locals;
if (!config) {
return next(new Error('layergroup GET needs a "config" parameter'));
}
try {
req.body = JSON.parse(config);
} catch (err) {
return next(err);
}
return next();
}
function createPostPrepareConfig(req, res, next) {
2017-11-02 17:22:30 +08:00
req.profiler.start('windshaft.createmap_post');
2017-11-02 00:57:35 +08:00
if (!req.is('application/json')) {
return next(new Error('layergroup POST data must be of type application/json'));
}
next();
}
function prepareTemplateParams(req, res, next) {
2017-11-02 17:22:30 +08:00
req.profiler.start('windshaft-cartodb.instance_template_post');
2017-11-02 00:57:35 +08:00
if (!req.is('application/json')) {
return next(new Error('Template POST data must be of type application/json'));
}
return next();
}
function prepareJsonTemplateParams(req, res, next) {
2017-11-02 17:22:30 +08:00
req.profiler.start('windshaft-cartodb.instance_template_get');
2017-11-02 00:57:35 +08:00
const { callback, config } = req.query;
if (callback === undefined || callback.length === 0) {
return next(new Error('callback parameter should be present and be a function name'));
}
if (config) {
try {
req.body = JSON.parse(config);
} catch(e) {
return next(new Error('Invalid config parameter, should be a valid JSON'));
}
}
return next();
}
MapController.prototype.prepareAdapterMapConfig = function (req, res, next) {
const requestMapConfig = req.body;
const { user, dbhost, dbport, dbname, dbuser, dbpassword, api_key } = res.locals;
const context = {
analysisConfiguration: {
user,
db: {
host: dbhost,
port: dbport,
dbname: dbname,
user: dbuser,
pass: dbpassword
},
batch: {
username: user,
apiKey: api_key
}
}
};
this.mapConfigAdapter.getMapConfig(user, requestMapConfig, res.locals, context, (err, requestMapConfig) => {
if (err) {
return next(err);
}
req.body = requestMapConfig;
res.locals.context = context;
next();
});
};
MapController.prototype.createLayergroup = function(req, res, next) {
const requestMapConfig = req.body;
const { context, user } = res.locals;
const datasource = context.datasource || Datasource.EmptyDatasource();
const mapconfig = new MapConfig(requestMapConfig, datasource);
const mapconfigProvider = new CreateLayergroupMapConfigProvider(mapconfig, user, this.userLimitsApi, res.locals);
2016-05-23 22:20:42 +08:00
res.locals.mapconfig = mapconfig;
res.locals.analysesResults = context.analysesResults;
this.mapBackend.createLayergroup(mapconfig, res.locals, mapconfigProvider, (err, layergroup) => {
if (err) {
return next(err);
}
res.locals.layergroup = layergroup;
2017-11-01 03:10:37 +08:00
next();
});
};
MapController.prototype.getTemplate = function(req, res, next) {
const templateParams = req.body;
const { user } = res.locals;
const mapconfigProvider = new NamedMapMapConfigProvider(
this.templateMaps,
this.pgConnection,
this.metadataBackend,
this.userLimitsApi,
this.mapConfigAdapter,
user,
req.params.template_id,
templateParams,
res.locals.auth_token,
res.locals
);
mapconfigProvider.getMapConfig((err, mapconfig, rendererParams) => {
if (err) {
return next(err);
}
res.locals.mapconfig = mapconfig;
res.locals.rendererParams = rendererParams;
res.locals.mapconfigProvider = mapconfigProvider;
next();
});
};
2017-11-03 01:28:37 +08:00
MapController.prototype.instantiateLayergroup = function(req, res, next) {
const { user, mapconfig, rendererParams } = res.locals;
2017-11-02 02:28:32 +08:00
const mapconfigProvider =
new CreateLayergroupMapConfigProvider(mapconfig, user, this.userLimitsApi, rendererParams);
this.mapBackend.createLayergroup(mapconfig, rendererParams, mapconfigProvider, (err, layergroup) => {
if (err) {
return next(err);
}
res.locals.layergroup = layergroup;
// TODO: Do not provide shortcuts
const { mapconfigProvider } = res.locals;
res.locals.analysesResults = mapconfigProvider.analysesResults;
res.locals.template = mapconfigProvider.template;
res.locals.templateName = mapconfigProvider.getTemplateName();
res.locals.context = mapconfigProvider.context;
next();
});
};
2017-11-03 01:39:46 +08:00
function sendResponse (req, res) {
const { layergroup } = res.locals;
res.status(200);
if (req.query && req.query.callback) {
res.jsonp(layergroup);
} else {
res.json(layergroup);
}
}
MapController.prototype.afterLayergroupCreateBuilder = function (options = {}) {
const {
useTemplateHash = false,
includeQuery = false
} = options;
return [
this.incrementMapViewCount.bind(this),
this.augmentLayergroupData.bind(this),
this.getAffectedTables.bind(this),
this.setCacheChannel.bind(this),
this.setLastUpdatedTimeToLayergroup.bind(this),
this.setCacheControl.bind(this),
this.setLayerStats.bind(this),
this.setLayergroupIdHeaderBuilder(useTemplateHash),
this.setDataviewsAndWidgetsUrlsToLayergroupMetadata.bind(this),
this.setAnalysesMetadataToLayergroupBuilder(includeQuery),
this.setTurboCartoMetadataToLayergroup.bind(this),
this.setSurrogateKeyHeader.bind(this)
];
};
MapController.prototype.incrementMapViewCount = function (req, res, callback) {
const { mapconfig, user } = res.locals;
// Error won't blow up, just be logged.
this.metadataBackend.incMapviewCount(user, mapconfig.obj().stat_tag, (err) => {
req.profiler.done('incMapviewCount');
if (err) {
global.logger.log(`ERROR: failed to increment mapview count for user '${user}': ${err.message}`);
}
callback();
});
};
MapController.prototype.augmentLayergroupData = function (req, res, callback) {
const { layergroup } = res.locals;
// include in layergroup response the variables in serverMedata
// those variables are useful to send to the client information
// about how to reach this server or information about it
_.extend(layergroup, global.environment.serverMetadata);
callback();
2017-10-31 23:58:00 +08:00
};
2017-11-02 18:29:43 +08:00
MapController.prototype.getAffectedTables = function (req, res, next) {
const { dbname, layergroup, user, mapconfig } = res.locals;
2017-11-02 18:29:43 +08:00
this.pgConnection.getConnection(user, (err, connection) => {
if (err) {
return next(err);
}
2017-11-02 18:29:43 +08:00
const sql = [];
mapconfig.getLayers().forEach(function(layer) {
sql.push(layer.options.sql);
if (layer.options.affected_tables) {
layer.options.affected_tables.map(function(table) {
sql.push('SELECT * FROM ' + table + ' LIMIT 0');
});
}
});
QueryTables.getAffectedTablesFromQuery(connection, sql.join(';'), (err, affectedTables) => {
if (err) {
2017-11-02 18:29:43 +08:00
return next(err);
}
// feed affected tables cache so it can be reused from, for instance, layergroup controller
2017-11-02 18:29:43 +08:00
this.layergroupAffectedTables.set(dbname, layergroup.layergroupId, affectedTables);
res.locals.affectedTables = affectedTables;
2017-11-02 18:29:43 +08:00
next();
});
});
};
MapController.prototype.setCacheChannel = function (req, res, callback) {
const { affectedTables } = res.locals;
if (req.method === 'GET') {
res.set('Last-Modified', (new Date()).toUTCString());
res.set('X-Cache-Channel', affectedTables.getCacheChannel());
}
callback();
};
MapController.prototype.setLastUpdatedTimeToLayergroup = function (req, res, callback) {
const { affectedTables, layergroup, analysesResults } = res.locals;
var lastUpdateTime = affectedTables.getLastUpdatedAt();
lastUpdateTime = getLastUpdatedTime(analysesResults, lastUpdateTime) || lastUpdateTime;
// last update for layergroup cache buster
layergroup.layergroupid = layergroup.layergroupid + ':' + lastUpdateTime;
layergroup.last_updated = new Date(lastUpdateTime).toISOString();
callback();
};
MapController.prototype.setCacheControl = function (req, res, callback) {
if (req.method === 'GET') {
var ttl = global.environment.varnish.layergroupTtl || 86400;
res.set('Cache-Control', 'public,max-age='+ttl+',must-revalidate');
}
callback();
};
MapController.prototype.setLayerStats = function (req, res, callback) {
const { user, mapconfig, layergroup } = res.locals;
this.pgConnection.getConnection(user, (err, connection) => {
if (err) {
return callback(err);
}
this.statsBackend.getStats(mapconfig, connection, function(err, layersStats) {
if (err) {
return callback(err);
}
if (layersStats.length > 0) {
layergroup.metadata.layers.forEach(function (layer, index) {
layer.meta.stats = layersStats[index];
});
}
callback();
});
});
};
MapController.prototype.setLayergroupIdHeaderBuilder = function (useTemplateHash) {
const self = this;
return function setLayergroupIdHeader(req, res, callback) {
const { layergroup, user, template } = res.locals;
if (useTemplateHash) {
var templateHash = self.templateMaps.fingerPrint(template).substring(0, 8);
layergroup.layergroupid = `${user}@${templateHash}@${layergroup.layergroupid}`;
}
res.set('X-Layergroup-Id', layergroup.layergroupid);
callback();
};
};
MapController.prototype.setDataviewsAndWidgetsUrlsToLayergroupMetadata = function (req, res, callback) {
const { layergroup, user, mapconfig } = res.locals;
this.addDataviewsAndWidgetsUrls(user, layergroup, mapconfig.obj());
callback();
};
MapController.prototype.setAnalysesMetadataToLayergroupBuilder = function (includeQuery) {
const self = this;
return function setAnalysesMetadataToLayergroup (req, res, callback) {
const { layergroup, user, analysesResults = [] } = res.locals;
self.addAnalysesMetadata(user, layergroup, analysesResults, includeQuery);
callback();
};
};
MapController.prototype.setTurboCartoMetadataToLayergroup = function (req, res, callback) {
const { layergroup, mapconfig, context } = res.locals;
addContextMetadata(layergroup, mapconfig.obj(), context);
callback();
};
function addContextMetadata(layergroup, mapConfig, context) {
if (layergroup.metadata && Array.isArray(layergroup.metadata.layers) && Array.isArray(mapConfig.layers)) {
layergroup.metadata.layers = layergroup.metadata.layers.map(function(layer, layerIndex) {
if (context.turboCarto && Array.isArray(context.turboCarto.layers)) {
layer.meta.cartocss_meta = context.turboCarto.layers[layerIndex];
}
return layer;
});
}
}
MapController.prototype.setSurrogateKeyHeader = function (req, res, callback) {
const { affectedTables, user, templateName } = res.locals;
if (req.method === 'GET' && affectedTables.tables && affectedTables.tables.length > 0) {
this.surrogateKeysCache.tag(res, affectedTables);
}
if (templateName) {
this.surrogateKeysCache.tag(res, new NamedMapsCacheEntry(user, templateName));
}
callback();
};
function getLastUpdatedTime(analysesResults, lastUpdateTime) {
if (!Array.isArray(analysesResults)) {
return lastUpdateTime;
}
return analysesResults.reduce(function(lastUpdateTime, analysis) {
return analysis.getNodes().reduce(function(lastNodeUpdatedAtTime, node) {
var nodeUpdatedAtDate = node.getUpdatedAt();
var nodeUpdatedTimeAt = (nodeUpdatedAtDate && nodeUpdatedAtDate.getTime()) || 0;
return nodeUpdatedTimeAt > lastNodeUpdatedAtTime ? nodeUpdatedTimeAt : lastNodeUpdatedAtTime;
}, lastUpdateTime);
}, lastUpdateTime);
}
MapController.prototype.addAnalysesMetadata = function(username, layergroup, analysesResults, includeQuery) {
includeQuery = includeQuery || false;
analysesResults = analysesResults || [];
layergroup.metadata.analyses = [];
analysesResults.forEach(function(analysis) {
var nodes = analysis.getNodes();
layergroup.metadata.analyses.push({
nodes: nodes.reduce(function(nodesIdMap, node) {
if (node.params.id) {
var nodeResource = layergroup.layergroupid + '/analysis/node/' + node.id();
var nodeRepr = {
2016-04-12 00:49:43 +08:00
status: node.getStatus(),
url: this.resourceLocator.getUrls(username, nodeResource)
};
if (includeQuery) {
nodeRepr.query = node.getQuery();
}
if (node.getStatus() === 'failed') {
nodeRepr.error_message = node.getErrorMessage();
}
nodesIdMap[node.params.id] = nodeRepr;
}
return nodesIdMap;
}.bind(this), {})
});
}.bind(this));
};
2016-06-02 20:14:11 +08:00
// TODO this should take into account several URL patterns
MapController.prototype.addDataviewsAndWidgetsUrls = function(username, layergroup, mapConfig) {
this.addDataviewsUrls(username, layergroup, mapConfig);
this.addWidgetsUrl(username, layergroup, mapConfig);
};
2016-06-02 20:14:11 +08:00
MapController.prototype.addDataviewsUrls = function(username, layergroup, mapConfig) {
2016-03-19 01:09:17 +08:00
layergroup.metadata.dataviews = layergroup.metadata.dataviews || {};
var dataviews = mapConfig.dataviews || {};
Object.keys(dataviews).forEach(function(dataviewName) {
var resource = layergroup.layergroupid + '/dataview/' + dataviewName;
layergroup.metadata.dataviews[dataviewName] = {
url: this.resourceLocator.getUrls(username, resource)
2016-03-19 01:09:17 +08:00
};
}.bind(this));
};
2016-03-19 01:09:17 +08:00
MapController.prototype.addWidgetsUrl = function(username, layergroup, mapConfig) {
2016-06-02 20:14:11 +08:00
if (layergroup.metadata && Array.isArray(layergroup.metadata.layers) && Array.isArray(mapConfig.layers)) {
layergroup.metadata.layers = layergroup.metadata.layers.map(function(layer, layerIndex) {
2016-06-02 20:14:11 +08:00
var mapConfigLayer = mapConfig.layers[layerIndex];
if (mapConfigLayer.options && mapConfigLayer.options.widgets) {
layer.widgets = layer.widgets || {};
Object.keys(mapConfigLayer.options.widgets).forEach(function(widgetName) {
var resource = layergroup.layergroupid + '/' + layerIndex + '/widget/' + widgetName;
2016-06-02 20:14:11 +08:00
layer.widgets[widgetName] = {
type: mapConfigLayer.options.widgets[widgetName].type,
url: this.resourceLocator.getUrls(username, resource)
2016-06-02 20:14:11 +08:00
};
}.bind(this));
}
return layer;
}.bind(this));
}
};
2017-11-03 01:38:34 +08:00
function augmentError (options) {
const { addContext = false, label = 'MAPS CONTROLLER' } = options;
return function mapError (err, req, res, next) {
const { mapconfig } = res.locals;
2017-11-03 15:48:13 +08:00
if (addContext) {
err = Number.isFinite(err.layerIndex) ? populateError(err, mapconfig) : err;
}
err.label = label;
next(err);
};
}
function populateError(err, mapConfig) {
var error = new Error(err.message);
error.http_status = err.http_status;
if (!err.http_status && err.message.indexOf('column "the_geom_webmercator" does not exist') >= 0) {
error.http_status = 400;
}
error.type = 'layer';
error.subtype = err.message.indexOf('Postgis Plugin') >= 0 ? 'query' : undefined;
error.layer = {
id: mapConfig.getLayerId(err.layerIndex),
index: err.layerIndex,
type: mapConfig.layerType(err.layerIndex)
};
return error;
}