This repository has been archived on 2025-03-01. You can view files and clone it, but cannot push or open issues or pull requests.
trantor/lib/stats.go

149 lines
3.1 KiB
Go
Raw Normal View History

2016-05-02 21:36:49 -04:00
package trantor
2013-04-22 23:28:00 +02:00
import (
2014-08-30 13:17:50 -05:00
log "github.com/cihub/seelog"
2013-04-22 23:28:00 +02:00
"net/http"
"strings"
"time"
"github.com/gorilla/mux"
2016-05-02 21:36:49 -04:00
"gitlab.com/trantor/trantor/lib/database"
"gitlab.com/trantor/trantor/lib/storage"
2013-04-22 23:28:00 +02:00
)
2014-08-21 19:24:23 -05:00
const (
stats_version = 2
statsChanSize = 100
2014-08-21 19:24:23 -05:00
)
type handler struct {
w http.ResponseWriter
r *http.Request
sess *Session
2017-05-21 10:16:16 +00:00
db database.DB
store storage.Store
template *Template
hostname string
2017-05-21 10:16:16 +00:00
ro bool
}
2014-08-21 19:24:23 -05:00
type StatsGatherer struct {
2017-05-21 10:16:16 +00:00
db database.DB
store storage.Store
template *Template
hostname string
channel chan statsRequest
2017-05-21 10:16:16 +00:00
ro bool
2013-04-22 23:28:00 +02:00
}
2017-05-21 10:16:16 +00:00
func InitStats(database database.DB, store storage.Store, hostname string, template *Template, ro bool) *StatsGatherer {
2016-05-03 10:11:59 -04:00
sg := StatsGatherer{
channel: make(chan statsRequest, statsChanSize),
db: database,
store: store,
template: template,
hostname: hostname,
2017-05-21 10:16:16 +00:00
ro: ro,
2016-05-03 10:11:59 -04:00
}
2014-08-21 19:24:23 -05:00
go sg.worker()
2016-05-03 10:11:59 -04:00
return &sg
2014-08-21 19:24:23 -05:00
}
func (sg StatsGatherer) Gather(function func(handler)) func(http.ResponseWriter, *http.Request) {
2013-04-22 23:28:00 +02:00
return func(w http.ResponseWriter, r *http.Request) {
2014-02-11 14:41:50 +01:00
log.Info("Query ", r.Method, " ", r.RequestURI)
2016-05-03 10:11:59 -04:00
h := handler{
store: sg.store,
db: sg.db,
2016-05-03 10:11:59 -04:00
template: sg.template,
hostname: sg.hostname,
w: w,
r: r,
sess: GetSession(r, sg.db),
2017-05-21 10:16:16 +00:00
ro: sg.ro,
2016-05-03 10:11:59 -04:00
}
function(h)
2014-08-21 19:24:23 -05:00
sg.channel <- statsRequest{time.Now(), mux.Vars(r), h.sess, r}
2013-04-22 23:28:00 +02:00
}
}
type statsRequest struct {
date time.Time
vars map[string]string
sess *Session
r *http.Request
}
2014-08-21 19:24:23 -05:00
func (sg StatsGatherer) worker() {
for req := range sg.channel {
2013-04-22 23:28:00 +02:00
stats := make(map[string]interface{})
appendFiles(req.r, stats)
appendMuxVars(req.vars, stats)
appendUrl(req.r, stats)
appendSession(req.sess, stats)
2014-08-21 19:24:23 -05:00
stats["version"] = stats_version
2013-04-22 23:28:00 +02:00
stats["method"] = req.r.Method
stats["date"] = req.date
sg.db.AddStats(stats)
2013-04-22 23:28:00 +02:00
}
}
func appendFiles(r *http.Request, stats map[string]interface{}) {
if r.Method == "POST" && r.MultipartForm != nil {
2013-04-22 23:28:00 +02:00
files := r.MultipartForm.File
for key := range files {
list := make([]string, len(files[key]))
for i, f := range files[key] {
list[i] = f.Filename
}
stats[key] = list
}
}
}
func appendMuxVars(vars map[string]string, stats map[string]interface{}) {
for key, value := range vars {
switch {
case key == "id":
2014-08-21 19:24:23 -05:00
stats["id"] = value
2013-04-22 23:28:00 +02:00
case key == "ids":
2014-08-21 19:24:23 -05:00
var objectIds []string
2013-04-22 23:28:00 +02:00
ids := strings.Split(value, "/")
for _, id := range ids {
2014-08-21 19:24:23 -05:00
objectIds = append(objectIds, id)
2013-04-22 23:28:00 +02:00
}
2013-05-09 09:42:58 +02:00
if len(objectIds) > 0 {
stats["ids"] = objectIds
stats["id"] = objectIds[0]
}
2013-04-22 23:28:00 +02:00
default:
stats[key] = value
}
}
}
func appendUrl(r *http.Request, stats map[string]interface{}) {
for key, value := range r.URL.Query() {
stats[key] = value
}
stats["host"] = r.Host
stats["path"] = r.URL.Path
pattern := strings.Split(r.URL.Path, "/")
if len(pattern) > 1 && pattern[1] != "" {
stats["section"] = pattern[1]
} else {
stats["section"] = "/"
}
}
func appendSession(sess *Session, stats map[string]interface{}) {
stats["session"] = sess.Id()
if sess.User != "" {
stats["user"] = sess.User
}
}