Merge pull request #1258 from bookwyrm-social/suggested-users-memory

Disable comically inefficient book count comparisons
This commit is contained in:
Mouse Reeve 2021-08-06 11:03:28 -06:00 committed by GitHub
commit 1126f47224
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 29 deletions

View file

@ -19,7 +19,7 @@ class SuggestedUsers(RedisStore):
def get_rank(self, obj):
"""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
"""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"""
return {
"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):
@ -95,7 +95,7 @@ class SuggestedUsers(RedisStore):
logger.exception(err)
continue
user.mutuals = counts["mutuals"]
user.shared_books = counts["shared_books"]
# user.shared_books = counts["shared_books"]
results.append(user)
return results
@ -115,16 +115,16 @@ def get_annotated_users(viewer, *args, **kwargs):
),
distinct=True,
),
shared_books=Count(
"shelfbook",
filter=Q(
~Q(id=viewer.id),
shelfbook__book__parent_work__in=[
s.book.parent_work for s in viewer.shelfbook_set.all()
],
),
distinct=True,
),
# shared_books=Count(
# "shelfbook",
# filter=Q(
# ~Q(id=viewer.id),
# shelfbook__book__parent_work__in=[
# s.book.parent_work for s in viewer.shelfbook_set.all()
# ],
# ),
# distinct=True,
# ),
)
)
@ -162,18 +162,18 @@ def update_suggestions_on_unfollow(sender, instance, **kwargs):
rerank_user_task.delay(instance.user_object.id, update_only=False)
@receiver(signals.post_save, sender=models.ShelfBook)
@receiver(signals.post_delete, sender=models.ShelfBook)
# pylint: disable=unused-argument
def update_rank_on_shelving(sender, instance, *args, **kwargs):
"""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 instance.user.local:
rerank_suggestions_task.delay(instance.user.id)
# if the user is discoverable, update their rankings
if instance.user.discoverable:
rerank_user_task.delay(instance.user.id)
# @receiver(signals.post_save, sender=models.ShelfBook)
# @receiver(signals.post_delete, sender=models.ShelfBook)
# # pylint: disable=unused-argument
# def update_rank_on_shelving(sender, instance, *args, **kwargs):
# """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 instance.user.local:
# rerank_suggestions_task.delay(instance.user.id)
#
# # if the user is discoverable, update their rankings
# if instance.user.discoverable:
# rerank_user_task.delay(instance.user.id)
@receiver(signals.post_save, sender=models.User)

View file

@ -29,7 +29,7 @@ class SuggestedUsers(TestCase):
Mock = namedtuple("AnnotatedUserMock", ("mutuals", "shared_books"))
annotated_user_mock = Mock(3, 27)
rank = suggested_users.get_rank(annotated_user_mock)
self.assertEqual(rank, 3.9642857142857144)
self.assertEqual(rank, 3) # 3.9642857142857144)
def test_store_id(self, *_):
"""redis key generation"""
@ -42,7 +42,7 @@ class SuggestedUsers(TestCase):
"""reverse the rank computation to get the mutuals and shared books counts"""
counts = suggested_users.get_counts_from_rank(3.9642857142857144)
self.assertEqual(counts["mutuals"], 3)
self.assertEqual(counts["shared_books"], 27)
# self.assertEqual(counts["shared_books"], 27)
def test_get_objects_for_store(self, *_):
"""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)
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, *_):
"""correct counting for multiple shared attributed"""

View file

@ -28,7 +28,7 @@ class Directory(View):
if sort == "recent":
users = users.order_by("-last_active_date")
else:
users = users.order_by("-mutuals", "-shared_books", "-last_active_date")
users = users.order_by("-mutuals", "-last_active_date")
paginated = Paginator(users, 12)