-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathHoldoutConfig.swift
More file actions
140 lines (119 loc) · 5.07 KB
/
HoldoutConfig.swift
File metadata and controls
140 lines (119 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//
// Copyright 2025, 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
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
struct HoldoutConfig {
var allHoldouts: [Holdout] {
didSet {
updateHoldoutMapping()
}
}
private(set) var global: [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 experimentHoldoutsMap: [String: [Holdout]] = [:]
init(allholdouts: [Holdout] = []) {
self.allHoldouts = allholdouts
updateHoldoutMapping()
}
/// 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 = []
includedHoldouts = [:]
excludedHoldouts = [:]
experimentHoldoutsMap = [:]
for holdout in allHoldouts {
// Handle experiment-specific holdouts (local holdouts)
if !holdout.experiments.isEmpty {
for experimentId in holdout.experiments {
if var existing = experimentHoldoutsMap[experimentId] {
existing.append(holdout)
experimentHoldoutsMap[experimentId] = existing
} else {
experimentHoldoutsMap[experimentId] = [holdout]
}
}
continue // Skip flag-level logic for experiment-specific holdouts
}
// Handle flag-level holdouts (global holdouts)
switch (holdout.includedFlags.isEmpty, holdout.excludedFlags.isEmpty) {
case (true, true):
global.append(holdout)
case (false, _):
holdout.includedFlags.forEach { flagId in
if var existing = includedHoldouts[flagId] {
existing.append(holdout)
includedHoldouts[flagId] = existing
} else {
includedHoldouts[flagId] = [holdout]
}
}
case (true, false):
global.append(holdout)
holdout.excludedFlags.forEach { flagId in
if var existing = excludedHoldouts[flagId] {
existing.append(holdout)
excludedHoldouts[flagId] = existing
} else {
excludedHoldouts[flagId] = [holdout]
}
}
}
}
}
/// 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
}
// Prioritize global holdouts first
var activeHoldouts: [Holdout] = []
let excluded = excludedHoldouts[id] ?? []
if !excluded.isEmpty {
activeHoldouts = global.filter { holdout in
return !excluded.contains(holdout)
}
} else {
activeHoldouts = global
}
let includedHoldouts = includedHoldouts[id] ?? []
activeHoldouts += includedHoldouts
flagHoldoutsMap[id] = activeHoldouts
return flagHoldoutsMap[id] ?? []
}
/// Returns the holdouts applicable to the given experiment ID.
/// - Parameter experimentId: The experiment identifier.
/// - Returns: An array of `Holdout` objects targeting this experiment.
func getHoldoutsForExperiment(experimentId: String) -> [Holdout] {
return experimentHoldoutsMap[experimentId] ?? []
}
/// Get a Holdout object for an Id.
func getHoldout(id: String) -> Holdout? {
return holdoutIdMap[id]
}
}