|
| 1 | +// Copyright © Martin Tournoij – This file is part of GoatCounter and published |
| 2 | +// under the terms of a slightly modified EUPL v1.2 license, which can be found |
| 3 | +// in the LICENSE file or at https://license.goatcounter.com |
| 4 | + |
| 5 | +package gomig |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "strconv" |
| 10 | + |
| 11 | + "golang.org/x/exp/slices" |
| 12 | + "zgo.at/errors" |
| 13 | + "zgo.at/goatcounter/v2" |
| 14 | + "zgo.at/zdb" |
| 15 | + "zgo.at/zstd/zjson" |
| 16 | +) |
| 17 | + |
| 18 | +func CorrectHitStats(ctx context.Context) error { |
| 19 | + // Only for SQLite |
| 20 | + if zdb.SQLDialect(ctx) == zdb.DialectPostgreSQL { |
| 21 | + return nil |
| 22 | + } |
| 23 | + |
| 24 | + err := zdb.TX(goatcounter.NewCache(goatcounter.NewConfig(ctx)), func(ctx context.Context) error { |
| 25 | + err := zdb.Exec(ctx, `delete from hit_stats where day >= '2022-11-08'`) |
| 26 | + if err != nil { |
| 27 | + return err |
| 28 | + } |
| 29 | + |
| 30 | + var sites goatcounter.Sites |
| 31 | + err = sites.UnscopedList(ctx) |
| 32 | + if err != nil { |
| 33 | + return err |
| 34 | + } |
| 35 | + |
| 36 | + for _, s := range sites { |
| 37 | + var hits goatcounter.Hits |
| 38 | + err = zdb.Select(ctx, &hits, `select * from hits where |
| 39 | + created_at >= '2022-11-08 00:00:00' and first_visit=1 and bot in (0, 1)`) |
| 40 | + if err != nil { |
| 41 | + return err |
| 42 | + } |
| 43 | + |
| 44 | + err = updateHitStats(goatcounter.WithSite(ctx, &s), hits) |
| 45 | + if err != nil { |
| 46 | + return err |
| 47 | + } |
| 48 | + } |
| 49 | + return nil |
| 50 | + }) |
| 51 | + |
| 52 | + if err == nil { |
| 53 | + err = zdb.Exec(ctx, `insert into version values ('2022-11-15-1-correct-hit-stats')`) |
| 54 | + } |
| 55 | + return err |
| 56 | +} |
| 57 | + |
| 58 | +// below is a copy of cron/hit_stat.go |
| 59 | + |
| 60 | +var empty = []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} |
| 61 | + |
| 62 | +func updateHitStats(ctx context.Context, hits []goatcounter.Hit) error { |
| 63 | + return errors.Wrap(zdb.TX(ctx, func(ctx context.Context) error { |
| 64 | + type gt struct { |
| 65 | + count []int |
| 66 | + day string |
| 67 | + hour string |
| 68 | + pathID int64 |
| 69 | + } |
| 70 | + grouped := map[string]gt{} |
| 71 | + for _, h := range hits { |
| 72 | + if h.Bot > 0 { |
| 73 | + continue |
| 74 | + } |
| 75 | + |
| 76 | + day := h.CreatedAt.Format("2006-01-02") |
| 77 | + dayHour := h.CreatedAt.Format("2006-01-02 15:00:00") |
| 78 | + k := day + strconv.FormatInt(h.PathID, 10) |
| 79 | + v := grouped[k] |
| 80 | + if len(v.count) == 0 { |
| 81 | + v.day = day |
| 82 | + v.hour = dayHour |
| 83 | + v.pathID = h.PathID |
| 84 | + v.count = make([]int, 24) |
| 85 | + |
| 86 | + if zdb.SQLDialect(ctx) == zdb.DialectSQLite { |
| 87 | + var err error |
| 88 | + v.count, err = existingHitStats(ctx, h.Site, day, v.pathID) |
| 89 | + if err != nil { |
| 90 | + return err |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + hour, _ := strconv.ParseInt(h.CreatedAt.Format("15"), 10, 8) |
| 96 | + if h.FirstVisit { |
| 97 | + v.count[hour] += 1 |
| 98 | + } |
| 99 | + grouped[k] = v |
| 100 | + } |
| 101 | + |
| 102 | + siteID := goatcounter.MustGetSite(ctx).ID |
| 103 | + ins := zdb.NewBulkInsert(ctx, "hit_stats", []string{"site_id", "day", "path_id", "stats"}) |
| 104 | + if zdb.SQLDialect(ctx) == zdb.DialectPostgreSQL { |
| 105 | + ins.OnConflict(`on conflict on constraint "hit_stats#site_id#path_id#day" do update set |
| 106 | + stats = ( |
| 107 | + with x as ( |
| 108 | + select |
| 109 | + unnest(string_to_array(trim(hit_stats.stats, '[]'), ',')::int[]) as orig, |
| 110 | + unnest(string_to_array(trim(excluded.stats, '[]'), ',')::int[]) as new |
| 111 | + ) |
| 112 | + select '[' || array_to_string(array_agg(orig + new), ',') || ']' from x |
| 113 | + ) `) |
| 114 | + } |
| 115 | + // } else { |
| 116 | + // TODO: merge the arrays here and get rid of existingHitStats(); |
| 117 | + // it's kinda tricky with SQLite :-/ |
| 118 | + // |
| 119 | + // ins.OnConflict(`on conflict(site_id, path_id, day) do update set |
| 120 | + // stats = excluded.stats |
| 121 | + // `) |
| 122 | + // } |
| 123 | + |
| 124 | + for _, v := range grouped { |
| 125 | + if slices.Equal(v.count, empty) { |
| 126 | + continue |
| 127 | + } |
| 128 | + ins.Values(siteID, v.day, v.pathID, zjson.MustMarshal(v.count)) |
| 129 | + } |
| 130 | + return errors.Wrap(ins.Finish(), "updateHitStats hit_stats") |
| 131 | + }), "cron.updateHitStats") |
| 132 | +} |
| 133 | + |
| 134 | +func existingHitStats(ctx context.Context, siteID int64, day string, pathID int64) ([]int, error) { |
| 135 | + var ex []struct { |
| 136 | + Stats []byte `db:"stats"` |
| 137 | + } |
| 138 | + err := zdb.Select(ctx, &ex, `/* existingHitStats */ |
| 139 | + select stats from hit_stats |
| 140 | + where site_id=$1 and day=$2 and path_id=$3 limit 1`, |
| 141 | + siteID, day, pathID) |
| 142 | + if err != nil { |
| 143 | + return nil, errors.Wrap(err, "existingHitStats") |
| 144 | + } |
| 145 | + if len(ex) == 0 { |
| 146 | + return make([]int, 24), nil |
| 147 | + } |
| 148 | + |
| 149 | + err = zdb.Exec(ctx, `delete from hit_stats where |
| 150 | + site_id=$1 and day=$2 and path_id=$3`, |
| 151 | + siteID, day, pathID) |
| 152 | + if err != nil { |
| 153 | + return nil, errors.Wrap(err, "delete") |
| 154 | + } |
| 155 | + |
| 156 | + var ru []int |
| 157 | + if ex[0].Stats != nil { |
| 158 | + zjson.MustUnmarshal(ex[0].Stats, &ru) |
| 159 | + } |
| 160 | + |
| 161 | + return ru, nil |
| 162 | +} |
0 commit comments