mirror of
https://github.com/mgerb/mywebsite
synced 2026-01-11 18:32:50 +00:00
more graph work
This commit is contained in:
@@ -23,23 +23,24 @@ router.get('/allsensors', function(req, res, next) {
|
||||
|
||||
});
|
||||
|
||||
router.get('/sensorbylocation', function(req, res, next) {
|
||||
router.get('/sensorbylocation/year', function(req, res, next) {
|
||||
|
||||
var loc = req.query.location;
|
||||
var year = req.query.year;
|
||||
var ye = req.query.year;
|
||||
|
||||
if (year == null){
|
||||
if (ye == null){
|
||||
var date = new Date();
|
||||
year = date.getFullYear();
|
||||
ye = date.getFullYear();
|
||||
}
|
||||
|
||||
//query finds a entries in a collection based on location and the year specified
|
||||
//they are then grouped by date and sorted by date as well
|
||||
temperature.aggregate([ {$match : {location : loc, updated : {$gte : new Date('1 Jan, ' + year), $lt : new Date('1 Jan ' + year +1)}}},
|
||||
{$group : { _id : {location : "$location", month: {$month: "$updated" }, day: { $dayOfMonth: "$updated" }, year: { $year: "$updated" }},
|
||||
temperature.aggregate([ {$project : {location : 1, temperature : 1, year : {$year : "$updated"}, month : {$month : "$updated"}, day : {$dayOfMonth : "$updated"}}},
|
||||
{$match : {location : loc, year : parseInt(ye)}},
|
||||
{$group : {_id : {location : "$location", day: "$day", month : "$month", year : "$year"},
|
||||
max : {$max : "$temperature"},
|
||||
min : {$min : "$temperature"}}},
|
||||
{$sort : {"_id.month" : 1, "_id.day" : 1, "_id.year" : 1}}]).exec(function(err, info){
|
||||
{$sort : {"_id.month" : 1, "_id.day" : 1}}]).exec(function(err, info){
|
||||
|
||||
console.log(info);
|
||||
res.setHeader('Content-Type', 'application/json');
|
||||
@@ -49,21 +50,43 @@ router.get('/sensorbylocation', function(req, res, next) {
|
||||
|
||||
});
|
||||
|
||||
router.post('/', function(req, res,next) {
|
||||
router.get('/sensorbylocation/month', function(req, res, next) {
|
||||
|
||||
var loc = req.query.location;
|
||||
var ye = req.query.year;
|
||||
var mo = req.query.month;
|
||||
var date = new Date();
|
||||
|
||||
if (ye == null){
|
||||
ye = date.getFullYear();
|
||||
}
|
||||
|
||||
if (mo == null){
|
||||
mo = date.getMonth();
|
||||
}
|
||||
|
||||
console.log(ye + "/" + mo);
|
||||
//query finds a entries in a collection based on location and the year specified
|
||||
//they are then grouped by date and sorted by date as well
|
||||
temperature.aggregate([ {$project : {location : 1, temperature : 1, year : {$year : "$updated"}, month : {$month : "$updated"}, day : {$dayOfMonth : "$updated"}}},
|
||||
{$match : {location : loc, year : parseInt(ye), month : parseInt(mo)}},
|
||||
{$group : {_id : {location : "$location", day: "$day", month : "$month", year : "$year"},
|
||||
max : {$max : "$temperature"},
|
||||
min : {$min : "$temperature"}}},
|
||||
{$sort : {"_id.day" : 1}}]).exec(function(err, info){
|
||||
|
||||
console.log(info);
|
||||
res.setHeader('Content-Type', 'application/json');
|
||||
res.send(JSON.stringify(info, null, 4));
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
||||
module.exports = router;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -74,9 +74,30 @@ router.get('/information', function(req, res, err){
|
||||
if (sensor_location == null){
|
||||
res.redirect('/404');
|
||||
}
|
||||
else{
|
||||
temperature.aggregate([{$project : {location : "$location", year : {$year : "$updated"}, month : {$month : "$updated"}}},
|
||||
{$match : {location : sensor_location}},
|
||||
{$group : {_id : {year : "$year", month : "$month", location : "$location"}}},
|
||||
{$sort : {"_id.year" : 1, "_id.month" : 1}}]).exec(function(err, info){
|
||||
console.log(info);
|
||||
|
||||
res.render('sensor_information', {info : {location : sensor_location}});
|
||||
//generate list of unique years to display in dropdown menu
|
||||
var years_list = [];
|
||||
|
||||
for (var i in info){
|
||||
if (years_list.indexOf(info[i]._id.year) > -1){
|
||||
|
||||
}
|
||||
else {
|
||||
years_list.push(info[i]._id.year);
|
||||
}
|
||||
}
|
||||
|
||||
res.render('sensor_information', {info, years_list});
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
|
||||
Reference in New Issue
Block a user