-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
12789/feat/AB Testing #12788
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Sadashii
wants to merge
11
commits into
internetarchive:master
Choose a base branch
from
Sadashii:feat/ab-testing-architecture
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+354
−26
Open
12789/feat/AB Testing #12788
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
93152c5
feat: Add client and server-side A/B testing architecture
Sadashii f37803b
feat: Add URL-based overrides for active experiments in ?experiment_[…
Sadashii 341b3aa
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 84f1e1e
feat: add authorized_only parameter to A/B testing experiments
Sadashii f4cec9d
refactor: optimize experiments middleware, filter query overrides, an…
Sadashii 2e76bd8
refactor(ab-testing): improve type safety and cryptographic session v…
Sadashii e9e432a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 4d31692
refactor(ab-testing): unify request evaluation, secure json serializa…
Sadashii 8d6f6f1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 8876e68
refactor(security): unify HTML-safe JSON serialization and replace du…
Sadashii fd51484
Merge branch 'upstream/master' into feat/ab-testing-architecture
Sadashii File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import hashlib | ||
| from typing import Any | ||
|
|
||
| ACTIVE_EXPERIMENTS: dict[str, dict[str, Any]] = { | ||
| "AB_Testing": { | ||
| "variants": { | ||
| "control": 30, | ||
| "a": 35, | ||
| "b": 35, | ||
| }, | ||
| "authorized_only": False, | ||
| } | ||
| } | ||
|
|
||
|
|
||
| def get_variant(experiment_name: str, user_identifier: str | None, is_logged_in: bool = False) -> str: | ||
| """Deterministically assigns a user to a variant based on configured weights.""" | ||
| if not user_identifier or experiment_name not in ACTIVE_EXPERIMENTS: | ||
| return "control" | ||
|
|
||
| exp_config = ACTIVE_EXPERIMENTS[experiment_name] | ||
| if exp_config.get("authorized_only") and not is_logged_in: | ||
| return "control" | ||
|
|
||
| variants = exp_config.get("variants", {}) | ||
|
|
||
| hash_key = f"{experiment_name}-{user_identifier}".encode() | ||
| bucket = int(hashlib.md5(hash_key).hexdigest()[:8], 16) % 100 | ||
|
|
||
| cumulative_weight = 0 | ||
| for variant, weight in variants.items(): | ||
| cumulative_weight += weight | ||
| if bucket < cumulative_weight: | ||
| return variant | ||
|
|
||
| return "control" | ||
|
|
||
|
|
||
| def get_user_experiments( | ||
| user_identifier: str | None, | ||
| overrides: dict[str, str] | None = None, | ||
| is_logged_in: bool = False, | ||
| ) -> dict[str, str]: | ||
| """Evaluates all active experiments for a user, applying optional overrides.""" | ||
| experiments = {} | ||
| for exp, exp_config in ACTIVE_EXPERIMENTS.items(): | ||
| variants = exp_config.get("variants", {}) | ||
| override_key = f"experiment_{exp}" | ||
| if overrides and override_key in overrides and overrides[override_key] in variants: | ||
| experiments[exp] = overrides[override_key] | ||
| else: | ||
| experiments[exp] = get_variant(exp, user_identifier, is_logged_in=is_logged_in) | ||
| return experiments |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| from fastapi import Request | ||
| from starlette.types import ASGIApp, Receive, Scope, Send | ||
|
|
||
| from infogami import config | ||
| from openlibrary.core.experiments import get_user_experiments | ||
| from openlibrary.fastapi.auth import authenticate_user_from_cookie | ||
|
|
||
|
|
||
| class ABTestingMiddleware: | ||
| def __init__(self, app: ASGIApp) -> None: | ||
| self.app = app | ||
|
|
||
| async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: | ||
| if scope["type"] != "http": | ||
| await self.app(scope, receive, send) | ||
| return | ||
|
|
||
| request = Request(scope, receive=receive) | ||
| session_cookie_name = config.get("login_cookie_name", "session") | ||
| session_value = request.cookies.get(session_cookie_name) | ||
| user = authenticate_user_from_cookie(session_value) | ||
| is_logged_in = user is not None | ||
|
|
||
| user_id = session_value or (request.client.host if request.client else "127.0.0.1") | ||
|
|
||
| query_overrides = {k: v for k, v in request.query_params.items() if k.startswith("experiment_")} | ||
| request.state.experiments = get_user_experiments(user_id, overrides=query_overrides, is_logged_in=is_logged_in) if user_id else {} | ||
|
|
||
| await self.app(scope, receive, send) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| export function getExperiment(experimentName) { | ||
| if (typeof window === 'undefined') { | ||
| return 'control'; | ||
| } | ||
| return window.OL_EXPERIMENTS?.[experimentName] || 'control'; | ||
| } | ||
|
|
||
| if (typeof window !== 'undefined') { | ||
| window.getExperiment = getExperiment; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| from openlibrary.core.experiments import ACTIVE_EXPERIMENTS, get_user_experiments, get_variant | ||
|
|
||
|
|
||
| def test_get_variant_fallback(): | ||
| # Empty or None user identifier should fall back to control | ||
| assert get_variant("AB_Testing", "") == "control" | ||
| assert get_variant("AB_Testing", None) == "control" | ||
|
|
||
| # Non-existent experiment should fall back to control | ||
| assert get_variant("Non_Existent", "user123") == "control" | ||
|
|
||
|
|
||
| def test_get_variant_distribution(): | ||
| # Evaluate distribution across a reasonable number of users | ||
| variants_seen = set() | ||
| for i in range(100): | ||
| variant = get_variant("AB_Testing", f"user_{i}") | ||
| assert variant in ACTIVE_EXPERIMENTS["AB_Testing"]["variants"] | ||
| variants_seen.add(variant) | ||
|
|
||
| # Ensure all variants are allocated at least once (probabilistically guaranteed for N=100) | ||
| assert len(variants_seen) > 1 | ||
| assert "control" in variants_seen | ||
| assert "a" in variants_seen | ||
| assert "b" in variants_seen | ||
|
|
||
|
|
||
| def test_get_user_experiments(): | ||
| experiments = get_user_experiments("user_456") | ||
| assert "AB_Testing" in experiments | ||
| assert experiments["AB_Testing"] in ACTIVE_EXPERIMENTS["AB_Testing"]["variants"] | ||
|
|
||
|
|
||
| def test_get_user_experiments_overrides(): | ||
| # Valid override should change the variant | ||
| overrides = {"experiment_AB_Testing": "b"} | ||
| experiments = get_user_experiments("user_456", overrides=overrides) | ||
| assert experiments["AB_Testing"] == "b" | ||
|
|
||
| # Invalid override group/variant should be ignored | ||
| overrides = {"experiment_AB_Testing": "invalid_group"} | ||
| experiments = get_user_experiments("user_456", overrides=overrides) | ||
| assert experiments["AB_Testing"] != "invalid_group" | ||
| assert experiments["AB_Testing"] in ACTIVE_EXPERIMENTS["AB_Testing"]["variants"] | ||
|
|
||
| # Invalid override key/name format should be ignored | ||
| overrides = {"AB_Testing": "b"} | ||
| experiments = get_user_experiments("user_456", overrides=overrides) | ||
| assert experiments["AB_Testing"] in ACTIVE_EXPERIMENTS["AB_Testing"]["variants"] | ||
|
|
||
|
|
||
| def test_authorized_only_experiment(): | ||
| ACTIVE_EXPERIMENTS["Auth_Only_Testing"] = { | ||
| "variants": { | ||
| "control": 25, | ||
| "treatment": 75, | ||
| }, | ||
| "authorized_only": True, | ||
| } | ||
| try: | ||
| # If not logged in, should always fall back to control | ||
| for i in range(100): | ||
| assert get_variant("Auth_Only_Testing", f"user_{i}", is_logged_in=False) == "control" | ||
|
|
||
| # If logged in, should distribute into variants | ||
| variants_seen = set() | ||
| for i in range(100): | ||
| variant = get_variant("Auth_Only_Testing", f"user_{i}", is_logged_in=True) | ||
| assert variant in ["control", "treatment"] | ||
| variants_seen.add(variant) | ||
|
|
||
| assert len(variants_seen) > 1 | ||
| finally: | ||
| ACTIVE_EXPERIMENTS.pop("Auth_Only_Testing", None) | ||
|
|
||
|
|
||
| def test_get_user_experiments_authorized_only(): | ||
| ACTIVE_EXPERIMENTS["Auth_Only_Testing"] = { | ||
| "variants": { | ||
| "control": 25, | ||
| "treatment": 75, | ||
| }, | ||
| "authorized_only": True, | ||
| } | ||
| try: | ||
| # If not logged in, should get control | ||
| experiments = get_user_experiments("user_456", is_logged_in=False) | ||
| assert experiments["Auth_Only_Testing"] == "control" | ||
|
|
||
| # If logged in, should evaluate variant | ||
| experiments_logged_in = get_user_experiments("user_456", is_logged_in=True) | ||
| assert experiments_logged_in["Auth_Only_Testing"] in ["control", "treatment"] | ||
| finally: | ||
| ACTIVE_EXPERIMENTS.pop("Auth_Only_Testing", None) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.