Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions pkg/decision/entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,23 @@ type ExperimentDecision struct {
Decision
Variation *entities.Variation
}

// UserDecisionKey is used to access the saved decisions in a user profile
type UserDecisionKey struct {
ExperimentID string
Field string
}

// NewUserDecisionKey returns a new UserDecisionKey with the given experiment ID
func NewUserDecisionKey(experimentID string) UserDecisionKey {
return UserDecisionKey{
ExperimentID: experimentID,
Field: "variation_id",
}
}

// UserProfile represents a saved user profile
type UserProfile struct {
ID string
ExperimentBucketMap map[UserDecisionKey]string
}
6 changes: 6 additions & 0 deletions pkg/decision/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,9 @@ type ExperimentService interface {
type FeatureService interface {
GetDecision(decisionContext FeatureDecisionContext, userContext entities.UserContext) (FeatureDecision, error)
}

// UserProfileService is used to save and retrieve past bucketing decisions for users
type UserProfileService interface {
Lookup(string) UserProfile
Save(UserProfile)
}
100 changes: 100 additions & 0 deletions pkg/decision/persisting_experiment_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/****************************************************************************
* Copyright 2019, Optimizely, Inc. and contributors *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
***************************************************************************/

// Package decision //
package decision

import (
"fmt"

"github.com/optimizely/go-sdk/pkg/entities"
"github.com/optimizely/go-sdk/pkg/logging"
)

var pesLogger = logging.GetLogger("pkg/decision/persisting_experiment_service")

// PersistingExperimentService attempts to retrieve a saved decision from the user profile service
// for the user before having the ExperimentBucketerService compute it.
// If computed, the decision is saved back to the user profile service if provided.
type PersistingExperimentService struct {
experimentBucketedService ExperimentService
userProfileService UserProfileService
}

// NewPersistingExperimentService returns a new instance of the PersistingExperimentService
func NewPersistingExperimentService(experimentBucketerService ExperimentService, userProfileService UserProfileService) *PersistingExperimentService {
persistingExperimentService := &PersistingExperimentService{
experimentBucketedService: experimentBucketerService,
userProfileService: userProfileService,
}

return persistingExperimentService
}

// GetDecision returns the decision with the variation the user is bucketed into
func (p PersistingExperimentService) GetDecision(decisionContext ExperimentDecisionContext, userContext entities.UserContext) (experimentDecision ExperimentDecision, err error) {
if p.userProfileService == nil {
return p.experimentBucketedService.GetDecision(decisionContext, userContext)
}

var userProfile UserProfile
// check to see if there is a saved decision for the user
experimentDecision, userProfile = p.getSavedDecision(decisionContext, userContext)
if experimentDecision.Variation != nil {
return experimentDecision, nil
}

experimentDecision, err = p.experimentBucketedService.GetDecision(decisionContext, userContext)
if experimentDecision.Variation != nil {
// save decision if a user profile service is provided
p.saveDecision(userProfile, decisionContext.Experiment, experimentDecision)
}

return experimentDecision, err
}

func (p PersistingExperimentService) getSavedDecision(decisionContext ExperimentDecisionContext, userContext entities.UserContext) (ExperimentDecision, UserProfile) {
experimentDecision := ExperimentDecision{}
userProfile := p.userProfileService.Lookup(userContext.ID)

// look up experiment decision from user profile
decisionKey := NewUserDecisionKey(decisionContext.Experiment.ID)
if userProfile.ExperimentBucketMap == nil {
return experimentDecision, userProfile
}

if savedVariationID, ok := userProfile.ExperimentBucketMap[decisionKey]; ok {
if variation, ok := decisionContext.Experiment.Variations[savedVariationID]; ok {
experimentDecision.Variation = &variation
pesLogger.Debug(fmt.Sprintf(`User "%s" was previously bucketed into variation "%s" of experiment "%s".`, userContext.ID, variation.Key, decisionContext.Experiment.Key))
} else {
pesLogger.Warning(fmt.Sprintf(`User "%s" was previously bucketed into variation with ID "%s" for experiment "%s", but no matching variation was found.`, userContext.ID, savedVariationID, decisionContext.Experiment.Key))
}
}

return experimentDecision, userProfile
}

func (p PersistingExperimentService) saveDecision(userProfile UserProfile, experiment *entities.Experiment, decision ExperimentDecision) {
if p.userProfileService != nil {
decisionKey := NewUserDecisionKey(experiment.ID)
if userProfile.ExperimentBucketMap == nil {
userProfile.ExperimentBucketMap = map[UserDecisionKey]string{}
}
userProfile.ExperimentBucketMap[decisionKey] = decision.Variation.ID
p.userProfileService.Save(userProfile)
}
}
139 changes: 139 additions & 0 deletions pkg/decision/persisting_experiment_service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/****************************************************************************
* Copyright 2019, Optimizely, Inc. and contributors *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
***************************************************************************/

// Package decision //
package decision

import (
"testing"

"github.com/optimizely/go-sdk/pkg/entities"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
)

type MockUserProfileService struct {
UserProfileService
mock.Mock
}

func (m *MockUserProfileService) Lookup(userID string) UserProfile {
args := m.Called(userID)
return args.Get(0).(UserProfile)
}

func (m *MockUserProfileService) Save(userProfile UserProfile) {
m.Called(userProfile)
}

var testUserContext entities.UserContext = entities.UserContext{
ID: "test_user_1",
}

type PersistingExperimentServiceTestSuite struct {
suite.Suite
mockProjectConfig *mockProjectConfig
mockExperimentService *MockExperimentDecisionService
mockUserProfileService *MockUserProfileService
testComputedDecision ExperimentDecision
testDecisionContext ExperimentDecisionContext
}

func (s *PersistingExperimentServiceTestSuite) SetupTest() {
s.mockProjectConfig = new(mockProjectConfig)
s.mockExperimentService = new(MockExperimentDecisionService)
s.mockUserProfileService = new(MockUserProfileService)
s.testDecisionContext = ExperimentDecisionContext{
Experiment: &testExp1113,
ProjectConfig: s.mockProjectConfig,
}

computedVariation := testExp1113.Variations["2223"]
s.testComputedDecision = ExperimentDecision{
Variation: &computedVariation,
}
s.mockExperimentService.On("GetDecision", s.testDecisionContext, testUserContext).Return(s.testComputedDecision, nil)
}

func (s *PersistingExperimentServiceTestSuite) TestNilUserProfileService() {
persistingExperimentService := NewPersistingExperimentService(s.mockExperimentService, nil)
decision, err := persistingExperimentService.GetDecision(s.testDecisionContext, testUserContext)
s.Equal(s.testComputedDecision, decision)
s.NoError(err)
s.mockExperimentService.AssertExpectations(s.T())
}

func (s *PersistingExperimentServiceTestSuite) TestSavedVariationFound() {
decisionKey := NewUserDecisionKey(s.testDecisionContext.Experiment.ID)
savedUserProfile := UserProfile{
ID: testUserContext.ID,
ExperimentBucketMap: map[UserDecisionKey]string{decisionKey: testExp1113Var2224.ID},
}
s.mockUserProfileService.On("Lookup", testUserContext.ID).Return(savedUserProfile)
s.mockUserProfileService.On("Save", mock.Anything)

persistingExperimentService := NewPersistingExperimentService(s.mockExperimentService, s.mockUserProfileService)
decision, err := persistingExperimentService.GetDecision(s.testDecisionContext, testUserContext)
savedDecision := ExperimentDecision{
Variation: &testExp1113Var2224,
}
s.Equal(savedDecision, decision)
s.NoError(err)
s.mockExperimentService.AssertNotCalled(s.T(), "GetDecision", s.testDecisionContext, testUserContext)
s.mockUserProfileService.AssertNotCalled(s.T(), "Save", mock.Anything)
}

func (s *PersistingExperimentServiceTestSuite) TestNoSavedVariation() {
s.mockUserProfileService.On("Lookup", testUserContext.ID).Return(UserProfile{ID: testUserContext.ID}) // empty user profile
decisionKey := NewUserDecisionKey(s.testDecisionContext.Experiment.ID)
updatedUserProfile := UserProfile{
ID: testUserContext.ID,
ExperimentBucketMap: map[UserDecisionKey]string{decisionKey: s.testComputedDecision.Variation.ID},
}

s.mockUserProfileService.On("Save", updatedUserProfile)
persistingExperimentService := NewPersistingExperimentService(s.mockExperimentService, s.mockUserProfileService)
decision, err := persistingExperimentService.GetDecision(s.testDecisionContext, testUserContext)
s.Equal(s.testComputedDecision, decision)
s.NoError(err)
s.mockExperimentService.AssertExpectations(s.T())
s.mockUserProfileService.AssertExpectations(s.T())
}

func (s *PersistingExperimentServiceTestSuite) TestSavedVariationNoLongerValid() {
decisionKey := NewUserDecisionKey(s.testDecisionContext.Experiment.ID)
savedUserProfile := UserProfile{
ID: testUserContext.ID,
ExperimentBucketMap: map[UserDecisionKey]string{decisionKey: "forgotten_variation"},
}
s.mockUserProfileService.On("Lookup", testUserContext.ID).Return(savedUserProfile) // empty user profile

updatedUserProfile := UserProfile{
ID: testUserContext.ID,
ExperimentBucketMap: map[UserDecisionKey]string{decisionKey: s.testComputedDecision.Variation.ID},
}
s.mockUserProfileService.On("Save", updatedUserProfile)
persistingExperimentService := NewPersistingExperimentService(s.mockExperimentService, s.mockUserProfileService)
decision, err := persistingExperimentService.GetDecision(s.testDecisionContext, testUserContext)
s.Equal(s.testComputedDecision, decision)
s.NoError(err)
s.mockExperimentService.AssertExpectations(s.T())
s.mockUserProfileService.AssertExpectations(s.T())
}

func TestPersistingExperimentServiceTestSuite(t *testing.T) {
suite.Run(t, new(PersistingExperimentServiceTestSuite))
}