forked from arp242/goatcounter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
path.go
170 lines (146 loc) · 3.96 KB
/
path.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
// 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"
"strconv"
"zgo.at/errors"
"zgo.at/zcache"
"zgo.at/zdb"
"zgo.at/zlog"
"zgo.at/zstd/zbool"
)
type Path struct {
ID int64 `db:"path_id"`
Site int64 `db:"site_id"`
Path string `db:"path"`
Title string `db:"title"`
Event zbool.Bool `db:"event"`
}
func (p *Path) Defaults(ctx context.Context) {}
func (p *Path) Validate(ctx context.Context) error {
v := NewValidate(ctx)
v.UTF8("path", p.Path)
v.UTF8("title", p.Title)
v.Len("path", p.Path, 1, 2048)
v.Len("title", p.Title, 0, 1024)
return v.ErrorOrNil()
}
func (p *Path) GetOrInsert(ctx context.Context) error {
site := MustGetSite(ctx)
title := p.Title
k := strconv.FormatInt(site.ID, 10) + p.Path
c, ok := cachePaths(ctx).Get(k)
if ok {
*p = c.(Path)
cachePaths(ctx).Touch(k, zcache.DefaultExpiration)
err := p.updateTitle(ctx, p.Title, title)
if err != nil {
zlog.Fields(zlog.F{
"path_id": p.ID,
"title": title,
}).Error(err)
}
return nil
}
p.Defaults(ctx)
err := p.Validate(ctx)
if err != nil {
return err
}
err = zdb.Get(ctx, p, `/* Path.GetOrInsert */
select * from paths
where site_id = $1 and lower(path) = lower($2)
limit 1`, site.ID, p.Path)
if err != nil && !zdb.ErrNoRows(err) {
return errors.Errorf("Path.GetOrInsert select: %w", err)
}
if err == nil {
err := p.updateTitle(ctx, p.Title, title)
if err != nil {
zlog.Fields(zlog.F{
"path_id": p.ID,
"title": title,
}).Error(err)
}
cachePaths(ctx).SetDefault(k, *p)
return nil
}
// Insert new row.
p.ID, err = zdb.InsertID(ctx, "path_id",
`insert into paths (site_id, path, title, event) values (?, ?, ?, ?)`,
site.ID, p.Path, p.Title, p.Event)
if err != nil {
return errors.Wrap(err, "Path.GetOrInsert insert")
}
cachePaths(ctx).SetDefault(k, *p)
return nil
}
func (p Path) updateTitle(ctx context.Context, currentTitle, newTitle string) error {
if newTitle == currentTitle {
return nil
}
k := strconv.FormatInt(p.ID, 10)
_, ok := cacheChangedTitles(ctx).Get(k)
if !ok {
cacheChangedTitles(ctx).SetDefault(k, []string{newTitle})
return nil
}
var titles []string
cacheChangedTitles(ctx).Modify(k, func(v interface{}) interface{} {
vv := v.([]string)
vv = append(vv, newTitle)
titles = vv
return vv
})
grouped := make(map[string]int)
for _, t := range titles {
grouped[t]++
}
for t, n := range grouped {
if n > 10 {
err := zdb.Exec(ctx, `update paths set title = $1 where path_id = $2`, t, p.ID)
if err != nil {
return errors.Wrap(err, "Paths.updateTitle")
}
cacheChangedTitles(ctx).Delete(k)
break
}
}
return nil
}
// PathFilter returns a list of IDs matching the path name.
//
// if matchTitle is true it will match the title as well.
func PathFilter(ctx context.Context, filter string, matchTitle bool) ([]int64, error) {
var paths []int64
// The limit is here because that's the limit in SQL parameters; the
// returned []int64 is passed as parameters later on.
//
// Having (and scrolling!) more than 65k pages is a rather curious usage
// pattern: it has happened, but this was just because they were sending
// data with many unique IDs in the URL which really ought to be removed.
err := zdb.Select(ctx, &paths, `/* PathFilter */
select path_id from paths
where
site_id = :site and (
lower(path) like lower(:filter)
{{:match_title or lower(title) like lower(:filter)}}
)
limit 65500`,
zdb.P{
"site": MustGetSite(ctx).ID,
"filter": "%" + filter + "%",
"match_title": matchTitle,
})
if err != nil {
return nil, errors.Wrap(err, "PathFilter")
}
// Nothing matches: make sure there's a slice with an invalid path_id, so
// the queries using the result don't select anything.
if len(paths) == 0 {
paths = []int64{-1}
}
return paths, nil
}