All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.getunleash.strategy.FlexibleRolloutStrategy Maven / Gradle / Ivy

There is a newer version: 9.2.4
Show newest version
package io.getunleash.strategy;

import io.getunleash.UnleashContext;
import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;

public class FlexibleRolloutStrategy implements Strategy {
    protected static final String PERCENTAGE = "rollout";
    protected static final String GROUP_ID = "groupId";

    private Supplier randomGenerator;

    public FlexibleRolloutStrategy() {
        this.randomGenerator = () -> Math.random() * 10000 + "";
    }

    public FlexibleRolloutStrategy(Supplier randomGenerator) {
        this.randomGenerator = randomGenerator;
    }

    @Override
    public String getName() {
        return "flexibleRollout";
    }

    @Override
    public boolean isEnabled(Map parameters) {
        return false;
    }

    private Optional resolveStickiness(String stickiness, UnleashContext context) {
        switch (stickiness) {
            case "userId":
                return context.getUserId();
            case "sessionId":
                return context.getSessionId();
            case "random":
                return Optional.of(randomGenerator.get());
            case "default":
                String value =
                        context.getUserId()
                                .orElse(context.getSessionId().orElse(this.randomGenerator.get()));
                return Optional.of(value);
            default:
                return context.getByName(stickiness);
        }
    }

    @Override
    public boolean isEnabled(Map parameters, UnleashContext unleashContext) {
        final String stickiness = getStickiness(parameters);
        final Optional stickinessId = resolveStickiness(stickiness, unleashContext);
        final int percentage = StrategyUtils.getPercentage(parameters.get(PERCENTAGE));
        final String groupId = parameters.getOrDefault(GROUP_ID, "");

        return stickinessId
                .map(stick -> StrategyUtils.getNormalizedNumber(stick, groupId, 0))
                .map(norm -> percentage > 0 && norm <= percentage)
                .orElse(false);
    }

    private String getStickiness(Map parameters) {
        return parameters.getOrDefault("stickiness", "default");
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy