Clean up db stats visits
This commit is contained in:
parent
8927a8cd6d
commit
cdc25f1094
2 changed files with 48 additions and 106 deletions
59
database.go
59
database.go
|
@ -4,6 +4,7 @@ import log "github.com/cihub/seelog"
|
|||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"errors"
|
||||
"labix.org/v2/mgo"
|
||||
"labix.org/v2/mgo/bson"
|
||||
"os"
|
||||
|
@ -258,14 +259,41 @@ func (d *DB) UpdateTags() error {
|
|||
return u.UpdateTags()
|
||||
}
|
||||
|
||||
type VisitType int
|
||||
|
||||
const (
|
||||
hourly_visits = iota
|
||||
daily_visits
|
||||
monthly_visits
|
||||
hourly_downloads
|
||||
daily_downloads
|
||||
monthly_downloads
|
||||
)
|
||||
|
||||
type Visits struct {
|
||||
Date time.Time "date"
|
||||
Count int "count"
|
||||
}
|
||||
|
||||
func (d *DB) GetHourVisits() ([]Visits, error) {
|
||||
hourlyColl := d.session.DB(DB_NAME).C(HOURLY_VISITS_COLL)
|
||||
return GetVisits(hourlyColl)
|
||||
func (d *DB) GetVisits(visitType VisitType) ([]Visits, error) {
|
||||
var coll *mgo.Collection
|
||||
switch visitType {
|
||||
case hourly_visits:
|
||||
coll = d.session.DB(DB_NAME).C(HOURLY_VISITS_COLL)
|
||||
case daily_visits:
|
||||
coll = d.session.DB(DB_NAME).C(DAILY_VISITS_COLL)
|
||||
case monthly_visits:
|
||||
coll = d.session.DB(DB_NAME).C(MONTHLY_VISITS_COLL)
|
||||
case hourly_downloads:
|
||||
coll = d.session.DB(DB_NAME).C(HOURLY_DOWNLOADS_COLL)
|
||||
case daily_downloads:
|
||||
coll = d.session.DB(DB_NAME).C(DAILY_DOWNLOADS_COLL)
|
||||
case monthly_downloads:
|
||||
coll = d.session.DB(DB_NAME).C(MONTHLY_DOWNLOADS_COLL)
|
||||
default:
|
||||
return nil, errors.New("Not valid VisitType")
|
||||
}
|
||||
return GetVisits(coll)
|
||||
}
|
||||
|
||||
func (d *DB) UpdateHourVisits() error {
|
||||
|
@ -275,11 +303,6 @@ func (d *DB) UpdateHourVisits() error {
|
|||
return u.UpdateHourVisits()
|
||||
}
|
||||
|
||||
func (d *DB) GetDayVisits() ([]Visits, error) {
|
||||
dailyColl := d.session.DB(DB_NAME).C(DAILY_VISITS_COLL)
|
||||
return GetVisits(dailyColl)
|
||||
}
|
||||
|
||||
func (d *DB) UpdateDayVisits() error {
|
||||
var u DBUpdate
|
||||
u.src = d.session.DB(DB_NAME).C(STATS_COLL)
|
||||
|
@ -287,11 +310,6 @@ func (d *DB) UpdateDayVisits() error {
|
|||
return u.UpdateDayVisits()
|
||||
}
|
||||
|
||||
func (d *DB) GetMonthVisits() ([]Visits, error) {
|
||||
monthlyColl := d.session.DB(DB_NAME).C(MONTHLY_VISITS_COLL)
|
||||
return GetVisits(monthlyColl)
|
||||
}
|
||||
|
||||
func (d *DB) UpdateMonthVisits() error {
|
||||
var u DBUpdate
|
||||
u.src = d.session.DB(DB_NAME).C(STATS_COLL)
|
||||
|
@ -299,11 +317,6 @@ func (d *DB) UpdateMonthVisits() error {
|
|||
return u.UpdateMonthVisits()
|
||||
}
|
||||
|
||||
func (d *DB) GetHourDownloads() ([]Visits, error) {
|
||||
hourlyColl := d.session.DB(DB_NAME).C(HOURLY_DOWNLOADS_COLL)
|
||||
return GetVisits(hourlyColl)
|
||||
}
|
||||
|
||||
func (d *DB) UpdateHourDownloads() error {
|
||||
var u DBUpdate
|
||||
u.src = d.session.DB(DB_NAME).C(STATS_COLL)
|
||||
|
@ -311,11 +324,6 @@ func (d *DB) UpdateHourDownloads() error {
|
|||
return u.UpdateHourDownloads()
|
||||
}
|
||||
|
||||
func (d *DB) GetDayDownloads() ([]Visits, error) {
|
||||
dailyColl := d.session.DB(DB_NAME).C(DAILY_DOWNLOADS_COLL)
|
||||
return GetVisits(dailyColl)
|
||||
}
|
||||
|
||||
func (d *DB) UpdateDayDownloads() error {
|
||||
var u DBUpdate
|
||||
u.src = d.session.DB(DB_NAME).C(STATS_COLL)
|
||||
|
@ -323,11 +331,6 @@ func (d *DB) UpdateDayDownloads() error {
|
|||
return u.UpdateDayDownloads()
|
||||
}
|
||||
|
||||
func (d *DB) GetMonthDownloads() ([]Visits, error) {
|
||||
monthlyColl := d.session.DB(DB_NAME).C(MONTHLY_DOWNLOADS_COLL)
|
||||
return GetVisits(monthlyColl)
|
||||
}
|
||||
|
||||
func (d *DB) UpdateMonthDownloads() error {
|
||||
var u DBUpdate
|
||||
u.src = d.session.DB(DB_NAME).C(STATS_COLL)
|
||||
|
|
95
stats.go
95
stats.go
|
@ -69,12 +69,12 @@ func statsHandler(h handler) {
|
|||
var data statsData
|
||||
data.S = GetStatus(h)
|
||||
data.S.Stats = true
|
||||
data.HVisits = getHourlyVisits(h.db)
|
||||
data.DVisits = getDailyVisits(h.db)
|
||||
data.MVisits = getMonthlyVisits(h.db)
|
||||
data.HDownloads = getHourlyDownloads(h.db)
|
||||
data.DDownloads = getDailyDownloads(h.db)
|
||||
data.MDownloads = getMonthlyDownloads(h.db)
|
||||
data.HVisits = getVisits(hourlyLabel, h.db, hourly_visits)
|
||||
data.DVisits = getVisits(dailyLabel, h.db, daily_visits)
|
||||
data.MVisits = getVisits(monthlyLabel, h.db, monthly_visits)
|
||||
data.HDownloads = getVisits(hourlyLabel, h.db, hourly_downloads)
|
||||
data.DDownloads = getVisits(dailyLabel, h.db, daily_downloads)
|
||||
data.MDownloads = getVisits(monthlyLabel, h.db, monthly_downloads)
|
||||
|
||||
loadTemplate(h.w, "stats", data)
|
||||
}
|
||||
|
@ -94,89 +94,28 @@ type visitData struct {
|
|||
Count int
|
||||
}
|
||||
|
||||
func getHourlyVisits(db *DB) []visitData {
|
||||
var visits []visitData
|
||||
|
||||
visit, _ := db.GetHourVisits()
|
||||
for _, v := range visit {
|
||||
var elem visitData
|
||||
hour := v.Date.UTC().Hour()
|
||||
elem.Label = strconv.Itoa(hour + 1)
|
||||
elem.Count = v.Count
|
||||
visits = append(visits, elem)
|
||||
}
|
||||
|
||||
return visits
|
||||
func hourlyLabel(date time.Time) string {
|
||||
return strconv.Itoa(date.Hour() + 1)
|
||||
}
|
||||
|
||||
func getDailyVisits(db *DB) []visitData {
|
||||
var visits []visitData
|
||||
|
||||
visit, _ := db.GetDayVisits()
|
||||
for _, v := range visit {
|
||||
var elem visitData
|
||||
day := v.Date.UTC().Day()
|
||||
elem.Label = strconv.Itoa(day)
|
||||
elem.Count = v.Count
|
||||
visits = append(visits, elem)
|
||||
}
|
||||
|
||||
return visits
|
||||
func dailyLabel(date time.Time) string {
|
||||
return strconv.Itoa(date.Day())
|
||||
}
|
||||
|
||||
func getMonthlyVisits(db *DB) []visitData {
|
||||
var visits []visitData
|
||||
|
||||
visit, _ := db.GetMonthVisits()
|
||||
for _, v := range visit {
|
||||
var elem visitData
|
||||
month := v.Date.UTC().Month()
|
||||
elem.Label = month.String()
|
||||
elem.Count = v.Count
|
||||
visits = append(visits, elem)
|
||||
}
|
||||
|
||||
return visits
|
||||
func monthlyLabel(date time.Time) string {
|
||||
return date.Month().String()
|
||||
}
|
||||
|
||||
func getHourlyDownloads(db *DB) []visitData {
|
||||
func getVisits(funcLabel func(time.Time) string, db *DB, visitType VisitType) []visitData {
|
||||
var visits []visitData
|
||||
|
||||
visit, _ := db.GetHourDownloads()
|
||||
for _, v := range visit {
|
||||
var elem visitData
|
||||
hour := v.Date.UTC().Hour()
|
||||
elem.Label = strconv.Itoa(hour + 1)
|
||||
elem.Count = v.Count
|
||||
visits = append(visits, elem)
|
||||
visit, err := db.GetVisits(visitType)
|
||||
if err != nil {
|
||||
log.Warn("GetVisits error (", visitType, "): ", err)
|
||||
}
|
||||
|
||||
return visits
|
||||
}
|
||||
|
||||
func getDailyDownloads(db *DB) []visitData {
|
||||
var visits []visitData
|
||||
|
||||
visit, _ := db.GetDayDownloads()
|
||||
for _, v := range visit {
|
||||
var elem visitData
|
||||
day := v.Date.UTC().Day()
|
||||
elem.Label = strconv.Itoa(day)
|
||||
elem.Count = v.Count
|
||||
visits = append(visits, elem)
|
||||
}
|
||||
|
||||
return visits
|
||||
}
|
||||
|
||||
func getMonthlyDownloads(db *DB) []visitData {
|
||||
var visits []visitData
|
||||
|
||||
visit, _ := db.GetMonthDownloads()
|
||||
for _, v := range visit {
|
||||
var elem visitData
|
||||
month := v.Date.UTC().Month()
|
||||
elem.Label = month.String()
|
||||
elem.Label = funcLabel(v.Date.UTC())
|
||||
elem.Count = v.Count
|
||||
visits = append(visits, elem)
|
||||
}
|
||||
|
|
Reference in a new issue