bugdown: Avoid ORM when there are no group names.

This change avoids hitting the Django ORM when
we don't find any possible group mentions in
the message content.

Django doesn't necessarily actually hit the database,
but it's still slow and shows up in profiles.
This commit is contained in:
Steve Howell 2018-11-02 18:17:07 +00:00 committed by Tim Abbott
parent 88f50b97fd
commit dfadbcd3bc
1 changed files with 12 additions and 2 deletions

View File

@ -1846,12 +1846,22 @@ class MentionData:
row['id']: row
for row in self.full_name_info.values()
}
self.init_user_group_data(realm_id=realm_id, content=content)
def init_user_group_data(self,
realm_id: int,
content: str) -> None:
user_group_names = possible_user_group_mentions(content)
self.user_group_name_info = get_user_group_name_info(realm_id, user_group_names)
group_ids = [group.id for group in self.user_group_name_info.values()]
membership = UserGroupMembership.objects.filter(user_group_id__in=group_ids)
self.user_group_members = defaultdict(list) # type: Dict[int, List[int]]
group_ids = [group.id for group in self.user_group_name_info.values()]
if not group_ids:
# Early-return to avoid the cost of hitting the ORM,
# which shows up in profiles.
return
membership = UserGroupMembership.objects.filter(user_group_id__in=group_ids)
for info in membership.values('user_group_id', 'user_profile_id'):
group_id = info['user_group_id']
user_profile_id = info['user_profile_id']