Merge pull request #462 from CartoDB/turbo-carto-category

Adds turbo-carto category quantification with exact strategy
This commit is contained in:
Raul Ochoa 2016-05-19 17:11:39 +02:00
commit 02ae50eef0

View File

@ -23,8 +23,35 @@ var methodTemplates = Object.keys(methods).reduce(function(methodTemplates, meth
return methodTemplates; return methodTemplates;
}, {}); }, {});
methodTemplates.category = dot.template([
'WITH',
'categories AS (',
' SELECT {{=it._column}} AS category, count(1) AS value, row_number() OVER (ORDER BY count(1) desc) as rank',
' FROM ({{=it._sql}}) _cdb_aggregation_all',
' GROUP BY {{=it._column}}',
' ORDER BY 2 DESC',
'),',
'agg_categories AS (',
' SELECT \'__other\' category',
' FROM categories',
' WHERE rank >= {{=it._buckets}}',
' GROUP BY 1',
' UNION ALL',
' SELECT CAST(category AS text)',
' FROM categories',
' WHERE rank < {{=it._buckets}}',
')',
'SELECT array_agg(category) AS category FROM agg_categories'
].join('\n'));
var STRATEGY = {
SPLIT: 'split',
EXACT: 'exact'
};
var method2strategy = { var method2strategy = {
headtails: 'split' headtails: STRATEGY.SPLIT,
category: STRATEGY.EXACT
}; };
function PostgresDatasource (pgQueryRunner, username, query) { function PostgresDatasource (pgQueryRunner, username, query) {
@ -53,11 +80,15 @@ PostgresDatasource.prototype.getRamp = function (column, buckets, method, callba
return callback(err); return callback(err);
} }
var ramp = result[0][methodName].sort(function(a, b) { var strategy = method2strategy[methodName];
return a - b; var ramp = result[0][methodName];
}); if (strategy !== STRATEGY.EXACT) {
ramp = ramp.sort(function(a, b) {
return a - b;
});
}
return callback(null, { ramp: ramp, strategy: method2strategy[methodName] }); return callback(null, { ramp: ramp, strategy: strategy });
}); });
}; };