Created
November 22, 2015 18:48
-
-
Save stevenbeeckman/e74804ee433dc6514bf9 to your computer and use it in GitHub Desktop.
Routes for Villo Analytics
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'); | |
var mongoose = require('mongoose'); | |
var moment = require('moment'); | |
var router = express.Router(); | |
router.get('/', function(req, res, next) { | |
req.db.StationName.find({}, {"name": 1, "number": 1, "_id": 0}, {sort: {"name": 1}}, function(err, names){ | |
if(err){ | |
next(err); | |
}else{ | |
console.log(names); | |
res.render('index', {title: 'Villo Analytics', stationNames: names}); | |
} | |
}); | |
}); | |
router.get('/station/:number.html', function(req, res, next){ | |
req.db.Station.findOne({"number": req.params.number}, null, null, function(err, s){ | |
if(err){ | |
console.err(err); | |
next(err); | |
}else{ | |
var station = s; | |
req.db.Station.find({"number": req.params.number}, {"last_update": 1, "available_bikes": 1, "_id": 0}, {sort: {"last_update": 1}}, function(err, m){ | |
if(err){ | |
next(err) | |
}else{ | |
var m_formatted_dates = m.map(function(measurement){ | |
return { | |
last_update: moment(measurement.last_update).format("YYYY/MM/DD HH:mm:ss") | |
, available_bikes: measurement.available_bikes | |
} | |
}) | |
station.measurements = m_formatted_dates; | |
res.render('station', {"station": station}); | |
} | |
}) | |
} | |
}); | |
}); | |
function measurementToCSV(measurement){ | |
var m = measurement.last_update + "," + measurement.available_bikes; | |
return m; | |
} | |
function arrayToCSV(measurements){ | |
var dygraph_data_string = ""; | |
for(var i = 0; i < measurements.length; i++){ | |
dygraph_data_string = dygraph_data_string + measurementToCSV(measurements[i]) + "\n"; | |
} | |
return dygraph_data_string.slice(0,-1); | |
} | |
module.exports = router; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment