-
Notifications
You must be signed in to change notification settings - Fork 18
refact(audience-logs): Added and refactored audience evaluation logs. #280
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
Changes from 4 commits
6757e72
dc4c618
c66092e
ae28341
0f1a60d
d12d565
47d6587
74aa1f6
af4847f
6b11c70
81e304a
3e8c1f5
d692e84
6e1116b
5cdef75
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| /**************************************************************************** | ||
| * Copyright 2019, Optimizely, Inc. and contributors * | ||
| * Copyright 2019-2020, 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. * | ||
|
|
@@ -22,6 +22,7 @@ import ( | |
|
|
||
| "github.com/optimizely/go-sdk/pkg/decision/evaluator/matchers" | ||
| "github.com/optimizely/go-sdk/pkg/entities" | ||
| "github.com/optimizely/go-sdk/pkg/logging" | ||
| ) | ||
|
|
||
| const ( | ||
|
|
@@ -38,14 +39,23 @@ type ItemEvaluator interface { | |
| } | ||
|
|
||
| // CustomAttributeConditionEvaluator evaluates conditions with custom attributes | ||
| type CustomAttributeConditionEvaluator struct{} | ||
| type CustomAttributeConditionEvaluator struct { | ||
| logger logging.OptimizelyLogProducer | ||
| } | ||
|
|
||
| // NewCustomAttributeConditionEvaluator creates a custom attribute condition | ||
| func NewCustomAttributeConditionEvaluator(logger logging.OptimizelyLogProducer) *CustomAttributeConditionEvaluator { | ||
| return &CustomAttributeConditionEvaluator{logger: logger} | ||
| } | ||
|
|
||
| // Evaluate returns true if the given user's attributes match the condition | ||
| func (c CustomAttributeConditionEvaluator) Evaluate(condition entities.Condition, condTreeParams *entities.TreeParameters) (bool, error) { | ||
| // We should only be evaluating custom attributes | ||
|
|
||
| if condition.Type != customAttributeType { | ||
| return false, fmt.Errorf(`unable to evaluator condition of type "%s"`, condition.Type) | ||
|
|
||
| c.logger.Warning(fmt.Sprintf(string(logging.UnknownConditionType), condition.StringRepresentation)) | ||
|
||
| return false, fmt.Errorf(`unable to evaluate condition of type "%s"`, condition.Type) | ||
| } | ||
|
|
||
| var matcher matchers.Matcher | ||
|
|
@@ -57,6 +67,7 @@ func (c CustomAttributeConditionEvaluator) Evaluate(condition entities.Condition | |
| case exactMatchType: | ||
| matcher = matchers.ExactMatcher{ | ||
| Condition: condition, | ||
| Logger: c.logger, | ||
|
||
| } | ||
| case existsMatchType: | ||
| matcher = matchers.ExistsMatcher{ | ||
|
|
@@ -65,16 +76,20 @@ func (c CustomAttributeConditionEvaluator) Evaluate(condition entities.Condition | |
| case ltMatchType: | ||
| matcher = matchers.LtMatcher{ | ||
| Condition: condition, | ||
| Logger: c.logger, | ||
| } | ||
| case gtMatchType: | ||
| matcher = matchers.GtMatcher{ | ||
| Condition: condition, | ||
| Logger: c.logger, | ||
| } | ||
| case substringMatchType: | ||
| matcher = matchers.SubstringMatcher{ | ||
| Condition: condition, | ||
| Logger: c.logger, | ||
| } | ||
| default: | ||
| c.logger.Warning(fmt.Sprintf(string(logging.UnknownMatchType), condition.StringRepresentation)) | ||
|
||
| return false, fmt.Errorf(`invalid Condition matcher "%s"`, condition.Match) | ||
| } | ||
|
|
||
|
|
@@ -84,20 +99,28 @@ func (c CustomAttributeConditionEvaluator) Evaluate(condition entities.Condition | |
| } | ||
|
|
||
| // AudienceConditionEvaluator evaluates conditions with audience condition | ||
| type AudienceConditionEvaluator struct{} | ||
| type AudienceConditionEvaluator struct { | ||
| logger logging.OptimizelyLogProducer | ||
| } | ||
|
|
||
| // NewAudienceConditionEvaluator creates a audience condition evaluator | ||
| func NewAudienceConditionEvaluator(logger logging.OptimizelyLogProducer) *AudienceConditionEvaluator { | ||
| return &AudienceConditionEvaluator{logger: logger} | ||
| } | ||
|
|
||
| // Evaluate returns true if the given user's attributes match the condition | ||
| func (c AudienceConditionEvaluator) Evaluate(audienceID string, condTreeParams *entities.TreeParameters) (bool, error) { | ||
|
|
||
| if audience, ok := condTreeParams.AudienceMap[audienceID]; ok { | ||
| c.logger.Debug(fmt.Sprintf(string(logging.AudienceEvaluationStarted), audienceID)) | ||
| condTree := audience.ConditionTree | ||
| conditionTreeEvaluator := NewMixedTreeEvaluator() | ||
| conditionTreeEvaluator := NewMixedTreeEvaluator(c.logger) | ||
| retValue, isValid := conditionTreeEvaluator.Evaluate(condTree, condTreeParams) | ||
| if !isValid { | ||
| return false, fmt.Errorf(`an error occurred while evaluating nested tree for audience ID "%s"`, audienceID) | ||
| } | ||
| c.logger.Debug(fmt.Sprintf(string(logging.AudienceEvaluatedTo), audienceID, retValue)) | ||
| return retValue, nil | ||
|
|
||
| } | ||
|
|
||
| return false, fmt.Errorf(`unable to evaluate nested tree for audience ID "%s"`, audienceID) | ||
|
|
||
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.
Why string is needed? string(logging.UnknownConditionType)
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.
Since
UnknownConditionTypeis of custom type, we must convert it to string before formatting.