apiController.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. var debug = require('debug')('ylt:server');
  2. var YellowLabTools = require('../../yellowlabtools');
  3. var RunsQueue = require('../datastores/runsQueue');
  4. var RunsDatastore = require('../datastores/runsDatastore');
  5. var ResultsDatastore = require('../datastores/resultsDatastore');
  6. var ApiController = function(app) {
  7. 'use strict';
  8. var queue = new RunsQueue();
  9. var runsDatastore = new RunsDatastore();
  10. var resultsDatastore = new ResultsDatastore();
  11. // Create a new run
  12. app.post('/api/runs', function(req, res) {
  13. // Grab the test parameters and generate a random run ID
  14. var run = {
  15. runId: (Date.now()*1000 + Math.round(Math.random()*1000)).toString(36),
  16. params: {
  17. url: req.body.url,
  18. waitForResponse: req.body.waitForResponse !== false,
  19. partialResult: req.body.partialResult || null
  20. }
  21. };
  22. // Add test to the testQueue
  23. debug('Adding test %s to the queue', run.runId);
  24. var queuePromise = queue.push(run.runId);
  25. // Save the run to the datastore
  26. runsDatastore.add(run, queuePromise.startingPosition);
  27. // Listening for position updates
  28. queuePromise.progress(function(position) {
  29. runsDatastore.updatePosition(run.runId, position);
  30. });
  31. // Let's start the run
  32. queuePromise.then(function() {
  33. runsDatastore.updatePosition(run.runId, 0);
  34. debug('Launching test %s on %s', run.runId, run.params.url);
  35. new YellowLabTools(run.params.url)
  36. .then(function(data) {
  37. debug('Success');
  38. // Save result in datastore
  39. data.runId = run.runId;
  40. resultsDatastore.saveResult(data)
  41. .then(function() {
  42. runsDatastore.markAsComplete(run.runId);
  43. // Send result if the user was waiting
  44. if (run.params.waitForResponse) {
  45. // If the user only wants a portion of the result (partialResult option)
  46. switch(run.params.partialResult) {
  47. case 'generalScores':
  48. res.redirect(302, '/api/results/' + run.runId + '/generalScores');
  49. break;
  50. case 'rules':
  51. res.redirect(302, '/api/results/' + run.runId + '/rules');
  52. break;
  53. case 'javascriptExecutionTree':
  54. res.redirect(302, '/api/results/' + run.runId + '/javascriptExecutionTree');
  55. break;
  56. case 'phantomas':
  57. res.redirect(302, '/api/results/' + run.runId + '/toolsResults/phantomas');
  58. break;
  59. default:
  60. res.redirect(302, '/api/results/' + run.runId);
  61. }
  62. }
  63. })
  64. .fail(function(err) {
  65. debug('Saving results to resultsDatastore failed:');
  66. debug(err);
  67. });
  68. }).fail(function(err) {
  69. console.error('Test failed for %s', run.params.url);
  70. console.error(err.toString());
  71. runsDatastore.markAsFailed(run.runId, err.toString());
  72. }).finally(function() {
  73. queue.remove(run.runId);
  74. });
  75. }).fail(function(err) {
  76. console.error('Error or YLT\'s core instanciation');
  77. console.error(err);
  78. console.error(err.stack);
  79. });
  80. // The user doesn't not want to wait for the response, sending the run ID only
  81. if (!run.params.waitForResponse) {
  82. console.log('Sending response without waiting.');
  83. res.setHeader('Content-Type', 'application/json');
  84. res.send(JSON.stringify({runId: run.runId}));
  85. }
  86. });
  87. // Retrive one run by id
  88. app.get('/api/runs/:id', function(req, res) {
  89. var runId = req.params.id;
  90. var run = runsDatastore.get(runId);
  91. if (run) {
  92. res.setHeader('Content-Type', 'application/json');
  93. res.send(JSON.stringify(run, null, 2));
  94. } else {
  95. res.status(404).send('Not found');
  96. }
  97. });
  98. // Retrieve the list of all runs
  99. /*app.get('/api/runs', function(req, res) {
  100. // NOT YET
  101. });*/
  102. // Delete one run by id
  103. /*app.delete('/api/runs/:id', function(req, res) {
  104. deleteRun()
  105. });*/
  106. // Delete all
  107. /*app.delete('/api/runs', function(req, res) {
  108. purgeRuns()
  109. });
  110. // List all
  111. app.get('/api/runs', function(req, res) {
  112. listRuns()
  113. });
  114. // Exists
  115. app.head('/api/runs/:id', function(req, res) {
  116. existsX();
  117. // Returns 200 if the result exists or 404 if not
  118. });
  119. */
  120. // Retrive one result by id
  121. app.get('/api/results/:id', function(req, res) {
  122. getPartialResults(req.params.id, res, function(data) {
  123. return data;
  124. });
  125. });
  126. // Retrieve one result and return only the generalScores part of the response
  127. app.get('/api/results/:id/generalScores', function(req, res) {
  128. getPartialResults(req.params.id, res, function(data) {
  129. return data.scoreProfiles.generic;
  130. });
  131. });
  132. app.get('/api/results/:id/generalScores/:scoreProfile', function(req, res) {
  133. getPartialResults(req.params.id, res, function(data) {
  134. return data.scoreProfiles[req.params.scoreProfile];
  135. });
  136. });
  137. app.get('/api/results/:id/rules', function(req, res) {
  138. getPartialResults(req.params.id, res, function(data) {
  139. return data.rules;
  140. });
  141. });
  142. app.get('/api/results/:id/javascriptExecutionTree', function(req, res) {
  143. getPartialResults(req.params.id, res, function(data) {
  144. return data.javascriptExecutionTree;
  145. });
  146. });
  147. app.get('/api/results/:id/toolsResults/phantomas', function(req, res) {
  148. getPartialResults(req.params.id, res, function(data) {
  149. return data.toolsResults.phantomas;
  150. });
  151. });
  152. function getPartialResults(runId, res, partialGetterFn) {
  153. resultsDatastore.getResult(runId)
  154. .then(function(data) {
  155. var results = partialGetterFn(data);
  156. if (typeof results === 'undefined') {
  157. res.status(404).send('Not found');
  158. return;
  159. }
  160. res.setHeader('Content-Type', 'application/json');
  161. res.send(JSON.stringify(results, null, 2));
  162. }).fail(function() {
  163. res.status(404).send('Not found');
  164. });
  165. }
  166. };
  167. module.exports = ApiController;