forked from arp242/goatcounter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemstore.go
98 lines (83 loc) · 1.97 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
93
94
95
96
97
98
// 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"
"strings"
"sync"
"zgo.at/zdb"
"zgo.at/zdb/bulk"
"zgo.at/zlog"
)
type ms struct {
sync.RWMutex
hits []Hit
}
var Memstore = ms{}
func (m *ms) Append(hits ...Hit) {
m.Lock()
m.hits = append(m.hits, hits...)
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),
"hits", []string{"site", "path", "ref", "ref_params", "ref_original",
"ref_scheme", "browser", "size", "location", "created_at", "bot",
"title", "event"})
usage := bulk.NewInsert(ctx, zdb.MustGet(ctx),
"usage", []string{"site", "domain", "count"})
for i, h := range hits {
// Ignore spammers.
h.RefURL, _ = url.Parse(h.Ref)
if h.RefURL != nil {
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
e := 0
if h.Event {
e = 1
}
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), h.Bot, h.Title, e)
if strings.HasPrefix(h.UsageDomain, "http") {
d, err := url.Parse(h.UsageDomain)
if err == nil && d.Host != "" {
h.UsageDomain = d.Host
}
}
usage.Values(h.Site, h.UsageDomain, 1)
}
err := usage.Finish()
if err != nil {
zlog.Error(err)
}
return hits, ins.Finish()
}