-
Notifications
You must be signed in to change notification settings - Fork 19
[FSSDK-11178] Update impression event for CMAB #404
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -14,8 +14,8 @@ | |||||
* limitations under the License. * | ||||||
***************************************************************************/ | ||||||
|
||||||
// Package decision provides CMAB client implementation | ||||||
package decision | ||||||
// Package cmab provides CMAB client implementation | ||||||
package cmab | ||||||
|
||||||
import ( | ||||||
"bytes" | ||||||
|
@@ -27,6 +27,9 @@ import ( | |||||
"net/http" | ||||||
"time" | ||||||
|
||||||
"github.com/optimizely/go-sdk/v2/pkg/config" | ||||||
"github.com/optimizely/go-sdk/v2/pkg/entities" | ||||||
"github.com/optimizely/go-sdk/v2/pkg/event" | ||||||
"github.com/optimizely/go-sdk/v2/pkg/logging" | ||||||
) | ||||||
|
||||||
|
@@ -44,34 +47,34 @@ const ( | |||||
DefaultBackoffMultiplier = 2.0 | ||||||
) | ||||||
|
||||||
// CMABAttribute represents an attribute in a CMAB request | ||||||
type CMABAttribute struct { | ||||||
// Attribute represents an attribute in a CMAB request | ||||||
type Attribute struct { | ||||||
ID string `json:"id"` | ||||||
Value interface{} `json:"value"` | ||||||
Type string `json:"type"` | ||||||
} | ||||||
|
||||||
// CMABInstance represents an instance in a CMAB request | ||||||
type CMABInstance struct { | ||||||
VisitorID string `json:"visitorId"` | ||||||
ExperimentID string `json:"experimentId"` | ||||||
Attributes []CMABAttribute `json:"attributes"` | ||||||
CmabUUID string `json:"cmabUUID"` | ||||||
// Instance represents an instance in a CMAB request | ||||||
type Instance struct { | ||||||
VisitorID string `json:"visitorId"` | ||||||
ExperimentID string `json:"experimentId"` | ||||||
Attributes []Attribute `json:"attributes"` | ||||||
CmabUUID string `json:"cmabUUID"` | ||||||
} | ||||||
|
||||||
// CMABRequest represents a request to the CMAB API | ||||||
type CMABRequest struct { | ||||||
Instances []CMABInstance `json:"instances"` | ||||||
// Request represents a request to the CMAB API | ||||||
type Request struct { | ||||||
Instances []Instance `json:"instances"` | ||||||
} | ||||||
|
||||||
// CMABPrediction represents a prediction in a CMAB response | ||||||
type CMABPrediction struct { | ||||||
// Prediction represents a prediction in a CMAB response | ||||||
type Prediction struct { | ||||||
VariationID string `json:"variation_id"` | ||||||
} | ||||||
|
||||||
// CMABResponse represents a response from the CMAB API | ||||||
type CMABResponse struct { | ||||||
Predictions []CMABPrediction `json:"predictions"` | ||||||
// Response represents a response from the CMAB API | ||||||
type Response struct { | ||||||
Predictions []Prediction `json:"predictions"` | ||||||
} | ||||||
|
||||||
// RetryConfig defines configuration for retry behavior | ||||||
|
@@ -88,20 +91,24 @@ type RetryConfig struct { | |||||
|
||||||
// DefaultCmabClient implements the CmabClient interface | ||||||
type DefaultCmabClient struct { | ||||||
httpClient *http.Client | ||||||
retryConfig *RetryConfig | ||||||
logger logging.OptimizelyLogProducer | ||||||
httpClient *http.Client | ||||||
retryConfig *RetryConfig | ||||||
logger logging.OptimizelyLogProducer | ||||||
eventProcessor event.Processor | ||||||
projectConfig config.ProjectConfig | ||||||
} | ||||||
|
||||||
// CmabClientOptions defines options for creating a CMAB client | ||||||
type CmabClientOptions struct { | ||||||
HTTPClient *http.Client | ||||||
RetryConfig *RetryConfig | ||||||
Logger logging.OptimizelyLogProducer | ||||||
// ClientOptions defines options for creating a CMAB client | ||||||
type ClientOptions struct { | ||||||
HTTPClient *http.Client | ||||||
RetryConfig *RetryConfig | ||||||
Logger logging.OptimizelyLogProducer | ||||||
EventProcessor event.Processor | ||||||
ProjectConfig config.ProjectConfig | ||||||
} | ||||||
|
||||||
// NewDefaultCmabClient creates a new instance of DefaultCmabClient | ||||||
func NewDefaultCmabClient(options CmabClientOptions) *DefaultCmabClient { | ||||||
func NewDefaultCmabClient(options ClientOptions) *DefaultCmabClient { | ||||||
httpClient := options.HTTPClient | ||||||
if httpClient == nil { | ||||||
httpClient = &http.Client{ | ||||||
|
@@ -119,9 +126,11 @@ func NewDefaultCmabClient(options CmabClientOptions) *DefaultCmabClient { | |||||
} | ||||||
|
||||||
return &DefaultCmabClient{ | ||||||
httpClient: httpClient, | ||||||
retryConfig: retryConfig, | ||||||
logger: logger, | ||||||
httpClient: httpClient, | ||||||
retryConfig: retryConfig, | ||||||
logger: logger, | ||||||
eventProcessor: options.EventProcessor, | ||||||
projectConfig: options.ProjectConfig, | ||||||
} | ||||||
} | ||||||
|
||||||
|
@@ -142,18 +151,18 @@ func (c *DefaultCmabClient) FetchDecision( | |||||
url := fmt.Sprintf(CMABPredictionEndpoint, ruleID) | ||||||
|
||||||
// Convert attributes to CMAB format | ||||||
cmabAttributes := make([]CMABAttribute, 0, len(attributes)) | ||||||
cmabAttributes := make([]Attribute, 0, len(attributes)) | ||||||
for key, value := range attributes { | ||||||
cmabAttributes = append(cmabAttributes, CMABAttribute{ | ||||||
cmabAttributes = append(cmabAttributes, Attribute{ | ||||||
ID: key, | ||||||
Value: value, | ||||||
Type: "custom_attribute", | ||||||
}) | ||||||
} | ||||||
|
||||||
// Create the request body | ||||||
requestBody := CMABRequest{ | ||||||
Instances: []CMABInstance{ | ||||||
requestBody := Request{ | ||||||
Instances: []Instance{ | ||||||
{ | ||||||
VisitorID: userID, | ||||||
ExperimentID: ruleID, | ||||||
|
@@ -248,7 +257,7 @@ func (c *DefaultCmabClient) doFetch(ctx context.Context, url string, bodyBytes [ | |||||
} | ||||||
|
||||||
// Parse response | ||||||
var cmabResponse CMABResponse | ||||||
var cmabResponse Response | ||||||
if err := json.Unmarshal(respBody, &cmabResponse); err != nil { | ||||||
return "", fmt.Errorf("failed to unmarshal CMAB response: %w", err) | ||||||
} | ||||||
|
@@ -263,6 +272,95 @@ func (c *DefaultCmabClient) doFetch(ctx context.Context, url string, bodyBytes [ | |||||
} | ||||||
|
||||||
// validateResponse validates the CMAB response | ||||||
func (c *DefaultCmabClient) validateResponse(response CMABResponse) bool { | ||||||
func (c *DefaultCmabClient) validateResponse(response Response) bool { | ||||||
return len(response.Predictions) > 0 && response.Predictions[0].VariationID != "" | ||||||
} | ||||||
|
||||||
// EventProcessor is an interface for processing events | ||||||
type EventProcessor interface { | ||||||
Process(userEvent interface{}) bool | ||||||
} | ||||||
|
||||||
// LogImpression logs a CMAB impression event | ||||||
func (c *DefaultCmabClient) LogImpression( | ||||||
ctx context.Context, | ||||||
eventProcessor EventProcessor, | ||||||
projectConfig config.ProjectConfig, | ||||||
ruleID string, | ||||||
userID string, | ||||||
variationKey string, | ||||||
variationID string, | ||||||
attributes map[string]interface{}, | ||||||
cmabUUID string, | ||||||
) error { | ||||||
// Instead of directly creating an event, we'll delegate this to the client | ||||||
// that has access to the event package | ||||||
return fmt.Errorf("CMAB impression logging not implemented") | ||||||
} | ||||||
|
||||||
// TrackCMABDecision tracks a CMAB decision event | ||||||
func (c *DefaultCmabClient) TrackCMABDecision( | ||||||
ruleID string, | ||||||
userID string, | ||||||
variationID string, | ||||||
variationKey string, | ||||||
attributes map[string]interface{}, | ||||||
cmabUUID string, | ||||||
) { | ||||||
if c.eventProcessor == nil || c.projectConfig == nil { | ||||||
c.logger.Debug("Event processor or project config not available, not tracking impression") | ||||||
return | ||||||
} | ||||||
|
||||||
// Get experiment from project config | ||||||
experiment, err := c.projectConfig.GetExperimentByID(ruleID) | ||||||
if err != nil { | ||||||
c.logger.Error("Error getting experiment", err) | ||||||
return | ||||||
} | ||||||
|
||||||
// Create variation object | ||||||
variation := entities.Variation{ | ||||||
ID: variationID, | ||||||
Key: variationKey, | ||||||
} | ||||||
|
||||||
// Create user context | ||||||
userContext := entities.UserContext{ | ||||||
ID: userID, | ||||||
Attributes: attributes, | ||||||
} | ||||||
|
||||||
// Look for associated feature flag (if any) | ||||||
flagKey := "" | ||||||
featureList := c.projectConfig.GetFeatureList() | ||||||
for _, feature := range featureList { | ||||||
for _, featureExperiment := range feature.FeatureExperiments { | ||||||
if featureExperiment.ID == ruleID { | ||||||
flagKey = feature.Key | ||||||
break | ||||||
} | ||||||
} | ||||||
if flagKey != "" { | ||||||
break | ||||||
} | ||||||
} | ||||||
|
||||||
// Create user event with CMAB impression | ||||||
userEvent, shouldDispatch := event.CreateCMABImpressionUserEvent( | ||||||
c.projectConfig, | ||||||
experiment, | ||||||
&variation, | ||||||
userContext, | ||||||
flagKey, | ||||||
experiment.Key, // ruleKey | ||||||
"experiment", // ruleType | ||||||
true, | ||||||
cmabUUID, | ||||||
) | ||||||
|
||||||
// Process the event if it should be dispatched | ||||||
if shouldDispatch { | ||||||
c.eventProcessor.ProcessEvent(userEvent) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The EventProcessor interface defines Process(...), not ProcessEvent; update the call or interface to match.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
} | ||||||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LogImpression is currently unimplemented and always errors; implement the logic or remove this stub to avoid runtime failures.
Copilot uses AI. Check for mistakes.