|
| 1 | +package cloud.eppo; |
| 2 | + |
| 3 | +import cloud.eppo.ufc.dto.BanditParameters; |
| 4 | +import cloud.eppo.ufc.dto.FlagConfig; |
| 5 | +import cloud.eppo.ufc.dto.FlagConfigResponse; |
| 6 | +import cloud.eppo.ufc.dto.adapters.EppoModule; |
| 7 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 8 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 9 | +import java.util.concurrent.ConcurrentHashMap; |
| 10 | +import org.slf4j.Logger; |
| 11 | +import org.slf4j.LoggerFactory; |
| 12 | + |
| 13 | +public class ConfigurationStore { |
| 14 | + private static final Logger log = LoggerFactory.getLogger(ConfigurationStore.class); |
| 15 | + private final ObjectMapper mapper = new ObjectMapper().registerModule(EppoModule.eppoModule()); |
| 16 | + |
| 17 | + private ConcurrentHashMap<String, FlagConfig> flags; |
| 18 | + private ConcurrentHashMap<String, BanditParameters> banditParameters; // TODO: bandit stuff |
| 19 | + // TODO: another map of bandit flag variations to handle no action case |
| 20 | + static ConfigurationStore instance = null; |
| 21 | + |
| 22 | + public ConfigurationStore() { |
| 23 | + // TODO: handle caching |
| 24 | + } |
| 25 | + |
| 26 | + public void setFlagsFromJsonString(String jsonString) throws JsonProcessingException { |
| 27 | + FlagConfigResponse config = mapper.readValue(jsonString, FlagConfigResponse.class); |
| 28 | + if (config == null || config.getFlags() == null) { |
| 29 | + log.warn("Flags missing in configuration response"); |
| 30 | + flags = new ConcurrentHashMap<>(); |
| 31 | + } else { |
| 32 | + // TODO: atomic flags to prevent clobbering like android does |
| 33 | + // Record that flags were set from a response so we don't later clobber them with a |
| 34 | + // slow cache read |
| 35 | + flags = new ConcurrentHashMap<>(config.getFlags()); |
| 36 | + log.debug("Loaded " + flags.size() + " flags from configuration response"); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + public BanditParameters getBanditParameters(String banditKey) { |
| 41 | + return this.banditParameters.get(banditKey); |
| 42 | + } |
| 43 | + |
| 44 | + public FlagConfig getFlag(String flagKey) { |
| 45 | + if (flags == null) { |
| 46 | + log.warn("Request for flag " + flagKey + " before flags have been loaded"); |
| 47 | + return null; |
| 48 | + } else if (flags.isEmpty()) { |
| 49 | + log.warn("Request for flag " + flagKey + " with empty flags"); |
| 50 | + } |
| 51 | + return flags.get(flagKey); |
| 52 | + } |
| 53 | +} |
0 commit comments