-
-
Notifications
You must be signed in to change notification settings - Fork 185
/
hit_stats.go
355 lines (330 loc) · 10.6 KB
/
hit_stats.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
// 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"
"strings"
"time"
"zgo.at/errors"
"zgo.at/zdb"
"zgo.at/zstd/ztime"
)
type HitStat struct {
// ID for selecting more details; not present in the detail view.
ID string `db:"id" json:"id,omitempty"`
Name string `db:"name" json:"name"` // Display name.
Count int `db:"count" json:"count"` // Number of visitors.
// What kind of referral this is; only set when retrieving referrals {enum: h g c o}.
//
// h HTTP Referal header.
// g Generated; for example are Google domains (google.com, google.nl,
// google.co.nz, etc.) are grouped as the generated referral "Google".
// c Campaign (via query parameter)
// o Other
RefScheme *string `db:"ref_scheme" json:"ref_scheme,omitempty"`
}
type HitStats struct {
More bool `json:"more"`
Stats []HitStat `json:"stats"`
}
func asUTCDate(u *User, t time.Time) string {
return t.In(u.Settings.Timezone.Location).Format("2006-01-02")
}
// 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 *HitStats) ListTopRefs(ctx context.Context, rng ztime.Range, pathFilter []int64, limit, offset int) error {
site := MustGetSite(ctx)
err := zdb.Select(ctx, &h.Stats, "load:ref.ListTopRefs.sql", map[string]any{
"site": site.ID,
"start": rng.Start,
"end": rng.End,
"filter": pathFilter,
"ref": site.LinkDomainURL(false) + "%",
"limit": limit + 1,
"limit2": limit + (limit * 3),
"offset": offset,
"has_domain": site.LinkDomain != "",
})
if err != nil {
return errors.Wrap(err, "HitStats.ListAllRefs")
}
if len(h.Stats) > limit {
h.More = true
h.Stats = h.Stats[:len(h.Stats)-1]
}
return nil
}
// ListTopRef lists all paths by referrer.
func (h *HitStats) ListTopRef(ctx context.Context, ref string, rng ztime.Range, pathFilter []int64, limit, offset int) error {
err := zdb.Select(ctx, &h.Stats, "load:hit_stats.ByRef", map[string]any{
"site": MustGetSite(ctx).ID,
"start": rng.Start,
"end": rng.End,
"filter": pathFilter,
"ref": ref,
"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, "HitStats.ByRef")
}
// ListBrowsers lists all browser statistics for the given time period.
func (h *HitStats) ListBrowsers(ctx context.Context, rng ztime.Range, pathFilter []int64, limit, offset int) error {
user := MustGetUser(ctx)
err := zdb.Select(ctx, &h.Stats, "load:hit_stats.ListBrowsers", map[string]any{
"site": MustGetSite(ctx).ID,
"start": asUTCDate(user, rng.Start),
"end": asUTCDate(user, rng.End),
"filter": pathFilter,
"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, "HitStats.ListBrowsers")
}
// ListBrowser lists all the versions for one browser.
func (h *HitStats) ListBrowser(ctx context.Context, browser string, rng ztime.Range, pathFilter []int64, limit, offset int) error {
user := MustGetUser(ctx)
err := zdb.Select(ctx, &h.Stats, "load:hit_stats.ListBrowser", map[string]any{
"site": MustGetSite(ctx).ID,
"start": asUTCDate(user, rng.Start),
"end": asUTCDate(user, rng.End),
"filter": pathFilter,
"browser": browser,
"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, "HitStats.ListBrowser")
}
// ListSystems lists OS statistics for the given time period.
func (h *HitStats) ListSystems(ctx context.Context, rng ztime.Range, pathFilter []int64, limit, offset int) error {
user := MustGetUser(ctx)
err := zdb.Select(ctx, &h.Stats, "load:hit_stats.ListSystems", map[string]any{
"site": MustGetSite(ctx).ID,
"start": asUTCDate(user, rng.Start),
"end": asUTCDate(user, rng.End),
"filter": pathFilter,
"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, "HitStats.ListSystems")
}
// ListSystem lists all the versions for one system.
func (h *HitStats) ListSystem(ctx context.Context, system string, rng ztime.Range, pathFilter []int64, limit, offset int) error {
user := MustGetUser(ctx)
err := zdb.Select(ctx, &h.Stats, "load:hit_stats.ListSystem", map[string]any{
"site": MustGetSite(ctx).ID,
"start": asUTCDate(user, rng.Start),
"end": asUTCDate(user, rng.End),
"filter": pathFilter,
"system": system,
"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, "HitStats.ListSystem")
}
const (
sizePhones = "phone"
sizeLargePhones = "largephone"
sizeTablets = "tablet"
sizeDesktop = "desktop"
sizeDesktopHD = "desktophd"
sizeUnknown = "unknown"
)
// ListSizes lists all device sizes.
func (h *HitStats) ListSizes(ctx context.Context, rng ztime.Range, pathFilter []int64) error {
user := MustGetUser(ctx)
err := zdb.Select(ctx, &h.Stats, "load:hit_stats.ListSizes", map[string]any{
"site": MustGetSite(ctx).ID,
"start": asUTCDate(user, rng.Start),
"end": asUTCDate(user, rng.End),
"filter": pathFilter,
})
if err != nil {
return errors.Wrap(err, "HitStats.ListSize")
}
// Group a bit more user-friendly.
ns := []HitStat{
{ID: sizePhones, Count: 0},
{ID: sizeLargePhones, Count: 0},
{ID: sizeTablets, Count: 0},
{ID: sizeDesktop, Count: 0},
{ID: sizeDesktopHD, Count: 0},
{ID: sizeUnknown, Count: 0},
}
for i := range h.Stats {
x, _ := strconv.ParseInt(h.Stats[i].Name, 10, 16)
switch {
case x == 0:
ns[5].Count += h.Stats[i].Count
case x <= 384:
ns[0].Count += h.Stats[i].Count
case x <= 1024:
ns[1].Count += h.Stats[i].Count
case x <= 1440:
ns[2].Count += h.Stats[i].Count
case x <= 1920:
ns[3].Count += h.Stats[i].Count
default:
ns[4].Count += h.Stats[i].Count
}
}
h.Stats = ns
return nil
}
// ListSize lists all sizes for one grouping.
func (h *HitStats) ListSize(ctx context.Context, id string, rng ztime.Range, pathFilter []int64, limit, offset int) error {
var (
minSize, maxSize int
empty bool
)
switch id {
case sizePhones:
maxSize = 384
case sizeLargePhones:
minSize, maxSize = 384, 1024
case sizeTablets:
minSize, maxSize = 1024, 1440
case sizeDesktop:
minSize, maxSize = 1440, 1920
case sizeDesktopHD:
minSize, maxSize = 1920, 99999
case sizeUnknown:
empty = true
default:
return errors.Errorf("HitStats.ListSizes: invalid value for name: %#v", id)
}
user := MustGetUser(ctx)
err := zdb.Select(ctx, &h.Stats, "load:hit_stats.ListSize", map[string]any{
"site": MustGetSite(ctx).ID,
"start": asUTCDate(user, rng.Start),
"end": asUTCDate(user, rng.End),
"filter": pathFilter,
"min_size": minSize,
"max_size": maxSize,
"empty": empty,
"limit": limit + 1,
"offset": offset,
})
if err != nil {
return errors.Wrap(err, "HitStats.ListSize")
}
if len(h.Stats) > limit {
h.More = true
h.Stats = h.Stats[:len(h.Stats)-1]
}
for i := range h.Stats { // TODO: see if we can do this in SQL.
h.Stats[i].Name = strings.ReplaceAll(h.Stats[i].Name, "↔", "↔\ufe0e")
}
return nil
}
// ListLocations lists all location statistics for the given time period.
func (h *HitStats) ListLocations(ctx context.Context, rng ztime.Range, pathFilter []int64, limit, offset int) error {
user := MustGetUser(ctx)
err := zdb.Select(ctx, &h.Stats, "load:hit_stats.ListLocations", map[string]any{
"site": MustGetSite(ctx).ID,
"start": asUTCDate(user, rng.Start),
"end": asUTCDate(user, rng.End),
"filter": pathFilter,
"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, "HitStats.ListLocations")
}
// ListLocation lists all divisions for a location
func (h *HitStats) ListLocation(ctx context.Context, country string, rng ztime.Range, pathFilter []int64, limit, offset int) error {
user := MustGetUser(ctx)
err := zdb.Select(ctx, &h.Stats, "load:hit_stats.ListLocation", map[string]any{
"site": MustGetSite(ctx).ID,
"start": asUTCDate(user, rng.Start),
"end": asUTCDate(user, rng.End),
"filter": pathFilter,
"country": country,
"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, "HitStats.ListLocation")
}
// ListLanguages lists all language statistics for the given time period.
func (h *HitStats) ListLanguages(ctx context.Context, rng ztime.Range, pathFilter []int64, limit, offset int) error {
user := MustGetUser(ctx)
err := zdb.Select(ctx, &h.Stats, "load:hit_stats.ListLanguages", map[string]any{
"site": MustGetSite(ctx).ID,
"start": asUTCDate(user, rng.Start),
"end": asUTCDate(user, rng.End),
"filter": pathFilter,
"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, "HitStats.ListLanguages")
}
// ListCampaigns lists all campaigns statistics for the given time period.
func (h *HitStats) ListCampaigns(ctx context.Context, rng ztime.Range, pathFilter []int64, limit, offset int) error {
user := MustGetUser(ctx)
err := zdb.Select(ctx, &h.Stats, "load:hit_stats.ListCampaigns", map[string]any{
"site": MustGetSite(ctx).ID,
"start": asUTCDate(user, rng.Start),
"end": asUTCDate(user, rng.End),
"filter": pathFilter,
"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, "HitStats.ListCampaigns")
}
// ListCampaign lists all statistics for a campaign.
func (h *HitStats) ListCampaign(ctx context.Context, campaign int64, rng ztime.Range, pathFilter []int64, limit, offset int) error {
user := MustGetUser(ctx)
err := zdb.Select(ctx, &h.Stats, "load:hit_stats.ListCampaign", map[string]any{
"site": MustGetSite(ctx).ID,
"start": asUTCDate(user, rng.Start),
"end": asUTCDate(user, rng.End),
"filter": pathFilter,
"campaign": campaign,
"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, "HitStats.ListCampaign")
}