Add payload functionality to our experimentation framework (#3506)
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
This commit is contained in:
@@ -9,6 +9,7 @@ class BaseExperimentationProvider(ABC):
|
|||||||
# feature_name -> distinct_id -> result
|
# feature_name -> distinct_id -> result
|
||||||
result_map: dict[str, dict[str, bool]] = {}
|
result_map: dict[str, dict[str, bool]] = {}
|
||||||
variant_map: dict[str, dict[str, str | None]] = {}
|
variant_map: dict[str, dict[str, str | None]] = {}
|
||||||
|
payload_map: dict[str, dict[str, str | None]] = {}
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def is_feature_enabled(self, feature_name: str, distinct_id: str, properties: dict | None = None) -> bool:
|
def is_feature_enabled(self, feature_name: str, distinct_id: str, properties: dict | None = None) -> bool:
|
||||||
@@ -29,6 +30,10 @@ class BaseExperimentationProvider(ABC):
|
|||||||
def get_value(self, feature_name: str, distinct_id: str, properties: dict | None = None) -> str | None:
|
def get_value(self, feature_name: str, distinct_id: str, properties: dict | None = None) -> str | None:
|
||||||
"""Get the value of a feature."""
|
"""Get the value of a feature."""
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def get_payload(self, feature_name: str, distinct_id: str, properties: dict | None = None) -> str | None:
|
||||||
|
"""Get the payload for a feature flag if it exists."""
|
||||||
|
|
||||||
def get_value_cached(self, feature_name: str, distinct_id: str, properties: dict | None = None) -> str | None:
|
def get_value_cached(self, feature_name: str, distinct_id: str, properties: dict | None = None) -> str | None:
|
||||||
"""Get the value of a feature."""
|
"""Get the value of a feature."""
|
||||||
if feature_name not in self.variant_map:
|
if feature_name not in self.variant_map:
|
||||||
@@ -40,6 +45,17 @@ class BaseExperimentationProvider(ABC):
|
|||||||
LOG.info("Feature is found", flag=feature_name, distinct_id=distinct_id, variant=variant)
|
LOG.info("Feature is found", flag=feature_name, distinct_id=distinct_id, variant=variant)
|
||||||
return self.variant_map[feature_name][distinct_id]
|
return self.variant_map[feature_name][distinct_id]
|
||||||
|
|
||||||
|
def get_payload_cached(self, feature_name: str, distinct_id: str, properties: dict | None = None) -> str | None:
|
||||||
|
"""Get the payload for a feature flag if it exists."""
|
||||||
|
if feature_name not in self.payload_map:
|
||||||
|
self.payload_map[feature_name] = {}
|
||||||
|
if distinct_id not in self.payload_map[feature_name]:
|
||||||
|
payload = self.get_payload(feature_name, distinct_id, properties)
|
||||||
|
self.payload_map[feature_name][distinct_id] = payload
|
||||||
|
if payload:
|
||||||
|
LOG.info("Feature payload is found", flag=feature_name, distinct_id=distinct_id)
|
||||||
|
return self.payload_map[feature_name][distinct_id]
|
||||||
|
|
||||||
|
|
||||||
class NoOpExperimentationProvider(BaseExperimentationProvider):
|
class NoOpExperimentationProvider(BaseExperimentationProvider):
|
||||||
def is_feature_enabled(self, feature_name: str, distinct_id: str, properties: dict | None = None) -> bool:
|
def is_feature_enabled(self, feature_name: str, distinct_id: str, properties: dict | None = None) -> bool:
|
||||||
@@ -47,3 +63,6 @@ class NoOpExperimentationProvider(BaseExperimentationProvider):
|
|||||||
|
|
||||||
def get_value(self, feature_name: str, distinct_id: str, properties: dict | None = None) -> str | None:
|
def get_value(self, feature_name: str, distinct_id: str, properties: dict | None = None) -> str | None:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
def get_payload(self, feature_name: str, distinct_id: str, properties: dict | None = None) -> str | None:
|
||||||
|
return None
|
||||||
|
|||||||
Reference in New Issue
Block a user