forked from arp242/goatcounter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
path.go
182 lines (156 loc) · 4.22 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
171
172
173
174
175
176
177
178
179
180
181
182
// 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"
"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" json:"id"` // Path ID
Site int64 `db:"site_id" json:"-"`
Path string `db:"path" json:"path"` // Path name
Title string `db:"title" json:"title"` // Page title
Event zbool.Bool `db:"event" json:"event"` // Is this an 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) ByID(ctx context.Context, id int64) error {
return errors.Wrapf(zdb.Get(ctx, p,
`/* Path.ByID */ select * from paths where path_id=? and site_id=?`,
id, MustGetSite(ctx).ID), "Path.ByID %d", id)
}
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 any) any {
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
}
type Paths []Path
// List all paths for a site.
func (p *Paths) List(ctx context.Context, siteID, after int64, limit int) (bool, error) {
err := zdb.Select(ctx, p, "load:paths.List", map[string]any{
"site": siteID,
"after": after,
"limit": limit + 1,
})
if err != nil {
return false, errors.Wrap(err, "Paths.List")
}
more := len(*p) > limit
if more {
pp := *p
pp = pp[:len(pp)-1]
*p = pp
}
return more, 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
err := zdb.Select(ctx, &paths, "load:paths.PathFilter", map[string]any{
"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
}