From 89590d32dfb2d407b77fc2c202a6e86af36494ce Mon Sep 17 00:00:00 2001 From: Javier Goizueta Date: Tue, 19 Jan 2016 19:31:43 +0100 Subject: [PATCH] Sketch of vector overviews support --- lib/cartodb/api/overviews_api.js | 65 +++++++++++++++++++ lib/cartodb/controllers/map.js | 14 ++++ .../models/mapconfig_overviews_adapter.js | 40 ++++++++++++ 3 files changed, 119 insertions(+) create mode 100644 lib/cartodb/api/overviews_api.js create mode 100644 lib/cartodb/models/mapconfig_overviews_adapter.js diff --git a/lib/cartodb/api/overviews_api.js b/lib/cartodb/api/overviews_api.js new file mode 100644 index 00000000..b3db3101 --- /dev/null +++ b/lib/cartodb/api/overviews_api.js @@ -0,0 +1,65 @@ +var queue = require('queue-async'); +var QueryTablesApi = require('./query_tables_api'); + +function OverviewsApi(pgQueryRunner) { + if (pgQueryRunner.pgQueryRunner != null) { + 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) { + this.queryTablesApi.getAffectedTablesInQuery(username, sql, function(err, tableNames){ + if (err) { + callback(err); + } else { + metadata = {}; + + var parallelism = 2; + var q = queue(parallelism); + + for ( var i=0; i < tableNames.length; ++i ) { + (function(tableName) { + q.defer(function(done){ + var sql = "SELECT * FROM CDB_Overviews('" + tableName + "');"; + this.pgQueryRunner.run(username, query, handleOverviewsRows, function(err, table_metadata){ + if (err) { + done(err); + } else { + metadata[tableName] = table_metadata; + done(null); + } + }); + }); + }(tableNames[i])); + } + + q.awaitAll(function(err, results){ + if (err) { + return callback(err); + } else { + return callback(null, metadata); + } + }); + }; + }); +}; + +function handleOverviewsRows(err, rows, callback) { + if (err){ + var msg = err.message ? err.message : err; + callback(new Error('could not get overviews metadata: ' + msg)); + return; + } + var metadata = {}; + for ( var i=0; i