Windshaft-cartodb/lib/backends/filter-stats.js

54 lines
1.5 KiB
JavaScript
Raw Normal View History

'use strict';
var _ = require('underscore');
var AnalysisFilter = require('../models/filter/analysis');
function FilterStatsBackends(pgQueryRunner) {
this.pgQueryRunner = pgQueryRunner;
}
module.exports = FilterStatsBackends;
function getEstimatedRows(pgQueryRunner, username, query, callback) {
2016-05-18 19:09:55 +08:00
pgQueryRunner.run(username, "EXPLAIN (FORMAT JSON)"+query, function(err, result_rows) {
if (err){
callback(err);
return;
}
var rows;
2016-05-18 19:09:55 +08:00
if ( result_rows[0] && result_rows[0]['QUERY PLAN'] &&
result_rows[0]['QUERY PLAN'][0] && result_rows[0]['QUERY PLAN'][0].Plan ) {
rows = result_rows[0]['QUERY PLAN'][0].Plan['Plan Rows'];
}
return callback(null, rows);
});
}
FilterStatsBackends.prototype.getFilterStats = function (username, unfiltered_query, filters, callback) {
2018-07-30 21:50:41 +08:00
var stats = {};
2018-07-30 21:52:41 +08:00
2018-07-30 22:44:30 +08:00
getEstimatedRows(this.pgQueryRunner, username, unfiltered_query, (err, rows) => {
2018-07-30 21:52:41 +08:00
if (err){
2018-07-30 21:55:24 +08:00
return callback(err);
2018-07-30 21:52:41 +08:00
}
2018-07-30 21:55:24 +08:00
2018-07-30 21:52:41 +08:00
stats.unfiltered_rows = rows;
2018-07-30 21:56:18 +08:00
2018-07-31 21:07:34 +08:00
if (!filters || _.isEmpty(filters)) {
return callback(null, stats);
}
2018-07-30 22:42:17 +08:00
2018-07-31 21:07:34 +08:00
var analysisFilter = new AnalysisFilter(filters);
var query = analysisFilter.sql(unfiltered_query);
2018-07-30 22:38:04 +08:00
2018-07-31 21:07:34 +08:00
getEstimatedRows(this.pgQueryRunner, username, query, (err, rows) => {
if (err){
return callback(err);
}
2018-07-30 22:42:17 +08:00
2018-07-31 21:07:34 +08:00
stats.filtered_rows = rows;
2018-07-30 22:38:04 +08:00
return callback(null, stats);
2018-07-31 21:07:34 +08:00
});
2018-07-30 22:42:17 +08:00
});
};