forked from arp242/goatcounter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemstore.go
92 lines (76 loc) · 1.83 KB
/
memstore.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
// Copyright © 2019 Martin Tournoij <[email protected]>
// This file is part of GoatCounter and published under the terms of the EUPL
// v1.2, which can be found in the LICENSE file or at http://eupl12.zgo.at
package goatcounter
import (
"context"
"net/url"
"sync"
"github.com/jmoiron/sqlx"
"zgo.at/zdb"
"zgo.at/zdb/bulk"
"zgo.at/zlog"
)
type ms struct {
sync.RWMutex
hits []Hit
}
var Memstore = ms{}
func (m *ms) Append(hit ...Hit) {
m.Lock()
m.hits = append(m.hits, hit...)
m.Unlock()
}
func (m *ms) Len() int {
m.Lock()
l := len(m.hits)
m.Unlock()
return l
}
func (m *ms) Persist(ctx context.Context) ([]Hit, error) {
if m.Len() == 0 {
return nil, nil
}
m.Lock()
hits := make([]Hit, len(m.hits))
copy(hits, m.hits)
m.hits = []Hit{}
m.Unlock()
ins := bulk.NewInsert(ctx, zdb.MustGet(ctx).(*sqlx.DB),
"hits", []string{"site", "path", "ref", "ref_params", "ref_original",
"ref_scheme", "browser", "size", "location", "created_at", "count_ref",
"bot", "title"})
for i, h := range hits {
var err error
h.RefURL, err = url.Parse(h.Ref)
if err != nil {
zlog.Field("ref", h.Ref).Errorf("could not parse ref: %s", err)
continue
}
// Ignore spammers.
if _, ok := blacklist[h.RefURL.Host]; ok {
continue
}
h.Defaults(ctx)
err = h.Validate(ctx)
if err != nil {
zlog.Error(err)
continue
}
// Some values are sanitized in Hit.Defaults(), make sure this is
// reflected in the hits object too, which matters for the hit_stats
// generation later.
hits[i] = h
countRef := h.CountRef
if countRef != "" {
u, _ := url.Parse(countRef)
if u != nil {
countRef = u.Host
}
}
ins.Values(h.Site, h.Path, h.Ref, h.RefParams, h.RefOriginal,
h.RefScheme, h.Browser, h.Size, h.Location, h.CreatedAt.Format(zdb.Date),
countRef, h.Bot, h.Title)
}
return hits, ins.Finish()
}