Skip to content

Commit

Permalink
refactor: store enabled modules into a dedicated struct
Browse files Browse the repository at this point in the history
  • Loading branch information
gpolaert committed Jul 21, 2020
1 parent b539ee0 commit f57d9d6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 29 deletions.
39 changes: 22 additions & 17 deletions analytics/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,20 @@ import (
"github.com/prebid/prebid-server/config"
)

type analyticsModule analytics.PBSAnalyticsModule

type pbsAnalyticsModule struct {
enabledModules []analyticsModule
}

//Modules that need to be logged to need to be initialized here
func NewPBSAnalytics(analytics *config.Analytics) analytics.PBSAnalyticsModule {
modules := make(enabledAnalytics, 0)

instance := &pbsAnalyticsModule{enabledModules: make([]analyticsModule, 0)}

if len(analytics.File.Filename) > 0 {
if mod, err := filesystem.NewFileLogger(analytics.File.Filename); err == nil {
modules = append(modules, mod)
instance.enabledModules = append(instance.enabledModules, mod)
} else {
glog.Fatalf("Could not initialize FileLogger for file %v :%v", analytics.File.Filename, err)
}
Expand All @@ -29,43 +37,40 @@ func NewPBSAnalytics(analytics *config.Analytics) analytics.PBSAnalyticsModule {
analytics.Pubstack.Buffers.BufferSize,
analytics.Pubstack.Buffers.Timeout)
if err == nil {
modules = append(modules, pubstackModule)
instance.enabledModules = append(instance.enabledModules, pubstackModule)
} else {
glog.Fatalf("Could not initialize PubstackModule: %v", err)
}
}
return modules
return instance
}

//Collection of all the correctly configured analytics modules - implements the PBSAnalyticsModule interface
type enabledAnalytics []analytics.PBSAnalyticsModule

func (ea enabledAnalytics) LogAuctionObject(ao *analytics.AuctionObject) {
for _, module := range ea {
func (pam pbsAnalyticsModule) LogAuctionObject(ao *analytics.AuctionObject) {
for _, module := range pam.enabledModules {
module.LogAuctionObject(ao)
}
}

func (ea enabledAnalytics) LogVideoObject(vo *analytics.VideoObject) {
for _, module := range ea {
func (pam pbsAnalyticsModule) LogVideoObject(vo *analytics.VideoObject) {
for _, module := range pam.enabledModules {
module.LogVideoObject(vo)
}
}

func (ea enabledAnalytics) LogCookieSyncObject(cso *analytics.CookieSyncObject) {
for _, module := range ea {
func (pam pbsAnalyticsModule) LogCookieSyncObject(cso *analytics.CookieSyncObject) {
for _, module := range pam.enabledModules {
module.LogCookieSyncObject(cso)
}
}

func (ea enabledAnalytics) LogSetUIDObject(so *analytics.SetUIDObject) {
for _, module := range ea {
func (pam pbsAnalyticsModule) LogSetUIDObject(so *analytics.SetUIDObject) {
for _, module := range pam.enabledModules {
module.LogSetUIDObject(so)
}
}

func (ea enabledAnalytics) LogAmpObject(ao *analytics.AmpObject) {
for _, module := range ea {
func (pam pbsAnalyticsModule) LogAmpObject(ao *analytics.AmpObject) {
for _, module := range pam.enabledModules {
module.LogAmpObject(ao)
}
}
23 changes: 11 additions & 12 deletions analytics/config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"github.com/stretchr/testify/assert"
"net/http"
"os"
"testing"
Expand Down Expand Up @@ -67,9 +68,11 @@ func (m *sampleModule) LogSetUIDObject(so *analytics.SetUIDObject) { *m.count++
func (m *sampleModule) LogAmpObject(ao *analytics.AmpObject) { *m.count++ }

func initAnalytics(count *int) analytics.PBSAnalyticsModule {
modules := make(enabledAnalytics, 0)
modules = append(modules, &sampleModule{count})
return &modules
instance := &pbsAnalyticsModule{
enabledModules: make([]analyticsModule, 0),
}
instance.enabledModules = append(instance.enabledModules, &sampleModule{count})
return instance
}

func TestNewPBSAnalytics(t *testing.T) {
Expand All @@ -79,13 +82,9 @@ func TestNewPBSAnalytics(t *testing.T) {
}
}
defer os.RemoveAll(TEST_DIR)
mod := NewPBSAnalytics(&config.Analytics{File: config.FileLogs{Filename: TEST_DIR + "/test"}})
switch modType := mod.(type) {
case enabledAnalytics:
if len(enabledAnalytics(modType)) != 1 {
t.Fatalf("Failed to add analytics module")
}
default:
t.Fatalf("Failed to initialize analytics module")
}
pbsAnalytics := NewPBSAnalytics(&config.Analytics{File: config.FileLogs{Filename: TEST_DIR + "/test"}})
instance := pbsAnalytics.(*pbsAnalyticsModule)

assert.Equal(t, len(instance.enabledModules), 1)
assert.NotNil(t, instance.enabledModules[0].(analyticsModule))
}

0 comments on commit f57d9d6

Please sign in to comment.