Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
f5e60a7
wip: Holdout Model created
muzahidul-opti Mar 20, 2025
38386cd
wip: add holdout model test cases
muzahidul-opti Mar 20, 2025
a732fdb
Merge branch 'master' into muzahid/holdout-FSSDK-11371
muzahidul-opti Mar 20, 2025
99eea3d
wip: assging holdoutids to feature flags
muzahidul-opti Mar 24, 2025
326adce
wip: included and excluded flags updated to required
muzahidul-opti Mar 24, 2025
d1cc64d
wip: move OptimizelyExperiment compliance to OptmizelyConfig file
muzahidul-opti Mar 25, 2025
247d0eb
Merge branch 'muzahid/holdout-FSSDK-11371' into muzahid/FSSDK-11372
muzahidul-opti Mar 25, 2025
01f3cd7
wip: fix uint test
muzahidul-opti Mar 25, 2025
5404d97
Merge branch 'master' into muzahid/FSSDK-11372
muzahidul-opti Apr 7, 2025
3560de9
wip: make holdouts array empty if no key presented
muzahidul-opti Apr 7, 2025
51ba12a
wip: add id and key to experiement core
muzahidul-opti Apr 7, 2025
2c29e82
wip: clean up
muzahidul-opti Apr 7, 2025
16920c7
wip: clean up
muzahidul-opti Apr 8, 2025
7bad66d
clean up
muzahidul-opti Apr 8, 2025
b337b35
wip: address review request
muzahidul-opti Apr 9, 2025
c694a57
wip: Update holdout sample data
muzahidul-opti Apr 9, 2025
fd6535a
wip: update flag to holdout mapping approach
muzahidul-opti Apr 9, 2025
987ff6c
wip: holdout config struct added
muzahidul-opti Apr 15, 2025
528aa0d
wip: unit tests updated
muzahidul-opti Apr 15, 2025
c868c29
wip: HoldoutConfig test cases added
muzahidul-opti Apr 15, 2025
e876960
wip: fix holdout config tests
muzahidul-opti Apr 15, 2025
9e1669c
clean up: fix test cases
muzahidul-opti Apr 15, 2025
f310734
cleanup: add unit test cases
muzahidul-opti Apr 16, 2025
0ce7aa3
Update Sources/Data Model/HoldoutConfig.swift
muzahidul-opti Apr 17, 2025
05ed9f0
cleanup: address review changes
muzahidul-opti Apr 17, 2025
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
6 changes: 6 additions & 0 deletions OptimizelySwiftSDK.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -2050,6 +2050,8 @@
98AC97EF2DAE4579001405DD /* HoldoutConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 98AC97E12DAE4579001405DD /* HoldoutConfig.swift */; };
98AC97F02DAE4579001405DD /* HoldoutConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 98AC97E12DAE4579001405DD /* HoldoutConfig.swift */; };
98AC97F12DAE4579001405DD /* HoldoutConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 98AC97E12DAE4579001405DD /* HoldoutConfig.swift */; };
98AC97F32DAE9685001405DD /* HoldoutConfigTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 98AC97F22DAE9685001405DD /* HoldoutConfigTests.swift */; };
98AC97F42DAE9685001405DD /* HoldoutConfigTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 98AC97F22DAE9685001405DD /* HoldoutConfigTests.swift */; };
BD1C3E8524E4399C0084B4DA /* SemanticVersion.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B97DD93249D327F003DE606 /* SemanticVersion.swift */; };
BD64853C2491474500F30986 /* Optimizely.h in Headers */ = {isa = PBXBuildFile; fileRef = 6E75167A22C520D400B2B157 /* Optimizely.h */; settings = {ATTRIBUTES = (Public, ); }; };
BD64853E2491474500F30986 /* Audience.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E75169822C520D400B2B157 /* Audience.swift */; };
Expand Down Expand Up @@ -2494,6 +2496,7 @@
984FE5102CC8AA88004F6F41 /* UserProfileTracker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserProfileTracker.swift; sourceTree = "<group>"; };
987F11D92AF3F56F0083D3F9 /* PrivacyInfo.xcprivacy */ = {isa = PBXFileReference; lastKnownFileType = text.xml; path = PrivacyInfo.xcprivacy; sourceTree = "<group>"; };
98AC97E12DAE4579001405DD /* HoldoutConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HoldoutConfig.swift; sourceTree = "<group>"; };
98AC97F22DAE9685001405DD /* HoldoutConfigTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HoldoutConfigTests.swift; sourceTree = "<group>"; };
BD6485812491474500F30986 /* Optimizely.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Optimizely.framework; sourceTree = BUILT_PRODUCTS_DIR; };
C78CAF572445AD8D009FE876 /* OptimizelyJSON.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OptimizelyJSON.swift; sourceTree = "<group>"; };
C78CAF652446DB91009FE876 /* OptimizelyClientTests_OptimizelyJSON.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OptimizelyClientTests_OptimizelyJSON.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -3066,6 +3069,7 @@
6E7519A622C5211100B2B157 /* AttributeValueTests_Evaluate.swift */,
6E7519A722C5211100B2B157 /* RolloutTests.swift */,
6E7519A822C5211100B2B157 /* ProjectConfigTests.swift */,
98AC97F22DAE9685001405DD /* HoldoutConfigTests.swift */,
6E7519A922C5211100B2B157 /* UserAttributeTests.swift */,
6E7519AA22C5211100B2B157 /* GroupTests.swift */,
6E7519AB22C5211100B2B157 /* VariationTests.swift */,
Expand Down Expand Up @@ -5085,6 +5089,7 @@
6E8A3D522637408500DAEA13 /* MockDatafileHandler.swift in Sources */,
6E75180E22C520D400B2B157 /* DataStoreFile.swift in Sources */,
6E9B11B822C5489600C22D81 /* OTUtils.swift in Sources */,
98AC97F42DAE9685001405DD /* HoldoutConfigTests.swift in Sources */,
6E9B119022C5488300C22D81 /* AttributeValueTests.swift in Sources */,
6E994B4025A3E6EA00999262 /* DecisionResponse.swift in Sources */,
84E2E9802855875E001114AB /* OdpEventManager.swift in Sources */,
Expand Down Expand Up @@ -5361,6 +5366,7 @@
6E8A3D4D2637408500DAEA13 /* MockDatafileHandler.swift in Sources */,
6E75180922C520D400B2B157 /* DataStoreFile.swift in Sources */,
6E9B11AE22C5489300C22D81 /* OTUtils.swift in Sources */,
98AC97F32DAE9685001405DD /* HoldoutConfigTests.swift in Sources */,
6E9B117A22C5488100C22D81 /* AttributeValueTests.swift in Sources */,
6E994B3B25A3E6EA00999262 /* DecisionResponse.swift in Sources */,
84E2E97B2855875E001114AB /* OdpEventManager.swift in Sources */,
Expand Down
68 changes: 47 additions & 21 deletions Sources/Data Model/HoldoutConfig.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//
// Copyright 2022, Optimizely, Inc. and contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// Copyright 2022, 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
//
// 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
Expand All @@ -19,38 +19,46 @@ import Foundation
struct HoldoutConfig {
var allHoldouts: [Holdout] {
didSet {
updateHoldoutProperties()
updateHoldoutMapping()
}
}
private(set) var holdoutIdMap: [String: Holdout] = [:]
private(set) var global: [Holdout] = []
private(set) var others: [Holdout] = []
private(set) var holdoutIdMap: [String: Holdout] = [:]
private(set) var flagHoldoutsMap: [String: [Holdout]] = [:]
private(set) var includedHoldouts: [String: [Holdout]] = [:]
private(set) var excludedHoldouts: [String: [Holdout]] = [:]
private(set) var flagHoldoutsMap: [String: [Holdout]] = [:]

var cacheWriteCount = 0 // for testing purpose only

var logger: OPTLogger = OPTLoggerFactory.getLogger()

init(allholdouts: [Holdout] = []) {
self.allHoldouts = allholdouts
updateHoldoutProperties()
updateHoldoutMapping()
}

mutating func updateHoldoutProperties() {
/// Updates internal mappings of holdouts including the id map, global list, and per-flag inclusion/exclusion maps.
mutating func updateHoldoutMapping() {
holdoutIdMap = {
var map = [String: Holdout]()
allHoldouts.forEach { map[$0.id] = $0 }
return map
}()

flagHoldoutsMap = [:]
global = []
others = []
includedHoldouts = [:]
excludedHoldouts = [:]

for holdout in allHoldouts {
switch (holdout.includedFlags.isEmpty, holdout.excludedFlags.isEmpty) {
case (true, true):
global.append(holdout)
case (false, _):

case (false, false):
logger.e(.holdoutToFlagMappingError)

case (false, true):
holdout.includedFlags.forEach { flagId in
if var existing = includedHoldouts[flagId] {
existing.append(holdout)
Expand All @@ -59,8 +67,10 @@ struct HoldoutConfig {
includedHoldouts[flagId] = [holdout]
}
}
case (_, false):
others.append(holdout)

case (true, false):
global.append(holdout)

holdout.excludedFlags.forEach { flagId in
if var existing = excludedHoldouts[flagId] {
existing.append(holdout)
Expand All @@ -73,25 +83,41 @@ struct HoldoutConfig {
}
}

/// Returns the applicable holdouts for the given flag ID by combining global holdouts (excluding any specified) and included holdouts, in that order.
/// Caches the result for future calls.
/// - Parameter id: The flag identifier.
/// - Returns: An array of `Holdout` objects relevant to the given flag.
mutating func getHoldoutForFlag(id: String) -> [Holdout] {
guard !allHoldouts.isEmpty else { return [] }

// Check cache and return persistent holdouts
if let holdouts = flagHoldoutsMap[id] {
return holdouts
}

if let included = includedHoldouts[id], !included.isEmpty {
flagHoldoutsMap[id] = global + included
} else {
let excluded = excludedHoldouts[id] ?? []
let filteredHoldouts = others.filter { holdout in
var activeHoldouts: [Holdout] = []

let excluded = excludedHoldouts[id] ?? []

if !excluded.isEmpty {
activeHoldouts = global.filter { holdout in
return !excluded.contains(holdout)
}
flagHoldoutsMap[id] = global + filteredHoldouts
} else {
activeHoldouts = global
}

let includedHoldouts = includedHoldouts[id] ?? []

activeHoldouts += includedHoldouts

flagHoldoutsMap[id] = activeHoldouts
cacheWriteCount += 1

return flagHoldoutsMap[id] ?? []
}

/// Get a Holdout object for an Id.
func getHoldout(id: String) -> Holdout? {
return holdoutIdMap[id]
}
Expand Down
28 changes: 1 addition & 27 deletions Sources/Data Model/ProjectConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -162,33 +162,7 @@ class ProjectConfig {
func getHoldoutForFlag(id: String) -> [Holdout] {
return holdoutConfig.getHoldoutForFlag(id: id)
}

// private func updateHoldoutsMapForFlag(id: String) {
// if let included = inlcudedHoldoutsMap[id] {
// return globalHoldouts + included
// }
//
//// for holdout in project.holdouts {
//// switch (holdout.includedFlags.isEmpty, holdout.excludedFlags.isEmpty) {
//// case (true, true):
//// // Global holdout
//// global.append(holdout.id)
////
//// case (false, _):
//// if holdout.includedFlags.contains(id) {
//// others.append(holdout.id)
//// }
////
//// case (_, false):
//// if !holdout.excludedFlags.contains(id) {
//// others.append(holdout.id)
//// }
//// }
//// }
////
// flagHoldoutsMap[id] = globalHoldouts + others
// }


func getAllRulesForFlag(_ flag: FeatureFlag) -> [Experiment] {
var rules = flag.experimentIds.compactMap { experimentIdMap[$0] }
let rollout = self.rolloutIdMap[flag.rolloutId]
Expand Down
2 changes: 2 additions & 0 deletions Sources/Utils/LogMessage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ enum LogMessage {
case failedToAssignValue
case valueForKeyNotFound(_ key: String)
case lowPeriodicDownloadInterval
case holdoutToFlagMappingError
}

extension LogMessage: CustomStringConvertible {
Expand Down Expand Up @@ -130,6 +131,7 @@ extension LogMessage: CustomStringConvertible {
case .failedToAssignValue: message = "Value for path could not be assigned to provided type."
case .valueForKeyNotFound(let key): message = "Value for JSON key (\(key)) not found."
case .lowPeriodicDownloadInterval: message = "Polling intervals below 30 seconds are not recommended."
case .holdoutToFlagMappingError: message = "A holdout cannot have include and exclude flags at the same time."
}

return message
Expand Down
Loading
Loading