Disable comically inefficient book count comparisons

Oops! Hopefully I can restore these in a way that doesn't break at even
a moderate scale
This commit is contained in:
Mouse Reeve 2021-08-06 08:43:05 -07:00
parent 5f10e7535b
commit d126d7ba91
2 changed files with 31 additions and 29 deletions

View file

@ -19,7 +19,7 @@ class SuggestedUsers(RedisStore):
def get_rank(self, obj): def get_rank(self, obj):
"""get computed rank""" """get computed rank"""
return obj.mutuals + (1.0 - (1.0 / (obj.shared_books + 1))) return obj.mutuals # + (1.0 - (1.0 / (obj.shared_books + 1)))
def store_id(self, user): # pylint: disable=no-self-use def store_id(self, user): # pylint: disable=no-self-use
"""the key used to store this user's recs""" """the key used to store this user's recs"""
@ -31,7 +31,7 @@ class SuggestedUsers(RedisStore):
"""calculate mutuals count and shared books count from rank""" """calculate mutuals count and shared books count from rank"""
return { return {
"mutuals": math.floor(rank), "mutuals": math.floor(rank),
"shared_books": int(1 / (-1 * (rank % 1 - 1))) - 1, # "shared_books": int(1 / (-1 * (rank % 1 - 1))) - 1,
} }
def get_objects_for_store(self, store): def get_objects_for_store(self, store):
@ -95,7 +95,7 @@ class SuggestedUsers(RedisStore):
logger.exception(err) logger.exception(err)
continue continue
user.mutuals = counts["mutuals"] user.mutuals = counts["mutuals"]
user.shared_books = counts["shared_books"] # user.shared_books = counts["shared_books"]
results.append(user) results.append(user)
return results return results
@ -103,7 +103,9 @@ class SuggestedUsers(RedisStore):
def get_annotated_users(viewer, *args, **kwargs): def get_annotated_users(viewer, *args, **kwargs):
"""Users, annotated with things they have in common""" """Users, annotated with things they have in common"""
return ( return (
models.User.objects.filter(discoverable=True, is_active=True, *args, **kwargs) models.User.objects.filter(
discoverable=True, is_active=True, bookwyrm_user=True, *args, **kwargs
)
.exclude(Q(id__in=viewer.blocks.all()) | Q(blocks=viewer)) .exclude(Q(id__in=viewer.blocks.all()) | Q(blocks=viewer))
.annotate( .annotate(
mutuals=Count( mutuals=Count(
@ -115,16 +117,16 @@ def get_annotated_users(viewer, *args, **kwargs):
), ),
distinct=True, distinct=True,
), ),
shared_books=Count( # shared_books=Count(
"shelfbook", # "shelfbook",
filter=Q( # filter=Q(
~Q(id=viewer.id), # ~Q(id=viewer.id),
shelfbook__book__parent_work__in=[ # shelfbook__book__parent_work__in=[
s.book.parent_work for s in viewer.shelfbook_set.all() # s.book.parent_work for s in viewer.shelfbook_set.all()
], # ],
), # ),
distinct=True, # distinct=True,
), # ),
) )
) )
@ -162,18 +164,18 @@ def update_suggestions_on_unfollow(sender, instance, **kwargs):
rerank_user_task.delay(instance.user_object.id, update_only=False) rerank_user_task.delay(instance.user_object.id, update_only=False)
@receiver(signals.post_save, sender=models.ShelfBook) # @receiver(signals.post_save, sender=models.ShelfBook)
@receiver(signals.post_delete, sender=models.ShelfBook) # @receiver(signals.post_delete, sender=models.ShelfBook)
# pylint: disable=unused-argument # # pylint: disable=unused-argument
def update_rank_on_shelving(sender, instance, *args, **kwargs): # def update_rank_on_shelving(sender, instance, *args, **kwargs):
"""when a user shelves or unshelves a book, re-compute their rank""" # """when a user shelves or unshelves a book, re-compute their rank"""
# if it's a local user, re-calculate who is rec'ed to them # # if it's a local user, re-calculate who is rec'ed to them
if instance.user.local: # if instance.user.local:
rerank_suggestions_task.delay(instance.user.id) # rerank_suggestions_task.delay(instance.user.id)
#
# if the user is discoverable, update their rankings # # if the user is discoverable, update their rankings
if instance.user.discoverable: # if instance.user.discoverable:
rerank_user_task.delay(instance.user.id) # rerank_user_task.delay(instance.user.id)
@receiver(signals.post_save, sender=models.User) @receiver(signals.post_save, sender=models.User)

View file

@ -29,7 +29,7 @@ class SuggestedUsers(TestCase):
Mock = namedtuple("AnnotatedUserMock", ("mutuals", "shared_books")) Mock = namedtuple("AnnotatedUserMock", ("mutuals", "shared_books"))
annotated_user_mock = Mock(3, 27) annotated_user_mock = Mock(3, 27)
rank = suggested_users.get_rank(annotated_user_mock) rank = suggested_users.get_rank(annotated_user_mock)
self.assertEqual(rank, 3.9642857142857144) self.assertEqual(rank, 3) # 3.9642857142857144)
def test_store_id(self, *_): def test_store_id(self, *_):
"""redis key generation""" """redis key generation"""
@ -42,7 +42,7 @@ class SuggestedUsers(TestCase):
"""reverse the rank computation to get the mutuals and shared books counts""" """reverse the rank computation to get the mutuals and shared books counts"""
counts = suggested_users.get_counts_from_rank(3.9642857142857144) counts = suggested_users.get_counts_from_rank(3.9642857142857144)
self.assertEqual(counts["mutuals"], 3) self.assertEqual(counts["mutuals"], 3)
self.assertEqual(counts["shared_books"], 27) # self.assertEqual(counts["shared_books"], 27)
def test_get_objects_for_store(self, *_): def test_get_objects_for_store(self, *_):
"""list of people to follow for a given user""" """list of people to follow for a given user"""
@ -126,7 +126,7 @@ class SuggestedUsers(TestCase):
user_1_annotated = result.get(id=user_1.id) user_1_annotated = result.get(id=user_1.id)
self.assertEqual(user_1_annotated.mutuals, 1) self.assertEqual(user_1_annotated.mutuals, 1)
self.assertEqual(user_1_annotated.shared_books, 1) # self.assertEqual(user_1_annotated.shared_books, 1)
def test_get_annotated_users_counts(self, *_): def test_get_annotated_users_counts(self, *_):
"""correct counting for multiple shared attributed""" """correct counting for multiple shared attributed"""