Create stats backend to decouple stats logic from map controller

This commit is contained in:
Mario de Frutos 2017-05-09 12:31:16 +02:00
parent 568d6b5458
commit 15c68711aa
3 changed files with 21 additions and 7 deletions

View File

@ -0,0 +1,12 @@
var windshaftStats = require('windshaft-stats');
function StatsBackend(rendererCache) {
this.rendererCache = rendererCache;
};
module.exports = StatsBackend;
StatsBackend.prototype.getStats = function(params, dbConnection, mapConfigProvider, callback) {
var layerStats = windshaftStats();
layerStats.getStats(this.rendererCache, params, dbConnection, mapConfigProvider, callback);
};

View File

@ -2,7 +2,6 @@ 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');
@ -32,12 +31,12 @@ var DummyMapConfigProvider = require('../models/mapconfig/provider/dummy-mapconf
* @param {UserLimitsApi} userLimitsApi
* @param {LayergroupAffectedTables} layergroupAffectedTables
* @param {MapConfigAdapter} mapConfigAdapter
* @param {RendererCache} rendererCache
* @param {StatsBackend} statsBackend
* @constructor
*/
function MapController(authApi, pgConnection, templateMaps, mapBackend, metadataBackend,
surrogateKeysCache, userLimitsApi, layergroupAffectedTables, mapConfigAdapter,
rendererCache) {
statsBackend) {
BaseController.call(this, authApi, pgConnection);
@ -52,7 +51,7 @@ function MapController(authApi, pgConnection, templateMaps, mapBackend, metadata
this.mapConfigAdapter = mapConfigAdapter;
this.resourceLocator = new ResourceLocator(global.environment);
this.rendererCache = rendererCache;
this.statsBackend = statsBackend;
}
util.inherits(MapController, BaseController);
@ -366,8 +365,7 @@ MapController.prototype.afterLayergroupCreate = function(req, res, mapconfig, la
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) {
self.statsBackend.getStats(rendererParams, dbConnection, mapConfigProvider, function(err, layersStats) {
if (err) {
return next(err);
}

View File

@ -41,6 +41,8 @@ var TurboCartoAdapter = require('./models/mapconfig/adapter/turbo-carto-adapter'
var DataviewsWidgetsAdapter = require('./models/mapconfig/adapter/dataviews-widgets-adapter');
var MapConfigAdapter = require('./models/mapconfig/adapter');
var StatsBackend = require('./backends/stats');
module.exports = function(serverOptions) {
// Make stats client globally accessible
global.statsClient = StatsClient.getInstance(serverOptions.statsd);
@ -150,6 +152,8 @@ module.exports = function(serverOptions) {
var analysisBackend = new AnalysisBackend(metadataBackend, serverOptions.analysis);
var statsBackend = new StatsBackend(rendererCache);
var layergroupAffectedTablesCache = new LayergroupAffectedTablesCache();
app.layergroupAffectedTablesCache = layergroupAffectedTablesCache;
@ -208,7 +212,7 @@ module.exports = function(serverOptions) {
userLimitsApi,
layergroupAffectedTablesCache,
mapConfigAdapter,
rendererCache
statsBackend
).register(app);
new controller.NamedMaps(