Auth: Rename Sessions to Devices in counting (#72432)
* rename session to device * rename session to device
This commit is contained in:
@@ -20,12 +20,12 @@ import (
|
||||
const thirtyDays = 30 * 24 * time.Hour
|
||||
const anonCachePrefix = "anon-session"
|
||||
|
||||
type AnonSession struct {
|
||||
type Device struct {
|
||||
ip string
|
||||
userAgent string
|
||||
}
|
||||
|
||||
func (a *AnonSession) Key() (string, error) {
|
||||
func (a *Device) Key() (string, error) {
|
||||
key := strings.Builder{}
|
||||
key.WriteString(a.ip)
|
||||
key.WriteString(a.userAgent)
|
||||
@@ -38,14 +38,14 @@ func (a *AnonSession) Key() (string, error) {
|
||||
return strings.Join([]string{anonCachePrefix, hex.EncodeToString(hash.Sum(nil))}, ":"), nil
|
||||
}
|
||||
|
||||
type AnonSessionService struct {
|
||||
type AnonDeviceService struct {
|
||||
remoteCache remotecache.CacheStorage
|
||||
log log.Logger
|
||||
localCache *localcache.CacheService
|
||||
}
|
||||
|
||||
func ProvideAnonymousSessionService(remoteCache remotecache.CacheStorage, usageStats usagestats.Service) *AnonSessionService {
|
||||
a := &AnonSessionService{
|
||||
func ProvideAnonymousDeviceService(remoteCache remotecache.CacheStorage, usageStats usagestats.Service) *AnonDeviceService {
|
||||
a := &AnonDeviceService{
|
||||
remoteCache: remoteCache,
|
||||
log: log.New("anonymous-session-service"),
|
||||
localCache: localcache.New(29*time.Minute, 15*time.Minute),
|
||||
@@ -56,7 +56,7 @@ func ProvideAnonymousSessionService(remoteCache remotecache.CacheStorage, usageS
|
||||
return a
|
||||
}
|
||||
|
||||
func (a *AnonSessionService) usageStatFn(ctx context.Context) (map[string]interface{}, error) {
|
||||
func (a *AnonDeviceService) usageStatFn(ctx context.Context) (map[string]interface{}, error) {
|
||||
sessionCount, err := a.remoteCache.Count(ctx, anonCachePrefix)
|
||||
if err != nil {
|
||||
return nil, nil
|
||||
@@ -67,7 +67,7 @@ func (a *AnonSessionService) usageStatFn(ctx context.Context) (map[string]interf
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (a *AnonSessionService) TagSession(ctx context.Context, httpReq *http.Request) error {
|
||||
func (a *AnonDeviceService) TagDevice(ctx context.Context, httpReq *http.Request) error {
|
||||
addr := web.RemoteAddr(httpReq)
|
||||
ip, err := network.GetIPFromAddress(addr)
|
||||
if err != nil {
|
||||
@@ -80,12 +80,12 @@ func (a *AnonSessionService) TagSession(ctx context.Context, httpReq *http.Reque
|
||||
clientIPStr = ""
|
||||
}
|
||||
|
||||
anonSession := &AnonSession{
|
||||
anonDevice := &Device{
|
||||
ip: clientIPStr,
|
||||
userAgent: httpReq.UserAgent(),
|
||||
}
|
||||
|
||||
key, err := anonSession.Key()
|
||||
key, err := anonDevice.Key()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -12,15 +12,15 @@ import (
|
||||
"github.com/grafana/grafana/pkg/infra/usagestats"
|
||||
)
|
||||
|
||||
func TestAnonSessionKey(t *testing.T) {
|
||||
func TestAnonDeviceKey(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
session *AnonSession
|
||||
session *Device
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
name: "should hash correctly",
|
||||
session: &AnonSession{
|
||||
session: &Device{
|
||||
ip: "10.10.10.10",
|
||||
userAgent: "test",
|
||||
},
|
||||
@@ -28,7 +28,7 @@ func TestAnonSessionKey(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "should hash correctly with different ip",
|
||||
session: &AnonSession{
|
||||
session: &Device{
|
||||
ip: "10.10.10.1",
|
||||
userAgent: "test",
|
||||
},
|
||||
@@ -36,7 +36,7 @@ func TestAnonSessionKey(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "should hash correctly with different user agent",
|
||||
session: &AnonSession{
|
||||
session: &Device{
|
||||
ip: "10.10.10.1",
|
||||
userAgent: "test2",
|
||||
},
|
||||
@@ -58,7 +58,7 @@ func TestAnonSessionKey(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestIntegrationAnonSessionService_tag(t *testing.T) {
|
||||
func TestIntegrationAnonDeviceService_tag(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
req []*http.Request
|
||||
@@ -134,10 +134,10 @@ func TestIntegrationAnonSessionService_tag(t *testing.T) {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
fakeStore := remotecache.NewFakeStore(t)
|
||||
|
||||
anonService := ProvideAnonymousSessionService(fakeStore, &usagestats.UsageStatsMock{})
|
||||
anonService := ProvideAnonymousDeviceService(fakeStore, &usagestats.UsageStatsMock{})
|
||||
|
||||
for _, req := range tc.req {
|
||||
err := anonService.TagSession(context.Background(), req)
|
||||
err := anonService.TagDevice(context.Background(), req)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
@@ -150,9 +150,9 @@ func TestIntegrationAnonSessionService_tag(t *testing.T) {
|
||||
}
|
||||
|
||||
// Ensure that the local cache prevents request from being tagged
|
||||
func TestIntegrationAnonSessionService_localCacheSafety(t *testing.T) {
|
||||
func TestIntegrationAnonDeviceService_localCacheSafety(t *testing.T) {
|
||||
fakeStore := remotecache.NewFakeStore(t)
|
||||
anonService := ProvideAnonymousSessionService(fakeStore, &usagestats.UsageStatsMock{})
|
||||
anonService := ProvideAnonymousDeviceService(fakeStore, &usagestats.UsageStatsMock{})
|
||||
|
||||
req := &http.Request{
|
||||
Header: http.Header{
|
||||
@@ -161,17 +161,17 @@ func TestIntegrationAnonSessionService_localCacheSafety(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
anonSession := &AnonSession{
|
||||
anonDevice := &Device{
|
||||
ip: "10.30.30.2",
|
||||
userAgent: "test",
|
||||
}
|
||||
|
||||
key, err := anonSession.Key()
|
||||
key, err := anonDevice.Key()
|
||||
require.NoError(t, err)
|
||||
|
||||
anonService.localCache.SetDefault(key, true)
|
||||
|
||||
err = anonService.TagSession(context.Background(), req)
|
||||
err = anonService.TagDevice(context.Background(), req)
|
||||
require.NoError(t, err)
|
||||
|
||||
stats, err := anonService.usageStatFn(context.Background())
|
||||
|
||||
Reference in New Issue
Block a user