Skip to content

Commit e827b27

Browse files
Reuse experiment bucketing logic
1 parent e9d01ac commit e827b27

File tree

3 files changed

+66
-57
lines changed

3 files changed

+66
-57
lines changed

Sources/Implementation/DefaultBucketer.swift

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -120,53 +120,53 @@ class DefaultBucketer: OPTBucketer {
120120
return DecisionResponse(result: nil, reasons: reasons)
121121
}
122122

123-
func bucketToEntityId(bucketingId: String,
124-
experiment: Experiment,
125-
trafficAllocation: [TrafficAllocation],
126-
group: Group?) -> DecisionResponse<String> {
127-
let reasons = DecisionReasons()
128-
129-
if let group = group, group.policy == .random {
130-
let hashId = makeHashIdFromBucketingId(bucketingId: bucketingId, entityId: group.id)
131-
let bucketValue = self.generateBucketValue(bucketingId: hashId)
132-
133-
var matched = false
134-
for allocation in group.trafficAllocation {
135-
if bucketValue < allocation.endOfRange {
136-
matched = true
137-
if allocation.entityId != experiment.id {
138-
let info = LogMessage.userNotBucketedIntoExperimentInGroup(bucketingId, experiment.key, group.id)
139-
reasons.addInfo(info)
140-
return DecisionResponse(result: nil, reasons: reasons)
141-
}
142-
143-
let info = LogMessage.userBucketedIntoExperimentInGroup(bucketingId, experiment.key, group.id)
144-
reasons.addInfo(info)
145-
break
146-
}
147-
}
148-
149-
if !matched {
150-
let info = LogMessage.userNotBucketedIntoAnyExperimentInGroup(bucketingId, group.id)
151-
reasons.addInfo(info)
152-
return DecisionResponse(result: nil, reasons: reasons)
153-
}
154-
}
155-
156-
let hashId = makeHashIdFromBucketingId(bucketingId: bucketingId, entityId: experiment.id)
157-
let bucketValue = self.generateBucketValue(bucketingId: hashId)
158-
159-
for allocation in trafficAllocation {
160-
if bucketValue < allocation.endOfRange {
161-
let info = LogMessage.userBucketedIntoEntity(allocation.entityId)
162-
reasons.addInfo(info)
163-
return DecisionResponse(result: allocation.entityId, reasons: reasons)
164-
}
165-
}
166-
let info = LogMessage.userNotBucketedIntoAnyEntity
167-
reasons.addInfo(info)
168-
return DecisionResponse(result: nil, reasons: reasons)
169-
}
123+
// func bucketToEntityId(bucketingId: String,
124+
// experiment: Experiment,
125+
// trafficAllocation: [TrafficAllocation],
126+
// group: Group?) -> DecisionResponse<String> {
127+
// let reasons = DecisionReasons()
128+
//
129+
// if let group = group, group.policy == .random {
130+
// let hashId = makeHashIdFromBucketingId(bucketingId: bucketingId, entityId: group.id)
131+
// let bucketValue = self.generateBucketValue(bucketingId: hashId)
132+
//
133+
// var matched = false
134+
// for allocation in group.trafficAllocation {
135+
// if bucketValue < allocation.endOfRange {
136+
// matched = true
137+
// if allocation.entityId != experiment.id {
138+
// let info = LogMessage.userNotBucketedIntoExperimentInGroup(bucketingId, experiment.key, group.id)
139+
// reasons.addInfo(info)
140+
// return DecisionResponse(result: nil, reasons: reasons)
141+
// }
142+
//
143+
// let info = LogMessage.userBucketedIntoExperimentInGroup(bucketingId, experiment.key, group.id)
144+
// reasons.addInfo(info)
145+
// break
146+
// }
147+
// }
148+
//
149+
// if !matched {
150+
// let info = LogMessage.userNotBucketedIntoAnyExperimentInGroup(bucketingId, group.id)
151+
// reasons.addInfo(info)
152+
// return DecisionResponse(result: nil, reasons: reasons)
153+
// }
154+
// }
155+
//
156+
// let hashId = makeHashIdFromBucketingId(bucketingId: bucketingId, entityId: experiment.id)
157+
// let bucketValue = self.generateBucketValue(bucketingId: hashId)
158+
//
159+
// for allocation in trafficAllocation {
160+
// if bucketValue < allocation.endOfRange {
161+
// let info = LogMessage.userBucketedIntoEntity(allocation.entityId)
162+
// reasons.addInfo(info)
163+
// return DecisionResponse(result: allocation.entityId, reasons: reasons)
164+
// }
165+
// }
166+
// let info = LogMessage.userNotBucketedIntoAnyEntity
167+
// reasons.addInfo(info)
168+
// return DecisionResponse(result: nil, reasons: reasons)
169+
// }
170170

171171
func bucketToVariation(experiment: ExperimentCore,
172172
bucketingId: String) -> DecisionResponse<Variation> {

Sources/Implementation/DefaultDecisionService.swift

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,17 +104,18 @@ class DefaultDecisionService: OPTDecisionService {
104104

105105
let dummyEntityId = "$"
106106
let cmabTrafficAllocation = TrafficAllocation(entityId: dummyEntityId, endOfRange: cmab.trafficAllocation)
107-
let group = config.getGroup(id: experiment.id)
108-
let bucketedResponse = (bucketer as? DefaultBucketer)?.bucketToEntityId(bucketingId: bucketingId,
109-
experiment: experiment,
110-
trafficAllocation: [cmabTrafficAllocation],
111-
group: group)
112-
if let _reasons = bucketedResponse?.reasons {
113-
reasons.merge(_reasons)
114-
}
107+
var cmabExp = experiment
108+
// Replace the regular allocaion with cmab traffic allocation
109+
// to reuse the experiment bucketing logic
110+
cmabExp.trafficAllocation = [cmabTrafficAllocation]
111+
112+
let bucketedResponse = bucketer.bucketExperiment(config: config, experiment: cmabExp, bucketingId: bucketingId)
113+
114+
reasons.merge(bucketedResponse.reasons)
115115

116-
let entityId = bucketedResponse?.result ?? ""
116+
let entityId = bucketedResponse.result?.id
117117

118+
// this means the user is not in the cmab experiment
118119
if entityId != dummyEntityId {
119120
return DecisionResponse(result: nil, reasons: reasons)
120121
}
@@ -264,7 +265,7 @@ class DefaultDecisionService: OPTDecisionService {
264265
reasons.merge(audienceResponse.reasons)
265266

266267
if audienceResponse.result ?? false {
267-
// Acquire bucketingId .
268+
// Acquire bucketingId
268269
let bucketingId = getBucketingId(userId: userId, attributes: attributes)
269270

270271
if experiment.isCmab {

Tests/OptimizelyTests-Common/DecisionServiceTests_Experiments.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,14 @@ extension DecisionServiceTests_Experiments {
704704
}
705705
}
706706

707+
// MARK: - CMAB
708+
709+
extension DecisionServiceTests_Experiments {
710+
func testGetVairationWithCMAB() {
711+
712+
}
713+
}
714+
707715
// MARK: - Test getBucketingId()
708716

709717
extension DecisionServiceTests_Experiments {

0 commit comments

Comments
 (0)