Skip to content

Commit

Permalink
Merge pull request #90 from reddit/get_exp_metrics
Browse files Browse the repository at this point in the history
Update `get_experiment()` to use decider shim & remove last decider bindings usage
  • Loading branch information
mrlevitas authored Jan 12, 2023
2 parents 02a4572 + 47c39fd commit 171b5fe
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 48 deletions.
2 changes: 1 addition & 1 deletion docs/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ docutils==0.16
Jinja2==2.11.2
MarkupSafe==1.1.1
pydocstyle==5.1.1
reddit-decider==1.2.30
reddit-decider==1.2.31
reddit-edgecontext==1.0.0a3
Sphinx==3.4.0
sphinx-autodoc-typehints==1.11.1
Expand Down
77 changes: 32 additions & 45 deletions reddit_decider/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
from rust_decider import DeciderException
from rust_decider import Decision
from rust_decider import FeatureNotFoundException
from rust_decider import make_ctx
from rust_decider import ValueTypeMismatchException
from typing_extensions import Literal

Expand Down Expand Up @@ -187,16 +186,6 @@ def __init__(
else:
self._event_logger = DebugLogger()

def _get_decider(self) -> Optional[T]:
if self._internal is not None:
return self._internal.get_decider()

return None

def _get_ctx(self) -> Any:
context_fields = self._decider_context.to_dict()
return make_ctx(context_fields)

def _send_expose(self, event: str, exposure_fields: dict) -> None:
event_fields = deepcopy(exposure_fields)
try:
Expand Down Expand Up @@ -379,30 +368,31 @@ def expose(
if variant_name is None or variant_name == "":
return

decider = self._get_decider()
if decider is None:
if self._internal is None:
logger.error("RustDecider is None--did not initialize.")
return

experiment = decider.get_experiment(experiment_name)
error = experiment.err()
if error:
logger.warning(f"Encountered error in decider.get_experiment(): {error}")
try:
feature = self._internal.get_feature(experiment_name)
except FeatureNotFoundException as exc:
warnings.warn(str(exc))
return
except DeciderException as exc:
logger.info(str(exc))
return

event_context_fields = self._decider_context.to_event_dict()
event_context_fields.update(exposure_kwargs or {})
event_fields = deepcopy(event_context_fields)

exp_dict = experiment.val()

experiment = ExperimentConfig(
id=int(exp_dict.get("id", 0)),
name=exp_dict.get("name"),
version=str(exp_dict.get("version")),
bucket_val=exp_dict.get("variant_set", {}).get("bucket_val"),
start_ts=exp_dict.get("variant_set", {}).get("start_ts"),
stop_ts=exp_dict.get("variant_set", {}).get("stop_ts"),
owner=exp_dict.get("owner"),
id=feature.id,
name=feature.name,
version=str(feature.version),
bucket_val=feature.bucket_val,
start_ts=feature.start_ts,
stop_ts=feature.stop_ts,
owner=feature.owner,
)

self._event_logger.log(
Expand Down Expand Up @@ -847,31 +837,28 @@ def get_experiment(self, experiment_name: str) -> Optional[ExperimentConfig]:
:return: an :py:class:`~reddit_decider.ExperimentConfig` `dataclass <https://github.com/reddit/experiments.py/blob/develop/reddit_decider/__init__.py#L44>`_
representation of an experiment if found, else :code:`None`.
"""
decider = self._get_decider()
if decider is None:
if self._internal is None:
logger.error("RustDecider is None--did not initialize.")
return None

experiment = decider.get_experiment(experiment_name)
error = experiment.err()
if error:
# sending to debug logger to avoid printing "Feature x not found." logs
logger.debug(f"Encountered error in decider.get_experiment(): {error}")
try:
feature = self._internal.get_feature(experiment_name)
except FeatureNotFoundException as exc:
warnings.warn(str(exc))
return None

exp_dict = experiment.val()

if exp_dict is None:
except DeciderException as exc:
logger.info(str(exc))
return None

return ExperimentConfig(
id=int(exp_dict.get("id", 0)),
name=exp_dict.get("name"),
version=str(exp_dict.get("version")),
bucket_val=exp_dict.get("variant_set", {}).get("bucket_val"),
start_ts=exp_dict.get("variant_set", {}).get("start_ts"),
stop_ts=exp_dict.get("variant_set", {}).get("stop_ts"),
owner=exp_dict.get("owner"),
emit_event=bool(exp_dict.get("emit_event")),
id=feature.id,
name=feature.name,
version=str(feature.version),
bucket_val=feature.bucket_val,
start_ts=feature.start_ts,
stop_ts=feature.stop_ts,
owner=feature.owner,
emit_event=feature.emit_event,
)


Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
-r requirements-transitive.txt
baseplate==2.0.0a1
black==21.4b2
reddit-decider==1.2.30
reddit-decider==1.2.31
flake8==3.9.1
mypy==0.790
pyramid==2.0 # required for `from baseplate.frameworks.pyramid import BaseplateRequest` which calls `import pyramid.events`
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
install_requires=[
"baseplate>=2.0.0a1,<3.0",
"reddit-edgecontext>=1.0.0a3,<2.0",
"reddit-decider~=1.2.30",
"reddit-decider~=1.2.31",
"typing_extensions>=3.10.0.0,<5.0",
],
package_data={"reddit_experiments": ["py.typed"]},
Expand Down

0 comments on commit 171b5fe

Please sign in to comment.