From e52e4f0c394e93abbc66a4c64fa09ad3cd343f5d Mon Sep 17 00:00:00 2001 From: Las Zenow Date: Wed, 25 Sep 2013 23:07:11 +0200 Subject: [PATCH] The downloads don't depends on the session --- mapreduce.go | 73 ++++++++++++++-------------------------------------- 1 file changed, 20 insertions(+), 53 deletions(-) diff --git a/mapreduce.go b/mapreduce.go index 8b097ae..cf39e1d 100644 --- a/mapreduce.go +++ b/mapreduce.go @@ -230,11 +230,6 @@ func (m *MR) GetMonthVisits(start time.Time, statsColl *mgo.Collection) ([]Visit func (m *MR) GetHourDownloads(start time.Time, statsColl *mgo.Collection) ([]Visits, error) { if m.isOutdated(HOURLY_DOWNLOADS_COLL, MINUTES_UPDATE_HOURLY) { - const reduce = `function(date, vals) { - var count = 0; - vals.forEach(function(v) { count += v; }); - return count; - }` var mr mgo.MapReduce mr.Map = `function() { if (this.section == "download") { @@ -245,18 +240,12 @@ func (m *MR) GetHourDownloads(start time.Time, statsColl *mgo.Collection) ([]Vis emit({date: date}, 1); } }` - mr.Reduce = reduce - err := m.update(&mr, bson.M{"date": bson.M{"$gte": start}}, statsColl, HOURLY_DOWNLOADS_COLL+"_raw") - if err != nil { - return nil, err - } - var mr2 mgo.MapReduce - mr2.Map = `function() { - emit(this['_id']['date'], 1); - }` - mr2.Reduce = reduce - hourly_raw := m.database.C(HOURLY_DOWNLOADS_COLL + "_raw") - err = m.update(&mr2, bson.M{}, hourly_raw, HOURLY_DOWNLOADS_COLL) + mr.Reduce = `function(date, vals) { + var count = 0; + vals.forEach(function(v) { count += v; }); + return count; + }` + err := m.update(&mr, bson.M{"date": bson.M{"$gte": start}}, statsColl, HOURLY_DOWNLOADS_COLL) if err != nil { return nil, err } @@ -270,32 +259,21 @@ func (m *MR) GetHourDownloads(start time.Time, statsColl *mgo.Collection) ([]Vis func (m *MR) GetDayDowloads(start time.Time, statsColl *mgo.Collection) ([]Visits, error) { if m.isOutdated(DAILY_DOWNLOADS_COLL, MINUTES_UPDATE_DAILY) { - const reduce = `function(date, vals) { - var count = 0; - vals.forEach(function(v) { count += v; }); - return count; - }` var mr mgo.MapReduce mr.Map = `function() { if (this.section == "download") { var date = Date.UTC(this.date.getUTCFullYear(), this.date.getUTCMonth(), this.date.getUTCDate()); - emit({date: date, session: this.session}, 1); + emit({date: date}, 1); } }` - mr.Reduce = reduce - err := m.update(&mr, bson.M{"date": bson.M{"$gte": start}}, statsColl, DAILY_DOWNLOADS_COLL+"_raw") - if err != nil { - return nil, err - } - var mr2 mgo.MapReduce - mr2.Map = `function() { - emit(this['_id']['date'], 1); - }` - mr2.Reduce = reduce - daily_raw := m.database.C(DAILY_DOWNLOADS_COLL + "_raw") - err = m.update(&mr2, bson.M{}, daily_raw, DAILY_DOWNLOADS_COLL) + mr.Reduce = `function(date, vals) { + var count = 0; + vals.forEach(function(v) { count += v; }); + return count; + }` + err := m.update(&mr, bson.M{"date": bson.M{"$gte": start}}, statsColl, DAILY_DOWNLOADS_COLL) if err != nil { return nil, err } @@ -309,31 +287,20 @@ func (m *MR) GetDayDowloads(start time.Time, statsColl *mgo.Collection) ([]Visit func (m *MR) GetMonthDowloads(start time.Time, statsColl *mgo.Collection) ([]Visits, error) { if m.isOutdated(MONTHLY_DOWNLOADS_COLL, MINUTES_UPDATE_MONTHLY) { - const reduce = `function(date, vals) { - var count = 0; - vals.forEach(function(v) { count += v; }); - return count; - }` var mr mgo.MapReduce mr.Map = `function() { if (this.section == "download") { var date = Date.UTC(this.date.getUTCFullYear(), this.date.getUTCMonth()); - emit({date: date, session: this.session}, 1); + emit({date: date}, 1); } }` - mr.Reduce = reduce - err := m.update(&mr, bson.M{"date": bson.M{"$gte": start}}, statsColl, MONTHLY_DOWNLOADS_COLL+"_raw") - if err != nil { - return nil, err - } - var mr2 mgo.MapReduce - mr2.Map = `function() { - emit(this['_id']['date'], 1); - }` - mr2.Reduce = reduce - monthly_raw := m.database.C(MONTHLY_DOWNLOADS_COLL + "_raw") - err = m.update(&mr2, bson.M{}, monthly_raw, MONTHLY_DOWNLOADS_COLL) + mr.Reduce = `function(date, vals) { + var count = 0; + vals.forEach(function(v) { count += v; }); + return count; + }` + err := m.update(&mr, bson.M{"date": bson.M{"$gte": start}}, statsColl, MONTHLY_DOWNLOADS_COLL) if err != nil { return nil, err }