Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 36 additions & 28 deletions lib/query.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var _ = require('underscore');
var async = require('async');
var KeenRequests = require('./requests');

/*!
Expand Down Expand Up @@ -31,40 +32,47 @@ Keen.Request.prototype.run = function(){
completions = 0,
response = [];

var handleResponse = function(err, res){
if (err && self.callback) {
return self.callback(err, null);
}
response[arguments[2]] = res, completions++;
if (completions == self.queries.length) {
self.data = (self.queries.length == 1) ? response[0] : response;
if (self.callback) self.callback(null, self.data);
}
};
var queue = [];

_.each(self.queries, function(query, index){
var data, path = '/projects/' + self.client.projectId;
var callbackSequencer = function(err, res){
handleResponse(err, res, index);
};

if (query instanceof Keen.Query) {
path += query.path;
data = query.params || {};
}
/* TODO: Test and deploy this
else if (_.isString(query)) {
path += '/saved_queries/' + query + '/result';
data = { api_key: self.client.readKey };
}*/
else {
throw new Error('Query #' + (index+1) +' is not valid');
queue.push(function(qCallback){
var data, path = '/projects/' + self.client.projectId;
var callbackSequencer = function(err, res){
if(err){
return qCallback(err);
}

}
response[index] = res;

qCallback();
};

KeenRequests.get.call(self.client, self.client.readKey, path, data, callbackSequencer);
if (query instanceof Keen.Query) {
path += query.path;
data = query.params || {};
}
/* TODO: Test and deploy this
else if (_.isString(query)) {
path += '/saved_queries/' + query + '/result';
data = { api_key: self.client.readKey };
}*/
else {
throw new Error('Query #' + (index+1) +' is not valid');

}

KeenRequests.get.call(self.client, self.client.readKey, path, data, callbackSequencer);
});
});

async.parallel(queue, function(err, results){
if (err && self.callback) {
return self.callback(err, null);
}

self.data = (self.queries.length == 1) ? response[0] : response;
if (self.callback) self.callback(null, self.data);
});
return self;
};

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@
],
"dependencies": {
"superagent": "~0.21.0",
"underscore": "~1.5.2"
"underscore": "~1.5.2",
"async": "0.9.0"
},
"devDependencies": {
"mocha": "~1.16.1",
Expand Down
13 changes: 13 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,19 @@ describe("keen", function() {

describe('Multiple analyses', function(){

it('should invoke the error callback only once', function(done){
var mockResponse = { result: 1 };
mockGetRequest(query_path, 401, mockResponse);
mockGetRequest(query_path, 401, mockResponse);
analysis.params = {};
var test = keen.run([analysis, analysis, analysis], function(err, res){
(err === null).should.be.false;
(res === null).should.be.true;

done();
});
});

it('should return a single response when successful', function(done){
var mockResponse = { result: 1 };
mockGetRequest(query_path, 200, mockResponse);
Expand Down