Skip to content
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

feat: Datafile parsing + audience evaluation for ODP integration #305

Merged
merged 45 commits into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
3d20b51
Updated user context
mikechu-optimizely Jul 15, 2022
0b4d1b9
Added ref to SDK from DemoApp so solution builds.
mikechu-optimizely Jul 15, 2022
7542ca6
Merge branch 'master' into mike/datafile-parsing-audience-evaluation
mikechu-optimizely Jul 18, 2022
b80966d
(Datafile)ProjectConfig & Integration added
mikechu-optimizely Jul 18, 2022
bac1f1d
Update Condition interface & implementing classes
mikechu-optimizely Jul 19, 2022
b8022ae
Add copyright notice
mikechu-optimizely Jul 19, 2022
15f5b63
Add AttributeType
mikechu-optimizely Jul 19, 2022
7915232
Implement changes from DoesUserMeetAudienceConditions
mikechu-optimizely Jul 19, 2022
af97fcc
Correct accessibility & missing interface item
mikechu-optimizely Jul 19, 2022
cdc6c29
Bring AttributeType into BaseCondition
mikechu-optimizely Jul 20, 2022
4c415b3
Add missing reference
mikechu-optimizely Jul 20, 2022
5cd1eeb
Fix parsing
mikechu-optimizely Jul 20, 2022
d998b57
Add test for Integration parsing
mikechu-optimizely Jul 20, 2022
7fedccb
Move reference to Integration
mikechu-optimizely Jul 20, 2022
a9295d2
Correction to NetStandard 2.0 project reference
mikechu-optimizely Jul 20, 2022
dd62705
Add empty Integration test
mikechu-optimizely Jul 20, 2022
5420bf6
Test for empty integration and non "odp" integration
mikechu-optimizely Jul 20, 2022
e29a61a
Fix missed Copyright update
mikechu-optimizely Jul 20, 2022
ecbd7ec
Fix copyright notice
mikechu-optimizely Jul 21, 2022
4d5bba9
Merge branch 'master' into mike/datafile-parsing-audience-evaluation
mikechu-optimizely Jul 25, 2022
c0bcf38
Add additional line to eof
mikechu-optimizely Jul 25, 2022
a7e0b19
Refactor `user` to `context` for OptimizelyUserContext param
mikechu-optimizely Jul 25, 2022
67d28cf
Refactor: use static array of valid types
mikechu-optimizely Jul 25, 2022
2cf6fb4
Oops use `userContext` instead of `context`
mikechu-optimizely Jul 25, 2022
3957c22
Merge branch 'master' into mike/datafile-parsing-audience-evaluation
mikechu-optimizely Jul 25, 2022
2d033eb
Update OptimizelySDK/AudienceConditions/ICondition.cs
mikechu-optimizely Jul 27, 2022
3ab49f8
Update OptimizelySDK/AudienceConditions/EmptyCondition.cs
mikechu-optimizely Jul 27, 2022
ee72d48
Support Copy() including QualifiedSegments
mikechu-optimizely Jul 27, 2022
d91c00b
Move datafile Integration tests
mikechu-optimizely Jul 27, 2022
fa56206
Merge branch 'master' into mike/datafile-parsing-audience-evaluation
mikechu-optimizely Jul 27, 2022
aeff581
Integration ToString made generic + tests
mikechu-optimizely Aug 1, 2022
f08359e
Minor refactor and doc to userconext
mikechu-optimizely Aug 1, 2022
92f38f8
Lock UserContext before determining IsQualifiedFor
mikechu-optimizely Aug 1, 2022
845cc30
Separate integration test datafile content
mikechu-optimizely Aug 1, 2022
3aa98ba
Add good integration config but also has other key-values
mikechu-optimizely Aug 1, 2022
006e63a
Add copyright and EOL
mikechu-optimizely Aug 1, 2022
514e5d5
Correction to Qualified Segments check + Add tests
mikechu-optimizely Aug 1, 2022
18b8085
Update OptimizelySDK/OptimizelyUserContext.cs
mikechu-optimizely Aug 2, 2022
b96a009
Update OptimizelySDK.Tests/ProjectConfigTest.cs
mikechu-optimizely Aug 2, 2022
9ec3300
Add thread-safe getter+setter; fix formatting
mikechu-optimizely Aug 2, 2022
a8581a7
Use third party dimension in qualified segments test
mikechu-optimizely Aug 2, 2022
2e97a8e
Fixing Mike's sloppiness
mikechu-optimizely Aug 2, 2022
3336c0c
Correct parameter order
mikechu-optimizely Aug 2, 2022
847ba3e
Rename tests
mikechu-optimizely Aug 2, 2022
e094199
Maintain previous constructor signature
mikechu-optimizely Aug 2, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -659,16 +659,13 @@ public void TestInvalidSemVersions()

#region Qualified Tests

private const string QUALIFIED = "qualified";
private const string CUSTOM_ATTRIBUTE = "custom_attribute";

[Test]
public void QualifiedConditionWithNonStringValueShouldFail()
{
var condition = new BaseCondition()
{
Type = CUSTOM_ATTRIBUTE,
Match = QUALIFIED,
Type = BaseCondition.THIRD_PARTY_DIMENSION,
Match = BaseCondition.QUALIFIED,
Value = 100,
};

Expand All @@ -690,8 +687,8 @@ public void QualifiedConditionWithMatchingValueShouldBeTrue()
{
var condition = new BaseCondition()
{
Type = CUSTOM_ATTRIBUTE,
Match = QUALIFIED,
Type = BaseCondition.THIRD_PARTY_DIMENSION,
Match = BaseCondition.QUALIFIED,
Value = QUALIFIED_VALUE,
};

Expand All @@ -706,8 +703,8 @@ public void QualifiedConditionWithNonMatchingValueShouldBeFalse()
{
var condition = new BaseCondition()
{
Type = CUSTOM_ATTRIBUTE,
Match = QUALIFIED,
Type = BaseCondition.THIRD_PARTY_DIMENSION,
Match = BaseCondition.QUALIFIED,
Value = "empire-star-system",
};

Expand Down
2 changes: 1 addition & 1 deletion OptimizelySDK.Tests/ProjectConfigTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,7 @@ public void TestRolloutWithConsistingOfANullRolloutId()
private const string ZAIUS_PUBLIC_KEY = "W4WzcEs-ABgXorzY7h1LCQ";

[Test]
public void TestGetOptimizelyConfigWithOdpIntegration()
public void TestProjectConfigWithOdpIntegration()
mikechu-optimizely marked this conversation as resolved.
Show resolved Hide resolved
{
var datafileProjectConfig = DatafileProjectConfig.Create(TestData.OdpIntegrationDatafile, new NoOpLogger(), new ErrorHandler.NoOpErrorHandler());

Expand Down
4 changes: 2 additions & 2 deletions OptimizelySDK.Tests/Utils/TestConversionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public static OptimizelyUserContext ToUserContext(this List<string> qualifiedSeg
var configManager = new FallbackProjectConfigManager(config);
var optimizely = new Optimizely(configManager);

return new OptimizelyUserContext(optimizely, "any-user", new UserAttributes(), null, errorHandler,
mockLogger.Object, qualifiedSegments);
return new OptimizelyUserContext(optimizely, "any-user", new UserAttributes(), null, qualifiedSegments, errorHandler,
mockLogger.Object);
}
}
}
10 changes: 5 additions & 5 deletions OptimizelySDK/AudienceConditions/BaseCondition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,22 @@ public class BaseCondition : ICondition
/// <summary>
/// String constant representing custom attribute condition type.
/// </summary>
private const string CUSTOM_ATTRIBUTE = "custom_attribute";
public const string CUSTOM_ATTRIBUTE = "custom_attribute";

/// <summary>
/// String constant representing a third-party condition type.
/// </summary>
private const string THIRD_PARTY_DIMENSION = "third_party_dimension";
public const string THIRD_PARTY_DIMENSION = "third_party_dimension";

/// <summary>
/// String constant to match status of qualified segments.
/// </summary>
private const string QUALIFIED = "qualified";
public const string QUALIFIED = "qualified";

/// <summary>
/// Valid types allowed for validation
/// </summary>
private static readonly string[] validTypes = {
public static readonly string[] ValidTypes = {
CUSTOM_ATTRIBUTE, THIRD_PARTY_DIMENSION,
};

Expand All @@ -63,7 +63,7 @@ public class BaseCondition : ICondition

public bool? Evaluate(ProjectConfig config, OptimizelyUserContext userContext, ILogger logger)
{
if (!validTypes.Contains(Type))
if (!ValidTypes.Contains(Type))
{
logger.Log(LogLevel.WARN, $@"Audience condition ""{this}"" uses an unknown condition type. You may need to upgrade to a newer release of the Optimizely SDK.");
return null;
Expand Down
102 changes: 55 additions & 47 deletions OptimizelySDK/OptimizelyUserContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
using OptimizelySDK.ErrorHandler;
using OptimizelySDK.Entity;
using OptimizelySDK.OptimizelyDecisions;
using System;

namespace OptimizelySDK
{
Expand All @@ -39,22 +38,17 @@ public class OptimizelyUserContext
private UserAttributes Attributes;

// set of qualified segments
public List<string> QualifiedSegments { get; }
private List<string> QualifiedSegments;

// Optimizely object to be used.
private Optimizely Optimizely;

private ForcedDecisionsStore ForcedDecisionsStore { get; set; }

public OptimizelyUserContext(Optimizely optimizely, string userId,
UserAttributes userAttributes, IErrorHandler errorHandler, ILogger logger
) :
this(optimizely, userId, userAttributes, null, errorHandler, logger) { }
public OptimizelyUserContext(Optimizely optimizely, string userId, UserAttributes userAttributes, IErrorHandler errorHandler, ILogger logger) :
this(optimizely, userId, userAttributes, null, null, errorHandler, logger) { }

public OptimizelyUserContext(Optimizely optimizely, string userId,
UserAttributes userAttributes, ForcedDecisionsStore forcedDecisionsStore,
IErrorHandler errorHandler, ILogger logger, List<string> qualifiedSegments = null
)
mikechu-optimizely marked this conversation as resolved.
Show resolved Hide resolved
public OptimizelyUserContext(Optimizely optimizely, string userId, UserAttributes userAttributes, ForcedDecisionsStore forcedDecisionsStore, List<string> qualifiedSegments, IErrorHandler errorHandler, ILogger logger)
{
ErrorHandler = errorHandler;
Logger = logger;
Expand All @@ -65,22 +59,7 @@ public OptimizelyUserContext(Optimizely optimizely, string userId,
QualifiedSegments = qualifiedSegments ?? new List<string>();
}

private OptimizelyUserContext Copy() =>
new OptimizelyUserContext(Optimizely, UserId, GetAttributes(),
GetForcedDecisionsStore(), ErrorHandler, Logger, QualifiedSegments);

/// <summary>
/// Returns true if the user is qualified for the given segment name
/// </summary>
/// <param name="segment">A String segment key which will be check in qualified segments list that if it exist then user is qualified.</param>
/// <returns>Is user qualified for a segment.</returns>
public bool IsQualifiedFor(string segment)
{
lock (mutex)
{
return QualifiedSegments.Contains(segment);
}
}
private OptimizelyUserContext Copy() => new OptimizelyUserContext(Optimizely, UserId, GetAttributes(), GetForcedDecisionsStore(), GetQualifiedSegments(), ErrorHandler, Logger);

/// <summary>
/// Returns Optimizely instance associated with the UserContext.
Expand All @@ -100,6 +79,47 @@ public virtual string GetUserId()
return UserId;
}

/// <summary>
/// Returns a copy of the current list of qualified segments
/// </summary>
/// <returns>List of qualified segments</returns>
public List<string> GetQualifiedSegments()
{
List<string> qualifiedSegmentsCopy;
lock (mutex)
{
qualifiedSegmentsCopy = new List<string>(QualifiedSegments);
}

return qualifiedSegmentsCopy;
}

/// <summary>
/// Clears and adds a collection of qualified segments
/// </summary>
/// <param name="qualifiedSegments">List of segments to replace current segments</param>
public void SetQualifiedSegments(List<string> qualifiedSegments)
{
lock (mutex)
{
QualifiedSegments.Clear();
QualifiedSegments.AddRange(qualifiedSegments);
}
}

/// <summary>
/// Returns true if the user is qualified for the given segment name
/// </summary>
/// <param name="segment">A String segment key which will be check in qualified segments list that if it exist then user is qualified.</param>
/// <returns>Is user qualified for a segment.</returns>
public bool IsQualifiedFor(string segment)
{
lock (mutex)
{
return QualifiedSegments.Contains(segment);
}
}

/// <summary>
/// Returns copy of UserAttributes associated with UserContext.
/// </summary>
Expand Down Expand Up @@ -127,8 +147,7 @@ public ForcedDecisionsStore GetForcedDecisionsStore()
if (ForcedDecisionsStore.Count == 0)
{
copiedForcedDecisionsStore = ForcedDecisionsStore.NullForcedDecision();
}
else
} else
{
copiedForcedDecisionsStore = new ForcedDecisionsStore(ForcedDecisionsStore);
}
Expand Down Expand Up @@ -167,8 +186,7 @@ public void SetAttribute(string key, object value)
/// <returns>A decision result.</returns>
public virtual OptimizelyDecision Decide(string key)
{
return Decide(key, new OptimizelyDecideOption[]
{ });
return Decide(key, new OptimizelyDecideOption[] { });
}

/// <summary>
Expand All @@ -181,8 +199,7 @@ public virtual OptimizelyDecision Decide(string key)
/// <param name="options">A list of options for decision-making.</param>
/// <returns>A decision result.</returns>
public virtual OptimizelyDecision Decide(string key,
OptimizelyDecideOption[] options
)
OptimizelyDecideOption[] options)
{
var optimizelyUserContext = Copy();
return Optimizely.Decide(optimizelyUserContext, key, options);
Expand All @@ -193,9 +210,7 @@ OptimizelyDecideOption[] options
/// </summary>
/// <param name="keys">list of flag keys for which a decision will be made.</param>
/// <returns>A dictionary of all decision results, mapped by flag keys.</returns>
public virtual Dictionary<string, OptimizelyDecision> DecideForKeys(string[] keys,
OptimizelyDecideOption[] options
)
public virtual Dictionary<string, OptimizelyDecision> DecideForKeys(string[] keys, OptimizelyDecideOption[] options)
{
var optimizelyUserContext = Copy();
return Optimizely.DecideForKeys(optimizelyUserContext, keys, options);
Expand All @@ -208,8 +223,7 @@ OptimizelyDecideOption[] options
/// <returns>A dictionary of all decision results, mapped by flag keys.</returns>
public virtual Dictionary<string, OptimizelyDecision> DecideForKeys(string[] keys)
{
return DecideForKeys(keys, new OptimizelyDecideOption[]
{ });
return DecideForKeys(keys, new OptimizelyDecideOption[] { });
}

/// <summary>
Expand All @@ -218,18 +232,15 @@ public virtual Dictionary<string, OptimizelyDecision> DecideForKeys(string[] key
/// <returns>A dictionary of all decision results, mapped by flag keys.</returns>
public virtual Dictionary<string, OptimizelyDecision> DecideAll()
{
return DecideAll(new OptimizelyDecideOption[]
{ });
return DecideAll(new OptimizelyDecideOption[] { });
}

/// <summary>
/// Returns a key-map of decision results ({@link OptimizelyDecision}) for all active flag keys.
/// </summary>
/// <param name="options">A list of options for decision-making.</param>
/// <returns>All decision results mapped by flag keys.</returns>
public virtual Dictionary<string, OptimizelyDecision> DecideAll(
OptimizelyDecideOption[] options
)
public virtual Dictionary<string, OptimizelyDecision> DecideAll(OptimizelyDecideOption[] options)
{
var optimizelyUserContext = Copy();
return Optimizely.DecideAll(optimizelyUserContext, options);
Expand All @@ -250,8 +261,7 @@ public virtual void TrackEvent(string eventName)
/// <param name="eventName">The event name.</param>
/// <param name="eventTags">A map of event tag names to event tag values.</param>
public virtual void TrackEvent(string eventName,
EventTags eventTags
)
EventTags eventTags)
{
Optimizely.Track(eventName, UserId, Attributes, eventTags);
}
Expand All @@ -262,9 +272,7 @@ EventTags eventTags
/// <param name="context">The context object containing flag and rule key.</param>
/// <param name="decision">OptimizelyForcedDecision object containing variation key.</param>
/// <returns></returns>
public bool SetForcedDecision(OptimizelyDecisionContext context,
OptimizelyForcedDecision decision
)
public bool SetForcedDecision(OptimizelyDecisionContext context, OptimizelyForcedDecision decision)
{
lock (mutex)
{
Expand Down