Use aggregation for most visited books
This commit is contained in:
parent
706fc88a61
commit
e4726661b8
3 changed files with 29 additions and 14 deletions
|
@ -34,6 +34,7 @@ const (
|
||||||
MINUTES_UPDATE_MONTHLY_D = 60*24 + 17
|
MINUTES_UPDATE_MONTHLY_D = 60*24 + 17
|
||||||
MINUTES_UPDATE_LOGGER = 5
|
MINUTES_UPDATE_LOGGER = 5
|
||||||
TAGS_DISPLAY = 50
|
TAGS_DISPLAY = 50
|
||||||
|
BOOKS_FRONT_PAGE = 6
|
||||||
SEARCH_ITEMS_PAGE = 20
|
SEARCH_ITEMS_PAGE = 20
|
||||||
NEW_ITEMS_PAGE = 50
|
NEW_ITEMS_PAGE = 50
|
||||||
NUM_NEWS = 10
|
NUM_NEWS = 10
|
||||||
|
|
36
mapreduce.go
36
mapreduce.go
|
@ -26,7 +26,7 @@ func GetBooksVisited(num int, visitedColl *mgo.Collection) ([]bson.ObjectId, err
|
||||||
var result []struct {
|
var result []struct {
|
||||||
Book bson.ObjectId "_id"
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -93,16 +93,30 @@ func (m *MR) updateMostBooks(statsColl *mgo.Collection, section string, resColl
|
||||||
const numDays = 30
|
const numDays = 30
|
||||||
start := time.Now().UTC().Add(-numDays * 24 * time.Hour)
|
start := time.Now().UTC().Add(-numDays * 24 * time.Hour)
|
||||||
|
|
||||||
var mr mgo.MapReduce
|
var books []struct {
|
||||||
mr.Map = `function() {
|
Book string "_id"
|
||||||
emit(this.id, 1);
|
Count int "count"
|
||||||
}`
|
}
|
||||||
mr.Reduce = `function(tag, vals) {
|
err := statsColl.Pipe([]bson.M{
|
||||||
var count = 0;
|
{"$match": bson.M{"date": bson.M{"$gt": start}, "section": section}},
|
||||||
vals.forEach(function() { count += 1; });
|
{"$project": bson.M{"id": 1}},
|
||||||
return count;
|
{"$group": bson.M{"_id": "$id", "count": bson.M{"$sum": 1}}},
|
||||||
}`
|
{"$sort": bson.M{"count": -1}},
|
||||||
return m.update(&mr, bson.M{"date": bson.M{"$gt": start}, "section": section}, statsColl, resColl)
|
{"$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 {
|
func (m *MR) UpdateHourVisits(statsColl *mgo.Collection) error {
|
||||||
|
|
|
@ -130,9 +130,9 @@ func indexHandler(h handler) {
|
||||||
data.Tags, _ = h.db.GetTags(TAGS_DISPLAY)
|
data.Tags, _ = h.db.GetTags(TAGS_DISPLAY)
|
||||||
data.S = GetStatus(h)
|
data.S = GetStatus(h)
|
||||||
data.S.Home = true
|
data.S.Home = true
|
||||||
data.Books, data.Count, _ = h.db.GetBooks(bson.M{"active": true}, 6)
|
data.Books, data.Count, _ = h.db.GetBooks(bson.M{"active": true}, BOOKS_FRONT_PAGE)
|
||||||
data.VisitedBooks, _ = h.db.GetVisitedBooks(6)
|
data.VisitedBooks, _ = h.db.GetVisitedBooks(BOOKS_FRONT_PAGE)
|
||||||
data.DownloadedBooks, _ = h.db.GetDownloadedBooks(6)
|
data.DownloadedBooks, _ = h.db.GetDownloadedBooks(BOOKS_FRONT_PAGE)
|
||||||
data.News = getNews(1, DAYS_NEWS_INDEXPAGE, h.db)
|
data.News = getNews(1, DAYS_NEWS_INDEXPAGE, h.db)
|
||||||
loadTemplate(h.w, "index", data)
|
loadTemplate(h.w, "index", data)
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue