Skip to content

Commit

Permalink
rolling out selected sources
Browse files Browse the repository at this point in the history
  • Loading branch information
ledovsky committed May 25, 2024
1 parent b22b55a commit 3d85214
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
19 changes: 18 additions & 1 deletion src/recommendations/candidates_ab.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Dict, List

from sqlalchemy import text

from src.database import fetch_all
Expand Down Expand Up @@ -89,11 +91,26 @@ async def get_selected_sources(
user_id: int,
limit: int = 10,
exclude_meme_ids: list[int] = [],
):
) -> List[Dict]:
"""Implements a test version of an algotighm which takes best memes from
some selected sources
"""

query_lang = f"""
SELECT language_code
FROM user_language
WHERE user_id = {user_id}
"""

res = await fetch_all(text(query_lang))
if not len(res):
return []
print(res)
is_lang_ru_en = all([row["language_code"] in ("ru", "en") for row in res])

if not is_lang_ru_en:
return []

query = f"""
SELECT
M.id
Expand Down
17 changes: 7 additions & 10 deletions src/recommendations/meme_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,9 @@ async def generate_cold_start_recommendations(user_id, limit=10):

candidates = []

# AB test
if user_id % 100 < 50:
candidates = await get_selected_sources(
user_id, limit=limit, exclude_meme_ids=meme_ids_in_queue
)
candidates = await get_selected_sources(
user_id, limit=limit, exclude_meme_ids=meme_ids_in_queue
)

if len(candidates) == 0:
candidates = await get_best_memes_from_each_source(
Expand Down Expand Up @@ -82,11 +80,10 @@ async def generate_recommendations(user_id, limit):
candidates = []

if user_info["nmemes_sent"] < 30:
# AB test
if user_id % 100 < 50:
candidates = await get_selected_sources(
user_id, limit=limit, exclude_meme_ids=meme_ids_in_queue
)
candidates = await get_selected_sources(
user_id, limit=limit, exclude_meme_ids=meme_ids_in_queue
)

if len(candidates) == 0:
candidates = await get_best_memes_from_each_source(
user_id, limit=limit, exclude_meme_ids=meme_ids_in_queue
Expand Down
13 changes: 7 additions & 6 deletions tests/recommendations/test_selected_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async def conn():

await conn.execute(
insert(user),
[{'id': 1, 'type': "user"}, {'id': 51, 'type': "user"}]
[{'id': 1, 'type': 'user'}, {'id': 21, 'type': 'user'}]
)
await conn.execute(
insert(meme_source),
Expand Down Expand Up @@ -46,14 +46,15 @@ async def conn():
insert(user_language),
[
{'user_id': 1, 'language_code': 'ru', 'created_at': datetime(2024, 1, 1)},
{'user_id': 51, 'language_code': 'ru', 'created_at': datetime(2024, 1, 1)}
{'user_id': 21, 'language_code': 'en', 'created_at': datetime(2024, 1, 1)},
{'user_id': 21, 'language_code': 'es', 'created_at': datetime(2024, 1, 1)}
]
)
await conn.execute(
insert(user_meme_reaction),
[
{'user_id': 1, 'meme_id': seen_meme, 'reaction_id': 1, 'recommended_by': '111', 'sent_at': datetime(2024, 1, 1)},
{'user_id': 51, 'meme_id': seen_meme, 'reaction_id': 1, 'recommended_by': '111', 'sent_at': datetime(2024, 1, 1)}
{'user_id': 21, 'meme_id': seen_meme, 'reaction_id': 1, 'recommended_by': '111', 'sent_at': datetime(2024, 1, 1)}
]
)

Expand All @@ -78,7 +79,7 @@ async def conn():


@pytest.mark.asyncio
async def test_random_best(conn: AsyncConnection):
async def test_selected_sources(conn: AsyncConnection):
recs = await get_selected_sources(1, 10)
assert len(recs) == 2

Expand All @@ -92,8 +93,8 @@ async def test_meme_queue(conn: AsyncConnection):
assert len(recs) == 2
assert recs[0]['recommended_by'] == 'selected_sources_240513'

user_id = 51
user_id = 21
await generate_cold_start_recommendations(user_id)
queue_key = redis.get_meme_queue_key(user_id)
recs = await redis.get_all_memes_in_queue_by_key(queue_key)
assert recs[0]['recommended_by'] != 'selected_sources_240513'
assert len(recs) == 0

0 comments on commit 3d85214

Please sign in to comment.