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
|
|
|
|
2019-10-22 01:07:24 +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
|
|
|
|
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) {
|
2016-05-17 21:41:31 +08:00
|
|
|
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'];
|
2016-05-17 21:41:31 +08:00
|
|
|
}
|
|
|
|
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
|
|
|
});
|
2016-05-17 21:41:31 +08:00
|
|
|
};
|