Writing the API (workin in progress)

This commit is contained in:
Gaël Métais
2014-12-02 00:08:07 +01:00
parent 35aed5fb55
commit e18cd87a07
14 changed files with 344 additions and 100 deletions
+86
View File
@@ -0,0 +1,86 @@
var debug = require('debug')('ylt:server');
var runsQueue = require('../datastores/runsQueue');
var runsDatastore = require('../datastores/runsDatastore');
function ApiController(app) {
'use strict';
// Retrieve the list of all runs
/*app.get('/runs', function(req, res) {
// NOT YET
});*/
// Create a new run
app.post('/runs', function(req, res) {
// Grab the test parameters
var run = {
// Generate a random run ID
_id: (Date.now()*1000 + Math.round(Math.random()*1000)).toString(36),
params: {
url: req.body.url,
waitForResponse: req.body.waitForResponse || true
}
};
// Add test to the testQueue
debug('Adding test %s to the queue', run._id);
var queuing = runsQueue.push(run._id);
// Save the run to the datastore
var position = runsQueue.getPosition(run._id);
run.status = {
statusCode: (position === 0) ? STATUS_RUNNING : STATUS_AWAITING,
position: position
};
runsDatastore.add(run);
// Listening for position updates
queuing.progress(function(position) {
var savedRun = runsDatastore.get(run._id);
savedRun.status = {
statusCode: STATUS_AWAITING,
position: position
};
runsDatastore.update(savedRun);
});
queuing.then(function() {
});
// The user doesn't not want to wait for the response
if (!params.waitForResponse) {
// Sending just the test id
res.setHeader('Content-Type', 'application/javascript');
res.send(JSON.stringify({
testId: testId
}));
}
});
// Retrive one run by id
app.get('/run/:id', function(req, res) {
});
// Delete one run by id
/*app.delete('/run/:id', function(req, res) {
// NOT YET
});*/
var STATUS_AWAITING = 'awaiting';
var STATUS_RUNNING = 'running';
var STATUS_DONE = 'done';
var STATUS_FAILED = 'failed';
}
module.exports = ApiController;
@@ -0,0 +1,9 @@
function ResultsDatastore() {
'use strict';
}
module.exports = ResultsDatastore;
+36
View File
@@ -0,0 +1,36 @@
function RunsDatastore() {
'use strict';
// NOT PERSISTING RUNS
// For the moment, maybe one day
var runs = {};
this.add = function(run) {
runs[run._id] = run;
};
this.get = function(runId) {
return runs[runId];
};
this.update = function(run) {
runs[run._id] = run;
};
this.delete = function(runId) {
delete runs[runId];
};
this.list = function() {
var runsArray = [];
Object.keys(runs).forEach(function(key) {
runsArray.push(runs[key]);
});
return runsArray;
};
}
module.exports = RunsDatastore;
+73
View File
@@ -0,0 +1,73 @@
var Q = require('q');
function RunsQueue() {
'use strict';
var queue = [];
this.push = function(runId) {
var deferred = Q.defer();
if (queue.length === 0) {
// The queue is empty, let's run immediatly
queue.push({
runId: runId
});
deferred.resolve();
} else {
queue.push({
runId: runId,
positionChangedCallback: function(position) {
deferred.notify(position);
},
itIsTimeCallback: function() {
deferred.resolve();
}
});
}
return deferred.promise;
};
this.getPosition = function(runId) {
// Position 0 means it's a work in progress (a run is removed AFTER it is finished, not before)
var position = -1;
queue.some(function(run, index) {
if (run.runId === runId) {
position = index;
return true;
}
return false;
});
return position;
};
this.remove = function(runId) {
var position = this.getPosition(runId);
if (position >= 0) {
queue.splice(position, 1);
}
// Update other runs' positions
queue.forEach(function(run, index) {
if (index === 0 && run.itIsTimeCallback) {
run.itIsTimeCallback();
} else if (index > 0 && run.positionChangedCallback) {
run.positionChangedCallback(index);
}
});
};
}
module.exports = RunsQueue;