-
Notifications
You must be signed in to change notification settings - Fork 3
/
ua.go
164 lines (148 loc) · 3.27 KB
/
ua.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
package ua
import (
"bytes"
_ "embed"
"log/slog"
"math/rand"
"sync"
"github.com/apache/arrow/go/v15/arrow"
"github.com/apache/arrow/go/v15/arrow/array"
"github.com/apache/arrow/go/v15/arrow/ipc"
"github.com/blevesearch/vellum"
"github.com/blevesearch/vellum/levenshtein"
"github.com/dgraph-io/ristretto"
"github.com/vinceanalytics/vince/internal/logger"
)
//go:generate go run gen/main.go device-detector/Tests/fixtures
//go:embed ua.arrow
var arrowBytes []byte
//go:embed ua.fst
var fstBytes []byte
var record Record
var fst *vellum.FST
var log *slog.Logger
var lb *levenshtein.LevenshteinAutomatonBuilder
var lbMu sync.Mutex
var cache *ristretto.Cache
func init() {
rd, err := ipc.NewReader(bytes.NewReader(arrowBytes))
if err != nil {
logger.Fail(err.Error())
}
rd.Next()
r := rd.Record()
for i := 0; i < int(r.NumCols()); i++ {
a := r.Column(i)
switch r.ColumnName(i) {
case "isBot":
record.IsBot = a.(*array.Int64)
case "oSName":
record.OSName = newStr(a)
case "oSVersion":
record.OSVersion = newStr(a)
case "clientName":
record.ClientName = newStr(a)
case "clientVersion":
record.ClientVersion = newStr(a)
}
}
fst, err = vellum.Load(fstBytes)
if err != nil {
logger.Fail(err.Error())
}
lb, err = levenshtein.NewLevenshteinAutomatonBuilder(2, false)
if err != nil {
logger.Fail(err.Error())
}
cache, err = ristretto.NewCache(&ristretto.Config{
NumCounters: 1e7,
MaxCost: 1 << 20,
BufferItems: 64,
})
log = slog.Default().With(slog.String(
"component", "ua",
))
}
type Record struct {
IsBot *array.Int64
OSName *Field
OSVersion *Field
ClientName *Field
ClientVersion *Field
}
func (r *Record) Get(i int) *Model {
return &Model{
IsBot: r.IsBot.Value(i) == 1,
Os: r.OSName.Get(i),
OsVersion: r.OSVersion.Get(i),
Browser: r.ClientName.Get(i),
BrowserVersion: r.ClientVersion.Get(i),
}
}
type Model struct {
IsBot bool
Os string
OsVersion string
Browser string
BrowserVersion string
}
func (m *Model) Size() int {
return 2 + len(m.Os) +
len(m.OsVersion) + len(m.Browser) + len(m.BrowserVersion)
}
type Field struct {
Dict *array.Dictionary
Str *array.String
}
func (f *Field) Get(i int) string {
return f.Str.Value(f.Dict.GetValueIndex(i))
}
func newStr(a arrow.Array) *Field {
d := a.(*array.Dictionary)
return &Field{
Dict: d,
Str: d.Dictionary().(*array.String),
}
}
func Get(agent string) *Model {
if v, ok := cache.Get(agent); ok {
return v.(*Model)
}
lbMu.Lock()
dfa, err := lb.BuildDfa(agent, 2)
if err != nil {
lbMu.Unlock()
log.Error("failed building dfal", slog.String("agent", agent), slog.String("err", err.Error()))
return &Model{}
}
lbMu.Unlock()
it, err := fst.Search(dfa, nil, nil)
for err == nil {
_, val := it.Current()
r := record.Get(int(val))
cache.Set(agent, r, int64(r.Size()))
return r
}
return &Model{}
}
func Random(count int) (o []string) {
top := fst.Len() - 1
from := rand.Intn(top)
if from+count > top {
from -= count
}
o = make([]string, 0, count)
it, err := fst.Iterator(nil, nil)
end := from + count
var n int
for err == nil && end > 0 {
end--
key, _ := it.Current()
if n > from {
o = append(o, string(key))
}
err = it.Next()
n++
}
return
}