forked from trantor-library/trantor
/
stats.go
197 lines (168 loc) · 4.23 KB
/
stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package main
import (
log "github.com/cihub/seelog"
"net/http"
"strconv"
"strings"
"time"
"git.gitorious.org/trantor/trantor.git/database"
"git.gitorious.org/trantor/trantor.git/storage"
"github.com/gorilla/mux"
)
const (
stats_version = 2
)
type handler struct {
w http.ResponseWriter
r *http.Request
sess *Session
db *database.DB
store *storage.Store
}
type StatsGatherer struct {
db *database.DB
store *storage.Store
channel chan statsRequest
}
func InitStats(database *database.DB, store *storage.Store) *StatsGatherer {
sg := new(StatsGatherer)
sg.channel = make(chan statsRequest, CHAN_SIZE)
sg.db = database
sg.store = store
go sg.worker()
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()
defer h.db.Close()
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}
}
}
type statsRequest struct {
date time.Time
vars map[string]string
sess *Session
r *http.Request
}
func (sg StatsGatherer) worker() {
db := sg.db.Copy()
defer db.Close()
for req := range sg.channel {
stats := make(map[string]interface{})
appendFiles(req.r, stats)
appendMuxVars(req.vars, stats)
appendUrl(req.r, stats)
appendSession(req.sess, stats)
stats["version"] = stats_version
stats["method"] = req.r.Method
stats["date"] = req.date
db.AddStats(stats)
}
}
func statsHandler(h handler) {
var data statsData
data.S = GetStatus(h)
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)
loadTemplate(h, "stats", data)
}
type statsData struct {
S Status
HVisits []visitData
DVisits []visitData
MVisits []visitData
HDownloads []visitData
DDownloads []visitData
MDownloads []visitData
}
type visitData struct {
Label string
Count int
}
func hourlyLabel(date time.Time) string {
return strconv.Itoa(date.Hour() + 1)
}
func dailyLabel(date time.Time) string {
return strconv.Itoa(date.Day())
}
func monthlyLabel(date time.Time) string {
return date.Month().String()
}
func getVisits(funcLabel func(time.Time) string, db *database.DB, visitType database.VisitType) []visitData {
var visits []visitData
visit, err := db.GetVisits(visitType)
if err != nil {
log.Warn("GetVisits error (", visitType, "): ", err)
}
for _, v := range visit {
var elem visitData
elem.Label = funcLabel(v.Date.UTC())
elem.Count = v.Count
visits = append(visits, elem)
}
return visits
}
func appendFiles(r *http.Request, stats map[string]interface{}) {
if r.Method == "POST" && r.MultipartForm != nil {
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":
stats["id"] = value
case key == "ids":
var objectIds []string
ids := strings.Split(value, "/")
for _, id := range ids {
objectIds = append(objectIds, id)
}
if len(objectIds) > 0 {
stats["ids"] = objectIds
stats["id"] = objectIds[0]
}
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
}
}