Use struct literal initialization
This commit is contained in:
parent
0e8f1e7b56
commit
baf2b5a6a9
1 changed files with 27 additions and 26 deletions
53
lib/stats.go
53
lib/stats.go
|
@ -37,34 +37,35 @@ type StatsGatherer struct {
|
|||
}
|
||||
|
||||
func InitStats(database *database.DB, store *storage.Store, hostname string, template *Template) *StatsGatherer {
|
||||
sg := new(StatsGatherer)
|
||||
sg.channel = make(chan statsRequest, statsChanSize)
|
||||
sg.db = database
|
||||
sg.store = store
|
||||
sg.template = template
|
||||
sg.hostname = hostname
|
||||
sg := StatsGatherer{
|
||||
channel: make(chan statsRequest, statsChanSize),
|
||||
db: database,
|
||||
store: store,
|
||||
template: template,
|
||||
hostname: hostname,
|
||||
}
|
||||
|
||||
go sg.worker()
|
||||
return sg
|
||||
return &sg
|
||||
}
|
||||
|
||||
func (sg StatsGatherer) Gather(function func(handler)) func(http.ResponseWriter, *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
log.Info("Query ", r.Method, " ", r.RequestURI)
|
||||
|
||||
var h handler
|
||||
h.store = sg.store
|
||||
h.db = sg.db.Copy()
|
||||
db := sg.db.Copy()
|
||||
h := handler{
|
||||
store: sg.store,
|
||||
db: db,
|
||||
template: sg.template,
|
||||
hostname: sg.hostname,
|
||||
w: w,
|
||||
r: r,
|
||||
sess: GetSession(r, db),
|
||||
}
|
||||
defer h.db.Close()
|
||||
h.template = sg.template
|
||||
h.hostname = sg.hostname
|
||||
|
||||
h.w = w
|
||||
h.r = r
|
||||
h.sess = GetSession(r, h.db)
|
||||
|
||||
function(h)
|
||||
|
||||
sg.channel <- statsRequest{time.Now(), mux.Vars(r), h.sess, r}
|
||||
}
|
||||
}
|
||||
|
@ -94,17 +95,17 @@ func (sg StatsGatherer) worker() {
|
|||
}
|
||||
|
||||
func statsHandler(h handler) {
|
||||
var data statsData
|
||||
data.S = GetStatus(h)
|
||||
data := statsData{
|
||||
S: GetStatus(h),
|
||||
HVisits: getVisits(hourlyLabel, h.db, database.Hourly_visits),
|
||||
DVisits: getVisits(dailyLabel, h.db, database.Daily_visits),
|
||||
MVisits: getVisits(monthlyLabel, h.db, database.Monthly_visits),
|
||||
HDownloads: getVisits(hourlyLabel, h.db, database.Hourly_downloads),
|
||||
DDownloads: getVisits(dailyLabel, h.db, database.Daily_downloads),
|
||||
MDownloads: getVisits(monthlyLabel, h.db, database.Monthly_downloads),
|
||||
}
|
||||
data.S.Title = "Stats -- " + data.S.Title
|
||||
data.S.Stats = true
|
||||
data.HVisits = getVisits(hourlyLabel, h.db, database.Hourly_visits)
|
||||
data.DVisits = getVisits(dailyLabel, h.db, database.Daily_visits)
|
||||
data.MVisits = getVisits(monthlyLabel, h.db, database.Monthly_visits)
|
||||
data.HDownloads = getVisits(hourlyLabel, h.db, database.Hourly_downloads)
|
||||
data.DDownloads = getVisits(dailyLabel, h.db, database.Daily_downloads)
|
||||
data.MDownloads = getVisits(monthlyLabel, h.db, database.Monthly_downloads)
|
||||
|
||||
h.template.load(h, "stats", data)
|
||||
}
|
||||
|
||||
|
|
Reference in a new issue