Windshaft-cartodb/lib/cartodb/api/overviews_api.js

62 lines
2.2 KiB
JavaScript
Raw Normal View History

2016-01-20 02:31:43 +08:00
var queue = require('queue-async');
var QueryTablesApi = require('./query_tables_api');
function OverviewsApi(pgQueryRunner) {
2016-01-20 18:51:46 +08:00
if (pgQueryRunner.pgQueryRunner !== undefined) {
2016-01-20 02:31:43 +08:00
this.queryTablesApi = pgQueryRunner;
this.pgQueryRunner = this.queryTablesApi.pgQueryRunner;
} else {
this.pgQueryRunner = pgQueryRunner;
this.queryTablesApi = new QueryTablesApi(pgQueryRunner);
}
}
module.exports = OverviewsApi;
OverviewsApi.prototype.getOverviewsMetadata = function (username, sql, callback) {
2016-01-20 20:13:02 +08:00
var self = this;
2016-01-20 02:31:43 +08:00
this.queryTablesApi.getAffectedTablesInQuery(username, sql, function(err, tableNames){
if (err) {
callback(err);
} else {
var parallelism = 2;
var q = queue(parallelism);
tableNames.forEach(function(tableName) {
q.defer(function(done){
2016-01-20 18:49:17 +08:00
var query = "SELECT * FROM CDB_Overviews('" + tableName + "');";
2016-01-20 20:13:02 +08:00
self.pgQueryRunner.run(username, query, function handleOverviewsRows(err, rows) {
if (err){
var msg = err.message ? err.message : err;
done(new Error('could not get overviews metadata: ' + msg));
return;
}
done(null, [tableName, rows]);
2016-01-20 02:31:43 +08:00
});
});
});
2016-01-20 02:31:43 +08:00
var metadata = {};
q.awaitAll(function(err, results){
2016-01-20 02:31:43 +08:00
if (err) {
return callback(err);
} else {
results.forEach(function(table_rows) {
var tableName = table_rows[0];
var rows = table_rows[1];
if ( rows.length > 0 ) {
var table_metadata = {};
for ( var i=0; i<rows.length; ++i ) {
var row = rows[i];
table_metadata[row.z] = { table: row.overview_table };
}
metadata[tableName] = table_metadata;
}
});
2016-01-20 02:31:43 +08:00
return callback(null, metadata);
}
});
2016-01-20 18:51:46 +08:00
}
2016-01-20 02:31:43 +08:00
});
};