mirror of
https://github.com/MeshCore-Beacon/beacon-server.git
synced 2026-10-23 11:02:25 +00:00
feat(stats): add bounded path and hash-width analytics (#159)
This commit is contained in:
1 parent
6107578c25
commit
8a3fa7e6bf
25 files changed
+1205
-4
No files matched your search
Vendored
+48
@@ -0,0 +1,48 @@
|
||||
// Copyright 2026 Beacon Contributors
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
package cache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"slices"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/MeshCore-Beacon/beacon-server/internal/api"
|
||||
)
|
||||
|
||||
type pathStatsReader struct {
|
||||
api.Reader
|
||||
calls int64
|
||||
}
|
||||
|
||||
func (r *pathStatsReader) GetPathStats(context.Context, time.Time, time.Time, []string) (*api.PathStats, error) {
|
||||
r.calls++
|
||||
return &api.PathStats{Receptions: r.calls}, nil
|
||||
}
|
||||
|
||||
func TestPathCacheSeparatesWindowAndRegion(t *testing.T) {
|
||||
c, mr := newTestClient(t)
|
||||
inner := &pathStatsReader{}
|
||||
r := NewCachedReader(inner, c, CacheTTLs{Stats: time.Minute})
|
||||
for _, tc := range []struct {
|
||||
iatas []string
|
||||
since, until, want int64
|
||||
}{
|
||||
{nil, 0, 10, 1}, {[]string{}, 0, 10, 1}, {[]string{""}, 0, 10, 2}, {[]string{"YVR"}, 0, 10, 3},
|
||||
{[]string{"YVR", "YYJ"}, 0, 10, 4}, {[]string{"YYJ", "YVR", "YYJ"}, 0, 10, 4},
|
||||
{nil, 1, 10, 5}, {nil, 0, 11, 6}, {nil, 0, 10, 1},
|
||||
} {
|
||||
before := slices.Clone(tc.iatas)
|
||||
got, err := r.GetPathStats(context.Background(), time.UnixMilli(tc.since), time.UnixMilli(tc.until), tc.iatas)
|
||||
if err != nil || got.Receptions != tc.want || !slices.Equal(before, tc.iatas) {
|
||||
t.Fatalf("%+v: %+v %v", tc, got, err)
|
||||
}
|
||||
}
|
||||
mr.FastForward(time.Minute)
|
||||
got, err := r.GetPathStats(context.Background(), time.UnixMilli(0), time.UnixMilli(10), nil)
|
||||
if err != nil || got.Receptions != 7 {
|
||||
t.Fatalf("expiry: %+v %v", got, err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user