Use windshaft-stats to get layer stats

This commit is contained in:
Mario de Frutos 2017-05-08 18:42:40 +02:00
parent 05d3b3bf66
commit 3f6c8fa51c
4 changed files with 65 additions and 6 deletions

1
.gitignore vendored
View File

@ -11,3 +11,4 @@ redis.pid
test.log
npm-debug.log
coverage/
.DS_Store

View File

@ -2,6 +2,7 @@ var _ = require('underscore');
var assert = require('assert');
var step = require('step');
var windshaft = require('windshaft');
var windshaftStats = require('windshaft-stats');
var QueryTables = require('cartodb-query-tables');
var ResourceLocator = require('../models/resource-locator');
@ -19,6 +20,7 @@ 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');
var DummyMapConfigProvider = require('../models/mapconfig/provider/dummy-mapconfig-provider');
/**
* @param {AuthApi} authApi
@ -33,7 +35,8 @@ var CreateLayergroupMapConfigProvider = require('../models/mapconfig/provider/cr
* @constructor
*/
function MapController(authApi, pgConnection, templateMaps, mapBackend, metadataBackend,
surrogateKeysCache, userLimitsApi, layergroupAffectedTables, mapConfigAdapter) {
surrogateKeysCache, userLimitsApi, layergroupAffectedTables, mapConfigAdapter,
rendererCache) {
BaseController.call(this, authApi, pgConnection);
@ -47,6 +50,8 @@ function MapController(authApi, pgConnection, templateMaps, mapBackend, metadata
this.mapConfigAdapter = mapConfigAdapter;
this.resourceLocator = new ResourceLocator(global.environment);
this.rendererCache = rendererCache;
}
util.inherits(MapController, BaseController);
@ -161,7 +166,7 @@ MapController.prototype.create = function(req, res, prepareConfigFn) {
},
function afterLayergroupCreate(err, layergroup) {
assert.ifError(err);
self.afterLayergroupCreate(req, res, mapConfig, layergroup, context.analysesResults, this);
self.afterLayergroupCreate(req, res, mapConfig, layergroup, context.analysesResults, req.params, this);
},
function finish(err, layergroup) {
if (err) {
@ -214,6 +219,7 @@ MapController.prototype.instantiateTemplate = function(req, res, prepareParamsFn
var mapConfigProvider;
var mapConfig;
var rendererParams;
step(
function setupParams(){
@ -238,8 +244,9 @@ MapController.prototype.instantiateTemplate = function(req, res, prepareParamsFn
);
mapConfigProvider.getMapConfig(this);
},
function createLayergroup(err, mapConfig_, rendererParams) {
function createLayergroup(err, mapConfig_, _rendererParams) {
assert.ifError(err);
rendererParams = _rendererParams;
mapConfig = mapConfig_;
self.mapBackend.createLayergroup(
mapConfig, rendererParams,
@ -249,7 +256,7 @@ MapController.prototype.instantiateTemplate = function(req, res, prepareParamsFn
},
function afterLayergroupCreate(err, layergroup) {
assert.ifError(err);
self.afterLayergroupCreate(req, res, mapConfig, layergroup, mapConfigProvider.analysesResults, this);
self.afterLayergroupCreate(req, res, mapConfig, layergroup, mapConfigProvider.analysesResults, rendererParams, this);
},
function finishTemplateInstantiation(err, layergroup) {
if (err) {
@ -272,7 +279,7 @@ MapController.prototype.instantiateTemplate = function(req, res, prepareParamsFn
);
};
MapController.prototype.afterLayergroupCreate = function(req, res, mapconfig, layergroup, analysesResults, callback) {
MapController.prototype.afterLayergroupCreate = function(req, res, mapconfig, layergroup, analysesResults, rendererParams, callback) {
var self = this;
var username = req.context.user;
@ -318,6 +325,7 @@ MapController.prototype.afterLayergroupCreate = function(req, res, mapconfig, la
var dbName = req.params.dbname;
var layergroupId = layergroup.layergroupid;
var dbConnection;
step(
function getPgConnection() {
@ -325,7 +333,8 @@ MapController.prototype.afterLayergroupCreate = function(req, res, mapconfig, la
},
function getAffectedTablesAndLastUpdatedTime(err, connection) {
assert.ifError(err);
QueryTables.getAffectedTablesFromQuery(connection, sql.join(';'), this);
dbConnection = connection;
QueryTables.getAffectedTablesFromQuery(dbConnection, sql.join(';'), this);
},
function handleAffectedTablesAndLastUpdatedTime(err, result) {
req.profiler.done('queryTablesAndLastUpdated');
@ -352,6 +361,34 @@ MapController.prototype.afterLayergroupCreate = function(req, res, mapconfig, la
return null;
},
function fetchLayersMetadata(err) {
assert.ifError(err);
var next = this;
var mapConfigProvider = new DummyMapConfigProvider(mapconfig, rendererParams);
var layerStats = windshaftStats();
layerStats.getStats(self.rendererCache, rendererParams, dbConnection, mapConfigProvider, function(err, layersStats) {
if (err) {
return next(err);
}
if (layersStats) {
res.metadata = res.metadata || {};
res.metadata.layers = layersStats;
// backwards compatibility for torque
var torqueMetadata = layersStats.reduce(function(acc, layer, layerId) {
if (layer.type === 'torque') {
acc[layerId] = layer.meta;
}
return acc;
}, {});
if (Object.keys(torqueMetadata).length) {
res.metadata.torque = torqueMetadata;
}
}
return next();
});
},
function finish(err) {
done(err);
}

View File

@ -0,0 +1,20 @@
var util = require('util');
var MapStoreMapConfigProvider = require('windshaft').model.provider.MapStoreMapConfig;
function DummyMapConfigProvider(mapConfig, params) {
MapStoreMapConfigProvider.call(this, undefined, params);
this.mapConfig = mapConfig;
}
util.inherits(DummyMapConfigProvider, MapStoreMapConfigProvider);
module.exports = DummyMapConfigProvider;
DummyMapConfigProvider.prototype.setParams = function(params) {
this.params = params;
};
DummyMapConfigProvider.prototype.getMapConfig = function(callback) {
return callback(null, this.mapConfig, this.params, {});
};

View File

@ -40,6 +40,7 @@
"turbo-carto": "0.19.0",
"underscore": "~1.6.0",
"windshaft": "cartodb/windshaft#548-vector-buffer-size",
"windshaft-stats": "0.0.1",
"yargs": "~5.0.0"
},
"devDependencies": {