|
| 1 | +/**************************************************************************** |
| 2 | + * Copyright 2025, Optimizely, Inc. and contributors * |
| 3 | + * * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); * |
| 5 | + * you may not use this file except in compliance with the License. * |
| 6 | + * You may obtain a copy of the License at * |
| 7 | + * * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 * |
| 9 | + * * |
| 10 | + * Unless required by applicable law or agreed to in writing, software * |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, * |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * |
| 13 | + * See the License for the specific language governing permissions and * |
| 14 | + * limitations under the License. * |
| 15 | + ***************************************************************************/ |
| 16 | + |
| 17 | +// Package decision // |
| 18 | +package decision |
| 19 | + |
| 20 | +import ( |
| 21 | + "errors" |
| 22 | + "fmt" |
| 23 | + |
| 24 | + "github.com/optimizely/go-sdk/v2/pkg/decide" |
| 25 | + "github.com/optimizely/go-sdk/v2/pkg/decision/reasons" |
| 26 | + "github.com/optimizely/go-sdk/v2/pkg/entities" |
| 27 | + "github.com/optimizely/go-sdk/v2/pkg/logging" |
| 28 | +) |
| 29 | + |
| 30 | +// ExperimentCmabService makes decisions for CMAB experiments |
| 31 | +type ExperimentCmabService struct { |
| 32 | + cmabService CmabService |
| 33 | + logger logging.OptimizelyLogProducer |
| 34 | +} |
| 35 | + |
| 36 | +// NewExperimentCmabService creates a new instance of ExperimentCmabService |
| 37 | +func NewExperimentCmabService(cmabService CmabService, logger logging.OptimizelyLogProducer) *ExperimentCmabService { |
| 38 | + return &ExperimentCmabService{ |
| 39 | + cmabService: cmabService, |
| 40 | + logger: logger, |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +// GetDecision returns a decision for the given experiment and user context |
| 45 | +func (s *ExperimentCmabService) GetDecision(decisionContext ExperimentDecisionContext, userContext entities.UserContext, options *decide.Options) (decision ExperimentDecision, decisionReasons decide.DecisionReasons, err error) { |
| 46 | + decisionReasons = decide.NewDecisionReasons(options) |
| 47 | + experiment := decisionContext.Experiment |
| 48 | + projectConfig := decisionContext.ProjectConfig |
| 49 | + |
| 50 | + // Check if experiment is nil or not a CMAB experiment |
| 51 | + if experiment == nil || !isCmab(*experiment) { |
| 52 | + message := "Not a CMAB experiment, skipping CMAB decision service" |
| 53 | + decisionReasons.AddInfo(message) |
| 54 | + return decision, decisionReasons, nil |
| 55 | + } |
| 56 | + |
| 57 | + // Check if CMAB service is available |
| 58 | + if s.cmabService == nil { |
| 59 | + message := "CMAB service is not available" |
| 60 | + decisionReasons.AddInfo(message) |
| 61 | + return decision, decisionReasons, errors.New(message) |
| 62 | + } |
| 63 | + |
| 64 | + // Get CMAB decision |
| 65 | + cmabDecision, err := s.cmabService.GetDecision(projectConfig, userContext, experiment.ID, options) |
| 66 | + if err != nil { |
| 67 | + message := fmt.Sprintf("Failed to get CMAB decision: %v", err) |
| 68 | + decisionReasons.AddInfo(message) |
| 69 | + return decision, decisionReasons, fmt.Errorf("failed to get CMAB decision: %w", err) |
| 70 | + } |
| 71 | + |
| 72 | + // Find variation by ID |
| 73 | + for _, variation := range experiment.Variations { |
| 74 | + if variation.ID != cmabDecision.VariationID { |
| 75 | + continue |
| 76 | + } |
| 77 | + |
| 78 | + // Create a copy of the variation to avoid memory aliasing |
| 79 | + variationCopy := variation |
| 80 | + decision.Variation = &variationCopy |
| 81 | + decision.Reason = reasons.CmabVariationAssigned |
| 82 | + message := fmt.Sprintf("User bucketed into variation %s by CMAB service", variation.Key) |
| 83 | + decisionReasons.AddInfo(message) |
| 84 | + return decision, decisionReasons, nil |
| 85 | + } |
| 86 | + |
| 87 | + // If we get here, the variation ID returned by CMAB service was not found |
| 88 | + message := fmt.Sprintf("variation with ID %s not found in experiment %s", cmabDecision.VariationID, experiment.ID) |
| 89 | + decisionReasons.AddInfo(message) |
| 90 | + return decision, decisionReasons, fmt.Errorf("variation with ID %s not found in experiment %s", cmabDecision.VariationID, experiment.ID) |
| 91 | + |
| 92 | +} |
| 93 | + |
| 94 | +// isCmab is a helper method to check if an experiment is a CMAB experiment |
| 95 | +func isCmab(experiment entities.Experiment) bool { |
| 96 | + return experiment.Cmab != nil |
| 97 | +} |
0 commit comments