improve speed of detect post vote manipulation detection #343

This commit is contained in:
rimu 2024-12-23 13:35:24 +13:00
parent 61fe30136a
commit b4382764f5

View file

@ -1251,10 +1251,16 @@ def get_task_session() -> Session:
return Session(bind=db.engine) return Session(bind=db.engine)
user2_cache = {}
def jaccard_similarity(user1_upvoted: set, user2_id: int): def jaccard_similarity(user1_upvoted: set, user2_id: int):
user2_upvoted_posts = ['post/' + str(id) for id in recently_upvoted_posts(user2_id)] if user2_id not in user2_cache:
user2_upvoted_replies = ['reply/' + str(id) for id in recently_upvoted_post_replies(user2_id)] user2_upvoted_posts = ['post/' + str(id) for id in recently_upvoted_posts(user2_id)]
user2_upvoted = set(user2_upvoted_posts + user2_upvoted_replies) user2_upvoted_replies = ['reply/' + str(id) for id in recently_upvoted_post_replies(user2_id)]
user2_cache[user2_id] = set(user2_upvoted_posts + user2_upvoted_replies)
user2_upvoted = user2_cache[user2_id]
if len(user2_upvoted) > 12: if len(user2_upvoted) > 12:
intersection = len(user1_upvoted.intersection(user2_upvoted)) intersection = len(user1_upvoted.intersection(user2_upvoted))