GlanceIt / GlanceService

Glance Service
1 stars 0 forks source link

Create a scheduled task that takes the average of user reviews and updates the spot ratings #10

Open mamadi opened 8 years ago

mamadi commented 8 years ago

There needs to be a periodic job that take the average and updates the spot ratings accordingly.

mamadi commented 8 years ago

db.ratingcollection.aggregate([ {$match: {"Spot":"Starbucks-Irvine-5"}}, {$group: { _id: "Spot", avgWifi: {$avg: "$Ratings.Wifi"}, avgStaff: {$avg: "$Ratings.Staff"}, avgCoffee: {$avg: "$Ratings.Coffee"}, avgSeating: {$avg: "$Ratings.Seating"}, avgParking: {$avg: "$Ratings.Parking"}, avgOveral: {$avg: "$Ratings.Overall"} }}])

mamadi commented 8 years ago

Do we need this? or just do it at rating insert/update time?

mamadi commented 8 years ago

/* GET spot average ratings from ratings collection and update them in the spots collection */ router.get('/ratings/:spot', function(req, res) { var db = req.db; var spotIndex = req.params.spot;

collection = db.get('spotcollection');
collection.findOne({index: spotIndex},{},function(err,currRatings){
    if (err) {
        console.log('Could not find spot [' + spotIndex + '] in DB');
        res.json({ result: 'Could not find spot [' + spotIndex + '] in DB' });
    } else {
        console.log('Found spot ' + spotIndex + ' in DB');
        updateSpotAvgRatings(db, currRatings, spotIndex, res);
    }
});

});