forked from arp242/goatcounter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathref.go
274 lines (238 loc) · 7.92 KB
/
ref.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// Copyright © 2019 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"
"net/url"
"strings"
"time"
"zgo.at/errors"
"zgo.at/zdb"
"zgo.at/zstd/zint"
)
// ref_scheme column
var (
RefSchemeHTTP = ptr("h")
RefSchemeOther = ptr("o")
RefSchemeGenerated = ptr("g")
RefSchemeCampaign = ptr("c")
)
var groups = map[string]string{
// HN has <meta name="referrer" content="origin"> so we only get the domain.
"news.ycombinator.com": "Hacker News",
"hn.algolia.com": "Hacker News",
"hckrnews.com": "Hacker News",
"hn.premii.com": "Hacker News",
"com.stefandekanski.hackernews.free": "Hacker News",
"io.github.hidroh.materialistic": "Hacker News",
"hackerweb.app": "Hacker News",
"www.daemonology.net/hn-daily": "Hacker News",
"quiethn.com": "Hacker News",
"hnews.xyz": "Hacker News",
"hackernewsmobile.com": "Hacker News",
// http://www.elegantreader.com/item/17358103
// https://www.daemonology.net/hn-daily/2019-05.html
"mail.google.com": "Email",
"com.google.android.gm": "Email",
"mail.yahoo.com": "Email",
// https://mailchi.mp
"org.fox.ttrss": "RSS",
"www.inoreader.com": "RSS",
"com.innologica.inoreader": "RSS",
"usepanda.com": "RSS",
"feedly.com": "RSS",
"com.google.android.googlequicksearchbox": "Google",
"com.google.android.googlequicksearchbox/https/www.google.com": "Google",
"com.andrewshu.android.reddit": "www.reddit.com",
"com.laurencedawson.reddit_sync": "www.reddit.com",
"com.laurencedawson.reddit_sync.dev": "www.reddit.com",
"com.laurencedawson.reddit_sync.pro": "www.reddit.com",
"m.facebook.com": "www.facebook.com",
"l.facebook.com": "www.facebook.com",
"lm.facebook.com": "www.facebook.com",
"org.telegram.messenger": "Telegram Messenger",
"com.Slack": "Slack Chat",
// Baidu
"baidu.com": "Baidu",
"c.tieba.baidu.com": "Baidu",
"m.baidu.com": "Baidu",
"tieba.baidu.com": "Baidu",
"www.baidu.com": "Baidu",
}
// update
var hostAlias = map[string]string{
"en.m.wikipedia.org": "en.wikipedia.org",
"m.facebook.com": "www.facebook.com",
"m.habr.com": "habr.com",
"old.reddit.com": "www.reddit.com",
"i.reddit.com": "www.reddit.com",
"np.reddit.com": "www.reddit.com",
"fr.reddit.com": "www.reddit.com",
}
func cleanRefURL(ref string, refURL *url.URL) (string, bool) {
// I'm not sure where these links are generated, but there are *a lot* of
// them.
if refURL.Host == "link.oreilly.com" {
return "link.oreilly.com", false
}
// Always remove protocol.
refURL.Scheme = ""
if p := strings.Index(ref, ":"); p > -1 && p < 7 {
ref = ref[p+3:]
}
// Normalize some hosts.
if a, ok := hostAlias[refURL.Host]; ok {
refURL.Host = a
}
// Group based on URL.
if strings.HasPrefix(refURL.Host, "www.google.") || strings.HasPrefix(refURL.Host, "google.") {
// Group all "google.co.nz", "google.nl", etc. as "Google".
return "Google", true
}
if strings.Contains(refURL.Host, "search.yahoo.com") {
return "Yahoo", true
}
if g, ok := groups[refURL.Host]; ok {
return g, true
}
if g, ok := groups[ref]; ok {
return g, true
}
// Useful: https://lobste.rs/s/tslw6k/why_i_m_still_using_jquery_2019
// Not really: https://lobste.rs/newest/page/8, https://lobste.rs/page/7
// https://lobste.rs/search, https://lobste.rs/t/javascript
if refURL.Host == "lobste.rs" && !strings.HasPrefix(refURL.Path, "/s/") {
return "lobste.rs", false
}
if refURL.Host == "gambe.ro" && !strings.HasPrefix(refURL.Path, "/s/") {
return "lobste.rs", false
}
// No sense in retaining path for Pocket:
// app.getpocket.com
// app.getpocket.com/read/2369667792
// getpocket.com
// getpocket.com/a/read/2580004052
// getpocket.com/recommendations
// getpocket.com/redirect
// getpocket.com/users/XXX/feed/read
if refURL.Host == "getpocket.com" || refURL.Host == "app.getpocket.com" {
return "getpocket.com", false
}
// Reddit
// www.reddit.com/r/programming/top
// www.reddit.com/r/programming/.compact
// www.reddit.com/r/programming.compact
// www.reddit.com/r/webdev/new
// www.reddit.com/r/vim/search
if refURL.Host == "www.reddit.com" {
switch {
case strings.HasSuffix(refURL.Path, "/top") || strings.HasSuffix(refURL.Path, "/new"):
refURL.Path = refURL.Path[:len(refURL.Path)-4]
case strings.HasSuffix(refURL.Path, "/search"):
refURL.Path = refURL.Path[:len(refURL.Path)-7]
case strings.HasSuffix(refURL.Path, ".compact"):
refURL.Path = refURL.Path[:len(refURL.Path)-8]
}
}
// Linking https://t.co/c3MITw38Yq isn't too useful as that will link back
// to the page, so link to the Tweet instead.
if refURL.Host == "t.co" && len(refURL.Path) > 1 {
return "twitter.com/search?q=https%3A%2F%2Ft.co" +
url.QueryEscape(refURL.Path), false
}
// Clean query parameters.
i := strings.Index(ref, "?")
if i == -1 {
// No parameters so no work.
return strings.TrimLeft(refURL.String(), "/"), false
}
q := refURL.Query()
refURL.RawQuery = ""
// Google analytics tracking parameters.
q.Del("utm_source")
q.Del("utm_medium")
q.Del("utm_campaign")
q.Del("utm_term")
// Cloudflare
q.Del("__cf_chl_captcha_tk__")
q.Del("__cf_chl_jschl_tk__")
if len(q) == 0 {
return refURL.String()[2:], false
}
return refURL.String()[2:], false
}
// ListRefsByPath lists all references for a path.
func (h *Stats) ListRefsByPath(ctx context.Context, path string, start, end time.Time, offset int) error {
site := MustGetSite(ctx)
limit := int(zint.NonZero(int64(site.Settings.LimitRefs()), 10))
err := zdb.Select(ctx, &h.Stats, `/* Stats.ListRefsByPath */
with x as (
select path_id from paths
where site_id = :site and lower(path) = lower(:path)
)
select
coalesce(sum(total), 0) as count,
coalesce(sum(total_unique), 0) as count_unique,
max(ref_scheme) as ref_scheme,
ref as name
from ref_counts
join x using (path_id)
where
site_id = :site and hour >= :start and hour <= :end
group by ref
order by count_unique desc, ref desc
limit :limit offset :offset`,
zdb.A{
"site": site.ID,
"start": start.Format(zdb.Date),
"end": end.Format(zdb.Date),
"path": path,
"limit": limit + 1,
"offset": offset,
})
if len(h.Stats) > limit {
h.More = true
h.Stats = h.Stats[:len(h.Stats)-1]
}
return errors.Wrap(err, "Stats.ListRefsByPath")
}
// ListTopRefs lists all ref statistics for the given time period, excluding
// referrals from the configured LinkDomain.
//
// The returned count is the count without LinkDomain, and is different from the
// total number of hits.
func (h *Stats) ListTopRefs(ctx context.Context, start, end time.Time, pathFilter []int64, offset int) error {
site := MustGetSite(ctx)
err := zdb.Select(ctx, &h.Stats, `/* Stats.ListTopRefs */
select
coalesce(sum(total), 0) as count,
coalesce(sum(total_unique), 0) as count_unique,
max(ref_scheme) as ref_scheme,
ref as name
from ref_counts
where
site_id = :site and hour >= :start and hour <= :end
{{:filter and path_id in (:filter)}}
{{:has_domain and ref not like :ref}}
group by ref
order by count_unique desc
limit 6 offset :offset`,
zdb.A{
"site": site.ID,
"start": start.Format(zdb.Date),
"end": end.Format(zdb.Date),
"filter": pathFilter,
"ref": site.LinkDomain + "%",
"offset": offset,
"has_domain": site.LinkDomain != "",
})
if err != nil {
return errors.Wrap(err, "Stats.ListAllRefs")
}
if len(h.Stats) > 6 {
h.More = true
h.Stats = h.Stats[:len(h.Stats)-1]
}
return nil
}