Anon: Small fixes to anon service structure (#79566)
* add ListDevices to service * improve fake * fix missing cfg field * cannot be unexported
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user