Skip to content

Commit

Permalink
add random filter to get_random to speed up
Browse files Browse the repository at this point in the history
  • Loading branch information
Adibuer-lab committed Jun 27, 2024
1 parent 20e44d7 commit b0e455f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/users/src/users_service/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class User:
name: Optional[str] = None
username: Optional[str] = "0"
claimed_user: Optional[int] = 0
email: Optional[str] = None
email: Optional[str] = "email"
primary_persona: Optional[str] = "0"
first_name: str = "first_name"
last_name: str = "last_name"
Expand Down
21 changes: 16 additions & 5 deletions src/users/src/users_service/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,21 @@ def get_user_by_identity_id(identity_id: str) -> Optional[User]:
current_app.logger.error(f"Error getting user by identity_id: {str(e)}")
return None

random_retry = 0
def get_random_user(count: int) -> List[User]:
unclaimed_users = get_unclaimed_users()
current_app.logger.debug(f"Found {len(unclaimed_users)} unclaimed users")
if not unclaimed_users:
global random_retry
if random_retry < 3:
random_retry += 1
current_app.logger.warning("No unclaimed users found. Retrying...")
return get_random_user(count)
else:
current_app.logger.error("No unclaimed users found after multiple retries.")
return []
return random.sample(unclaimed_users, min(count, len(unclaimed_users)))

def get_unclaimed_users(query: Optional[Dict[str, Any]] = None) -> List[User]:
current_app.logger.debug(f"Querying for unclaimed users with query: {query}")
try:
Expand All @@ -258,6 +273,7 @@ def get_unclaimed_users(query: Optional[Dict[str, Any]] = None) -> List[User]:
else:
# Use claimed-index if neither primaryPersona nor ageRange is provided
index_name = 'claimed-index'
filter_expression = Attr('age').eq(random.randrange(18, 75))
key_condition_expression = Key('claimed_user').eq(0)

query_params = {
Expand Down Expand Up @@ -289,11 +305,6 @@ def get_unclaimed_users(query: Optional[Dict[str, Any]] = None) -> List[User]:
current_app.logger.error(f"Error getting unclaimed users: {str(e)}")
return []

def get_random_user(count: int) -> List[User]:
unclaimed_users = get_unclaimed_users()
current_app.logger.debug(f"Found {len(unclaimed_users)} unclaimed users")
return random.sample(unclaimed_users, min(count, len(unclaimed_users)))


def verify_and_update_phone(user_id: str, phone_number: str) -> Optional[User]:
user, _ = upsert_user({"phone_number": phone_number}, user_id=user_id)
Expand Down

0 comments on commit b0e455f

Please sign in to comment.