-
-
Notifications
You must be signed in to change notification settings - Fork 176
/
bosmang.go
93 lines (83 loc) · 2.12 KB
/
bosmang.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
// 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"
"fmt"
"time"
"zgo.at/errors"
"zgo.at/zcache"
"zgo.at/zdb"
"zgo.at/zstd/zjson"
"zgo.at/zstd/zruntime"
)
type BosmangStat struct {
ID int64 `db:"site_id"`
Codes string `db:"codes"`
Email string `db:"email"`
CreatedAt time.Time `db:"created_at"`
LastMonth int `db:"last_month"`
Total int `db:"total"`
Avg int `db:"avg"`
}
type BosmangStats []BosmangStat
// List stats for all sites, for all time.
func (a *BosmangStats) List(ctx context.Context) error {
err := zdb.Select(ctx, a, "load:bosmang.List")
if err != nil {
return errors.Wrap(err, "BosmangStats.List")
}
return nil
}
func ListCache(ctx context.Context) map[string]struct {
Size int64
Items map[string]string
} {
c := make(map[string]struct {
Size int64
Items map[string]string
})
caches := map[string]func(context.Context) *zcache.Cache{
"sites": cacheSites,
"ua": cacheUA,
"browsers": cacheBrowsers,
"systems": cacheSystems,
"paths": cachePaths,
"loc": cacheLoc,
"changed_titles": cacheChangedTitles,
//"loader": handler.loader.conns,
}
for name, f := range caches {
var (
content = f(ctx).Items()
s = zruntime.SizeOf(content)
items = make(map[string]string)
)
for k, v := range content {
items[k] = fmt.Sprintf("%s\n", zjson.MustMarshalIndent(v.Object, "", " "))
s += c[name].Size + zruntime.SizeOf(v.Object)
}
c[name] = struct {
Size int64
Items map[string]string
}{s / 1024, items}
}
{
var (
name = "sites_host"
content = cacheSitesHost(ctx).Items()
s = zruntime.SizeOf(content)
items = make(map[string]string)
)
for k, v := range content {
items[k] = v
s += c[name].Size + zruntime.SizeOf(v)
}
c[name] = struct {
Size int64
Items map[string]string
}{s / 1024, items}
}
return c
}