From 665ca3aa942a5c2ce8752997092106bb8f3b60b0 Mon Sep 17 00:00:00 2001 From: Las Zenow Date: Wed, 6 Nov 2019 02:50:18 +0000 Subject: [PATCH] For stats don't pass around the request --- lib/stats.go | 47 ++++++++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/lib/stats.go b/lib/stats.go index bb68514..709f9a2 100644 --- a/lib/stats.go +++ b/lib/stats.go @@ -121,12 +121,27 @@ func (sg StatsGatherer) Gather(function func(handler)) func(http.ResponseWriter, if h.t1.IsZero() { h.t1 = time.Now() } - sg.channel <- statsRequest{r, h.t1.Sub(t0)} + + pattern := strings.Split(r.URL.Path, "/") + section := "/" + if len(pattern) > 1 && pattern[1] != "" { + section = pattern[1] + } + sg.channel <- statsRequest{ + id: mux.Vars(r)["id"], + search: strings.Join(r.Form["q"], " "), + fmt: r.FormValue("fmt"), + section: section, + duration: h.t1.Sub(t0), + } } } type statsRequest struct { - r *http.Request + id string + search string + fmt string + section string duration time.Duration } @@ -137,35 +152,25 @@ func (sg StatsGatherer) worker() { } func (sg StatsGatherer) save(req statsRequest) { - id := mux.Vars(req.r)["id"] - search := strings.Join(req.r.Form["q"], " ") - fmt := req.r.FormValue("fmt") - pattern := strings.Split(req.r.URL.Path, "/") - - section := "/" - if len(pattern) > 1 && pattern[1] != "" { - section = pattern[1] - } - sg.instrument.Request(instrument.RequestData{ - Section: section, - ID: id, - Search: search, - Fmt: fmt, + Section: req.section, + ID: req.id, + Search: req.search, + Fmt: req.fmt, Duration: req.duration, }) - _, err := sg.db.GetBookID(id) + _, err := sg.db.GetBookID(req.id) if err != nil { return } - switch section { + switch req.section { case "download": - err = sg.db.IncDownloads(id) + err = sg.db.IncDownloads(req.id) case "book": - err = sg.db.IncViews(id) + err = sg.db.IncViews(req.id) case "read": - err = sg.db.IncViews(id) + err = sg.db.IncViews(req.id) } if err != nil {