Advertisement
Guest User

Untitled

a guest
Apr 21st, 2015
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.81 KB | None | 0 0
  1. var fs = require('fs');
  2.  
  3. module.exports = {
  4.  
  5. /**
  6. * Returns the available dates we have data.
  7. * @param {[type]} req [description]
  8. * @param {[type]} res [description]
  9. * @param {Function} next [description]
  10. * @return {Array} [description]
  11. */
  12. getDates: function (req, res, next) {
  13.  
  14. var dates = [];
  15. fs.readdir('data', function (err, files) {
  16.  
  17. if (err) {
  18. console.warn(err);
  19. return res.negotiate(err);
  20. }
  21.  
  22. files.forEach(function (file) {
  23. if(~file.indexOf('.json')) {
  24. dates.push(file.replace('.json', ''));
  25. }
  26. });
  27.  
  28. res.json(dates);
  29. });
  30.  
  31. },
  32.  
  33. /**
  34. * Calculate the "periods"
  35. * @param {[type]} req [description]
  36. * @param {[type]} res [description]
  37. * @return {[type]} [description]
  38. */
  39. calculated: function (req, res) {
  40. /* jshint maxstatements: 60*/
  41.  
  42. var rotationId = req.params.id || 120;
  43. var startDate = req.params.start || '2015-03-01';
  44. var endDate = req.params.end || '2015-03-13';
  45. var allDates = getDatesBetween(new Date(startDate), (new Date(endDate)));
  46. var allRotations = [];
  47. var delay = 0;
  48. var Y = 0;
  49.  
  50. // Push all dates to array
  51. for(var h = 0; h < allDates.length; h++){
  52. allRotations.push({
  53. date: allDates[h],
  54. rotations: require('../../data/' + allDates[h] + '.json')
  55. });
  56. }
  57.  
  58. // Get the position from ID.
  59. var pos = allRotations[0].rotations.map(function(e) {
  60. return e.id;
  61. }).indexOf(parseInt(rotationId));
  62.  
  63.  
  64. // Removes days that the bus didn't go on that rotation
  65. for(var g = 0; g < allRotations.length; g++) {
  66. if(allRotations[g].rotations[pos].trips.length <= 0){
  67.  
  68. allRotations.splice(g, 1);
  69. }
  70. }
  71.  
  72. // Quick and dirty clone
  73. var calculatedRotation = _.cloneDeep(allRotations[0].rotations[pos]);
  74.  
  75. /* jshint maxdepth: 5*/
  76. // looping trips
  77.  
  78. for (var i = 0, tripLen = allRotations[0].rotations[pos].trips.length; i < tripLen; i++) {
  79.  
  80. // looping stops
  81. for (var j = 0, stopLen = allRotations[0].rotations[pos].trips[i].stops.length; j < stopLen; j++) {
  82.  
  83. calculatedRotation.trips[i].stops[j].y = Y;
  84.  
  85. arrStop = 0;
  86. delay = 0;
  87. stopDelayAvg = 0;
  88.  
  89. // looping dates
  90. for (var k = 0, rotLen = allRotations.length; k < rotLen; k++) {
  91.  
  92. if (j < allRotations[k].rotations[pos].trips[i].stops.length-1) {
  93. var arrToNextStop = allRotations[k].rotations[pos].trips[i].stops[j].arrivalToNextStop;
  94. var schToNextStop = allRotations[k].rotations[pos].trips[i].stops[j].schedualedTimeToNextStop;
  95. var delayNextStop = allRotations[k].rotations[pos].trips[i].stops[j].delayNextStop;
  96. var stopDelay = allRotations[k].rotations[pos].trips[i].stops[j].stopDelay;
  97. //var Y = allRotations[k].rotations[pos].trips[i].stops[j].y;
  98.  
  99.  
  100. // If we have null values
  101. if (arrToNextStop == null) {
  102. arrToNextStop = schToNextStop;
  103. delayNextStop = 0;
  104. }
  105.  
  106. arrStop += arrToNextStop;
  107. delay += delayNextStop;
  108. stopDelayAvg += stopDelay;
  109.  
  110. }
  111.  
  112. }
  113.  
  114.  
  115. delay = delay / allRotations.length;
  116. stopDelayAvg = stopDelayAvg / allRotations.length;
  117. arrStop = arrStop / allRotations.length;
  118.  
  119. Y += arrStop;
  120.  
  121. calculatedRotation.trips[i].stops[j].delayToNextStop = delay;
  122. calculatedRotation.trips[i].stops[j].arrivalToNextStop = arrStop;
  123. calculatedRotation.trips[i].stops[j].stopDelay = stopDelayAvg;
  124.  
  125.  
  126.  
  127. }
  128.  
  129. if (i != tripLen-1) {
  130.  
  131. calculatedRotation.trips[i].stops[stops.length-1].y = allRotations[k].rotations[pos].trips[i].pause;
  132.  
  133. }
  134.  
  135.  
  136. }
  137.  
  138. res.json(calculatedRotation);
  139. }
  140. };
  141.  
  142.  
  143.  
  144.  
  145. // jshint freeze:false
  146.  
  147. Date.prototype.addDays = function(days) {
  148. var dat = new Date(this.valueOf());
  149. dat.setDate(dat.getDate() + days);
  150. return dat;
  151. };
  152.  
  153. /**
  154. * [getDates description]
  155. * @param {[type]} startDate [description]
  156. * @param {[type]} stopDate [description]
  157. * @return {[type]} [description]
  158. */
  159. function getDatesBetween(startDate, stopDate) {
  160. var dateArray = [];
  161. var currentDate = startDate;
  162.  
  163. while (currentDate <= stopDate) {
  164. dateArray.push(currentDate.toISOString().substring(0, 10));
  165. currentDate = currentDate.addDays(1);
  166. }
  167. return dateArray;
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement