2015-07-10 17:24:32 +08:00
|
|
|
var _ = require('underscore');
|
2015-07-05 02:41:22 +08:00
|
|
|
var assert = require('assert');
|
|
|
|
var step = require('step');
|
2015-07-10 17:24:32 +08:00
|
|
|
var windshaft = require('windshaft');
|
2016-02-23 02:11:54 +08:00
|
|
|
var QueryTables = require('cartodb-query-tables');
|
2015-07-05 02:41:22 +08:00
|
|
|
|
2017-03-23 01:58:37 +08:00
|
|
|
var ResourceLocator = require('../models/resource-locator');
|
|
|
|
|
2015-07-08 19:27:56 +08:00
|
|
|
var cors = require('../middleware/cors');
|
2015-09-30 23:17:01 +08:00
|
|
|
var userMiddleware = require('../middleware/user');
|
2015-07-08 19:27:56 +08:00
|
|
|
|
2015-07-10 07:30:38 +08:00
|
|
|
var MapConfig = windshaft.model.MapConfig;
|
|
|
|
var Datasource = windshaft.model.Datasource;
|
|
|
|
|
2015-07-10 17:24:32 +08:00
|
|
|
var NamedMapsCacheEntry = require('../cache/model/named_maps_entry');
|
|
|
|
|
2016-05-23 19:32:28 +08:00
|
|
|
var NamedMapMapConfigProvider = require('../models/mapconfig/provider/named-map-provider');
|
2016-05-23 19:28:11 +08:00
|
|
|
var CreateLayergroupMapConfigProvider = require('../models/mapconfig/provider/create-layergroup-provider');
|
2017-05-10 23:17:01 +08:00
|
|
|
|
2015-07-05 02:41:22 +08:00
|
|
|
/**
|
2015-09-16 22:18:26 +08:00
|
|
|
* @param {AuthApi} authApi
|
2015-07-10 07:30:38 +08:00
|
|
|
* @param {PgConnection} pgConnection
|
|
|
|
* @param {TemplateMaps} templateMaps
|
2015-07-05 02:41:22 +08:00
|
|
|
* @param {MapBackend} mapBackend
|
2015-07-10 17:24:32 +08:00
|
|
|
* @param metadataBackend
|
|
|
|
* @param {SurrogateKeysCache} surrogateKeysCache
|
2015-07-14 19:40:41 +08:00
|
|
|
* @param {UserLimitsApi} userLimitsApi
|
|
|
|
* @param {LayergroupAffectedTables} layergroupAffectedTables
|
2016-05-24 05:35:42 +08:00
|
|
|
* @param {MapConfigAdapter} mapConfigAdapter
|
2017-05-09 18:31:16 +08:00
|
|
|
* @param {StatsBackend} statsBackend
|
2015-07-05 02:41:22 +08:00
|
|
|
* @constructor
|
|
|
|
*/
|
2017-09-26 01:40:27 +08:00
|
|
|
function MapController(prepareContext, pgConnection, templateMaps, mapBackend, metadataBackend,
|
2017-05-09 00:42:40 +08:00
|
|
|
surrogateKeysCache, userLimitsApi, layergroupAffectedTables, mapConfigAdapter,
|
2017-05-09 18:31:16 +08:00
|
|
|
statsBackend) {
|
2015-07-10 07:30:38 +08:00
|
|
|
this.pgConnection = pgConnection;
|
|
|
|
this.templateMaps = templateMaps;
|
2015-07-09 02:51:36 +08:00
|
|
|
this.mapBackend = mapBackend;
|
2015-07-10 17:24:32 +08:00
|
|
|
this.metadataBackend = metadataBackend;
|
|
|
|
this.surrogateKeysCache = surrogateKeysCache;
|
2015-07-11 01:10:55 +08:00
|
|
|
this.userLimitsApi = userLimitsApi;
|
2015-07-14 19:40:41 +08:00
|
|
|
this.layergroupAffectedTables = layergroupAffectedTables;
|
2015-07-14 17:55:49 +08:00
|
|
|
|
2016-05-24 05:35:42 +08:00
|
|
|
this.mapConfigAdapter = mapConfigAdapter;
|
2017-03-23 01:58:37 +08:00
|
|
|
this.resourceLocator = new ResourceLocator(global.environment);
|
2017-05-09 00:42:40 +08:00
|
|
|
|
2017-05-09 18:31:16 +08:00
|
|
|
this.statsBackend = statsBackend;
|
2017-09-26 01:40:27 +08:00
|
|
|
this.prepareContext = prepareContext;
|
2015-07-05 02:41:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = MapController;
|
|
|
|
|
|
|
|
MapController.prototype.register = function(app) {
|
2017-09-22 03:52:34 +08:00
|
|
|
app.get(
|
|
|
|
app.base_url_mapconfig,
|
|
|
|
cors(),
|
|
|
|
userMiddleware,
|
2017-09-22 23:56:47 +08:00
|
|
|
this.prepareContext,
|
2017-11-02 00:57:35 +08:00
|
|
|
createGetPrepareConfig,
|
2017-11-02 02:02:07 +08:00
|
|
|
this.prepareAdapterMapConfig.bind(this),
|
2017-11-01 03:49:26 +08:00
|
|
|
this.createGet.bind(this),
|
|
|
|
mapErrorMiddleware({
|
|
|
|
label: 'ANONYMOUS LAYERGROUP',
|
|
|
|
augmentError: true
|
|
|
|
})
|
2017-09-22 03:52:34 +08:00
|
|
|
);
|
|
|
|
app.post(
|
|
|
|
app.base_url_mapconfig,
|
|
|
|
cors(),
|
|
|
|
userMiddleware,
|
2017-09-22 23:56:47 +08:00
|
|
|
this.prepareContext,
|
2017-11-02 00:57:35 +08:00
|
|
|
createPostPrepareConfig,
|
2017-11-02 02:02:07 +08:00
|
|
|
this.prepareAdapterMapConfig.bind(this),
|
2017-11-01 03:49:26 +08:00
|
|
|
this.createPost.bind(this),
|
|
|
|
mapErrorMiddleware({
|
|
|
|
label: 'ANONYMOUS LAYERGROUP',
|
|
|
|
augmentError: true
|
|
|
|
})
|
2017-09-22 03:52:34 +08:00
|
|
|
);
|
2017-09-22 06:31:16 +08:00
|
|
|
app.get(
|
|
|
|
app.base_url_templated + '/:template_id/jsonp',
|
|
|
|
cors(),
|
|
|
|
userMiddleware,
|
2017-09-22 23:56:47 +08:00
|
|
|
this.prepareContext,
|
2017-11-02 00:57:35 +08:00
|
|
|
prepareJsonTemplateParams,
|
2017-11-02 02:02:07 +08:00
|
|
|
this.getTemplate.bind(this),
|
2017-11-01 03:49:26 +08:00
|
|
|
this.jsonp.bind(this),
|
|
|
|
mapErrorMiddleware({
|
|
|
|
label: 'NAMED MAP LAYERGROUP'
|
|
|
|
})
|
2017-09-22 06:31:16 +08:00
|
|
|
);
|
|
|
|
app.post(
|
|
|
|
app.base_url_templated + '/:template_id',
|
|
|
|
cors(),
|
|
|
|
userMiddleware,
|
2017-09-22 23:56:47 +08:00
|
|
|
this.prepareContext,
|
2017-11-02 00:57:35 +08:00
|
|
|
prepareTemplateParams,
|
2017-11-02 02:02:07 +08:00
|
|
|
this.getTemplate.bind(this),
|
2017-11-01 03:49:26 +08:00
|
|
|
this.instantiate.bind(this),
|
|
|
|
mapErrorMiddleware({
|
|
|
|
label: 'NAMED MAP LAYERGROUP'
|
|
|
|
})
|
2017-09-22 06:31:16 +08:00
|
|
|
);
|
2015-07-10 17:24:32 +08:00
|
|
|
app.options(app.base_url_mapconfig, cors('Content-Type'));
|
2015-07-05 02:41:22 +08:00
|
|
|
};
|
|
|
|
|
2017-11-02 00:57:35 +08:00
|
|
|
function createGetPrepareConfig (req, res, next) {
|
|
|
|
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) {
|
|
|
|
if (!req.is('application/json')) {
|
|
|
|
return next(new Error('layergroup POST data must be of type application/json'));
|
|
|
|
}
|
|
|
|
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
|
|
|
|
function prepareTemplateParams(req, res, next) {
|
|
|
|
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) {
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2017-09-21 17:46:31 +08:00
|
|
|
MapController.prototype.createGet = function(req, res, next){
|
2015-07-10 17:24:32 +08:00
|
|
|
req.profiler.start('windshaft.createmap_get');
|
2015-07-05 02:41:22 +08:00
|
|
|
|
2017-11-02 00:57:35 +08:00
|
|
|
this.create(req, res, next);
|
2015-07-10 17:24:32 +08:00
|
|
|
};
|
2015-07-05 02:41:22 +08:00
|
|
|
|
2017-09-21 17:46:31 +08:00
|
|
|
MapController.prototype.createPost = function(req, res, next) {
|
2015-07-10 17:24:32 +08:00
|
|
|
req.profiler.start('windshaft.createmap_post');
|
2015-07-05 02:41:22 +08:00
|
|
|
|
2017-11-02 00:57:35 +08:00
|
|
|
this.create(req, res, next);
|
2015-07-10 17:24:32 +08:00
|
|
|
};
|
2015-07-05 02:41:22 +08:00
|
|
|
|
2017-09-21 17:46:31 +08:00
|
|
|
MapController.prototype.instantiate = function(req, res, next) {
|
2017-03-31 02:31:53 +08:00
|
|
|
req.profiler.start('windshaft-cartodb.instance_template_post');
|
2015-07-05 02:41:22 +08:00
|
|
|
|
2017-11-02 00:57:35 +08:00
|
|
|
this.instantiateTemplate(req, res, next);
|
2015-07-10 17:24:32 +08:00
|
|
|
};
|
|
|
|
|
2017-09-21 17:46:31 +08:00
|
|
|
MapController.prototype.jsonp = function(req, res, next) {
|
2017-03-31 02:31:53 +08:00
|
|
|
req.profiler.start('windshaft-cartodb.instance_template_get');
|
2017-11-02 00:57:35 +08:00
|
|
|
this.instantiateTemplate(req, res, next);
|
2015-07-05 02:41:22 +08:00
|
|
|
};
|
|
|
|
|
2017-11-02 02:02:07 +08:00
|
|
|
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;
|
2015-07-05 02:41:22 +08:00
|
|
|
|
2017-11-02 02:02:07 +08:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
};
|
2015-07-10 17:24:32 +08:00
|
|
|
|
2017-11-02 02:02:07 +08:00
|
|
|
MapController.prototype.create = function(req, res, next) {
|
|
|
|
var self = this;
|
2016-05-23 22:20:42 +08:00
|
|
|
|
2015-07-05 02:41:22 +08:00
|
|
|
step(
|
2017-11-02 02:02:07 +08:00
|
|
|
function createLayergroup() {
|
2017-11-02 00:57:35 +08:00
|
|
|
const requestMapConfig = req.body;
|
2017-11-02 02:02:07 +08:00
|
|
|
const { context } = res.locals;
|
2017-11-02 00:57:35 +08:00
|
|
|
|
2017-11-02 02:02:07 +08:00
|
|
|
const datasource = context.datasource || Datasource.EmptyDatasource();
|
|
|
|
const mapconfig = res.locals.mapconfig = new MapConfig(requestMapConfig, datasource);
|
2015-07-11 01:10:55 +08:00
|
|
|
self.mapBackend.createLayergroup(
|
2017-11-02 02:02:07 +08:00
|
|
|
mapconfig,
|
2017-10-02 18:07:35 +08:00
|
|
|
res.locals,
|
2017-11-02 02:02:07 +08:00
|
|
|
new CreateLayergroupMapConfigProvider(mapconfig, res.locals.user, self.userLimitsApi, res.locals),
|
2015-07-11 01:10:55 +08:00
|
|
|
this
|
|
|
|
);
|
2015-07-10 17:24:32 +08:00
|
|
|
},
|
|
|
|
function afterLayergroupCreate(err, layergroup) {
|
|
|
|
assert.ifError(err);
|
2017-11-01 03:10:37 +08:00
|
|
|
|
2017-11-02 02:02:07 +08:00
|
|
|
const { context } = res.locals;
|
|
|
|
|
2017-10-31 20:36:17 +08:00
|
|
|
res.locals.analysesResults = context.analysesResults;
|
|
|
|
res.locals.layergroup = layergroup;
|
2017-11-01 01:42:11 +08:00
|
|
|
res.locals.context = context;
|
2017-10-31 20:36:17 +08:00
|
|
|
|
2017-11-01 01:25:17 +08:00
|
|
|
const afterLayergroupCreate = self.afterLayergroupCreateBuilder({
|
|
|
|
includeQuery: true
|
|
|
|
});
|
2017-11-01 00:29:55 +08:00
|
|
|
|
|
|
|
afterLayergroupCreate(req, res, this);
|
2015-07-05 02:41:22 +08:00
|
|
|
},
|
2017-10-31 22:47:59 +08:00
|
|
|
function finish(err) {
|
2015-07-05 02:41:22 +08:00
|
|
|
if (err) {
|
2017-10-31 02:29:19 +08:00
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
|
2017-11-01 01:25:17 +08:00
|
|
|
const { layergroup } = res.locals;
|
2017-10-31 22:47:59 +08:00
|
|
|
|
2017-10-31 02:29:19 +08:00
|
|
|
res.status(200);
|
|
|
|
|
|
|
|
if (req.query && req.query.callback) {
|
|
|
|
res.jsonp(layergroup);
|
2015-07-05 02:41:22 +08:00
|
|
|
} else {
|
2017-10-31 02:29:19 +08:00
|
|
|
res.json(layergroup);
|
2015-07-05 02:41:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2017-11-02 02:02:07 +08:00
|
|
|
MapController.prototype.getTemplate = function(req, res, next) {
|
|
|
|
const templateParams = req.body;
|
|
|
|
const { user } = res.locals;
|
|
|
|
|
|
|
|
const mapConfigProvider = res.locals.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;
|
|
|
|
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-11-02 00:57:35 +08:00
|
|
|
MapController.prototype.instantiateTemplate = function(req, res, next) {
|
2015-07-10 17:24:32 +08:00
|
|
|
var self = this;
|
2015-07-05 02:41:22 +08:00
|
|
|
|
|
|
|
step(
|
2017-11-02 02:02:07 +08:00
|
|
|
function createLayergroup() {
|
|
|
|
const { user, mapconfig, rendererParams } = res.locals;
|
|
|
|
|
2015-07-11 01:10:55 +08:00
|
|
|
self.mapBackend.createLayergroup(
|
2017-11-02 02:02:07 +08:00
|
|
|
mapconfig, rendererParams,
|
|
|
|
new CreateLayergroupMapConfigProvider(mapconfig, user, self.userLimitsApi, rendererParams),
|
2015-07-11 01:10:55 +08:00
|
|
|
this
|
|
|
|
);
|
2015-07-05 02:41:22 +08:00
|
|
|
},
|
2015-07-10 17:24:32 +08:00
|
|
|
function afterLayergroupCreate(err, layergroup) {
|
|
|
|
assert.ifError(err);
|
2017-10-31 20:36:17 +08:00
|
|
|
|
|
|
|
res.locals.layergroup = layergroup;
|
2017-11-02 02:02:07 +08:00
|
|
|
|
|
|
|
const { mapconfigProvider } = res.locals;
|
|
|
|
|
|
|
|
res.locals.analysesResults = mapconfigProvider.analysesResults;
|
|
|
|
res.locals.template = mapconfigProvider.template;
|
|
|
|
res.locals.templateName = mapconfigProvider.getTemplateName();
|
|
|
|
res.locals.context = mapconfigProvider.context;
|
2017-10-31 20:36:17 +08:00
|
|
|
|
2017-11-01 01:02:23 +08:00
|
|
|
const afterLayergroupCreate = self.afterLayergroupCreateBuilder({
|
|
|
|
useTemplateHash: true
|
|
|
|
});
|
2017-11-01 00:29:55 +08:00
|
|
|
|
|
|
|
afterLayergroupCreate(req, res, this);
|
2015-07-10 17:24:32 +08:00
|
|
|
},
|
2017-10-31 22:47:59 +08:00
|
|
|
function finishTemplateInstantiation(err) {
|
2015-07-10 17:24:32 +08:00
|
|
|
if (err) {
|
2017-10-31 22:58:41 +08:00
|
|
|
return next(err);
|
2017-10-31 22:49:10 +08:00
|
|
|
}
|
2017-10-31 22:47:59 +08:00
|
|
|
|
2017-11-01 01:25:17 +08:00
|
|
|
const { layergroup } = res.locals;
|
2015-07-10 17:24:32 +08:00
|
|
|
|
2017-10-31 22:49:10 +08:00
|
|
|
res.status(200);
|
|
|
|
|
|
|
|
if (req.query && req.query.callback) {
|
|
|
|
res.jsonp(layergroup);
|
|
|
|
} else {
|
|
|
|
res.json(layergroup);
|
2015-07-05 02:41:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2017-11-01 01:02:23 +08:00
|
|
|
MapController.prototype.afterLayergroupCreateBuilder = function (options = {}) {
|
|
|
|
const self = this;
|
2017-11-01 01:25:17 +08:00
|
|
|
const {
|
|
|
|
useTemplateHash = false,
|
|
|
|
includeQuery = false
|
|
|
|
} = options;
|
2015-07-05 02:41:22 +08:00
|
|
|
|
2017-11-01 00:29:55 +08:00
|
|
|
return function (req, res, callback) {
|
|
|
|
step(
|
|
|
|
function incrementMapViewCount () {
|
|
|
|
self.incrementMapViewCount(req, res, this);
|
|
|
|
},
|
|
|
|
function augmentLayergroupData (err) {
|
|
|
|
assert.ifError(err);
|
|
|
|
self.augmentLayergroupData(req, res, this);
|
|
|
|
},
|
|
|
|
function getAffectedTables (err) {
|
|
|
|
assert.ifError(err);
|
|
|
|
self.getAffectedTables(req, res, this);
|
|
|
|
},
|
|
|
|
function setCacheChannel (err) {
|
|
|
|
assert.ifError(err);
|
|
|
|
self.setCacheChannel(req, res, this);
|
|
|
|
},
|
|
|
|
function setLastUpdatedTime (err) {
|
|
|
|
assert.ifError(err);
|
|
|
|
self.setLastUpdatedTimeToLayergroup(req, res, this);
|
|
|
|
},
|
|
|
|
function setCacheControl (err) {
|
|
|
|
assert.ifError(err);
|
|
|
|
self.setCacheControl(req, res, this);
|
|
|
|
},
|
|
|
|
function setLayerStats (err) {
|
|
|
|
assert.ifError(err);
|
|
|
|
self.setLayerStats(req, res, this);
|
|
|
|
},
|
2017-11-01 01:54:32 +08:00
|
|
|
function setLayergroupIdHeader (err) {
|
2017-11-01 01:02:23 +08:00
|
|
|
assert.ifError(err);
|
|
|
|
|
2017-11-01 01:54:32 +08:00
|
|
|
const setLayergroupIdHeader = self.setLayergroupIdHeaderBuilder(useTemplateHash);
|
2017-11-01 01:02:23 +08:00
|
|
|
|
2017-11-01 01:54:32 +08:00
|
|
|
setLayergroupIdHeader(req, res, this);
|
2017-11-01 01:02:23 +08:00
|
|
|
},
|
|
|
|
function (err) {
|
|
|
|
assert.ifError(err);
|
|
|
|
self.setDataviewsAndWidgetsUrlsToLayergroupMetadata(req, res, this);
|
|
|
|
},
|
2017-11-01 01:25:17 +08:00
|
|
|
function setAnalysesMetadataToLayergroup(err) {
|
|
|
|
assert.ifError(err);
|
|
|
|
|
|
|
|
const setAnalysesMetadataToLayergroup = self.setAnalysesMetadataToLayergroupBuilder(includeQuery);
|
|
|
|
|
|
|
|
setAnalysesMetadataToLayergroup(req, res, this);
|
|
|
|
},
|
2017-11-01 01:42:11 +08:00
|
|
|
function setTurboCartoMetadataToLayergroup (err) {
|
|
|
|
assert.ifError(err);
|
|
|
|
self.setTurboCartoMetadataToLayergroup(req, res, this);
|
|
|
|
},
|
2017-11-01 02:49:12 +08:00
|
|
|
function setSurrogateKeyHeader (err) {
|
|
|
|
assert.ifError(err);
|
|
|
|
self.setSurrogateKeyHeader(req, res, this);
|
|
|
|
},
|
2017-11-01 00:29:55 +08:00
|
|
|
function finish(err) {
|
|
|
|
callback(err);
|
|
|
|
}
|
|
|
|
);
|
2015-07-10 17:24:32 +08:00
|
|
|
};
|
2017-11-01 00:29:55 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
MapController.prototype.incrementMapViewCount = function (req, res, callback) {
|
|
|
|
const { mapconfig, user } = res.locals;
|
2015-07-09 02:51:36 +08:00
|
|
|
|
2017-11-01 00:29:55 +08:00
|
|
|
// Error won't blow up, just be logged.
|
|
|
|
this.metadataBackend.incMapviewCount(user, mapconfig.obj().stat_tag, (err) => {
|
2017-03-31 02:31:53 +08:00
|
|
|
req.profiler.done('incMapviewCount');
|
2017-11-01 00:29:55 +08:00
|
|
|
|
|
|
|
if (err) {
|
|
|
|
global.logger.log(`ERROR: failed to increment mapview count for user '${user}': ${err.message}`);
|
2015-07-10 17:24:32 +08:00
|
|
|
}
|
2017-10-31 21:47:29 +08:00
|
|
|
|
2017-11-01 00:29:55 +08:00
|
|
|
callback();
|
2015-07-09 02:51:36 +08:00
|
|
|
});
|
2017-10-31 20:13:20 +08:00
|
|
|
};
|
|
|
|
|
2017-10-31 20:46:03 +08:00
|
|
|
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-10-31 20:46:03 +08:00
|
|
|
|
2017-10-31 20:13:20 +08:00
|
|
|
MapController.prototype.getAffectedTables = function (req, res, callback) {
|
|
|
|
const self = this;
|
|
|
|
const { dbname, layergroup, user, mapconfig } = res.locals;
|
|
|
|
|
2017-03-14 00:28:29 +08:00
|
|
|
var sql = [];
|
2017-03-14 01:36:13 +08:00
|
|
|
mapconfig.getLayers().forEach(function(layer) {
|
2017-03-14 00:28:29 +08:00
|
|
|
sql.push(layer.options.sql);
|
|
|
|
if (layer.options.affected_tables) {
|
|
|
|
layer.options.affected_tables.map(function(table) {
|
|
|
|
sql.push('SELECT * FROM ' + table + ' LIMIT 0');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2015-07-09 02:51:36 +08:00
|
|
|
|
|
|
|
step(
|
2016-03-04 02:01:58 +08:00
|
|
|
function getPgConnection() {
|
2017-10-31 20:13:20 +08:00
|
|
|
self.pgConnection.getConnection(user, this);
|
2015-07-14 19:40:41 +08:00
|
|
|
},
|
2016-03-04 02:01:58 +08:00
|
|
|
function getAffectedTablesAndLastUpdatedTime(err, connection) {
|
2015-07-14 19:40:41 +08:00
|
|
|
assert.ifError(err);
|
2017-10-31 20:13:20 +08:00
|
|
|
QueryTables.getAffectedTablesFromQuery(connection, sql.join(';'), this);
|
2015-07-09 02:51:36 +08:00
|
|
|
},
|
2017-10-31 20:13:20 +08:00
|
|
|
function handleAffectedTablesAndLastUpdatedTime(err, affectedTables) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
2015-07-09 02:51:36 +08:00
|
|
|
}
|
2015-07-10 17:24:32 +08:00
|
|
|
|
2017-10-31 20:13:20 +08:00
|
|
|
// feed affected tables cache so it can be reused from, for instance, layergroup controller
|
|
|
|
self.layergroupAffectedTables.set(dbname, layergroup.layergroupId, affectedTables);
|
|
|
|
|
|
|
|
res.locals.affectedTables = affectedTables;
|
|
|
|
|
|
|
|
callback();
|
2015-07-09 02:51:36 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
2015-10-26 21:35:51 +08:00
|
|
|
|
2017-10-31 20:13:20 +08:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-11-01 01:54:32 +08:00
|
|
|
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}`;
|
|
|
|
}
|
2017-11-01 01:02:23 +08:00
|
|
|
|
2017-11-01 01:54:32 +08:00
|
|
|
res.set('X-Layergroup-Id', layergroup.layergroupid);
|
2017-11-01 01:02:23 +08:00
|
|
|
|
2017-11-01 01:54:32 +08:00
|
|
|
callback();
|
|
|
|
};
|
2017-11-01 01:02:23 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
MapController.prototype.setDataviewsAndWidgetsUrlsToLayergroupMetadata = function (req, res, callback) {
|
|
|
|
const { layergroup, user, mapconfig } = res.locals;
|
|
|
|
|
|
|
|
this.addDataviewsAndWidgetsUrls(user, layergroup, mapconfig.obj());
|
|
|
|
|
|
|
|
callback();
|
2017-11-01 01:25:17 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
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();
|
|
|
|
};
|
|
|
|
};
|
2017-11-01 01:02:23 +08:00
|
|
|
|
2017-11-01 01:42:11 +08:00
|
|
|
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;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-01 02:49:12 +08:00
|
|
|
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();
|
|
|
|
};
|
|
|
|
|
2016-06-17 20:46:22 +08:00
|
|
|
function getLastUpdatedTime(analysesResults, lastUpdateTime) {
|
|
|
|
if (!Array.isArray(analysesResults)) {
|
|
|
|
return lastUpdateTime;
|
|
|
|
}
|
|
|
|
return analysesResults.reduce(function(lastUpdateTime, analysis) {
|
2016-07-05 17:26:52 +08:00
|
|
|
return analysis.getNodes().reduce(function(lastNodeUpdatedAtTime, node) {
|
2016-06-17 20:46:22 +08:00
|
|
|
var nodeUpdatedAtDate = node.getUpdatedAt();
|
|
|
|
var nodeUpdatedTimeAt = (nodeUpdatedAtDate && nodeUpdatedAtDate.getTime()) || 0;
|
|
|
|
return nodeUpdatedTimeAt > lastNodeUpdatedAtTime ? nodeUpdatedTimeAt : lastNodeUpdatedAtTime;
|
|
|
|
}, lastUpdateTime);
|
|
|
|
}, lastUpdateTime);
|
|
|
|
}
|
|
|
|
|
2016-11-22 23:41:31 +08:00
|
|
|
MapController.prototype.addAnalysesMetadata = function(username, layergroup, analysesResults, includeQuery) {
|
2016-04-21 23:03:41 +08:00
|
|
|
includeQuery = includeQuery || false;
|
2016-04-07 23:58:12 +08:00
|
|
|
analysesResults = analysesResults || [];
|
2016-04-21 23:03:41 +08:00
|
|
|
layergroup.metadata.analyses = [];
|
2016-04-07 23:58:12 +08:00
|
|
|
|
2016-04-14 17:08:39 +08:00
|
|
|
analysesResults.forEach(function(analysis) {
|
2016-07-05 17:26:52 +08:00
|
|
|
var nodes = analysis.getNodes();
|
2016-04-07 23:58:12 +08:00
|
|
|
layergroup.metadata.analyses.push({
|
|
|
|
nodes: nodes.reduce(function(nodesIdMap, node) {
|
|
|
|
if (node.params.id) {
|
2016-04-14 17:08:39 +08:00
|
|
|
var nodeResource = layergroup.layergroupid + '/analysis/node/' + node.id();
|
2016-07-11 16:42:46 +08:00
|
|
|
var nodeRepr = {
|
2016-04-12 00:49:43 +08:00
|
|
|
status: node.getStatus(),
|
2017-03-23 01:58:37 +08:00
|
|
|
url: this.resourceLocator.getUrls(username, nodeResource)
|
2016-04-07 23:58:12 +08:00
|
|
|
};
|
2016-04-21 23:03:41 +08:00
|
|
|
if (includeQuery) {
|
2016-07-11 16:42:46 +08:00
|
|
|
nodeRepr.query = node.getQuery();
|
2016-04-21 23:03:41 +08:00
|
|
|
}
|
2016-07-11 16:42:46 +08:00
|
|
|
if (node.getStatus() === 'failed') {
|
|
|
|
nodeRepr.error_message = node.getErrorMessage();
|
|
|
|
}
|
|
|
|
nodesIdMap[node.params.id] = nodeRepr;
|
2016-04-07 23:58:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return nodesIdMap;
|
2016-11-22 23:41:31 +08:00
|
|
|
}.bind(this), {})
|
2016-04-07 23:58:12 +08:00
|
|
|
});
|
2016-11-22 23:41:31 +08:00
|
|
|
}.bind(this));
|
|
|
|
};
|
2016-04-07 23:58:12 +08:00
|
|
|
|
2016-06-02 20:14:11 +08:00
|
|
|
// TODO this should take into account several URL patterns
|
2016-11-22 23:41:31 +08:00
|
|
|
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
|
|
|
|
2016-11-22 23:41:31 +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] = {
|
2017-03-23 01:58:37 +08:00
|
|
|
url: this.resourceLocator.getUrls(username, resource)
|
2016-03-19 01:09:17 +08:00
|
|
|
};
|
2016-11-22 23:41:31 +08:00
|
|
|
}.bind(this));
|
|
|
|
};
|
2016-03-19 01:09:17 +08:00
|
|
|
|
2016-11-22 23:41:31 +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)) {
|
2015-10-26 21:35:51 +08:00
|
|
|
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) {
|
2015-10-26 21:35:51 +08:00
|
|
|
var resource = layergroup.layergroupid + '/' + layerIndex + '/widget/' + widgetName;
|
2016-06-02 20:14:11 +08:00
|
|
|
layer.widgets[widgetName] = {
|
|
|
|
type: mapConfigLayer.options.widgets[widgetName].type,
|
2017-03-23 01:58:37 +08:00
|
|
|
url: this.resourceLocator.getUrls(username, resource)
|
2016-06-02 20:14:11 +08:00
|
|
|
};
|
2016-11-22 23:41:31 +08:00
|
|
|
}.bind(this));
|
2015-10-26 21:35:51 +08:00
|
|
|
}
|
|
|
|
return layer;
|
2016-11-22 23:41:31 +08:00
|
|
|
}.bind(this));
|
2015-10-26 21:35:51 +08:00
|
|
|
}
|
2016-11-22 23:41:31 +08:00
|
|
|
};
|
2017-11-01 03:49:26 +08:00
|
|
|
|
|
|
|
function mapErrorMiddleware (options) {
|
|
|
|
const { augmentError = false, label = 'MAPS CONTROLLER' } = options;
|
|
|
|
|
|
|
|
return function mapError (err, req, res, next) {
|
|
|
|
const { mapconfig } = res.locals;
|
|
|
|
|
|
|
|
if (augmentError) {
|
|
|
|
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;
|
|
|
|
}
|