Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/0.4' into 0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
Prabhakhar K committed Jan 12, 2012
2 parents 7044626 + df3974f commit c526c96
Show file tree
Hide file tree
Showing 11 changed files with 356 additions and 74 deletions.
110 changes: 56 additions & 54 deletions modules/console/app.js
Expand Up @@ -186,69 +186,68 @@ var Console = module.exports = function(config, cb) {
}

// register routes
var routes = engine.routes;
var routes = engine.routes.verbMap;
_.each(routes, function(verbRoutes, uri) {
if(uri != 'simpleMap'){
_.each(verbRoutes, function(verbRouteVariants, verb) {
engine.emit(Engine.Events.EVENT, {}, new Date() + ' Adding route ' + uri + ' for ' + verb);
app[verb](uri, function(req, res) {
var holder = {
params: {},
headers: {},
routeParams: {}
};

// get all query params
collectHttpQueryParams(req, holder, false);

// find a route (i.e. associated cooked script)
var route = _.detect(verbRouteVariants, function(verbRouteVariant) {
return _.isEqual(_.intersection(_.keys(holder.params), _.keys(verbRouteVariant.query)),
_.keys(verbRouteVariant.query));
});

if (!route) {
res.writeHead(400, 'Bad input', {
'content-type' : 'application/json'
});
res.write(JSON.stringify({'err' : 'No matching route'}));
res.end();
return;
}
_.each(verbRoutes, function(verbRouteVariants, verb) {
engine.emit(Engine.Events.EVENT, {}, new Date() + ' Adding route ' + uri + ' for ' + verb);
app[verb](uri, function(req, res) {
var holder = {
params: {},
headers: {},
routeParams: {}
};

// get all query params
collectHttpQueryParams(req, holder, false);

// find a route (i.e. associated cooked script)
var route = _.detect(verbRouteVariants, function(verbRouteVariant) {
return _.isEqual(_.intersection(_.keys(holder.params), _.keys(verbRouteVariant.query)),
_.keys(verbRouteVariant.query));
});

// collect the path params
var keys = _.keys(req.params);
_.each(keys, function(key) {
holder.routeParams[key] = req.params[key];
if (!route) {
res.writeHead(400, 'Bad input', {
'content-type' : 'application/json'
});
res.write(JSON.stringify({'err' : 'No matching route'}));
res.end();
return;
}

_.each(route.query, function(queryParam, paramName) {
holder.routeParams[queryParam] = holder.params[paramName].toString();
});
// collect the path params
var keys = _.keys(req.params);
_.each(keys, function(key) {
holder.routeParams[key] = req.params[key];
});

// collect headers
collectHttpHeaders(req, holder);

var execState = [];
engine.execute(route.script,
{
request: holder,
route: uri,
context: req.body || {}
},
function(emitter) {
setupExecStateEmitter(emitter, execState, req.param('events'));
setupCounters(emitter);
emitter.on('end', function(err, results) {
return handleResponseCB(req, res, execState, err, results);
});
}
);
_.each(route.query, function(queryParam, paramName) {
holder.routeParams[queryParam] = holder.params[paramName].toString();
});

// collect headers
collectHttpHeaders(req, holder);

var execState = [];
engine.execute(route.script,
{
request: holder,
route: uri,
context: req.body || {}
},
function(emitter) {
setupExecStateEmitter(emitter, execState, req.param('events'));
setupCounters(emitter);
emitter.on('end', function(err, results) {
return handleResponseCB(req, res, execState, err, results);
});
}
);
});
}
});
});

// HTTP indirection for 'show tables' command
app.get('/tables', function(req,res){
var holder = {
params: {fromRoute: true},
Expand All @@ -269,6 +268,7 @@ var Console = module.exports = function(config, cb) {
);
});

// HTTP indirection for 'describe <table>' command and it returns json (and not html)
app.get('/table', function(req,res){
var holder = {
params: {fromRoute: true},
Expand Down Expand Up @@ -303,6 +303,7 @@ var Console = module.exports = function(config, cb) {
);
});

// HTTP indirection for 'show routes' command
app.get('/routes', function(req,res){
var holder = {
params: {},
Expand All @@ -323,6 +324,7 @@ var Console = module.exports = function(config, cb) {
);
});

// HTTP indirection for 'describe route "<route>" using method <http-verb>' command
app.get('/route', function(req,res){
var holder = {
params: {},
Expand Down
2 changes: 1 addition & 1 deletion modules/console/package.json
Expand Up @@ -16,7 +16,7 @@
"ejs": "",
"underscore": "",
"ql.io-engine": "",
"winston": "0.3.5",
"winston": "",
"browserify": "",
"uglify-js": "",
"headers": "",
Expand Down
5 changes: 5 additions & 0 deletions modules/engine/lib/engine/http.request.js
Expand Up @@ -463,6 +463,11 @@ function prepareParams() {
params.__proto__ = ref;
}
else {
// Delete undefined properties as an undefined will override a defined in the __proto__
// chain
_.each(arg, function(v, p) {
if(v === undefined) delete arg[p];
});
ref.__proto__ = arg;
ref = arg;
}
Expand Down
14 changes: 8 additions & 6 deletions modules/engine/lib/engine/load-routes.js
Expand Up @@ -31,15 +31,17 @@ exports.load = function (opts) {
return {};
}

var routes = {simpleMap:{}};
var routes = {
simpleMap:{},
verbMap:{}
};
loadInternal(rootdir, '', logEmitter, routes);
return routes;

};

function loadInternal(path, prefix, logEmitter, routes) {
assert.ok(path, 'path should not be null');
assert.ok(routes, 'routes should not be null');

var script, stats, paths;
path = path.charAt(path.length - 1) == '/' ? path : path + '/';
Expand Down Expand Up @@ -108,12 +110,12 @@ function loadInternal(path, prefix, logEmitter, routes) {
typeReturn.route.method = typeReturn.route.method == 'delete' ? 'del' : typeReturn.route.method;

// Get record for given route
routes[pieces.pathname] = routes[pieces.pathname] || {};
routes.verbMap[pieces.pathname] = routes.verbMap[pieces.pathname] || {};
// Get record for http verb in the route record
routes[pieces.pathname][typeReturn.route.method] = routes[pieces.pathname][typeReturn.route.method]
routes.verbMap[pieces.pathname][typeReturn.route.method] = routes.verbMap[pieces.pathname][typeReturn.route.method]
|| [];
// Add info for the current route
if (!_.detect(routes[pieces.pathname][typeReturn.route.method], function(record) {
if (!_.detect(routes.verbMap[pieces.pathname][typeReturn.route.method], function(record) {
return _.isEqual(record.query, pieces.query);
})) {
var routeRecord = {
Expand All @@ -123,7 +125,7 @@ function loadInternal(path, prefix, logEmitter, routes) {
tables: tables,
info: info
};
routes[pieces.pathname][typeReturn.route.method].push(routeRecord);
routes.verbMap[pieces.pathname][typeReturn.route.method].push(routeRecord);
routes.simpleMap[typeReturn.route.method + ':' + typeReturn.route.path.value]=routeRecord;
} else {
logEmitter.emitError("Route already defined: " + script);
Expand Down
23 changes: 23 additions & 0 deletions modules/engine/lib/engine/select.js
Expand Up @@ -24,6 +24,8 @@ var httpRequest = require('./http.request.js'),
jsonPath = require('JSONPath'),
assert = require('assert');

var maxNestedRequests;

exports.exec = function(opts, statement, cb, parentEvent) {

assert.ok(opts.tables, 'Argument tables can not be undefined');
Expand Down Expand Up @@ -66,6 +68,9 @@ exports.exec = function(opts, statement, cb, parentEvent) {
}(cloned));
});

// Determine whether the number of funcs is within the limit and prune the funcs array
funcs = funcs.slice(0, maxNestedRequests || getMaxNestedRequests(opts));

// Execute joins
async.parallel(funcs, function(err, more) {
// If there is nothing to loop throough, leave the body undefined.
Expand Down Expand Up @@ -162,6 +167,10 @@ function execInternal(opts, statement, cb, parentEvent) {
return function(callback) {
ret = {};
ret[name] = [];

// Determine whether the number of values is within the limit and prune the values array
cond.rhs.value = cond.rhs.value.slice(0, maxNestedRequests || getMaxNestedRequests(opts));

// Expand variables from context
_.each(cond.rhs.value, function(key) {
var arr = jsonfill.lookup(key, context);
Expand Down Expand Up @@ -315,3 +324,17 @@ var clone = function(obj) {
return temp;
};

function getMaxNestedRequests(opts) {
var config = opts.config;

if (config && config.maxNestedRequests) {
maxNestedRequests = config.maxNestedRequests;
}

if (typeof maxNestedRequests == 'undefined') {
maxNestedRequests = 50;
opts.logEmitter.emitWarning('config.maxNestedRequests is undefined! Defaulting to ' + maxNestedRequests);
}

return maxNestedRequests;
}
6 changes: 2 additions & 4 deletions modules/engine/lib/engine/show-routes.js
Expand Up @@ -41,10 +41,8 @@ exports.exec = function(opts, statement, cb) {
'content-type': 'application/json'
},
body:
_(routes).chain()
.filter(function(route, key){
return key != 'simpleMap';
})
_(routes.verbMap).chain()
.values()
.map(function(aUrl){
return _.values(aUrl);
})
Expand Down
6 changes: 3 additions & 3 deletions modules/engine/package.json
@@ -1,7 +1,7 @@
{
"author": "ql.io",
"name": "ql.io-engine",
"version": "0.3.13",
"version": "0.3.14",
"repository": {
"type": "git",
"url": "https://github.com/ql-io/ql.io"
Expand All @@ -11,7 +11,7 @@
},
"main": "lib/engine.js",
"dependencies": {
"winston": "0.3.5",
"winston": "",
"underscore": "",
"xml2json": "",
"ql.io-compiler": "",
Expand All @@ -25,7 +25,7 @@
"dox": "",
"node-uuid": "",
"markdown": "",
"csv": "0.0.10"
"csv": ""
},
"devDependencies": {
"nodeunit": ""
Expand Down
7 changes: 4 additions & 3 deletions modules/engine/test/config/dev.json
@@ -1,7 +1,8 @@
{
"ebay": {
"apikey": "Qlio1a92e-fea5-485d-bcdb-1140ee96527",
"maxNestedCalls" : 100
}
"apikey": "Qlio1a92e-fea5-485d-bcdb-1140ee96527"

},
"maxNestedCalls" : 50
}

0 comments on commit c526c96

Please sign in to comment.