From ce811b0b822e0db958d247ab887297251e528a4b Mon Sep 17 00:00:00 2001 From: Aswanth Vc Date: Thu, 26 Sep 2024 21:30:49 +0530 Subject: [PATCH 1/3] perf(register): reduce number of queries executed --- api/dashboard/profile/profile_serializer.py | 56 +++++++++++---------- 1 file changed, 30 insertions(+), 26 deletions(-) diff --git a/api/dashboard/profile/profile_serializer.py b/api/dashboard/profile/profile_serializer.py index d549b965..70679d42 100644 --- a/api/dashboard/profile/profile_serializer.py +++ b/api/dashboard/profile/profile_serializer.py @@ -84,6 +84,21 @@ class Meta: "percentile", ) + def _get_user_org_link(self, obj, org_type): + if not getattr(self, "user_org_link", None): + self.user_org_link = obj.user_organization_link_user.filter( + org__org_type=org_type + ).first() + return self.user_org_link + + def _get_org_type(self, obj): + roles = self.get_roles(obj) + return ( + OrganizationType.COMPANY.value + if MainRoles.MENTOR.value in roles + else OrganizationType.COLLEGE.value + ) + def get_percentile(self, obj): users_count_lt_user_karma = Wallet.objects.filter( karma__lt=obj.wallet_user.karma @@ -96,30 +111,22 @@ def get_percentile(self, obj): ) def get_roles(self, obj): - return list( + if "role_values" in self.context: + return self.context["role_values"] + role_values = list( {link.role.title for link in obj.user_role_link_user.filter(verified=True)} ) + self.context["role_values"] = role_values + return role_values def get_college_id(self, obj): - org_type = ( - OrganizationType.COMPANY.value - if MainRoles.MENTOR.value in self.get_roles(obj) - else OrganizationType.COLLEGE.value - ) - user_org_link = obj.user_organization_link_user.filter( - org__org_type=org_type - ).first() + org_type = self._get_org_type(obj) + user_org_link = self._get_user_org_link(obj, org_type) return user_org_link.org.id if user_org_link else None def get_org_district_id(self, obj): - org_type = ( - OrganizationType.COMPANY.value - if MainRoles.MENTOR.value in self.get_roles(obj) - else OrganizationType.COLLEGE.value - ) - user_org_link = obj.user_organization_link_user.filter( - org__org_type=org_type - ).first() + org_type = self._get_org_type(obj) + user_org_link = self._get_user_org_link(obj, org_type) return ( user_org_link.org.district.id if user_org_link and hasattr(user_org_link.org, "district") @@ -127,14 +134,13 @@ def get_org_district_id(self, obj): ) def get_college_code(self, obj): - if user_org_link := obj.user_organization_link_user.filter( - org__org_type=OrganizationType.COLLEGE.value - ).first(): - return user_org_link.org.code + org_type = self._get_org_type(obj) + if org_type == OrganizationType.COLLEGE.value: + user_org_link = self._get_user_org_link(obj, org_type) + return user_org_link.org.code if user_org_link else None return None def get_rank(self, obj): - roles = self.get_roles(obj) user_karma = obj.wallet_user.karma if RoleType.MENTOR.value in roles: @@ -162,10 +168,8 @@ def get_rank(self, obj): ) .order_by("-karma") ) - - for count, _rank in enumerate(ranks, start=1): - if obj.id == _rank.user_id: - return count + ranks = ranks.values_list("user_id", flat=True) + return ranks.index(obj.id) + 1 def get_karma_distribution(self, obj): return ( From 713b716ac239f06eb134a4dc23ce5d3d903b566e Mon Sep 17 00:00:00 2001 From: Aswanth Vc Date: Thu, 26 Sep 2024 21:54:47 +0530 Subject: [PATCH 2/3] perf: rank calculation optimization --- api/dashboard/profile/profile_serializer.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/api/dashboard/profile/profile_serializer.py b/api/dashboard/profile/profile_serializer.py index 397fdcd4..bbd35fc0 100644 --- a/api/dashboard/profile/profile_serializer.py +++ b/api/dashboard/profile/profile_serializer.py @@ -168,7 +168,7 @@ def get_rank(self, obj): ) .order_by("-karma") ) - ranks = ranks.values_list("user_id", flat=True) + ranks = list(ranks.values_list("user_id", flat=True)) return ranks.index(obj.id) + 1 def get_karma_distribution(self, obj): @@ -251,7 +251,7 @@ def get_role(self, obj): return ["Learner"] if len(roles) == 0 else roles def get_rank(self, obj): - roles = self.get_roles(obj) + roles = self.get_role(obj) user_karma = obj.wallet_user.karma if RoleType.MENTOR.value in roles: ranks = Wallet.objects.filter( @@ -279,9 +279,8 @@ def get_rank(self, obj): .order_by("-karma") ) - for count, _rank in enumerate(ranks, start=1): - if obj.id == _rank.user_id: - return count + ranks = list(ranks.values_list("user_id", flat=True)) + return ranks.index(obj.id) + 1 def get_karma(self, obj): return total_karma.karma if (total_karma := obj.wallet_user) else None From 09f646b741253ffcb6b44f1e013dc3eda03d9c75 Mon Sep 17 00:00:00 2001 From: muhammedr7025 Date: Sun, 6 Oct 2024 23:33:46 +0530 Subject: [PATCH 3/3] feat: added categoery in interest group --- api/dashboard/ig/dash_ig_serializer.py | 4 +++- db/task.py | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/api/dashboard/ig/dash_ig_serializer.py b/api/dashboard/ig/dash_ig_serializer.py index a0b8f3df..b32a7bc3 100644 --- a/api/dashboard/ig/dash_ig_serializer.py +++ b/api/dashboard/ig/dash_ig_serializer.py @@ -9,7 +9,7 @@ class InterestGroupSerializer(serializers.ModelSerializer): updated_by = serializers.CharField(source='updated_by.full_name') created_by = serializers.CharField(source='created_by.full_name') members = serializers.SerializerMethodField() - + category = serializers.ChoiceField(choices=["maker", "software", "creative", "management", "others"]) class Meta: model = InterestGroup fields = [ @@ -17,6 +17,7 @@ class Meta: "name", "icon", "code", + "category", "members", "updated_by", "updated_at", @@ -35,6 +36,7 @@ class Meta: fields = [ "name", "code", + "category", "icon", "created_by", "updated_by" diff --git a/db/task.py b/db/task.py index cccd6666..e88c4f03 100644 --- a/db/task.py +++ b/db/task.py @@ -40,6 +40,7 @@ class InterestGroup(models.Model): name = models.CharField(max_length=75, unique=True) code = models.CharField(max_length=10, unique=True) icon = models.CharField(max_length=10) + category =models.CharField(max_length=20,default="others",blank=False,null=False) updated_by = models.ForeignKey(User, on_delete=models.SET(settings.SYSTEM_ADMIN_ID), db_column="updated_by", related_name="interest_group_updated_by") updated_at = models.DateTimeField(auto_now=True)