2018-10-23 23:45:42 +08:00
|
|
|
'use strict';
|
|
|
|
|
2016-05-17 21:41:31 +08:00
|
|
|
var _ = require('underscore');
|
2018-04-10 00:56:01 +08:00
|
|
|
var AnalysisFilter = require('../models/filter/analysis');
|
2016-05-17 21:41:31 +08:00
|
|
|
|
2018-04-10 00:56:01 +08:00
|
|
|
function FilterStatsBackends(pgQueryRunner) {
|
2016-05-17 21:41:31 +08:00
|
|
|
this.pgQueryRunner = pgQueryRunner;
|
|
|
|
}
|
|
|
|
|
2018-04-10 00:56:01 +08:00
|
|
|
module.exports = FilterStatsBackends;
|
2016-05-17 21:41:31 +08:00
|
|
|
|
|
|
|
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) {
|
2016-05-17 21:41:31 +08:00
|
|
|
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'];
|
2016-05-17 21:41:31 +08:00
|
|
|
}
|
|
|
|
return callback(null, rows);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-10 00:56:01 +08:00
|
|
|
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
|
|
|
});
|
2016-05-17 21:41:31 +08:00
|
|
|
};
|