2015-12-07 16:40:51 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var _ = require('underscore');
|
2016-04-18 22:24:52 +08:00
|
|
|
var util = require('util');
|
2015-12-07 16:40:51 +08:00
|
|
|
|
2016-10-04 21:19:31 +08:00
|
|
|
var userMiddleware = require('../middlewares/user');
|
2016-10-04 21:40:56 +08:00
|
|
|
var authenticatedMiddleware = require('../middlewares/authenticated-request');
|
2015-12-07 16:40:51 +08:00
|
|
|
var handleException = require('../utils/error_handler');
|
|
|
|
|
2016-04-18 22:24:52 +08:00
|
|
|
var ONE_KILOBYTE_IN_BYTES = 1024;
|
2016-11-07 23:50:44 +08:00
|
|
|
var MAX_LIMIT_QUERY_SIZE_IN_KB = 16;
|
2016-05-24 21:31:54 +08:00
|
|
|
var MAX_LIMIT_QUERY_SIZE_IN_BYTES = MAX_LIMIT_QUERY_SIZE_IN_KB * ONE_KILOBYTE_IN_BYTES;
|
2016-04-18 21:30:16 +08:00
|
|
|
|
2016-04-18 22:24:52 +08:00
|
|
|
function getMaxSizeErrorMessage(sql) {
|
2016-04-25 17:18:30 +08:00
|
|
|
return util.format([
|
2016-05-24 20:28:00 +08:00
|
|
|
'Your payload is too large: %s bytes. Max size allowed is %s bytes (%skb).',
|
2016-04-25 17:18:30 +08:00
|
|
|
'Are you trying to import data?.',
|
|
|
|
'Please, check out import api http://docs.cartodb.com/cartodb-platform/import-api/'
|
|
|
|
].join(' '),
|
2016-04-18 22:24:52 +08:00
|
|
|
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-05-24 17:19:00 +08:00
|
|
|
function JobController(userDatabaseService, jobService, statsdClient) {
|
2016-01-22 00:24:48 +08:00
|
|
|
this.userDatabaseService = userDatabaseService;
|
2016-05-14 00:50:55 +08:00
|
|
|
this.jobService = jobService;
|
2016-05-30 18:27:19 +08:00
|
|
|
this.statsdClient = statsdClient || { increment: function () {} };
|
2015-12-07 16:40:51 +08:00
|
|
|
}
|
|
|
|
|
2016-05-24 20:28:00 +08:00
|
|
|
function bodyPayloadSizeMiddleware(req, res, next) {
|
|
|
|
var payload = JSON.stringify(req.body);
|
|
|
|
if (payload.length > MAX_LIMIT_QUERY_SIZE_IN_BYTES) {
|
|
|
|
return handleException(new Error(getMaxSizeErrorMessage(payload)), res);
|
|
|
|
} else {
|
|
|
|
return next(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-14 00:50:55 +08:00
|
|
|
module.exports = JobController;
|
2016-05-24 20:28:00 +08:00
|
|
|
module.exports.MAX_LIMIT_QUERY_SIZE_IN_BYTES = MAX_LIMIT_QUERY_SIZE_IN_BYTES;
|
|
|
|
module.exports.getMaxSizeErrorMessage = getMaxSizeErrorMessage;
|
2016-05-14 00:50:55 +08:00
|
|
|
|
2015-12-07 16:40:51 +08:00
|
|
|
JobController.prototype.route = function (app) {
|
2016-10-04 21:40:56 +08:00
|
|
|
app.post(
|
|
|
|
global.settings.base_url + '/sql/job',
|
|
|
|
bodyPayloadSizeMiddleware, userMiddleware, authenticatedMiddleware(this.userDatabaseService),
|
|
|
|
this.createJob.bind(this)
|
|
|
|
);
|
2016-10-28 21:08:42 +08:00
|
|
|
app.get(
|
2017-09-20 21:46:29 +08:00
|
|
|
global.settings.base_url + '/jobs-wip',
|
2016-10-28 21:08:42 +08:00
|
|
|
this.listWorkInProgressJobs.bind(this)
|
|
|
|
);
|
2016-10-04 21:40:56 +08:00
|
|
|
app.get(
|
|
|
|
global.settings.base_url + '/sql/job/:job_id',
|
|
|
|
userMiddleware, authenticatedMiddleware(this.userDatabaseService),
|
|
|
|
this.getJob.bind(this)
|
|
|
|
);
|
|
|
|
app.delete(
|
|
|
|
global.settings.base_url + '/sql/job/:job_id',
|
|
|
|
userMiddleware, authenticatedMiddleware(this.userDatabaseService),
|
|
|
|
this.cancelJob.bind(this)
|
|
|
|
);
|
2015-12-31 03:16:18 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
JobController.prototype.cancelJob = function (req, res) {
|
2016-10-04 22:07:13 +08:00
|
|
|
this.jobService.cancel(req.params.job_id, jobResponse(req, res, this.statsdClient, 'cancel'));
|
2015-12-07 16:40:51 +08:00
|
|
|
};
|
|
|
|
|
2015-12-25 00:42:49 +08:00
|
|
|
JobController.prototype.getJob = function (req, res) {
|
2016-10-04 22:07:13 +08:00
|
|
|
this.jobService.get(req.params.job_id, jobResponse(req, res, this.statsdClient, 'retrieve'));
|
2015-12-25 00:42:49 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
JobController.prototype.createJob = function (req, res) {
|
|
|
|
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;
|
|
|
|
|
2016-10-04 22:07:13 +08:00
|
|
|
var data = {
|
2017-11-24 22:49:25 +08:00
|
|
|
user: res.locals.user,
|
2016-10-04 22:07:13 +08:00
|
|
|
query: sql,
|
2018-02-15 19:30:04 +08:00
|
|
|
host: res.locals.userDbParams.host,
|
|
|
|
port: res.locals.userDbParams.port,
|
|
|
|
pass: res.locals.userDbParams.pass,
|
|
|
|
dbname: res.locals.userDbParams.dbname,
|
|
|
|
dbuser: res.locals.userDbParams.user
|
2016-10-04 22:07:13 +08:00
|
|
|
};
|
2016-05-14 00:50:55 +08:00
|
|
|
|
2016-10-04 22:07:13 +08:00
|
|
|
this.jobService.create(data, jobResponse(req, res, this.statsdClient, 'create', 201));
|
|
|
|
};
|
2015-12-07 16:40:51 +08:00
|
|
|
|
2016-10-28 21:08:42 +08:00
|
|
|
JobController.prototype.listWorkInProgressJobs = function (req, res) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
this.jobService.listWorkInProgressJobs(function (err, list) {
|
|
|
|
if (err) {
|
|
|
|
self.statsdClient.increment('sqlapi.job.error');
|
|
|
|
return handleException(err, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
req.profiler.done('list');
|
|
|
|
req.profiler.end();
|
|
|
|
req.profiler.sendStats();
|
|
|
|
|
|
|
|
res.header('X-SQLAPI-Profiler', req.profiler.toJSONString());
|
|
|
|
self.statsdClient.increment('sqlapi.job.success');
|
|
|
|
|
|
|
|
if (process.env.NODE_ENV !== 'test') {
|
|
|
|
console.info(JSON.stringify({
|
|
|
|
type: 'sql_api_batch_job',
|
2017-11-24 22:49:25 +08:00
|
|
|
username: res.locals.user,
|
2016-10-28 21:08:42 +08:00
|
|
|
action: 'list'
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
res.status(200).send(list);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-10-04 22:07:13 +08:00
|
|
|
function jobResponse(req, res, statsdClient, action, status) {
|
|
|
|
return function handler(err, job) {
|
|
|
|
status = status || 200;
|
2016-01-07 19:06:01 +08:00
|
|
|
|
2016-10-04 22:07:13 +08:00
|
|
|
if (err) {
|
|
|
|
statsdClient.increment('sqlapi.job.error');
|
|
|
|
return handleException(err, res);
|
|
|
|
}
|
2016-05-27 01:44:59 +08:00
|
|
|
|
2017-11-24 22:49:25 +08:00
|
|
|
res.header('X-Served-By-DB-Host', res.locals.userDbParams.host);
|
2016-05-27 01:44:59 +08:00
|
|
|
|
2016-10-04 22:07:13 +08:00
|
|
|
req.profiler.done(action);
|
|
|
|
req.profiler.end();
|
|
|
|
req.profiler.sendStats();
|
2016-05-27 01:44:59 +08:00
|
|
|
|
2016-10-04 22:07:13 +08:00
|
|
|
res.header('X-SQLAPI-Profiler', req.profiler.toJSONString());
|
|
|
|
statsdClient.increment('sqlapi.job.success');
|
2016-08-31 00:43:09 +08:00
|
|
|
|
2016-10-12 07:40:14 +08:00
|
|
|
if (process.env.NODE_ENV !== 'test') {
|
|
|
|
console.info(JSON.stringify({
|
|
|
|
type: 'sql_api_batch_job',
|
2017-11-24 22:49:25 +08:00
|
|
|
username: res.locals.user,
|
2016-10-12 07:40:14 +08:00
|
|
|
action: action,
|
|
|
|
job_id: job.job_id
|
|
|
|
}));
|
|
|
|
}
|
2016-10-04 22:07:13 +08:00
|
|
|
|
|
|
|
res.status(status).send(job.serialize());
|
|
|
|
};
|
|
|
|
}
|