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');
2019-10-22 01:07:24 +08:00
function FilterStatsBackends (pgQueryRunner) {
this.pgQueryRunner = pgQueryRunner;
}
module.exports = FilterStatsBackends;
2019-10-22 01:07:24 +08:00
function getEstimatedRows (pgQueryRunner, username, query, callback) {
2019-11-14 18:36:47 +08:00
pgQueryRunner.run(username, 'EXPLAIN (FORMAT JSON)' + query, function (err, resultRows) {
2019-10-22 01:07:24 +08:00
if (err) {
callback(err);
return;
}
var rows;
2019-11-14 18:36:47 +08:00
if (resultRows[0] && resultRows[0]['QUERY PLAN'] &&
resultRows[0]['QUERY PLAN'][0] && resultRows[0]['QUERY PLAN'][0].Plan) {
rows = resultRows[0]['QUERY PLAN'][0].Plan['Plan Rows'];
}
return callback(null, rows);
});
}
2019-11-14 18:36:47 +08:00
FilterStatsBackends.prototype.getFilterStats = function (username, unfilteredQuery, filters, callback) {
2018-07-30 21:50:41 +08:00
var stats = {};
2018-07-30 21:52:41 +08:00
2019-11-14 18:36:47 +08:00
getEstimatedRows(this.pgQueryRunner, username, unfilteredQuery, (err, rows) => {
2019-10-22 01:07:24 +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);
2019-11-14 18:36:47 +08:00
var query = analysisFilter.sql(unfilteredQuery);
2018-07-30 22:38:04 +08:00
2018-07-31 21:07:34 +08:00
getEstimatedRows(this.pgQueryRunner, username, query, (err, rows) => {
2019-10-22 01:07:24 +08:00
if (err) {
2018-07-31 21:07:34 +08:00
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
});
};