forked from arp242/goatcounter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_agent.go
214 lines (182 loc) · 5.05 KB
/
user_agent.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Copyright © Martin Tournoij – This file is part of GoatCounter and published
// under the terms of a slightly modified EUPL v1.2 license, which can be found
// in the LICENSE file or at https://license.goatcounter.com
package goatcounter
import (
"context"
"zgo.at/errors"
"zgo.at/gadget"
"zgo.at/isbot"
"zgo.at/zcache"
"zgo.at/zdb"
)
type UserAgent struct {
ID int64 `db:"user_agent_id"`
UserAgent string `db:"ua"`
Isbot uint8 `db:"isbot"`
BrowserID int64 `db:"browser_id"`
SystemID int64 `db:"system_id"`
}
func (p *UserAgent) Defaults(ctx context.Context) {
}
func (p *UserAgent) Validate(ctx context.Context) error {
v := NewValidate(ctx)
// UserAgent may be an empty string, as some browsers send that.
v.UTF8("user_agent", p.UserAgent)
return v.ErrorOrNil()
}
func (p *UserAgent) ByID(ctx context.Context, id int64) error {
err := zdb.Get(ctx, p, `/* UserAgent.ByID */
select * from user_agents where user_agent_id=$1`, id)
return errors.Wrapf(err, "UserAgent.ByID %d", id)
}
// Update this useragent; re-parse it and set the new Browser/System (if any).
func (p *UserAgent) Update(ctx context.Context) error {
if p.ID == 0 {
panic("ID is 0")
}
var (
changed = false
ua = gadget.ParseUA(p.UserAgent)
browser Browser
system System
)
err := browser.GetOrInsert(ctx, ua.BrowserName, ua.BrowserVersion)
if err != nil {
return errors.Wrap(err, "UserAgent.Update")
}
if p.BrowserID != browser.ID {
changed = true
p.BrowserID = browser.ID
}
err = system.GetOrInsert(ctx, ua.OSName, ua.OSVersion)
if err != nil {
return errors.Wrap(err, "UserAgent.Update")
}
if p.SystemID != system.ID {
changed = true
p.SystemID = system.ID
}
bot := isbot.UserAgent(p.UserAgent)
if uint8(bot) != p.Isbot {
changed = true
p.Isbot = uint8(bot)
}
if !changed {
return nil
}
err = zdb.Exec(ctx,
`update user_agents set isbot=$1, browser_id=$2, system_id=$3 where user_agent_id=$4`,
p.Isbot, p.BrowserID, p.SystemID, p.ID)
if err != nil {
return errors.Wrap(err, "UserAgent.Update")
}
cacheUA(ctx).Delete(gadget.ShortenUA(p.UserAgent))
return nil
}
func (p *UserAgent) GetOrInsert(ctx context.Context) error {
shortUA := gadget.ShortenUA(p.UserAgent)
c, ok := cacheUA(ctx).Get(shortUA)
if ok {
*p = c.(UserAgent)
cacheUA(ctx).Touch(shortUA, zcache.DefaultExpiration)
return nil
}
p.Defaults(ctx)
err := p.Validate(ctx)
if err != nil {
return err
}
err = zdb.Get(ctx, p, `/* UserAgent.GetOrInsert */
select * from user_agents where ua = $1 limit 1`, shortUA)
if err != nil && !zdb.ErrNoRows(err) {
return errors.Errorf("UserAgent.GetOrInsert select: %w", err)
}
if err == nil {
cacheUA(ctx).SetDefault(shortUA, *p)
return nil // Got a row already, no need for a new one.
}
var (
ua = gadget.ParseUA(p.UserAgent)
browser Browser
system System
)
err = browser.GetOrInsert(ctx, ua.BrowserName, ua.BrowserVersion)
if err != nil {
return errors.Wrap(err, "UserAgent.GetOrInsert")
}
p.BrowserID = browser.ID
err = system.GetOrInsert(ctx, ua.OSName, ua.OSVersion)
if err != nil {
return errors.Wrap(err, "UserAgent.GetOrInsert")
}
p.SystemID = system.ID
// Insert new row.
p.Isbot = uint8(isbot.UserAgent(p.UserAgent))
p.ID, err = zdb.InsertID(ctx, "user_agent_id",
`insert into user_agents (ua, isbot, browser_id, system_id) values (?, ?, ?, ?)`,
shortUA, p.Isbot, p.BrowserID, p.SystemID)
if err != nil {
return errors.Wrap(err, "UserAgent.GetOrInsert insert")
}
cacheUA(ctx).SetDefault(shortUA, *p)
return nil
}
type Browser struct {
ID int64 `db:"browser_id"`
Name string `db:"name"`
Version string `db:"version"`
}
func (b *Browser) GetOrInsert(ctx context.Context, name, version string) error {
k := name + version
c, ok := cacheBrowsers(ctx).Get(k)
if ok {
*b = c.(Browser)
cacheBrowsers(ctx).Touch(k, zcache.DefaultExpiration)
return nil
}
b.Name = name
b.Version = version
err := zdb.Get(ctx, &b.ID,
`select browser_id from browsers where name=$1 and version=$2`,
name, version)
if zdb.ErrNoRows(err) {
b.ID, err = zdb.InsertID(ctx, "browser_id",
`insert into browsers (name, version) values ($1, $2)`,
name, version)
}
if err != nil {
return errors.Wrapf(err, "Browser.GetOrInsert %q %q", name, version)
}
cacheBrowsers(ctx).SetDefault(k, *b)
return nil
}
type System struct {
ID int64 `db:"system_id"`
Name string `db:"name"`
Version string `db:"version"`
}
func (s *System) GetOrInsert(ctx context.Context, name, version string) error {
k := name + version
c, ok := cacheSystems(ctx).Get(k)
if ok {
*s = c.(System)
cacheSystems(ctx).Touch(k, zcache.DefaultExpiration)
return nil
}
s.Name = name
s.Version = version
err := zdb.Get(ctx, &s.ID,
`select system_id from systems where name=$1 and version=$2`,
name, version)
if zdb.ErrNoRows(err) {
s.ID, err = zdb.InsertID(ctx, "system_id",
`insert into systems (name, version) values ($1, $2)`,
name, version)
}
if err != nil {
return errors.Wrapf(err, "System.GetOrInsert %q %q", name, version)
}
cacheSystems(ctx).SetDefault(k, *s)
return nil
}