2015-12-07 16:40:51 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var _ = require('underscore');
|
|
|
|
var step = require('step');
|
|
|
|
var assert = require('assert');
|
2016-04-18 22:24:52 +08:00
|
|
|
var util = require('util');
|
2015-12-07 16:40:51 +08:00
|
|
|
|
2016-01-21 23:17:17 +08:00
|
|
|
var AuthApi = require('../auth/auth_api');
|
2015-12-07 16:40:51 +08:00
|
|
|
var CdbRequest = require('../models/cartodb_request');
|
|
|
|
var handleException = require('../utils/error_handler');
|
|
|
|
var cdbReq = new CdbRequest();
|
|
|
|
|
2016-04-18 22:24:52 +08:00
|
|
|
var ONE_KILOBYTE_IN_BYTES = 1024;
|
|
|
|
var MAX_LIMIT_QUERY_SIZE_IN_BYTES = 4 * ONE_KILOBYTE_IN_BYTES; // 4kb
|
2016-04-18 21:30:16 +08:00
|
|
|
|
|
|
|
function reachMaxQuerySizeLimit(query) {
|
2016-04-18 22:24:52 +08:00
|
|
|
var querySize;
|
2016-04-18 21:30:16 +08:00
|
|
|
|
|
|
|
try {
|
2016-04-18 22:24:52 +08:00
|
|
|
querySize = (typeof query === 'string') ? query.length : JSON.stringify(query).length;
|
2016-04-18 21:30:16 +08:00
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-18 22:24:52 +08:00
|
|
|
return querySize > MAX_LIMIT_QUERY_SIZE_IN_BYTES;
|
|
|
|
}
|
2016-04-18 21:30:16 +08:00
|
|
|
|
2016-04-18 22:24:52 +08:00
|
|
|
function getMaxSizeErrorMessage(sql) {
|
|
|
|
return util.format(
|
|
|
|
'Query is too long (%s). Max size allowed is %s (%skb)',
|
|
|
|
sql.length,
|
|
|
|
MAX_LIMIT_QUERY_SIZE_IN_BYTES,
|
|
|
|
Math.round(MAX_LIMIT_QUERY_SIZE_IN_BYTES / ONE_KILOBYTE_IN_BYTES)
|
|
|
|
);
|
2016-04-18 21:30:16 +08:00
|
|
|
}
|
|
|
|
|
2016-01-28 20:10:21 +08:00
|
|
|
function JobController(userDatabaseService, jobBackend, jobCanceller) {
|
2016-01-22 00:24:48 +08:00
|
|
|
this.userDatabaseService = userDatabaseService;
|
|
|
|
this.jobBackend = jobBackend;
|
|
|
|
this.jobCanceller = jobCanceller;
|
2015-12-07 16:40:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
JobController.prototype.route = function (app) {
|
2016-01-19 03:12:44 +08:00
|
|
|
app.post(global.settings.base_url + '/sql/job', this.createJob.bind(this));
|
|
|
|
app.get(global.settings.base_url + '/sql/job', this.listJob.bind(this));
|
|
|
|
app.get(global.settings.base_url + '/sql/job/:job_id', this.getJob.bind(this));
|
|
|
|
app.delete(global.settings.base_url + '/sql/job/:job_id', this.cancelJob.bind(this));
|
|
|
|
app.put(global.settings.base_url + '/sql/job/:job_id', this.updateJob.bind(this));
|
|
|
|
app.patch(global.settings.base_url + '/sql/job/:job_id', this.updateJob.bind(this));
|
2015-12-31 03:16:18 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
JobController.prototype.cancelJob = function (req, res) {
|
|
|
|
var self = this;
|
|
|
|
var job_id = req.params.job_id;
|
|
|
|
var body = (req.body) ? req.body : {};
|
|
|
|
var params = _.extend({}, req.query, body); // clone so don't modify req.params or req.body so oauth is not broken
|
|
|
|
var cdbUsername = cdbReq.userByReq(req);
|
|
|
|
|
|
|
|
if ( req.profiler ) {
|
|
|
|
req.profiler.start('sqlapi.job');
|
|
|
|
req.profiler.done('init');
|
|
|
|
}
|
|
|
|
|
|
|
|
step(
|
|
|
|
function getUserDBInfo() {
|
2016-01-21 22:05:46 +08:00
|
|
|
var next = this;
|
2016-01-21 23:17:17 +08:00
|
|
|
var authApi = new AuthApi(req, params);
|
|
|
|
|
2016-01-28 21:14:31 +08:00
|
|
|
self.userDatabaseService.getConnectionParams(authApi, cdbUsername, next);
|
2015-12-31 03:16:18 +08:00
|
|
|
},
|
|
|
|
function cancelJob(err, userDatabase) {
|
|
|
|
assert.ifError(err);
|
|
|
|
|
|
|
|
if (!userDatabase.authenticated) {
|
|
|
|
throw new Error('permission denied');
|
|
|
|
}
|
|
|
|
|
|
|
|
var next = this;
|
|
|
|
|
|
|
|
if ( req.profiler ) {
|
|
|
|
req.profiler.done('setDBAuth');
|
|
|
|
}
|
|
|
|
|
2016-01-08 22:47:59 +08:00
|
|
|
self.jobCanceller.cancel(job_id, function (err, job) {
|
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
2016-01-05 02:08:13 +08:00
|
|
|
|
2016-01-08 22:47:59 +08:00
|
|
|
next(null, {
|
|
|
|
job: job,
|
|
|
|
host: userDatabase.host
|
2015-12-31 03:16:18 +08:00
|
|
|
});
|
2016-01-08 22:47:59 +08:00
|
|
|
});
|
2015-12-31 03:16:18 +08:00
|
|
|
},
|
|
|
|
function handleResponse(err, result) {
|
|
|
|
if ( err ) {
|
|
|
|
return handleException(err, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( req.profiler ) {
|
|
|
|
req.profiler.done('cancelJob');
|
|
|
|
res.header('X-SQLAPI-Profiler', req.profiler.toJSONString());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (global.settings.api_hostname) {
|
|
|
|
res.header('X-Served-By-Host', global.settings.api_hostname);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result.host) {
|
|
|
|
res.header('X-Served-By-DB-Host', result.host);
|
|
|
|
}
|
|
|
|
|
2016-01-12 03:14:15 +08:00
|
|
|
res.send(result.job);
|
2015-12-31 03:16:18 +08:00
|
|
|
}
|
|
|
|
);
|
2015-12-07 16:40:51 +08:00
|
|
|
};
|
|
|
|
|
2015-12-25 00:42:49 +08:00
|
|
|
JobController.prototype.listJob = function (req, res) {
|
2015-12-22 18:06:46 +08:00
|
|
|
var self = this;
|
|
|
|
var body = (req.body) ? req.body : {};
|
|
|
|
var params = _.extend({}, req.query, body); // clone so don't modify req.params or req.body so oauth is not broken
|
|
|
|
var cdbUsername = cdbReq.userByReq(req);
|
|
|
|
|
2015-12-22 18:45:25 +08:00
|
|
|
if ( req.profiler ) {
|
|
|
|
req.profiler.start('sqlapi.job');
|
|
|
|
req.profiler.done('init');
|
|
|
|
}
|
|
|
|
|
|
|
|
step(
|
|
|
|
function getUserDBInfo() {
|
2016-01-21 22:05:46 +08:00
|
|
|
var next = this;
|
2016-01-21 23:17:17 +08:00
|
|
|
var authApi = new AuthApi(req, params);
|
|
|
|
|
2016-01-28 21:14:31 +08:00
|
|
|
self.userDatabaseService.getConnectionParams(authApi, cdbUsername, next);
|
2015-12-22 18:45:25 +08:00
|
|
|
},
|
2015-12-25 00:47:41 +08:00
|
|
|
function listJob(err, userDatabase) {
|
2015-12-22 18:45:25 +08:00
|
|
|
assert.ifError(err);
|
|
|
|
|
2015-12-23 21:55:49 +08:00
|
|
|
if (!userDatabase.authenticated) {
|
|
|
|
throw new Error('permission denied');
|
|
|
|
}
|
|
|
|
|
2015-12-22 18:45:25 +08:00
|
|
|
var next = this;
|
|
|
|
|
|
|
|
if ( req.profiler ) {
|
|
|
|
req.profiler.done('setDBAuth');
|
|
|
|
}
|
|
|
|
|
2015-12-25 00:47:41 +08:00
|
|
|
self.jobBackend.list(cdbUsername, function (err, jobs) {
|
2015-12-22 18:45:25 +08:00
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
next(null, {
|
2015-12-25 00:47:41 +08:00
|
|
|
jobs: jobs,
|
2015-12-31 03:16:18 +08:00
|
|
|
host: userDatabase.host
|
2015-12-22 18:45:25 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function handleResponse(err, result) {
|
|
|
|
if ( err ) {
|
2015-12-23 21:55:49 +08:00
|
|
|
return handleException(err, res);
|
2015-12-22 18:45:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( req.profiler ) {
|
2015-12-31 22:42:31 +08:00
|
|
|
req.profiler.done('listJob');
|
2015-12-22 18:45:25 +08:00
|
|
|
res.header('X-SQLAPI-Profiler', req.profiler.toJSONString());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (global.settings.api_hostname) {
|
|
|
|
res.header('X-Served-By-Host', global.settings.api_hostname);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result.host) {
|
|
|
|
res.header('X-Served-By-DB-Host', result.host);
|
|
|
|
}
|
|
|
|
|
2015-12-25 00:47:41 +08:00
|
|
|
res.send(result.jobs);
|
2015-12-22 18:45:25 +08:00
|
|
|
}
|
|
|
|
);
|
2015-12-22 18:06:46 +08:00
|
|
|
};
|
2015-12-22 18:02:16 +08:00
|
|
|
|
2015-12-25 00:42:49 +08:00
|
|
|
JobController.prototype.getJob = function (req, res) {
|
2015-12-07 16:40:51 +08:00
|
|
|
var self = this;
|
2015-12-25 00:42:49 +08:00
|
|
|
var job_id = req.params.job_id;
|
2015-12-07 16:40:51 +08:00
|
|
|
var body = (req.body) ? req.body : {};
|
|
|
|
var params = _.extend({}, req.query, body); // clone so don't modify req.params or req.body so oauth is not broken
|
|
|
|
var cdbUsername = cdbReq.userByReq(req);
|
|
|
|
|
|
|
|
if ( req.profiler ) {
|
|
|
|
req.profiler.start('sqlapi.job');
|
|
|
|
req.profiler.done('init');
|
|
|
|
}
|
|
|
|
|
|
|
|
step(
|
|
|
|
function getUserDBInfo() {
|
2016-01-21 22:05:46 +08:00
|
|
|
var next = this;
|
2016-01-21 23:17:17 +08:00
|
|
|
var authApi = new AuthApi(req, params);
|
|
|
|
|
2016-01-28 21:14:31 +08:00
|
|
|
self.userDatabaseService.getConnectionParams(authApi, cdbUsername, next);
|
2015-12-07 16:40:51 +08:00
|
|
|
},
|
2015-12-25 00:42:49 +08:00
|
|
|
function getJob(err, userDatabase) {
|
2015-12-07 16:40:51 +08:00
|
|
|
assert.ifError(err);
|
|
|
|
|
2015-12-23 21:55:49 +08:00
|
|
|
if (!userDatabase.authenticated) {
|
|
|
|
throw new Error('permission denied');
|
|
|
|
}
|
|
|
|
|
2015-12-07 16:40:51 +08:00
|
|
|
var next = this;
|
|
|
|
|
|
|
|
if ( req.profiler ) {
|
|
|
|
req.profiler.done('setDBAuth');
|
|
|
|
}
|
|
|
|
|
2015-12-25 00:42:49 +08:00
|
|
|
self.jobBackend.get(job_id, function (err, job) {
|
2015-12-07 16:40:51 +08:00
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
2015-12-09 07:02:08 +08:00
|
|
|
|
2015-12-07 16:40:51 +08:00
|
|
|
next(null, {
|
2015-12-25 00:42:49 +08:00
|
|
|
job: job,
|
2015-12-31 03:16:18 +08:00
|
|
|
host: userDatabase.host
|
2015-12-09 07:02:08 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2015-12-25 00:42:49 +08:00
|
|
|
function handleResponse(err, result) {
|
|
|
|
if ( err ) {
|
|
|
|
return handleException(err, res);
|
|
|
|
}
|
2015-12-09 07:02:08 +08:00
|
|
|
|
2015-12-23 21:55:49 +08:00
|
|
|
if ( req.profiler ) {
|
2015-12-31 22:42:31 +08:00
|
|
|
req.profiler.done('getJob');
|
2015-12-25 00:42:49 +08:00
|
|
|
res.header('X-SQLAPI-Profiler', req.profiler.toJSONString());
|
2015-12-23 21:55:49 +08:00
|
|
|
}
|
|
|
|
|
2015-12-25 00:42:49 +08:00
|
|
|
if (global.settings.api_hostname) {
|
|
|
|
res.header('X-Served-By-Host', global.settings.api_hostname);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result.host) {
|
|
|
|
res.header('X-Served-By-DB-Host', result.host);
|
|
|
|
}
|
|
|
|
|
|
|
|
res.send(result.job);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2016-03-18 21:57:18 +08:00
|
|
|
function isValidJob(sql) {
|
|
|
|
if (_.isArray(sql)) {
|
|
|
|
for (var i = 0; i < sql.length; i++) {
|
|
|
|
if (!_.isString(sql[i])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_.isString(sql)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-12-25 00:42:49 +08:00
|
|
|
JobController.prototype.createJob = function (req, res) {
|
2016-04-18 21:30:16 +08:00
|
|
|
// jshint maxcomplexity: 7
|
2015-12-25 00:42:49 +08:00
|
|
|
var self = this;
|
|
|
|
var body = (req.body) ? req.body : {};
|
|
|
|
var params = _.extend({}, req.query, body); // clone so don't modify req.params or req.body so oauth is not broken
|
|
|
|
var sql = (params.query === "" || _.isUndefined(params.query)) ? null : params.query;
|
|
|
|
var cdbUsername = cdbReq.userByReq(req);
|
|
|
|
|
2016-04-18 21:30:16 +08:00
|
|
|
|
2016-03-18 21:57:18 +08:00
|
|
|
if (!isValidJob(sql)) {
|
2016-04-18 21:40:40 +08:00
|
|
|
return handleException(new Error('You must indicate a valid SQL'), res);
|
2016-04-18 21:30:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (reachMaxQuerySizeLimit(sql)) {
|
2016-04-18 22:24:52 +08:00
|
|
|
return handleException(new Error(getMaxSizeErrorMessage(sql)), res);
|
2015-12-25 00:42:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( req.profiler ) {
|
|
|
|
req.profiler.start('sqlapi.job');
|
|
|
|
req.profiler.done('init');
|
|
|
|
}
|
|
|
|
|
|
|
|
step(
|
|
|
|
function getUserDBInfo() {
|
2016-01-21 22:05:46 +08:00
|
|
|
var next = this;
|
2016-01-21 23:17:17 +08:00
|
|
|
var authApi = new AuthApi(req, params);
|
|
|
|
|
2016-01-28 21:14:31 +08:00
|
|
|
self.userDatabaseService.getConnectionParams(authApi, cdbUsername, next);
|
2015-12-25 00:42:49 +08:00
|
|
|
},
|
|
|
|
function persistJob(err, userDatabase) {
|
|
|
|
assert.ifError(err);
|
|
|
|
|
|
|
|
if (!userDatabase.authenticated) {
|
|
|
|
throw new Error('permission denied');
|
|
|
|
}
|
2015-12-22 18:06:46 +08:00
|
|
|
|
2015-12-09 07:02:08 +08:00
|
|
|
var next = this;
|
|
|
|
|
2015-12-25 00:42:49 +08:00
|
|
|
if ( req.profiler ) {
|
|
|
|
req.profiler.done('setDBAuth');
|
|
|
|
}
|
|
|
|
|
|
|
|
self.jobBackend.create(cdbUsername, sql, userDatabase.host, function (err, result) {
|
2015-12-09 07:02:08 +08:00
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
next(null, {
|
2015-12-25 00:42:49 +08:00
|
|
|
job: result,
|
|
|
|
host: userDatabase.host
|
2015-12-07 16:40:51 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function handleResponse(err, result) {
|
|
|
|
if ( err ) {
|
2015-12-23 21:55:49 +08:00
|
|
|
return handleException(err, res);
|
2015-12-07 16:40:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( req.profiler ) {
|
2015-12-31 22:42:31 +08:00
|
|
|
req.profiler.done('persistJob');
|
|
|
|
res.header('X-SQLAPI-Profiler', req.profiler.toJSONString());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (global.settings.api_hostname) {
|
2016-01-21 22:05:46 +08:00
|
|
|
res.header('X-Served-By-Host', global.settings.api_hostname);
|
2015-12-31 22:42:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (result.host) {
|
2016-01-21 22:05:46 +08:00
|
|
|
res.header('X-Served-By-DB-Host', result.host);
|
2015-12-31 22:42:31 +08:00
|
|
|
}
|
2016-01-07 19:06:01 +08:00
|
|
|
|
|
|
|
res.status(201).send(result.job);
|
2015-12-31 22:42:31 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
JobController.prototype.updateJob = function (req, res) {
|
2016-04-18 21:30:16 +08:00
|
|
|
// jshint maxcomplexity: 7
|
2015-12-31 22:42:31 +08:00
|
|
|
var self = this;
|
|
|
|
var job_id = req.params.job_id;
|
|
|
|
var body = (req.body) ? req.body : {};
|
|
|
|
var params = _.extend({}, req.query, body); // clone so don't modify req.params or req.body so oauth is not broken
|
|
|
|
var sql = (params.query === "" || _.isUndefined(params.query)) ? null : params.query;
|
|
|
|
var cdbUsername = cdbReq.userByReq(req);
|
|
|
|
|
2016-03-18 21:57:18 +08:00
|
|
|
if (!isValidJob(sql)) {
|
2016-04-18 21:40:40 +08:00
|
|
|
return handleException(new Error('You must indicate a valid SQL'), res);
|
2015-12-31 22:42:31 +08:00
|
|
|
}
|
|
|
|
|
2016-04-18 21:30:16 +08:00
|
|
|
if (reachMaxQuerySizeLimit(sql)) {
|
2016-04-18 22:24:52 +08:00
|
|
|
return handleException(new Error(getMaxSizeErrorMessage(sql)), res);
|
2016-04-18 21:30:16 +08:00
|
|
|
}
|
|
|
|
|
2015-12-31 22:42:31 +08:00
|
|
|
if ( req.profiler ) {
|
|
|
|
req.profiler.start('sqlapi.job');
|
|
|
|
req.profiler.done('init');
|
|
|
|
}
|
|
|
|
|
|
|
|
step(
|
|
|
|
function getUserDBInfo() {
|
2016-01-21 22:05:46 +08:00
|
|
|
var next = this;
|
2016-01-21 23:17:17 +08:00
|
|
|
var authApi = new AuthApi(req, params);
|
|
|
|
|
2016-01-28 21:14:31 +08:00
|
|
|
self.userDatabaseService.getConnectionParams(authApi, cdbUsername, next);
|
2015-12-31 22:42:31 +08:00
|
|
|
},
|
|
|
|
function updateJob(err, userDatabase) {
|
|
|
|
assert.ifError(err);
|
|
|
|
|
2016-01-05 20:54:18 +08:00
|
|
|
if (!userDatabase.authenticated) {
|
|
|
|
throw new Error('permission denied');
|
|
|
|
}
|
|
|
|
|
2015-12-31 22:42:31 +08:00
|
|
|
var next = this;
|
|
|
|
|
|
|
|
if ( req.profiler ) {
|
|
|
|
req.profiler.done('setDBAuth');
|
|
|
|
}
|
|
|
|
|
|
|
|
self.jobBackend.update(job_id, sql, function (err, job) {
|
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
next(null, {
|
|
|
|
job: job,
|
|
|
|
host: userDatabase.host
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function handleResponse(err, result) {
|
|
|
|
if ( err ) {
|
|
|
|
return handleException(err, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( req.profiler ) {
|
|
|
|
req.profiler.done('updateJob');
|
2015-12-07 16:40:51 +08:00
|
|
|
res.header('X-SQLAPI-Profiler', req.profiler.toJSONString());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (global.settings.api_hostname) {
|
|
|
|
res.header('X-Served-By-Host', global.settings.api_hostname);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result.host) {
|
|
|
|
res.header('X-Served-By-DB-Host', result.host);
|
|
|
|
}
|
2015-12-07 18:29:55 +08:00
|
|
|
res.send(result.job);
|
2015-12-07 16:40:51 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = JobController;
|