From baf2b5a6a93c614cbae437e3ce6f0ca652cda0a3 Mon Sep 17 00:00:00 2001 From: Las Zenow Date: Tue, 3 May 2016 10:11:59 -0400 Subject: [PATCH] Use struct literal initialization --- lib/stats.go | 53 ++++++++++++++++++++++++++-------------------------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/lib/stats.go b/lib/stats.go index f362cc9..f4fa359 100644 --- a/lib/stats.go +++ b/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) }