From ffbf1b14131aea5eec97af71c4cc1856ef5f883e Mon Sep 17 00:00:00 2001 From: Jo Date: Fri, 15 Dec 2023 15:15:04 +0100 Subject: [PATCH] Anon: Small fixes to anon service structure (#79566) * add ListDevices to service * improve fake * fix missing cfg field * cannot be unexported --- pkg/services/anonymous/anonimpl/impl.go | 13 +++++++++++-- pkg/services/anonymous/anontest/fake.go | 13 ++++++++----- pkg/services/anonymous/service.go | 3 +++ 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/pkg/services/anonymous/anonimpl/impl.go b/pkg/services/anonymous/anonimpl/impl.go index c85463708f2..826be4d0a90 100644 --- a/pkg/services/anonymous/anonimpl/impl.go +++ b/pkg/services/anonymous/anonimpl/impl.go @@ -31,6 +31,7 @@ type AnonDeviceService struct { localCache *localcache.CacheService anonStore anonstore.AnonStore serverLock *serverlock.ServerLockService + cfg *setting.Cfg } func ProvideAnonymousDeviceService(usageStats usagestats.Service, authBroker authn.Service, @@ -42,6 +43,7 @@ func ProvideAnonymousDeviceService(usageStats usagestats.Service, authBroker aut localCache: localcache.New(29*time.Minute, 15*time.Minute), anonStore: anonstore.ProvideAnonDBStore(sqlStore, cfg.AnonymousDeviceLimit), serverLock: serverLockService, + cfg: cfg, } usageStats.RegisterMetricsFunc(a.usageStatFn) @@ -53,7 +55,7 @@ func ProvideAnonymousDeviceService(usageStats usagestats.Service, authBroker aut anonDeviceService: a, } - if anonClient.cfg.AnonymousEnabled { + if cfg.AnonymousEnabled { authBroker.RegisterClient(anonClient) authBroker.RegisterPostLoginHook(a.untagDevice, 100) } @@ -114,7 +116,6 @@ func (a *AnonDeviceService) untagDevice(ctx context.Context, } } -// FIXME: Unexport and remove interface func (a *AnonDeviceService) TagDevice(ctx context.Context, httpReq *http.Request, kind anonymous.DeviceKind) error { deviceID := httpReq.Header.Get(deviceIDHeader) if deviceID == "" { @@ -152,11 +153,19 @@ func (a *AnonDeviceService) TagDevice(ctx context.Context, httpReq *http.Request // ListDevices returns all devices that have been updated between the given times. func (a *AnonDeviceService) ListDevices(ctx context.Context, from *time.Time, to *time.Time) ([]*anonstore.Device, error) { + if !a.cfg.AnonymousEnabled { + return []*anonstore.Device{}, nil + } + return a.anonStore.ListDevices(ctx, from, to) } // CountDevices returns the number of devices that have been updated between the given times. func (a *AnonDeviceService) CountDevices(ctx context.Context, from time.Time, to time.Time) (int64, error) { + if !a.cfg.AnonymousEnabled { + return 0, nil + } + return a.anonStore.CountDevices(ctx, from, to) } diff --git a/pkg/services/anonymous/anontest/fake.go b/pkg/services/anonymous/anontest/fake.go index 97b7fcdb840..91a97de0172 100644 --- a/pkg/services/anonymous/anontest/fake.go +++ b/pkg/services/anonymous/anontest/fake.go @@ -6,10 +6,12 @@ import ( "time" "github.com/grafana/grafana/pkg/services/anonymous" + "github.com/grafana/grafana/pkg/services/anonymous/anonimpl/anonstore" ) type FakeService struct { ExpectedCountDevices int64 + ExpectedListDevices []*anonstore.Device ExpectedError error } @@ -17,13 +19,14 @@ func NewFakeService() *FakeService { return &FakeService{} } -type FakeAnonymousSessionService struct { -} - func (f *FakeService) TagDevice(ctx context.Context, httpReq *http.Request, kind anonymous.DeviceKind) error { - return nil + return f.ExpectedError } func (f *FakeService) CountDevices(ctx context.Context, from time.Time, to time.Time) (int64, error) { - return f.ExpectedCountDevices, nil + return f.ExpectedCountDevices, f.ExpectedError +} + +func (f *FakeService) ListDevices(ctx context.Context, from *time.Time, to *time.Time) ([]*anonstore.Device, error) { + return f.ExpectedListDevices, f.ExpectedError } diff --git a/pkg/services/anonymous/service.go b/pkg/services/anonymous/service.go index 7716b11a6b3..5765816e61a 100644 --- a/pkg/services/anonymous/service.go +++ b/pkg/services/anonymous/service.go @@ -4,6 +4,8 @@ import ( "context" "net/http" "time" + + "github.com/grafana/grafana/pkg/services/anonymous/anonimpl/anonstore" ) type DeviceKind string @@ -15,4 +17,5 @@ const ( type Service interface { TagDevice(context.Context, *http.Request, DeviceKind) error CountDevices(ctx context.Context, from time.Time, to time.Time) (int64, error) + ListDevices(ctx context.Context, from *time.Time, to *time.Time) ([]*anonstore.Device, error) }