Skip to content

Commit

Permalink
hide custom leaderboard size behind a feature flag (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsunyoku authored Sep 11, 2024
1 parent 4a96152 commit 28e0336
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,5 @@ REDIS_DB=0
REDIS_USE_SSL=false
SCORE_SUBMISSION_ROUTING_KEYS=score_submission
PULL_SECRETS_FROM_VAULT=
LEADERBOARD_SIZE=100
AMPLITUDE_DEPLOYMENT_KEY=
37 changes: 37 additions & 0 deletions app/adapters/feature_flags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from __future__ import annotations

import logging

from amplitude_experiment import Experiment
from amplitude_experiment import User

import config

experiment = Experiment.initialize_local(config.AMPLITUDE_DEPLOYMENT_KEY)


def is_feature_enabled(
feature_name: str,
*,
user_id: str | None = None,
device_id: str | None = None,
) -> bool:
try:
if not experiment.poller.is_running:
experiment.start()

if device_id is None and user_id is None:
user_id = "1xx"

user = User(device_id=device_id, user_id=user_id) # type: ignore[unused-ignore]
variant = experiment.evaluate_v2(user, {feature_name}).get(feature_name)
if variant is None:
return False

return variant.value == "on" # type: ignore[no-any-return]
except Exception:
logging.exception(
"Failed to retrieve experiment",
extra={"feature_name": feature_name},
)
return False
5 changes: 5 additions & 0 deletions app/api/leaderboards.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from fastapi import Query
from fastapi import Response

import app.adapters.feature_flags
import app.state
import app.usecases
import config
Expand Down Expand Up @@ -110,6 +111,10 @@ async def get_leaderboard(
if (
user.privileges & Privileges.USER_PREMIUM
and user.leaderboard_size is not None
and app.adapters.feature_flags.is_feature_enabled(
"use_custom_leaderboard_size",
user_id=str(user.id),
)
):
leaderboard_size = user.leaderboard_size
else:
Expand Down
2 changes: 2 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,5 @@
)

LEADERBOARD_SIZE = config("LEADERBOARD_SIZE", cast=int, default=100)

AMPLITUDE_DEPLOYMENT_KEY = config("AMPLITUDE_DEPLOYMENT_KEY")
3 changes: 3 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ ignore_missing_imports = True

[mypy-py3rijndael.*]
ignore_missing_imports = True

[mypy-amplitude_experiment.*]
ignore_missing_imports = True
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
aio-pika
aiobotocore
amplitude-experiment
bcrypt
cryptography
databases[asyncmy]
Expand Down

0 comments on commit 28e0336

Please sign in to comment.