Use aggregation for most visited books

This commit is contained in:
Las Zenow 2014-02-18 21:50:44 +01:00
parent 706fc88a61
commit e4726661b8
3 changed files with 29 additions and 14 deletions

View file

@ -34,6 +34,7 @@ const (
MINUTES_UPDATE_MONTHLY_D = 60*24 + 17
MINUTES_UPDATE_LOGGER = 5
TAGS_DISPLAY = 50
BOOKS_FRONT_PAGE = 6
SEARCH_ITEMS_PAGE = 20
NEW_ITEMS_PAGE = 50
NUM_NEWS = 10

View file

@ -26,7 +26,7 @@ func GetBooksVisited(num int, visitedColl *mgo.Collection) ([]bson.ObjectId, err
var result []struct {
Book bson.ObjectId "_id"
}
err := visitedColl.Find(nil).Sort("-value").Limit(num).All(&result)
err := visitedColl.Find(nil).Sort("-count").Limit(num).All(&result)
if err != nil {
return nil, err
}
@ -93,16 +93,30 @@ func (m *MR) updateMostBooks(statsColl *mgo.Collection, section string, resColl
const numDays = 30
start := time.Now().UTC().Add(-numDays * 24 * time.Hour)
var mr mgo.MapReduce
mr.Map = `function() {
emit(this.id, 1);
}`
mr.Reduce = `function(tag, vals) {
var count = 0;
vals.forEach(function() { count += 1; });
return count;
}`
return m.update(&mr, bson.M{"date": bson.M{"$gt": start}, "section": section}, statsColl, resColl)
var books []struct {
Book string "_id"
Count int "count"
}
err := statsColl.Pipe([]bson.M{
{"$match": bson.M{"date": bson.M{"$gt": start}, "section": section}},
{"$project": bson.M{"id": 1}},
{"$group": bson.M{"_id": "$id", "count": bson.M{"$sum": 1}}},
{"$sort": bson.M{"count": -1}},
{"$limit": BOOKS_FRONT_PAGE},
}).All(&books)
if err != nil {
return err
}
coll := m.database.C(resColl)
coll.DropCollection()
for _, book := range books {
err = coll.Insert(book)
if err != nil {
return err
}
}
return nil
}
func (m *MR) UpdateHourVisits(statsColl *mgo.Collection) error {

View file

@ -130,9 +130,9 @@ func indexHandler(h handler) {
data.Tags, _ = h.db.GetTags(TAGS_DISPLAY)
data.S = GetStatus(h)
data.S.Home = true
data.Books, data.Count, _ = h.db.GetBooks(bson.M{"active": true}, 6)
data.VisitedBooks, _ = h.db.GetVisitedBooks(6)
data.DownloadedBooks, _ = h.db.GetDownloadedBooks(6)
data.Books, data.Count, _ = h.db.GetBooks(bson.M{"active": true}, BOOKS_FRONT_PAGE)
data.VisitedBooks, _ = h.db.GetVisitedBooks(BOOKS_FRONT_PAGE)
data.DownloadedBooks, _ = h.db.GetDownloadedBooks(BOOKS_FRONT_PAGE)
data.News = getNews(1, DAYS_NEWS_INDEXPAGE, h.db)
loadTemplate(h.w, "index", data)
}