2016-03-08 21:34:57 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var dot = require('dot');
|
|
|
|
dot.templateSettings.strip = false;
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function createTemplate (method) {
|
2016-03-08 21:34:57 +08:00
|
|
|
return dot.template([
|
|
|
|
'SELECT',
|
2016-09-20 22:00:06 +08:00
|
|
|
'min({{=it._column}}) min_val,',
|
|
|
|
'max({{=it._column}}) max_val,',
|
|
|
|
'avg({{=it._column}}) avg_val,',
|
2016-03-08 21:34:57 +08:00
|
|
|
method,
|
2017-08-02 23:28:46 +08:00
|
|
|
'FROM ({{=it._sql}}) _table_sql WHERE {{=it._column}} IS NOT NULL',
|
|
|
|
'AND',
|
|
|
|
' {{=it._column}} != \'infinity\'::float',
|
|
|
|
'AND',
|
|
|
|
' {{=it._column}} != \'-infinity\'::float',
|
|
|
|
'AND',
|
|
|
|
' {{=it._column}} != \'NaN\'::float'
|
2016-03-08 21:34:57 +08:00
|
|
|
].join('\n'));
|
|
|
|
}
|
|
|
|
|
|
|
|
var methods = {
|
2017-09-07 20:39:25 +08:00
|
|
|
quantiles: 'CDB_QuantileBins(array_agg({{=it._column}}::numeric), {{=it._buckets}}) as quantiles',
|
2016-03-08 21:34:57 +08:00
|
|
|
equal: 'CDB_EqualIntervalBins(array_agg({{=it._column}}::numeric), {{=it._buckets}}) as equal',
|
2017-09-07 20:39:25 +08:00
|
|
|
jenks: 'CDB_JenksBins(array_agg({{=it._column}}::numeric), {{=it._buckets}}) as jenks',
|
|
|
|
headtails: 'CDB_HeadsTailsBins(array_agg({{=it._column}}::numeric), {{=it._buckets}}) as headtails'
|
2016-03-08 21:34:57 +08:00
|
|
|
};
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
var methodTemplates = Object.keys(methods).reduce(function (methodTemplates, methodName) {
|
2016-03-08 21:34:57 +08:00
|
|
|
methodTemplates[methodName] = createTemplate(methods[methodName]);
|
|
|
|
return methodTemplates;
|
|
|
|
}, {});
|
|
|
|
|
2016-05-18 01:45:37 +08:00
|
|
|
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}}',
|
2016-10-26 19:16:24 +08:00
|
|
|
' ORDER BY 2 DESC, 1 ASC',
|
2016-05-18 01:45:37 +08:00
|
|
|
'),',
|
|
|
|
'agg_categories AS (',
|
2016-07-18 17:58:19 +08:00
|
|
|
' SELECT category',
|
2016-05-18 01:45:37 +08:00
|
|
|
' FROM categories',
|
2016-07-18 17:58:19 +08:00
|
|
|
' WHERE rank <= {{=it._buckets}}',
|
2016-05-18 01:45:37 +08:00
|
|
|
')',
|
|
|
|
'SELECT array_agg(category) AS category FROM agg_categories'
|
|
|
|
].join('\n'));
|
|
|
|
|
|
|
|
var STRATEGY = {
|
|
|
|
SPLIT: 'split',
|
|
|
|
EXACT: 'exact'
|
|
|
|
};
|
|
|
|
|
2016-05-13 18:57:43 +08:00
|
|
|
var method2strategy = {
|
2016-05-18 01:45:37 +08:00
|
|
|
headtails: STRATEGY.SPLIT,
|
|
|
|
category: STRATEGY.EXACT
|
2016-05-13 18:57:43 +08:00
|
|
|
};
|
|
|
|
|
2016-06-21 18:08:40 +08:00
|
|
|
function PostgresDatasource (psql, query) {
|
|
|
|
this.psql = psql;
|
2016-03-08 21:34:57 +08:00
|
|
|
this.query = query;
|
|
|
|
}
|
|
|
|
|
|
|
|
PostgresDatasource.prototype.getName = function () {
|
|
|
|
return 'PostgresDatasource';
|
|
|
|
};
|
|
|
|
|
|
|
|
PostgresDatasource.prototype.getRamp = function (column, buckets, method, callback) {
|
2016-05-19 21:54:58 +08:00
|
|
|
if (method && !methodTemplates.hasOwnProperty(method)) {
|
|
|
|
return callback(new Error(
|
|
|
|
'Invalid method "' + method + '", valid methods: [' + Object.keys(methodTemplates).join(',') + ']'
|
|
|
|
));
|
|
|
|
}
|
|
|
|
var methodName = method || 'quantiles';
|
2016-03-08 21:34:57 +08:00
|
|
|
var template = methodTemplates[methodName];
|
|
|
|
|
|
|
|
var query = template({ _column: column, _sql: this.query, _buckets: buckets });
|
|
|
|
|
2016-06-21 18:08:40 +08:00
|
|
|
this.psql.query(query, function (err, resultSet) {
|
2016-03-08 21:34:57 +08:00
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
2016-09-20 22:00:33 +08:00
|
|
|
var result = getResult(resultSet);
|
2016-05-18 01:45:37 +08:00
|
|
|
var strategy = method2strategy[methodName];
|
2016-09-20 22:00:33 +08:00
|
|
|
var ramp = result[methodName] || [];
|
|
|
|
var stats = {
|
|
|
|
min_val: result.min_val,
|
|
|
|
max_val: result.max_val,
|
|
|
|
avg_val: result.avg_val
|
|
|
|
};
|
2016-07-04 08:15:54 +08:00
|
|
|
// Skip null values from ramp
|
|
|
|
// Generated turbo-carto won't be correct, but better to keep it working than failing
|
|
|
|
// TODO fix cartodb-postgres extension quantification functions
|
2019-10-22 01:07:24 +08:00
|
|
|
ramp = ramp.filter(function (value) { return value !== null; });
|
2016-05-18 01:45:37 +08:00
|
|
|
if (strategy !== STRATEGY.EXACT) {
|
2019-10-22 01:07:24 +08:00
|
|
|
ramp = ramp.sort(function (a, b) {
|
2016-05-18 01:45:37 +08:00
|
|
|
return a - b;
|
|
|
|
});
|
|
|
|
}
|
2016-03-08 21:34:57 +08:00
|
|
|
|
2016-09-20 22:00:33 +08:00
|
|
|
return callback(null, { ramp: ramp, strategy: strategy, stats: stats });
|
2016-06-21 18:08:40 +08:00
|
|
|
}, true); // use read-only transaction
|
2016-03-08 21:34:57 +08:00
|
|
|
};
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function getResult (resultSet) {
|
2016-09-20 22:00:33 +08:00
|
|
|
resultSet = resultSet || {};
|
|
|
|
var result = resultSet.rows || [];
|
|
|
|
result = result[0] || {};
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-03-08 21:34:57 +08:00
|
|
|
module.exports = PostgresDatasource;
|