Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var fs = require('fs');
- module.exports = {
- /**
- * Returns the available dates we have data.
- * @param {[type]} req [description]
- * @param {[type]} res [description]
- * @param {Function} next [description]
- * @return {Array} [description]
- */
- getDates: function (req, res, next) {
- var dates = [];
- fs.readdir('data', function (err, files) {
- if (err) {
- console.warn(err);
- return res.negotiate(err);
- }
- files.forEach(function (file) {
- if(~file.indexOf('.json')) {
- dates.push(file.replace('.json', ''));
- }
- });
- res.json(dates);
- });
- },
- /**
- * Calculate the "periods"
- * @param {[type]} req [description]
- * @param {[type]} res [description]
- * @return {[type]} [description]
- */
- calculated: function (req, res) {
- /* jshint maxstatements: 60*/
- var rotationId = req.params.id || 120;
- var startDate = req.params.start || '2015-03-01';
- var endDate = req.params.end || '2015-03-13';
- var allDates = getDatesBetween(new Date(startDate), (new Date(endDate)));
- var allRotations = [];
- var delay = 0;
- var Y = 0;
- // Push all dates to array
- for(var h = 0; h < allDates.length; h++){
- allRotations.push({
- date: allDates[h],
- rotations: require('../../data/' + allDates[h] + '.json')
- });
- }
- // Get the position from ID.
- var pos = allRotations[0].rotations.map(function(e) {
- return e.id;
- }).indexOf(parseInt(rotationId));
- // Removes days that the bus didn't go on that rotation
- for(var g = 0; g < allRotations.length; g++) {
- if(allRotations[g].rotations[pos].trips.length <= 0){
- allRotations.splice(g, 1);
- }
- }
- // Quick and dirty clone
- var calculatedRotation = _.cloneDeep(allRotations[0].rotations[pos]);
- /* jshint maxdepth: 5*/
- // looping trips
- for (var i = 0, tripLen = allRotations[0].rotations[pos].trips.length; i < tripLen; i++) {
- // looping stops
- for (var j = 0, stopLen = allRotations[0].rotations[pos].trips[i].stops.length; j < stopLen; j++) {
- calculatedRotation.trips[i].stops[j].y = Y;
- arrStop = 0;
- delay = 0;
- stopDelayAvg = 0;
- // looping dates
- for (var k = 0, rotLen = allRotations.length; k < rotLen; k++) {
- if (j < allRotations[k].rotations[pos].trips[i].stops.length-1) {
- var arrToNextStop = allRotations[k].rotations[pos].trips[i].stops[j].arrivalToNextStop;
- var schToNextStop = allRotations[k].rotations[pos].trips[i].stops[j].schedualedTimeToNextStop;
- var delayNextStop = allRotations[k].rotations[pos].trips[i].stops[j].delayNextStop;
- var stopDelay = allRotations[k].rotations[pos].trips[i].stops[j].stopDelay;
- //var Y = allRotations[k].rotations[pos].trips[i].stops[j].y;
- // If we have null values
- if (arrToNextStop == null) {
- arrToNextStop = schToNextStop;
- delayNextStop = 0;
- }
- arrStop += arrToNextStop;
- delay += delayNextStop;
- stopDelayAvg += stopDelay;
- }
- }
- delay = delay / allRotations.length;
- stopDelayAvg = stopDelayAvg / allRotations.length;
- arrStop = arrStop / allRotations.length;
- Y += arrStop;
- calculatedRotation.trips[i].stops[j].delayToNextStop = delay;
- calculatedRotation.trips[i].stops[j].arrivalToNextStop = arrStop;
- calculatedRotation.trips[i].stops[j].stopDelay = stopDelayAvg;
- }
- if (i != tripLen-1) {
- calculatedRotation.trips[i].stops[stops.length-1].y = allRotations[k].rotations[pos].trips[i].pause;
- }
- }
- res.json(calculatedRotation);
- }
- };
- // jshint freeze:false
- Date.prototype.addDays = function(days) {
- var dat = new Date(this.valueOf());
- dat.setDate(dat.getDate() + days);
- return dat;
- };
- /**
- * [getDates description]
- * @param {[type]} startDate [description]
- * @param {[type]} stopDate [description]
- * @return {[type]} [description]
- */
- function getDatesBetween(startDate, stopDate) {
- var dateArray = [];
- var currentDate = startDate;
- while (currentDate <= stopDate) {
- dateArray.push(currentDate.toISOString().substring(0, 10));
- currentDate = currentDate.addDays(1);
- }
- return dateArray;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement