2016-07-12 22:08:48 +08:00
|
|
|
var PSQL = require('cartodb-psql');
|
|
|
|
var cors = require('../middleware/cors');
|
|
|
|
var userMiddleware = require('../middleware/user');
|
2018-03-15 00:46:19 +08:00
|
|
|
const rateLimit = require('../middleware/rate-limit');
|
|
|
|
const { RATE_LIMIT_ENDPOINTS_GROUPS } = rateLimit;
|
2016-07-12 22:08:48 +08:00
|
|
|
|
2018-03-15 00:46:19 +08:00
|
|
|
function AnalysesController(prepareContext, userLimitsApi) {
|
2017-09-26 01:40:27 +08:00
|
|
|
this.prepareContext = prepareContext;
|
2018-03-15 00:46:19 +08:00
|
|
|
this.userLimitsApi = userLimitsApi;
|
2016-07-12 22:08:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = AnalysesController;
|
|
|
|
|
2017-11-18 02:20:42 +08:00
|
|
|
AnalysesController.prototype.register = function (app) {
|
2017-10-05 18:12:21 +08:00
|
|
|
app.get(
|
2017-11-19 21:05:20 +08:00
|
|
|
`${app.base_url_mapconfig}/analyses/catalog`,
|
2017-09-22 06:42:17 +08:00
|
|
|
cors(),
|
2018-03-01 22:42:03 +08:00
|
|
|
userMiddleware(),
|
2018-03-15 00:46:19 +08:00
|
|
|
rateLimit(this.userLimitsApi, RATE_LIMIT_ENDPOINTS_GROUPS.ANALYSIS_CATALOG),
|
2018-03-15 01:06:06 +08:00
|
|
|
this.prepareContext,
|
2018-03-13 18:42:25 +08:00
|
|
|
createPGClient(),
|
|
|
|
getDataFromQuery({ queryTemplate: catalogQueryTpl, key: 'catalog' }),
|
|
|
|
getDataFromQuery({ queryTemplate: tablesQueryTpl, key: 'tables' }),
|
|
|
|
prepareResponse(),
|
|
|
|
setCacheControlHeader(),
|
|
|
|
sendResponse(),
|
2018-03-13 18:43:08 +08:00
|
|
|
unauthorizedError()
|
2017-09-22 06:42:17 +08:00
|
|
|
);
|
2016-07-12 22:08:48 +08:00
|
|
|
};
|
|
|
|
|
2018-03-13 18:42:25 +08:00
|
|
|
function createPGClient () {
|
2017-11-18 02:20:42 +08:00
|
|
|
return function createPGClientMiddleware (req, res, next) {
|
|
|
|
res.locals.pg = new PSQL(dbParamsFromReqParams(res.locals));
|
|
|
|
next();
|
|
|
|
};
|
2018-03-13 18:42:25 +08:00
|
|
|
}
|
2017-11-18 02:20:42 +08:00
|
|
|
|
2018-03-13 18:42:25 +08:00
|
|
|
function getDataFromQuery({ queryTemplate, key }) {
|
2017-11-19 19:37:09 +08:00
|
|
|
const readOnlyTransactionOn = true;
|
2017-11-18 02:14:31 +08:00
|
|
|
|
2017-11-19 19:37:09 +08:00
|
|
|
return function getCatalogMiddleware(req, res, next) {
|
2017-11-18 02:20:42 +08:00
|
|
|
const { pg, user } = res.locals;
|
2017-11-19 19:37:09 +08:00
|
|
|
const sql = queryTemplate({ _username: user });
|
2017-11-18 02:14:31 +08:00
|
|
|
|
2017-11-19 19:37:09 +08:00
|
|
|
pg.query(sql, (err, resultSet = {}) => {
|
2017-11-18 02:14:31 +08:00
|
|
|
if (err) {
|
2017-11-18 01:32:19 +08:00
|
|
|
return next(err);
|
2017-11-18 01:28:37 +08:00
|
|
|
}
|
2017-11-18 01:32:19 +08:00
|
|
|
|
2017-11-19 19:37:09 +08:00
|
|
|
res.locals[key] = resultSet.rows || [];
|
2017-11-18 02:14:31 +08:00
|
|
|
|
2017-11-18 01:32:19 +08:00
|
|
|
next();
|
2017-11-18 02:14:31 +08:00
|
|
|
}, readOnlyTransactionOn);
|
2017-11-18 01:28:37 +08:00
|
|
|
};
|
2018-03-13 18:42:25 +08:00
|
|
|
}
|
2017-11-18 01:28:37 +08:00
|
|
|
|
2018-03-13 18:42:25 +08:00
|
|
|
function prepareResponse () {
|
2017-11-18 01:25:13 +08:00
|
|
|
return function prepareResponseMiddleware (req, res, next) {
|
2017-11-18 02:14:31 +08:00
|
|
|
const { catalog, tables } = res.locals;
|
2017-11-18 01:25:13 +08:00
|
|
|
|
2017-11-18 02:14:31 +08:00
|
|
|
const analysisIdToTable = tables.reduce((analysisIdToTable, table) => {
|
2017-11-18 01:25:13 +08:00
|
|
|
const analysisId = table.relname.split('_')[2];
|
2017-11-19 21:05:20 +08:00
|
|
|
|
2017-11-18 01:25:13 +08:00
|
|
|
if (analysisId && analysisId.length === 40) {
|
|
|
|
analysisIdToTable[analysisId] = table;
|
|
|
|
}
|
2017-11-19 21:05:20 +08:00
|
|
|
|
2017-11-18 01:25:13 +08:00
|
|
|
return analysisIdToTable;
|
|
|
|
}, {});
|
|
|
|
|
2017-11-18 02:14:31 +08:00
|
|
|
const analysisCatalog = catalog.map(analysis => {
|
2017-11-18 01:25:13 +08:00
|
|
|
if (analysisIdToTable.hasOwnProperty(analysis.node_id)) {
|
|
|
|
analysis.table = analysisIdToTable[analysis.node_id];
|
|
|
|
}
|
2017-11-19 21:05:20 +08:00
|
|
|
|
2017-11-18 01:25:13 +08:00
|
|
|
return analysis;
|
|
|
|
})
|
|
|
|
.sort((analysisA, analysisB) => {
|
|
|
|
if (!!analysisA.table && !!analysisB.table) {
|
|
|
|
return analysisB.table.size - analysisA.table.size;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!!analysisA.table) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!!analysisB.table) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
});
|
|
|
|
|
2017-11-18 02:14:31 +08:00
|
|
|
res.body = { catalog: analysisCatalog };
|
|
|
|
|
2017-11-18 01:25:13 +08:00
|
|
|
next();
|
|
|
|
};
|
2018-03-13 18:42:25 +08:00
|
|
|
}
|
2017-11-18 01:25:13 +08:00
|
|
|
|
2018-03-13 18:42:25 +08:00
|
|
|
function setCacheControlHeader () {
|
2017-11-14 20:53:42 +08:00
|
|
|
return function setCacheControlHeaderMiddleware (req, res, next) {
|
2017-11-14 20:49:12 +08:00
|
|
|
res.set('Cache-Control', 'public,max-age=10,must-revalidate');
|
2017-11-14 20:53:42 +08:00
|
|
|
next();
|
|
|
|
};
|
2018-03-13 18:42:25 +08:00
|
|
|
}
|
2017-10-31 02:28:40 +08:00
|
|
|
|
2018-03-13 18:42:25 +08:00
|
|
|
function sendResponse () {
|
2017-11-14 20:53:42 +08:00
|
|
|
return function sendResponseMiddleware (req, res) {
|
2017-11-14 20:49:12 +08:00
|
|
|
res.status(200);
|
2017-10-31 02:28:40 +08:00
|
|
|
|
2017-11-14 20:49:12 +08:00
|
|
|
if (req.query && req.query.callback) {
|
|
|
|
res.jsonp(res.body);
|
|
|
|
} else {
|
|
|
|
res.json(res.body);
|
|
|
|
}
|
|
|
|
};
|
2018-03-13 18:42:25 +08:00
|
|
|
}
|
2016-07-12 22:08:48 +08:00
|
|
|
|
2018-03-13 18:43:08 +08:00
|
|
|
function unauthorizedError () {
|
2017-11-18 02:14:31 +08:00
|
|
|
return function unathorizedErrorMiddleware(err, req, res, next) {
|
|
|
|
if (err.message.match(/permission\sdenied/)) {
|
|
|
|
err = new Error('Unauthorized');
|
|
|
|
err.http_status = 401;
|
|
|
|
}
|
|
|
|
|
|
|
|
next(err);
|
|
|
|
};
|
2018-03-13 18:42:25 +08:00
|
|
|
}
|
2017-11-18 02:14:31 +08:00
|
|
|
|
2017-11-19 19:51:35 +08:00
|
|
|
const catalogQueryTpl = ctx => `
|
|
|
|
SELECT analysis_def->>'type' as type, * FROM cdb_analysis_catalog WHERE username = '${ctx._username}'
|
|
|
|
`;
|
|
|
|
|
|
|
|
var tablesQueryTpl = ctx => `
|
|
|
|
WITH analysis_tables AS (
|
|
|
|
SELECT
|
|
|
|
n.nspname AS nspname,
|
|
|
|
c.relname AS relname,
|
|
|
|
pg_total_relation_size(
|
|
|
|
format('%s.%s', pg_catalog.quote_ident(n.nspname), pg_catalog.quote_ident(c.relname))
|
|
|
|
) AS size,
|
|
|
|
format('%s.%s', pg_catalog.quote_ident(nspname), pg_catalog.quote_ident(relname)) AS fully_qualified_name
|
|
|
|
FROM pg_catalog.pg_class c, pg_catalog.pg_namespace n
|
|
|
|
WHERE c.relnamespace = n.oid
|
|
|
|
AND pg_catalog.quote_ident(c.relname) ~ '^analysis_[a-z0-9]{10}_[a-z0-9]{40}$'
|
|
|
|
AND n.nspname IN ('${ctx._username}', 'public')
|
|
|
|
)
|
|
|
|
SELECT *, pg_size_pretty(size) as size_pretty
|
|
|
|
FROM analysis_tables
|
|
|
|
ORDER BY size DESC
|
|
|
|
`;
|
2016-07-12 22:08:48 +08:00
|
|
|
|
|
|
|
function dbParamsFromReqParams(params) {
|
|
|
|
var dbParams = {};
|
|
|
|
if ( params.dbuser ) {
|
|
|
|
dbParams.user = params.dbuser;
|
|
|
|
}
|
|
|
|
if ( params.dbpassword ) {
|
|
|
|
dbParams.pass = params.dbpassword;
|
|
|
|
}
|
|
|
|
if ( params.dbhost ) {
|
|
|
|
dbParams.host = params.dbhost;
|
|
|
|
}
|
|
|
|
if ( params.dbport ) {
|
|
|
|
dbParams.port = params.dbport;
|
|
|
|
}
|
|
|
|
if ( params.dbname ) {
|
|
|
|
dbParams.dbname = params.dbname;
|
|
|
|
}
|
|
|
|
return dbParams;
|
|
|
|
}
|