From e0a9db9bfe422df92521a4594cc9dd9d4704aba3 Mon Sep 17 00:00:00 2001 From: Daniil Okhlopkov <5613295+ohld@users.noreply.github.com> Date: Sun, 15 Sep 2024 15:46:16 +0100 Subject: [PATCH 1/2] add recently_liked --- src/recommendations/candidates.py | 49 +++++++++++++++++++++++++++++++ src/recommendations/meme_queue.py | 4 ++- 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/recommendations/candidates.py b/src/recommendations/candidates.py index 9f0a7bf..90f5436 100644 --- a/src/recommendations/candidates.py +++ b/src/recommendations/candidates.py @@ -645,6 +645,54 @@ async def get_fast_dopamine( return res +async def get_recently_liked( + user_id: int, + limit: int = 10, + exclude_meme_ids: list[int] = [], +): + query = f""" + WITH EVENTS AS ( + SELECT * + FROM user_meme_reaction UMR + WHERE reaction_id = 1 + ORDER BY sent_at DESC + LIMIT 10000 + ) + , CANDIDATES AS ( + SELECT meme_id AS id + FROM EVENTS + GROUP BY meme_id + HAVING COUNT(*) > 1 + ORDER BY COUNT(*) DESC + ) + + SELECT + M.id + , M.type, M.telegram_file_id, M.caption + , 'recently_liked' AS recommended_by + + FROM CANDIDATES C + INNER JOIN meme M + ON M.id = C.id + + INNER JOIN user_language L + ON L.language_code = M.language_code + AND L.user_id = {user_id} + + LEFT JOIN user_meme_reaction R + ON R.meme_id = M.id + AND R.user_id = {user_id} + + WHERE 1=1 + AND M.status = 'ok' + AND R.meme_id IS NULL + {exclude_meme_ids_sql_filter(exclude_meme_ids)} + LIMIT {limit} + """ + res = await fetch_all(text(query)) + return res + + class CandidatesRetriever: """CandidatesRetriever class is used for unit testing""" @@ -656,6 +704,7 @@ class CandidatesRetriever: "selected_sources": get_selected_sources, "best_memes_from_each_source": get_best_memes_from_each_source, "like_spread_and_recent_memes": like_spread_and_recent_memes, + "recently_liked": get_recently_liked, } async def get_candidates( diff --git a/src/recommendations/meme_queue.py b/src/recommendations/meme_queue.py index bcc6e4f..936ec65 100644 --- a/src/recommendations/meme_queue.py +++ b/src/recommendations/meme_queue.py @@ -214,7 +214,8 @@ async def get_candidates(user_id, limit): "uploaded_memes": 0.2, "fast_dopamine": 0.2, "best_memes_from_each_source": 0.2, - "lr_smoothed": 0.4, + "lr_smoothed": 0.2, + "recently_liked": 0.2, } engines = [ @@ -222,6 +223,7 @@ async def get_candidates(user_id, limit): "fast_dopamine", "best_memes_from_each_source", "lr_smoothed", + "recently_liked", ] candidates_dict = await retriever.get_candidates_dict( engines, user_id, limit, exclude_mem_ids=meme_ids_in_queue From 0a60dffb833dcb58ac76ec715a6837337f4c3c39 Mon Sep 17 00:00:00 2001 From: Daniil Okhlopkov <5613295+ohld@users.noreply.github.com> Date: Sun, 15 Sep 2024 15:46:21 +0100 Subject: [PATCH 2/2] run tests in CI too --- .github/workflows/linters.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/linters.yml b/.github/workflows/linters.yml index 3fa8dca..2e09a73 100644 --- a/.github/workflows/linters.yml +++ b/.github/workflows/linters.yml @@ -37,6 +37,6 @@ jobs: # - name: Lint with mypy # run: | # mypy src tests - # - name: Run tests - # run: | - # pytest + - name: Run tests + run: | + pytest