Clean up visit updater
This commit is contained in:
parent
cdc25f1094
commit
e67e23c050
2 changed files with 25 additions and 52 deletions
12
database.go
12
database.go
|
@ -300,40 +300,40 @@ func (d *DB) UpdateHourVisits() error {
|
|||
var u DBUpdate
|
||||
u.src = d.session.DB(DB_NAME).C(STATS_COLL)
|
||||
u.dst = d.session.DB(DB_NAME).C(HOURLY_VISITS_COLL)
|
||||
return u.UpdateHourVisits()
|
||||
return u.UpdateHourVisits(false)
|
||||
}
|
||||
|
||||
func (d *DB) UpdateDayVisits() error {
|
||||
var u DBUpdate
|
||||
u.src = d.session.DB(DB_NAME).C(STATS_COLL)
|
||||
u.dst = d.session.DB(DB_NAME).C(DAILY_VISITS_COLL)
|
||||
return u.UpdateDayVisits()
|
||||
return u.UpdateDayVisits(false)
|
||||
}
|
||||
|
||||
func (d *DB) UpdateMonthVisits() error {
|
||||
var u DBUpdate
|
||||
u.src = d.session.DB(DB_NAME).C(STATS_COLL)
|
||||
u.dst = d.session.DB(DB_NAME).C(MONTHLY_VISITS_COLL)
|
||||
return u.UpdateMonthVisits()
|
||||
return u.UpdateMonthVisits(false)
|
||||
}
|
||||
|
||||
func (d *DB) UpdateHourDownloads() error {
|
||||
var u DBUpdate
|
||||
u.src = d.session.DB(DB_NAME).C(STATS_COLL)
|
||||
u.dst = d.session.DB(DB_NAME).C(HOURLY_DOWNLOADS_COLL)
|
||||
return u.UpdateHourDownloads()
|
||||
return u.UpdateHourVisits(true)
|
||||
}
|
||||
|
||||
func (d *DB) UpdateDayDownloads() error {
|
||||
var u DBUpdate
|
||||
u.src = d.session.DB(DB_NAME).C(STATS_COLL)
|
||||
u.dst = d.session.DB(DB_NAME).C(DAILY_DOWNLOADS_COLL)
|
||||
return u.UpdateDayDownloads()
|
||||
return u.UpdateDayVisits(true)
|
||||
}
|
||||
|
||||
func (d *DB) UpdateMonthDownloads() error {
|
||||
var u DBUpdate
|
||||
u.src = d.session.DB(DB_NAME).C(STATS_COLL)
|
||||
u.dst = d.session.DB(DB_NAME).C(MONTHLY_DOWNLOADS_COLL)
|
||||
return u.UpdateMonthDownloads()
|
||||
return u.UpdateMonthVisits(true)
|
||||
}
|
||||
|
|
59
db_stats.go
59
db_stats.go
|
@ -104,77 +104,50 @@ func (u *DBUpdate) UpdateMostBooks(section string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (u *DBUpdate) UpdateHourVisits() error {
|
||||
f := func(t time.Time) time.Time {
|
||||
const span = time.Hour
|
||||
return t.Add(span).Truncate(span)
|
||||
}
|
||||
func (u *DBUpdate) UpdateHourVisits(isDownloads bool) error {
|
||||
const numDays = 2
|
||||
spanStore := numDays * 24 * time.Hour
|
||||
return u.updateVisits(f, spanStore, HOURLY_VISITS_COLL, true)
|
||||
return u.updateVisits(hourInc, spanStore, isDownloads)
|
||||
}
|
||||
|
||||
func (u *DBUpdate) UpdateDayVisits() error {
|
||||
f := func(t time.Time) time.Time {
|
||||
const span = 24 * time.Hour
|
||||
return t.Add(span).Truncate(span)
|
||||
}
|
||||
func (u *DBUpdate) UpdateDayVisits(isDownloads bool) error {
|
||||
const numDays = 30
|
||||
spanStore := numDays * 24 * time.Hour
|
||||
return u.updateVisits(f, spanStore, DAILY_VISITS_COLL, true)
|
||||
return u.updateVisits(dayInc, spanStore, isDownloads)
|
||||
}
|
||||
|
||||
func (u *DBUpdate) UpdateMonthVisits() error {
|
||||
f := func(t time.Time) time.Time {
|
||||
const span = 24 * time.Hour
|
||||
return t.AddDate(0, 1, 1-t.Day()).Truncate(span)
|
||||
}
|
||||
func (u *DBUpdate) UpdateMonthVisits(isDownloads bool) error {
|
||||
const numDays = 365
|
||||
spanStore := numDays * 24 * time.Hour
|
||||
return u.updateVisits(f, spanStore, MONTHLY_VISITS_COLL, true)
|
||||
return u.updateVisits(monthInc, spanStore, isDownloads)
|
||||
}
|
||||
|
||||
func (u *DBUpdate) UpdateHourDownloads() error {
|
||||
f := func(t time.Time) time.Time {
|
||||
func hourInc(date time.Time) time.Time {
|
||||
const span = time.Hour
|
||||
return t.Add(span).Truncate(span)
|
||||
}
|
||||
const numDays = 2
|
||||
spanStore := numDays * 24 * time.Hour
|
||||
return u.updateVisits(f, spanStore, HOURLY_DOWNLOADS_COLL, false)
|
||||
return date.Add(span).Truncate(span)
|
||||
}
|
||||
|
||||
func (u *DBUpdate) UpdateDayDownloads() error {
|
||||
f := func(t time.Time) time.Time {
|
||||
func dayInc(date time.Time) time.Time {
|
||||
const span = 24 * time.Hour
|
||||
return t.Add(span).Truncate(span)
|
||||
}
|
||||
const numDays = 30
|
||||
spanStore := numDays * 24 * time.Hour
|
||||
return u.updateVisits(f, spanStore, DAILY_DOWNLOADS_COLL, false)
|
||||
return date.Add(span).Truncate(span)
|
||||
}
|
||||
|
||||
func (u *DBUpdate) UpdateMonthDownloads() error {
|
||||
f := func(t time.Time) time.Time {
|
||||
func monthInc(date time.Time) time.Time {
|
||||
const span = 24 * time.Hour
|
||||
return t.AddDate(0, 1, 1-t.Day()).Truncate(span)
|
||||
}
|
||||
const numDays = 365
|
||||
spanStore := numDays * 24 * time.Hour
|
||||
return u.updateVisits(f, spanStore, MONTHLY_DOWNLOADS_COLL, false)
|
||||
return date.AddDate(0, 1, 1-date.Day()).Truncate(span)
|
||||
}
|
||||
|
||||
func (u *DBUpdate) updateVisits(incTime func(time.Time) time.Time, spanStore time.Duration, coll string, useSession bool) error {
|
||||
func (u *DBUpdate) updateVisits(incTime func(time.Time) time.Time, spanStore time.Duration, isDownloads bool) error {
|
||||
start := u.calculateStart(spanStore)
|
||||
for start.Before(time.Now().UTC()) {
|
||||
stop := incTime(start)
|
||||
|
||||
var count int
|
||||
var err error
|
||||
if useSession {
|
||||
count = u.countVisits(start, stop)
|
||||
} else {
|
||||
if isDownloads {
|
||||
count, err = u.countDownloads(start, stop)
|
||||
} else {
|
||||
count = u.countVisits(start, stop)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
Reference in a new issue