Get the visits for unique users
This commit is contained in:
parent
5693b3e58f
commit
392078aae0
1 changed files with 71 additions and 35 deletions
90
mapreduce.go
90
mapreduce.go
|
@ -9,6 +9,9 @@ import (
|
||||||
type MR struct {
|
type MR struct {
|
||||||
meta *mgo.Collection
|
meta *mgo.Collection
|
||||||
tags *mgo.Collection
|
tags *mgo.Collection
|
||||||
|
hourly_raw *mgo.Collection
|
||||||
|
daily_raw *mgo.Collection
|
||||||
|
monthly_raw *mgo.Collection
|
||||||
hourly *mgo.Collection
|
hourly *mgo.Collection
|
||||||
daily *mgo.Collection
|
daily *mgo.Collection
|
||||||
monthly *mgo.Collection
|
monthly *mgo.Collection
|
||||||
|
@ -18,6 +21,9 @@ func NewMR(database *mgo.Database) *MR {
|
||||||
m := new(MR)
|
m := new(MR)
|
||||||
m.meta = database.C(META_COLL)
|
m.meta = database.C(META_COLL)
|
||||||
m.tags = database.C(TAGS_COLL)
|
m.tags = database.C(TAGS_COLL)
|
||||||
|
m.hourly_raw = database.C(HOURLY_VISITS_COLL + "_raw")
|
||||||
|
m.daily_raw = database.C(DAILY_VISITS_COLL + "_raw")
|
||||||
|
m.monthly_raw = database.C(MONTHLY_VISITS_COLL + "_raw")
|
||||||
m.hourly = database.C(HOURLY_VISITS_COLL)
|
m.hourly = database.C(HOURLY_VISITS_COLL)
|
||||||
m.daily = database.C(DAILY_VISITS_COLL)
|
m.daily = database.C(DAILY_VISITS_COLL)
|
||||||
m.monthly = database.C(MONTHLY_VISITS_COLL)
|
m.monthly = database.C(MONTHLY_VISITS_COLL)
|
||||||
|
@ -60,20 +66,30 @@ func (m *MR) GetTags(numTags int, booksColl *mgo.Collection) ([]string, error) {
|
||||||
|
|
||||||
func (m *MR) GetHourVisits(start time.Time, statsColl *mgo.Collection) ([]Visits, error) {
|
func (m *MR) GetHourVisits(start time.Time, statsColl *mgo.Collection) ([]Visits, error) {
|
||||||
if m.isOutdated(HOURLY_VISITS_COLL, MINUTES_UPDATE_HOURLY) {
|
if m.isOutdated(HOURLY_VISITS_COLL, MINUTES_UPDATE_HOURLY) {
|
||||||
var mr mgo.MapReduce
|
const reduce = `function(date, vals) {
|
||||||
mr.Map = `function() {
|
|
||||||
var day = Date.UTC(this.date.getUTCFullYear(),
|
|
||||||
this.date.getUTCMonth(),
|
|
||||||
this.date.getUTCDate(),
|
|
||||||
this.date.getUTCHours());
|
|
||||||
emit(day, 1);
|
|
||||||
}`
|
|
||||||
mr.Reduce = `function(date, vals) {
|
|
||||||
var count = 0;
|
var count = 0;
|
||||||
vals.forEach(function(v) { count += v; });
|
vals.forEach(function(v) { count += v; });
|
||||||
return count;
|
return count;
|
||||||
}`
|
}`
|
||||||
err := m.update(&mr, bson.M{"date": bson.M{"$gte": start}}, statsColl, HOURLY_VISITS_COLL)
|
var mr mgo.MapReduce
|
||||||
|
mr.Map = `function() {
|
||||||
|
var date = Date.UTC(this.date.getUTCFullYear(),
|
||||||
|
this.date.getUTCMonth(),
|
||||||
|
this.date.getUTCDate(),
|
||||||
|
this.date.getUTCHours());
|
||||||
|
emit({date: date, session: this.session}, 1);
|
||||||
|
}`
|
||||||
|
mr.Reduce = reduce
|
||||||
|
err := m.update(&mr, bson.M{"date": bson.M{"$gte": start}}, statsColl, HOURLY_VISITS_COLL+"_raw")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var mr2 mgo.MapReduce
|
||||||
|
mr2.Map = `function() {
|
||||||
|
emit(this['_id']['date'], 1);
|
||||||
|
}`
|
||||||
|
mr2.Reduce = reduce
|
||||||
|
err = m.update(&mr2, bson.M{}, m.hourly_raw, HOURLY_VISITS_COLL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -86,19 +102,29 @@ func (m *MR) GetHourVisits(start time.Time, statsColl *mgo.Collection) ([]Visits
|
||||||
|
|
||||||
func (m *MR) GetDayVisits(start time.Time, statsColl *mgo.Collection) ([]Visits, error) {
|
func (m *MR) GetDayVisits(start time.Time, statsColl *mgo.Collection) ([]Visits, error) {
|
||||||
if m.isOutdated(DAILY_VISITS_COLL, MINUTES_UPDATE_DAILY) {
|
if m.isOutdated(DAILY_VISITS_COLL, MINUTES_UPDATE_DAILY) {
|
||||||
var mr mgo.MapReduce
|
const reduce = `function(date, vals) {
|
||||||
mr.Map = `function() {
|
|
||||||
var day = Date.UTC(this.date.getUTCFullYear(),
|
|
||||||
this.date.getUTCMonth(),
|
|
||||||
this.date.getUTCDate());
|
|
||||||
emit(day, 1);
|
|
||||||
}`
|
|
||||||
mr.Reduce = `function(date, vals) {
|
|
||||||
var count = 0;
|
var count = 0;
|
||||||
vals.forEach(function(v) { count += v; });
|
vals.forEach(function(v) { count += v; });
|
||||||
return count;
|
return count;
|
||||||
}`
|
}`
|
||||||
err := m.update(&mr, bson.M{"date": bson.M{"$gte": start}}, statsColl, DAILY_VISITS_COLL)
|
var mr mgo.MapReduce
|
||||||
|
mr.Map = `function() {
|
||||||
|
var date = Date.UTC(this.date.getUTCFullYear(),
|
||||||
|
this.date.getUTCMonth(),
|
||||||
|
this.date.getUTCDate());
|
||||||
|
emit({date: date, session: this.session}, 1);
|
||||||
|
}`
|
||||||
|
mr.Reduce = reduce
|
||||||
|
err := m.update(&mr, bson.M{"date": bson.M{"$gte": start}}, statsColl, DAILY_VISITS_COLL+"_raw")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var mr2 mgo.MapReduce
|
||||||
|
mr2.Map = `function() {
|
||||||
|
emit(this['_id']['date'], 1);
|
||||||
|
}`
|
||||||
|
mr2.Reduce = reduce
|
||||||
|
err = m.update(&mr2, bson.M{}, m.daily_raw, DAILY_VISITS_COLL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -111,18 +137,28 @@ func (m *MR) GetDayVisits(start time.Time, statsColl *mgo.Collection) ([]Visits,
|
||||||
|
|
||||||
func (m *MR) GetMonthVisits(start time.Time, statsColl *mgo.Collection) ([]Visits, error) {
|
func (m *MR) GetMonthVisits(start time.Time, statsColl *mgo.Collection) ([]Visits, error) {
|
||||||
if m.isOutdated(MONTHLY_VISITS_COLL, MINUTES_UPDATE_MONTHLY) {
|
if m.isOutdated(MONTHLY_VISITS_COLL, MINUTES_UPDATE_MONTHLY) {
|
||||||
var mr mgo.MapReduce
|
const reduce = `function(date, vals) {
|
||||||
mr.Map = `function() {
|
|
||||||
var day = Date.UTC(this.date.getUTCFullYear(),
|
|
||||||
this.date.getUTCMonth());
|
|
||||||
emit(day, 1);
|
|
||||||
}`
|
|
||||||
mr.Reduce = `function(date, vals) {
|
|
||||||
var count = 0;
|
var count = 0;
|
||||||
vals.forEach(function(v) { count += v; });
|
vals.forEach(function(v) { count += v; });
|
||||||
return count;
|
return count;
|
||||||
}`
|
}`
|
||||||
err := m.update(&mr, bson.M{"date": bson.M{"$gte": start}}, statsColl, MONTHLY_VISITS_COLL)
|
var mr mgo.MapReduce
|
||||||
|
mr.Map = `function() {
|
||||||
|
var date = Date.UTC(this.date.getUTCFullYear(),
|
||||||
|
this.date.getUTCMonth());
|
||||||
|
emit({date: date, session: this.session}, 1);
|
||||||
|
}`
|
||||||
|
mr.Reduce = reduce
|
||||||
|
err := m.update(&mr, bson.M{"date": bson.M{"$gte": start}}, statsColl, MONTHLY_VISITS_COLL+"_raw")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var mr2 mgo.MapReduce
|
||||||
|
mr2.Map = `function() {
|
||||||
|
emit(this['_id']['date'], 1);
|
||||||
|
}`
|
||||||
|
mr2.Reduce = reduce
|
||||||
|
err = m.update(&mr2, bson.M{}, m.monthly_raw, MONTHLY_VISITS_COLL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue