74 lines
2.4 KiB
Java
74 lines
2.4 KiB
Java
package com.navi.medici.strategy;
|
|
|
|
import com.navi.medici.context.LitmusContext;
|
|
import com.navi.medici.util.StrategyUtils;
|
|
import java.util.Map;
|
|
import java.util.Optional;
|
|
import java.util.function.Supplier;
|
|
import lombok.extern.log4j.Log4j2;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
@Component
|
|
@Log4j2
|
|
public class FlexibleRolloutStrategy implements Strategy {
|
|
|
|
protected static final String PERCENTAGE = "rollout";
|
|
protected static final String GROUP_ID = "groupId";
|
|
|
|
private final Supplier<String> randomGenerator;
|
|
|
|
public FlexibleRolloutStrategy() {
|
|
this.randomGenerator = () -> Math.random() * 100 + "";
|
|
}
|
|
|
|
|
|
@Override
|
|
public String getName() {
|
|
return "flexibleRollout";
|
|
}
|
|
|
|
@Override
|
|
public boolean isEnabled(Map<String, String> parameters) {
|
|
return false;
|
|
}
|
|
|
|
private Optional<String> resolveStickiness(String stickiness, LitmusContext context) {
|
|
switch (stickiness) {
|
|
case "userId":
|
|
return context.getUserId();
|
|
case "sessionId":
|
|
return context.getSessionId();
|
|
case "deviceId":
|
|
return context.getDeviceId();
|
|
case "appVersionCode":
|
|
return context.getAppVersionCode();
|
|
case "osType":
|
|
return context.getOsType();
|
|
case "random":
|
|
return Optional.of(randomGenerator.get());
|
|
case "default":
|
|
return Optional.of(context.getUserId()
|
|
.orElse(context.getSessionId().orElse(this.randomGenerator.get())));
|
|
default:
|
|
return context.getByName(stickiness);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public boolean isEnabled(Map<String, String> parameters, LitmusContext litmusContext) {
|
|
final String stickiness = getStickiness(parameters);
|
|
final Optional<String> stickinessId = resolveStickiness(stickiness, litmusContext);
|
|
final int percentage = StrategyUtils.getPercentage(parameters.get(PERCENTAGE));
|
|
final String groupId = parameters.getOrDefault(GROUP_ID, "");
|
|
|
|
return stickinessId
|
|
.map(stick -> StrategyUtils.getNormalizedNumber(stick, groupId))
|
|
.map(norm -> percentage > 0 && norm <= percentage)
|
|
.orElse(false);
|
|
}
|
|
|
|
private String getStickiness(Map<String, String> parameters) {
|
|
return parameters.getOrDefault("stickiness", "default");
|
|
}
|
|
}
|