Skip to content

Commit 74b986a

Browse files
committed
Merge branch 'master' into mpirnovar-holdouts-projconfig-fssdk-11551
2 parents 8cf5b32 + afaf5d6 commit 74b986a

File tree

13 files changed

+237
-7
lines changed

13 files changed

+237
-7
lines changed

pkg/client/client_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ func (TestConfig) GetClientVersion() string {
177177
return "1.0.0"
178178
}
179179

180+
func (TestConfig) GetRegion() string {
181+
return "US"
182+
}
183+
180184
type MockODPManager struct {
181185
odp.Manager
182186
mock.Mock
@@ -347,6 +351,7 @@ func TestSendODPEvent(t *testing.T) {
347351

348352
func TestTrack(t *testing.T) {
349353
mockProcessor := new(MockProcessor)
354+
mockProcessor.Events = make([]event.UserEvent, 0) // Initialize Events slice
350355
mockDecisionService := new(MockDecisionService)
351356
mockProcessor.On("ProcessEvent", mock.AnythingOfType("UserEvent")).Return(true)
352357

pkg/client/fixtures_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ func (c *MockProjectConfig) SendFlagDecisions() bool {
8282
return false
8383
}
8484

85+
func (c *MockProjectConfig) GetRegion() string {
86+
return "US"
87+
}
88+
8589
type MockProjectConfigManager struct {
8690
projectConfig config.ProjectConfig
8791
mock.Mock

pkg/cmab/service_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,11 @@ func (m *MockProjectConfig) GetDatafile() string {
224224
return args.String(0)
225225
}
226226

227+
func (m *MockProjectConfig) GetRegion() string {
228+
args := m.Called()
229+
return args.String(0)
230+
}
231+
227232
type CmabServiceTestSuite struct {
228233
suite.Suite
229234
mockClient *MockCmabClient

pkg/config/datafileprojectconfig/config.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ type DatafileProjectConfig struct {
5757
sendFlagDecisions bool
5858
sdkKey string
5959
environmentKey string
60+
region string
6061

6162
flagVariationsMap map[string][]entities.Variation
6263
}
@@ -275,6 +276,11 @@ func (c DatafileProjectConfig) GetFlagVariationsMap() map[string][]entities.Vari
275276
return c.flagVariationsMap
276277
}
277278

279+
// GetRegion returns the region for the datafile
280+
func (c DatafileProjectConfig) GetRegion() string {
281+
return c.region
282+
}
283+
278284
// NewDatafileProjectConfig initializes a new datafile from a json byte array using the default JSON datafile parser
279285
func NewDatafileProjectConfig(jsonDatafile []byte, logger logging.OptimizelyLogProducer) (*DatafileProjectConfig, error) {
280286
datafile, err := Parse(jsonDatafile)
@@ -326,6 +332,11 @@ func NewDatafileProjectConfig(jsonDatafile []byte, logger logging.OptimizelyLogP
326332
attributeKeyMap[attribute.Key] = attribute
327333
}
328334

335+
region := datafile.Region
336+
if region == "" {
337+
region = "US"
338+
}
339+
329340
config := &DatafileProjectConfig{
330341
hostForODP: hostForODP,
331342
publicKeyForODP: publicKeyForODP,
@@ -353,6 +364,7 @@ func NewDatafileProjectConfig(jsonDatafile []byte, logger logging.OptimizelyLogP
353364
flagVariationsMap: flagVariationsMap,
354365
attributeKeyMap: attributeKeyMap,
355366
attributeIDToKeyMap: attributeIDToKeyMap,
367+
region: region,
356368
}
357369

358370
logger.Info("Datafile is valid.")

pkg/config/datafileprojectconfig/entities/entities.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,5 @@ type Datafile struct {
142142
SendFlagDecisions bool `json:"sendFlagDecisions"`
143143
SDKKey string `json:"sdkKey,omitempty"`
144144
EnvironmentKey string `json:"environmentKey,omitempty"`
145+
Region string `json:"region,omitempty"`
145146
}

pkg/config/interface.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ type ProjectConfig interface {
5555
GetEnvironmentKey() string
5656
GetAttributes() []entities.Attribute
5757
GetFlagVariationsMap() map[string][]entities.Variation
58+
GetRegion() string
5859
}
5960

6061
// ProjectConfigManager maintains an instance of the ProjectConfig

pkg/decision/evaluator/audience_evaluator_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,11 @@ func (m *MockProjectConfig) GetDatafile() string {
195195
return args.String(0)
196196
}
197197

198+
func (m *MockProjectConfig) GetRegion() string {
199+
args := m.Called()
200+
return args.String(0)
201+
}
202+
198203
// MockLogger is a mock implementation of OptimizelyLogProducer
199204
// (This declaration has been removed to resolve the redeclaration error)
200205

pkg/event/dispatcher_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func TestQueueEventDispatcher_DispatchEvent(t *testing.T) {
122122
batch := createBatchEvent(conversionUserEvent, createVisitorFromUserEvent(conversionUserEvent))
123123
assert.Equal(t, conversionUserEvent.Timestamp, batch.Visitors[0].Snapshots[0].Events[0].Timestamp)
124124

125-
logEvent := createLogEvent(batch, DefaultEventEndPoint)
125+
logEvent := createLogEvent(batch, EventEndPoints["US"])
126126

127127
success, _ := q.DispatchEvent(logEvent)
128128

@@ -181,7 +181,7 @@ func TestQueueEventDispatcher_FailDispath(t *testing.T) {
181181
batch := createBatchEvent(conversionUserEvent, createVisitorFromUserEvent(conversionUserEvent))
182182
assert.Equal(t, conversionUserEvent.Timestamp, batch.Visitors[0].Snapshots[0].Events[0].Timestamp)
183183

184-
logEvent := createLogEvent(batch, DefaultEventEndPoint)
184+
logEvent := createLogEvent(batch, EventEndPoints["US"])
185185

186186
q.DispatchEvent(logEvent)
187187

@@ -221,7 +221,7 @@ func TestQueueEventDispatcher_WaitForDispatchingEventsOnClose(t *testing.T) {
221221
batch := createBatchEvent(conversionUserEvent, createVisitorFromUserEvent(conversionUserEvent))
222222
assert.Equal(t, conversionUserEvent.Timestamp, batch.Visitors[0].Snapshots[0].Events[0].Timestamp)
223223

224-
logEvent := createLogEvent(batch, DefaultEventEndPoint)
224+
logEvent := createLogEvent(batch, EventEndPoints["US"])
225225

226226
success, _ := q.DispatchEvent(logEvent)
227227

pkg/event/events.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type Context struct {
2626
ClientName string `json:"client_name"`
2727
AnonymizeIP bool `json:"anonymize_ip"`
2828
BotFiltering bool `json:"bot_filtering"`
29+
Region string `json:"region"`
2930
}
3031

3132
// UserEvent represents a user event
@@ -87,6 +88,7 @@ type Batch struct {
8788
ClientName string `json:"client_name"`
8889
AnonymizeIP bool `json:"anonymize_ip"`
8990
EnrichDecisions bool `json:"enrich_decisions"`
91+
Region string `json:"region"`
9092
}
9193

9294
// Visitor represents a visitor of an eventbatch

pkg/event/factory.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package event
1919

2020
import (
2121
"errors"
22+
"log"
2223
"strings"
2324
"time"
2425

@@ -37,7 +38,26 @@ const botFilteringKey = "$opt_bot_filtering"
3738
const revenueKey = "revenue"
3839
const valueKey = "value"
3940

41+
// SupportedRegions maps of supported regions for event endpoints
42+
var SupportedRegions = map[string]bool{"US": true, "EU": true}
43+
44+
func getEventEndPoint(region, eventEndPoint string) string {
45+
if eventEndPoint != "" {
46+
return eventEndPoint
47+
}
48+
49+
region = strings.ToUpper(region)
50+
51+
if !SupportedRegions[region] {
52+
log.Print("Unsupported region provided for event endpoint. Defaulting to US.")
53+
region = "US"
54+
}
55+
56+
return EventEndPoints[region]
57+
}
58+
4059
func createLogEvent(event Batch, eventEndPoint string) LogEvent {
60+
eventEndPoint = getEventEndPoint(event.Region, eventEndPoint)
4161
return LogEvent{EndPoint: eventEndPoint, Event: event}
4262
}
4363

@@ -55,6 +75,7 @@ func CreateEventContext(projectConfig config.ProjectConfig) Context {
5575
context.ClientVersion = Version
5676
context.AnonymizeIP = projectConfig.GetAnonymizeIP()
5777
context.BotFiltering = projectConfig.GetBotFiltering()
78+
context.Region = projectConfig.GetRegion()
5879

5980
return context
6081
}
@@ -253,6 +274,7 @@ func createBatchEvent(userEvent UserEvent, visitor Visitor) Batch {
253274
eventBatch.ClientVersion = userEvent.EventContext.ClientVersion
254275
eventBatch.AnonymizeIP = userEvent.EventContext.AnonymizeIP
255276
eventBatch.EnrichDecisions = true
277+
eventBatch.Region = userEvent.EventContext.Region
256278

257279
return eventBatch
258280
}

0 commit comments

Comments
 (0)