For stats don't pass around the request

This commit is contained in:
Las Zenow 2019-11-06 02:50:18 +00:00
parent b2e3a93e7d
commit 665ca3aa94

View file

@ -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 {