-
-
Notifications
You must be signed in to change notification settings - Fork 202
/
Copy pathhit_list.go
542 lines (482 loc) · 14.3 KB
/
hit_list.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
// 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"
"sort"
"strconv"
"time"
"zgo.at/errors"
"zgo.at/tz"
"zgo.at/zdb"
"zgo.at/zstd/zbool"
"zgo.at/zstd/zjson"
"zgo.at/zstd/ztime"
)
type HitList struct {
Count int `db:"count"`
CountUnique int `db:"count_unique"`
PathID int64 `db:"path_id"`
Path string `db:"path"`
Event zbool.Bool `db:"event"`
Title string `db:"title"`
RefScheme *string `db:"ref_scheme"`
Max int
Stats []HitListStat
}
type HitListStat struct {
Day string
Hourly []int
HourlyUnique []int
Daily int
DailyUnique int
}
// PathCount gets the total and total_unique for one path.
func (h *HitList) PathCount(ctx context.Context, path string, rng ztime.Range) error {
err := zdb.Get(ctx, h, "load:hit_list.PathCount", zdb.P{
"site": MustGetSite(ctx).ID,
"path": path,
"start": rng.Start,
"end": rng.End,
})
return errors.Wrap(err, "HitList.PathCount")
}
// SiteTotal gets the total counts for all paths.
//
// This always uses UTC.
func (h *HitList) SiteTotalUTC(ctx context.Context, rng ztime.Range) error {
err := zdb.Get(ctx, h, `/* *HitList.SiteTotalUTC */
select
coalesce(sum(total), 0) as count,
coalesce(sum(total_unique), 0) as count_unique
from hit_counts
where site_id = :site
{{:start and hour >= :start}}
{{:end and hour <= :end}}
`, zdb.P{
"site": MustGetSite(ctx).ID,
"start": rng.Start,
"end": rng.End,
})
return errors.Wrap(err, "HitList.SiteTotalUTC")
}
type HitLists []HitList
// ListPathsLike lists all paths matching the like pattern.
func (h *HitLists) ListPathsLike(ctx context.Context, search string, matchTitle bool) error {
err := zdb.Select(ctx, h, "load:hit_list.ListPathsLike", zdb.P{
"site": MustGetSite(ctx).ID,
"search": search,
"match_title": matchTitle,
})
return errors.Wrap(err, "Hits.ListPathsLike")
}
var allDays = []int{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
// List the top paths for this site in the given time period.
func (h *HitLists) List(
ctx context.Context, rng ztime.Range, pathFilter, exclude []int64, limit int, daily bool,
) (int, int, bool, error) {
site := MustGetSite(ctx)
user := MustGetUser(ctx)
// List the pages for this page; this gets the path_id, path, title.
var more bool
{
err := zdb.Select(ctx, h, `/* HitLists.List */
with x as (
select path_id from hit_counts
where
hit_counts.site_id = :site and
{{:exclude path_id not in (:exclude) and}}
{{:filter path_id in (:filter) and}}
hour>=:start and hour<=:end
group by path_id
order by sum(total_unique) desc, path_id desc
limit :limit
)
select path_id, paths.path, paths.title, paths.event from x
join paths using (path_id)`,
zdb.P{
"site": site.ID,
"start": rng.Start,
"end": rng.End,
"filter": pathFilter,
"limit": limit + 1,
"exclude": exclude,
})
if err != nil {
return 0, 0, false, errors.Wrap(err, "HitLists.List hit_counts")
}
// Check if there are more entries.
if len(*h) > limit {
hh := *h
hh = hh[:len(hh)-1]
*h = hh
more = true
}
}
if len(*h) == 0 { // No data yet.
return 0, 0, false, nil
}
// Get stats for every page.
hh := *h
var st []struct {
PathID int64 `db:"path_id"`
Day time.Time `db:"day"`
Stats []byte `db:"stats"`
StatsUnique []byte `db:"stats_unique"`
}
{
paths := make([]int64, len(hh))
for i := range hh {
paths[i] = hh[i].PathID
}
err := zdb.Select(ctx, &st, `/* HitLists.List */
select path_id, day, stats, stats_unique
from hit_stats
where
hit_stats.site_id = :site and
path_id in (:paths) and
day >= :start and day <= :end
order by day asc`,
zdb.P{
"site": site.ID,
"start": rng.Start.Format("2006-01-02"),
"end": rng.End.Format("2006-01-02"),
"paths": paths,
})
if err != nil {
return 0, 0, false, errors.Wrap(err, "HitLists.List hit_stats")
}
}
// Add the hit_stats.
{
for i := range hh {
for _, s := range st {
if s.PathID == hh[i].PathID {
var x, y []int
zjson.MustUnmarshal(s.Stats, &x)
zjson.MustUnmarshal(s.StatsUnique, &y)
hh[i].Stats = append(hh[i].Stats, HitListStat{
Day: s.Day.Format("2006-01-02"),
Hourly: x,
HourlyUnique: y,
})
}
}
}
}
// Fill in blank days.
fillBlankDays(hh, rng)
// Apply TZ offset.
applyOffset(hh, user.Settings.Timezone)
// Add total and max.
var totalDisplay, totalUniqueDisplay int
addTotals(hh, daily, &totalDisplay, &totalUniqueDisplay)
return totalDisplay, totalUniqueDisplay, more, nil
}
// PathTotals is a special path to indicate this is the "total" overview.
//
// Trailing whitespace is trimmed on paths, so this should never conflict.
const PathTotals = "TOTAL "
// Totals gets the data for the "Totals" chart/widget.
func (h *HitList) Totals(ctx context.Context, rng ztime.Range, pathFilter []int64, daily, noEvents bool) (int, error) {
site := MustGetSite(ctx)
user := MustGetUser(ctx)
var tc []struct {
Hour time.Time `db:"hour"`
Total int `db:"total"`
TotalUnique int `db:"total_unique"`
}
err := zdb.Select(ctx, &tc, "load:hit_list.Totals", zdb.P{
"site": site.ID,
"start": rng.Start,
"end": rng.End,
"filter": pathFilter,
"no_events": noEvents,
})
if err != nil {
return 0, errors.Wrap(err, "HitList.Totals")
}
totalst := HitList{
Path: PathTotals,
Title: "",
}
stats := make(map[string]HitListStat)
for _, t := range tc {
d := t.Hour.Format("2006-01-02")
hour, _ := strconv.ParseInt(t.Hour.Format("15"), 10, 32)
s, ok := stats[d]
if !ok {
s = HitListStat{
Day: d,
Hourly: make([]int, 24),
HourlyUnique: make([]int, 24),
}
}
s.Hourly[hour] += t.Total
s.HourlyUnique[hour] += t.TotalUnique
totalst.Count += t.Total
totalst.CountUnique += t.TotalUnique
stats[d] = s
}
max := 0
for _, v := range stats {
totalst.Stats = append(totalst.Stats, v)
if !daily {
for _, x := range v.Hourly {
if x > max {
max = x
}
}
}
}
sort.Slice(totalst.Stats, func(i, j int) bool {
return totalst.Stats[i].Day < totalst.Stats[j].Day
})
hh := []HitList{totalst}
fillBlankDays(hh, rng)
applyOffset(hh, user.Settings.Timezone)
if daily {
for i := range hh[0].Stats {
for _, n := range hh[0].Stats[i].Hourly {
hh[0].Stats[i].Daily += n
}
for _, n := range hh[0].Stats[i].HourlyUnique {
hh[0].Stats[i].DailyUnique += n
}
if daily && hh[0].Stats[i].Daily > max {
max = hh[0].Stats[i].Daily
}
}
}
if max < 10 {
max = 10
}
*h = hh[0]
return max, nil
}
// The database stores everything in UTC, so we need to apply
// the offset for HitLists.List()
//
// Let's say we have two days with an offset of UTC+2, this means we
// need to transform this:
//
// 2019-12-05 → [0,0,0,0,0,0,0,0,0,0,0,4,7,0,0,0,0,0,0,0,0,0,1,0]
// 2019-12-06 → [0,0,0,0,0,0,0,0,0,0,0,4,7,0,0,0,0,0,0,0,0,0,1,0]
// 2019-12-07 → [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
//
// To:
//
// 2019-12-05 → [0,0,0,0,0,0,0,0,0,0,0,0,0,4,7,0,0,0,0,0,0,0,0,0]
// 2019-12-06 → [1,0,0,0,0,0,0,0,0,0,0,0,0,4,7,0,0,0,0,0,0,0,0,0]
// 2019-12-07 → [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
//
// And skip the first 2 hours of the first day.
//
// Or, for UTC-2:
//
// 2019-12-04 → [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
// 2019-12-05 → [0,0,0,0,0,0,0,0,0,4,7,0,0,0,0,0,0,0,0,0,1,0,0,0]
// 2019-12-06 → [0,0,0,0,0,0,0,0,0,4,7,0,0,0,0,0,0,0,0,0,1,0,0,0]
//
// And skip the last 2 hours of the last day.
//
// Offsets that are not whole hours (e.g. 6:30) are treated like 7:00. I don't
// know how to do that otherwise.
func applyOffset(hh HitLists, tz *tz.Zone) {
if len(hh) == 0 {
return
}
offset := tz.Offset()
if offset%60 != 0 {
offset += 30
}
offset /= 60
switch {
case offset > 0:
for i := range hh {
stats := hh[i].Stats
popped := make([]int, offset)
poppedUnique := make([]int, offset)
for i := range stats {
stats[i].Hourly = append(popped, stats[i].Hourly...)
o := len(stats[i].Hourly) - offset
popped = stats[i].Hourly[o:]
stats[i].Hourly = stats[i].Hourly[:o]
stats[i].HourlyUnique = append(poppedUnique, stats[i].HourlyUnique...)
poppedUnique = stats[i].HourlyUnique[o:]
stats[i].HourlyUnique = stats[i].HourlyUnique[:o]
}
hh[i].Stats = stats[1:] // Overselect a day to get the stats for it, remove it.
}
case offset < 0:
offset = -offset
for i := range hh {
stats := hh[i].Stats
popped := make([]int, offset)
poppedUnique := make([]int, offset)
for i := len(stats) - 1; i >= 0; i-- {
stats[i].Hourly = append(stats[i].Hourly, popped...)
popped = stats[i].Hourly[:offset]
stats[i].Hourly = stats[i].Hourly[offset:]
stats[i].HourlyUnique = append(stats[i].HourlyUnique, poppedUnique...)
poppedUnique = stats[i].HourlyUnique[:offset]
stats[i].HourlyUnique = stats[i].HourlyUnique[offset:]
}
hh[i].Stats = stats[:len(stats)-1] // Overselect a day to get the stats for it, remove it.
}
}
}
func fillBlankDays(hh HitLists, rng ztime.Range) {
// Should Never Happen™ but if it does the below loop will never break, so
// be safe.
if rng.Start.After(rng.End) {
return
}
endFmt := rng.End.Format("2006-01-02")
for i := range hh {
var (
day = rng.Start.Add(-24 * time.Hour)
newStat []HitListStat
j int
)
for {
day = day.Add(24 * time.Hour)
dayFmt := day.Format("2006-01-02")
if len(hh[i].Stats)-1 >= j && dayFmt == hh[i].Stats[j].Day {
newStat = append(newStat, hh[i].Stats[j])
j++
} else {
newStat = append(newStat, HitListStat{Day: dayFmt, Hourly: allDays, HourlyUnique: allDays})
}
if dayFmt == endFmt {
break
}
}
hh[i].Stats = newStat
}
}
func addTotals(hh HitLists, daily bool, totalDisplay, totalUniqueDisplay *int) {
for i := range hh {
for j := range hh[i].Stats {
for k := range hh[i].Stats[j].Hourly {
hh[i].Stats[j].Daily += hh[i].Stats[j].Hourly[k]
hh[i].Stats[j].DailyUnique += hh[i].Stats[j].HourlyUnique[k]
if !daily && hh[i].Stats[j].Hourly[k] > hh[i].Max {
hh[i].Max = hh[i].Stats[j].Hourly[k]
}
}
hh[i].Count += hh[i].Stats[j].Daily
hh[i].CountUnique += hh[i].Stats[j].DailyUnique
if daily && hh[i].Stats[j].Daily > hh[i].Max {
hh[i].Max = hh[i].Stats[j].Daily
}
}
*totalDisplay += hh[i].Count
*totalUniqueDisplay += hh[i].CountUnique
}
// We sort in SQL, but this is not always 100% correct after applying
// the TZ offset, so order here as well.
//
// TODO: this is still not 100% correct, as the "first 10" after
// applying the TZ offset may be different than the first 10 being
// fetched in the SQL query. There is no easy fix for that in the
// current design. I considered storing everything in the DB as the
// configured TZ, but that would make changing the TZ expensive, I'm not
// 100% sure yet what a good solution here is. For now, this is "good
// enough".
sort.Slice(hh, func(i, j int) bool { return hh[i].CountUnique > hh[j].CountUnique })
}
type TotalCount struct {
Total int `db:"total"`
TotalUnique int `db:"total_unique"`
TotalUniqueUTC int `db:"total_unique_utc"`
TotalEvents int `db:"total_events"`
TotalEventsUnique int `db:"total_events_unique"`
}
// GetTotalCount gets the total number of pageviews for the selected timeview in
// the timezone the user configured.
//
// This also gets the total number of pageviews for the selected time period in
// UTC. This is needed since the _stats tables are per day, rather than
// per-hour, so we need to use the correct totals to make sure the percentage
// calculations are accurate.
func GetTotalCount(ctx context.Context, rng ztime.Range, pathFilter []int64, noEvents bool) (TotalCount, error) {
site := MustGetSite(ctx)
user := MustGetUser(ctx)
var t TotalCount
err := zdb.Get(ctx, &t, "load:hit_list.GetTotalCount", zdb.P{
"site": site.ID,
"start": rng.Start,
"end": rng.End,
"start_utc": rng.Start.In(user.Settings.Timezone.Location),
"end_utc": rng.End.In(user.Settings.Timezone.Location),
"filter": pathFilter,
"no_events": noEvents,
"tz": user.Settings.Timezone.Offset(),
})
return t, errors.Wrap(err, "GetTotalCount")
}
// GetMax gets the path with the higest number of pageviews per hour or day for
// this date range.
func GetMax(ctx context.Context, rng ztime.Range, pathFilter []int64, daily bool) (int, error) {
site := MustGetSite(ctx)
user := MustGetUser(ctx)
var (
query string
params zdb.P
)
if daily {
query = "load:hit_list.GetMax-daily"
params = zdb.P{
"site": site.ID,
"start": rng.Start,
"end": rng.End,
"tz": user.Settings.Timezone.OffsetRFC3339(),
"filter": pathFilter,
"pgsql": zdb.SQLDialect(ctx) == zdb.DialectPostgreSQL,
"sqlite": zdb.SQLDialect(ctx) == zdb.DialectSQLite,
}
} else {
query = "load:hit_list.GetMax-hourly"
params = zdb.P{
"site": site.ID,
"start": rng.Start,
"end": rng.End,
"filter": pathFilter,
}
}
var max int
err := zdb.Get(ctx, &max, query, params)
if err != nil && !zdb.ErrNoRows(err) {
return 0, errors.Wrap(err, "getMax")
}
if max < 10 {
max = 10
}
return max, nil
}
// Diff gets the difference in percentage of all paths in this HitList.
//
// e.g. if called with start=2020-01-20; end=2020-01-2020-01-27, then it will
// compare this to start=2020-01-12; end=2020-01-19
//
// The return value is in the same order as paths.
func (h HitLists) Diff(ctx context.Context, rng, prev ztime.Range) ([]float64, error) {
d := -rng.End.Sub(rng.Start)
prev = ztime.NewRange(rng.Start.Add(d)).To(rng.End.Add(d))
paths := make([]int64, 0, len(h))
for _, hh := range h {
paths = append(paths, hh.PathID)
}
var diffs []float64
err := zdb.Select(ctx, &diffs, "load:hit_list.DiffTotal", zdb.P{
"site": MustGetSite(ctx).ID,
"start": rng.Start,
"end": rng.End,
"prevstart": prev.Start,
"prevend": prev.End,
"paths": paths,
})
return diffs, errors.Wrap(err, "HitList.DiffTotal")
}