Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
mtipnis committed Feb 28, 2013
2 parents ece99a5 + d2f759b commit 9fde488
Show file tree
Hide file tree
Showing 10 changed files with 353 additions and 15 deletions.
4 changes: 2 additions & 2 deletions modules/app/package.json
@@ -1,7 +1,7 @@
{
"author": "ql.io",
"name": "ql.io-app",
"version": "0.8.8a",
"version": "0.8.9",
"repository": {
"type": "git",
"url": "https://github.com/ql-io/ql.io"
Expand All @@ -15,7 +15,7 @@
},
"dependencies": {
"commander": "1.0.0",
"ql.io-console": "0.8.7a",
"ql.io-console": "0.8.8",
"ql.io-compiler": "0.8.4",
"winston": "0.6.2",
"express": "2.5.11",
Expand Down
4 changes: 4 additions & 0 deletions modules/console/app.js
Expand Up @@ -943,6 +943,10 @@ var Console = module.exports = function(opts, cb) {

function handleResponseCB(req, res, execState, err, results) {
compress(req, res, {logEmitter : engine}); // TODO replace with a middleware
var reqSize = req.url.length + JSON.stringify(req.headers).length + req.method.length +1
var resSize = results ? JSON.stringify(results).length : JSON.stringify(err).length
engine.emitEvent("User's request size is " + reqSize + ", response size is "+resSize)

var cb = req.param('callback');
if (err) {
var status = err.status || 400;
Expand Down
4 changes: 2 additions & 2 deletions modules/console/package.json
@@ -1,7 +1,7 @@
{
"author": "ql.io",
"name": "ql.io-console",
"version": "0.8.7a",
"version": "0.8.8",
"repository": {
"type": "git",
"url": "https://github.com/ql-io/ql.io"
Expand All @@ -15,7 +15,7 @@
"express": "2.5.11",
"ejs": "0.8.0",
"underscore": "1.3.3",
"ql.io-engine": "0.8.6a",
"ql.io-engine": "0.8.7",
"ql.io-mutable-uri": "0.8.0",
"ql.io-compiler": "0.8.4",
"winston": "0.6.2",
Expand Down
50 changes: 48 additions & 2 deletions modules/engine/lib/engine.js
Expand Up @@ -46,7 +46,8 @@ var configLoader = require('./engine/config.js'),
_ = require('underscore'),
EventEmitter = require('events').EventEmitter,
util = require('util'),
assert = require('assert');
assert = require('assert'),
aop = require('./engine/aop.js');

exports.version = require('../package.json').version;

Expand Down Expand Up @@ -178,7 +179,24 @@ util.inherits(Engine, LogEmitter);
* });
* });
*/
Engine.prototype.execute = function() {
Engine.prototype.execute = function(){
var grandParentEvent = null;
if(arguments.length > 1){
grandParentEvent = arguments[1].parentEvent
}

var parentEvent = this.beginEvent({
parent: grandParentEvent,
name: 'start-exec',
message: 'none',
cb: function(err, results) {
grandParentEvent.cb()
}
});
arguments[1].parentEvent = parentEvent;
this.doExecute.apply(this,arguments);
}
Engine.prototype.doExecute = function() {
var script, opts, func;

var route, context, plan, parentEvent,
Expand Down Expand Up @@ -823,3 +841,31 @@ function preReqNotFound(statement, opts, parentEvent) {
// Export event types
Engine.Events = {};
_.extend(Engine.Events, eventTypes);

// logging aop
var processingEvent;
Engine.doProcessing = function(){
// original function is last
var myDoExec = Engine.doProcessing.prototype._innerFunc
var parentEvent = null
if(arguments.length > 1){
parentEvent = arguments[1].parentEvent
}

var processingEvent = this.beginEvent({
parent: parentEvent,
name: 'processingEvent',
message: 'calculates cpu time.',
cb: function(){}
});
myDoExec.apply(this,arguments);
processingEvent.end(null, null);

}
try {
aop.addAround(Engine.doProcessing, Engine, "doExecute")
}
catch(e)
{
console.log('epic fail')
}
83 changes: 83 additions & 0 deletions modules/engine/lib/engine/aop.js
@@ -0,0 +1,83 @@
/*
* Code taken from Danne Lundqvist with custom modification
* http://www.dotvoid.com/2005/06/aspect-oriented-programming-and-javascript/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var InvalidAspect = new Error("Missing a valid aspect. Aspect is not a function.");
var InvalidObject = new Error("Missing valid object or an array of valid objects.");
var InvalidMethod = new Error("Missing valid method to apply aspect on.");

function doAround(func, aroundFunc){
return function(){
aroundFunc.prototype._innerFunc = func

return aroundFunc.apply(this,arguments)
};
}

Aspects = function(){};
Aspects.prototype={


addIntroduction : function(aspect, objs){
function _addIntroduction(intro, obj){
for (var m in intro.prototype) {
obj.prototype[m] = intro.prototype[m];
}
}
var oType = typeof(objs);

if (typeof(aspect) != 'function')
throw(InvalidAspect);

if (oType == 'function'){
_addIntroduction(aspect, objs);
}
else if (oType == 'object'){
for (var n = 0; n < objs.length; n++){
_addIntroduction(aspect, objs[n]);
}
}
else{
throw InvalidObject;
}
},

addAround : function(aspect, obj, funcs){
if (typeof(aspect) != 'function')
throw InvalidAspect;

if (typeof(funcs) != 'object')
funcs = Array(funcs);

for (var n = 0; n < funcs.length; n++)
{
var fName = funcs[n];
var old;
if(obj){
old = obj.prototype[fName];
}
if (!old)
throw InvalidMethod;

var res = doAround(old,aspect);
obj.prototype[fName] = res;
}

}

}
exports.addIntroduction = Aspects.prototype.addIntroduction
exports.addAround = Aspects.prototype.addAround
37 changes: 30 additions & 7 deletions modules/engine/lib/engine/http/request.js
Expand Up @@ -7,6 +7,7 @@
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand Down Expand Up @@ -109,11 +110,33 @@ function sendHttpRequest(client, options, args, start, timings, reqStart, key, c
});
});

var responseLength = 0;
args.httpReqTx = args.logEmitter.beginEvent({
parent: args.parentEvent,
name: 'http-request',
message: packet,
cb: args.cb
cb: function(err, results){
var processingEvent = args.logEmitter.beginEvent({
parent: args.parentEvent,
name: 'processingEvent',
message: 'calculates cpu time',
cb: function(){}
})
if(args.logEmitter){
var reqlength = JSON.stringify(options.headers).length +options.host.length;
if(options.body){
reqlength += JSON.stringify(options.body).length
}
args.logEmitter.emitEvent(JSON.stringify({
reqSize: reqlength,
resSize: responseLength
}))
}
var toreturn = args.cb(err, results)
processingEvent.end();
return toreturn

}
});

if(args.emitter) {
Expand All @@ -123,6 +146,7 @@ function sendHttpRequest(client, options, args, start, timings, reqStart, key, c
packet.body = args.body;
}
args.emitter.emit(packet.type, packet);

}

if (args.parts && args.statement.parts) {
Expand Down Expand Up @@ -230,7 +254,6 @@ function sendHttpRequest(client, options, args, start, timings, reqStart, key, c
}

var bufs = []; // array for bufs for each chunk
var responseLength = 0;
var contentEncoding = res.headers['content-encoding'];
var zipped = false, unzip;
var result;
Expand Down Expand Up @@ -403,11 +426,11 @@ function sendMessage(args, client, options, retry) {
}
else {
args.httpReqTx = args.logEmitter.beginEvent({
parent: args.parentEvent,
type: 'http-request',
message: key, // TODO
cb: args.cb
});
parent: args.parentEvent,
type: 'http-request',
message: key, // TODO
cb: args.cb
});
args.logEmitter.emitEvent(args.httpReqTx.event, {
'cache-key': key,
'hit': true
Expand Down
2 changes: 1 addition & 1 deletion modules/engine/package.json
@@ -1,7 +1,7 @@
{
"author": "ql.io",
"name": "ql.io-engine",
"version": "0.8.6a",
"version": "0.8.7",
"repository": {
"type": "git",
"url": "https://github.com/ql-io/ql.io"
Expand Down
87 changes: 87 additions & 0 deletions modules/engine/test/aop-test.js
@@ -0,0 +1,87 @@
/*
* Code taken from Danne Lundqvist with custom modification
* http://www.dotvoid.com/2005/06/aspect-oriented-programming-and-javascript/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var aop = require('../lib/engine/aop.js')
module.exports = {
'naive' : function(test){
function Document(){}
Document.prototype = {
_id: 0,
_name: '',

name: function() {
return this._name;
},

id: function() {
return this._id;
},

save: function() {
return true;
},

open: function(id) {
this._id = id;
this._name = 'Ajax on AOP steroids'
return true;
}
}
function openDocument(id)
{
var doc = new Document();
try {
doc.open(id);
}
catch(e)
{
alert(e);
return;
}

// Update icons and other user elements affected alert("Doc id: " + doc.id());
return doc;
}
function Lockable(){}
Lockable.prototype = {
_locked: false,

locked: function() {
return this._locked;
}
}
function lockOnOpen()
{
// Lock this object
// If we didn't succeed
//throw (new Error ("Failed locking " + this._name));

// The object is locked
this._locked = true;
var ret = arguments[0].apply(this, arguments)
return ret;
}
try {
aop.addIntroduction(Lockable, Document);
aop.addAround(lockOnOpen, Document, "open");
test.done()
}
catch(e)
{
console.log('epic fail')
}
}
}

0 comments on commit 9fde488

Please sign in to comment.