From 1eb3f60657b2ad547707b5e18ba4b76a980dff40 Mon Sep 17 00:00:00 2001 From: jelanmathewjames Date: Wed, 13 Dec 2023 01:31:36 +0530 Subject: [PATCH] [FIX]: not updating landing stats db signals replaced signals outside consumer class created class a new class LandingStats --- api/common/common_consumer.py | 132 +++++++++++++++++----------------- 1 file changed, 64 insertions(+), 68 deletions(-) diff --git a/api/common/common_consumer.py b/api/common/common_consumer.py index 86a3e627..9858089a 100644 --- a/api/common/common_consumer.py +++ b/api/common/common_consumer.py @@ -6,6 +6,7 @@ from django.db.models.functions import Coalesce from channels.generic.websocket import WebsocketConsumer +from channels.layers import get_channel_layer from asgiref.sync import async_to_sync from db.learning_circle import LearningCircle @@ -16,47 +17,22 @@ from utils.types import IntegrationType, OrganizationType - -class GlobalCount(WebsocketConsumer): +class LandingStats: data = {} - group_name = "landing_stats" - - def connect(self): - async_to_sync(self.channel_layer.group_add)( - self.group_name, - self.channel_name - ) - self.accept() - - self.data = { - 'members': self.members_count, - 'org_type_counts': self.org_type_counts, - 'enablers_mentors_count': self.enablers_mentors_count, - 'ig_count': self.interest_groups_count, - 'learning_circle_count': self.learning_circles_count - } - self.send(text_data=json.dumps(self.data)) - - def disconnect(self, code): - self.channel_layer.group_discard(self.group_name, self.channel_name) - - @property def members_count(self): members_count = User.objects.all().count() return members_count - - @property + def org_type_counts(self): org_type_counts = Organization.objects.filter( org_type__in=[OrganizationType.COLLEGE.value, OrganizationType.COMPANY.value, - OrganizationType.COMMUNITY.value] + OrganizationType.COMMUNITY.value] ).values('org_type').annotate(org_count=Coalesce(Count('org_type'), 0)) org_type_counts = list(org_type_counts) return org_type_counts - - @property + def enablers_mentors_count(self): enablers_mentors_count = UserRoleLink.objects.filter( role__title__in=["Mentor", "Enabler"]).values( @@ -64,61 +40,81 @@ def enablers_mentors_count(self): enablers_mentors_count = list(enablers_mentors_count) return enablers_mentors_count - - @property + def interest_groups_count(self): interest_groups_count = InterestGroup.objects.all().count() return interest_groups_count - - @property + def learning_circles_count(self): learning_circles_count = LearningCircle.objects.all().count() return learning_circles_count - - def get_based_on_sender(self, sender): - if sender == User: - self.data['members'] = self.members_count + + def get_data(self, sender): + if sender == None: + self.data = { + 'members': self.members_count(), + 'org_type_counts': self.org_type_counts(), + 'enablers_mentors_count': self.enablers_mentors_count(), + 'ig_count': self.interest_groups_count(), + 'learning_circle_count': self.learning_circles_count() + } + elif sender == User: + self.data['members'] = self.members_count() elif sender == Organization: - self.data['org_type_counts'] = self.org_type_counts + self.data['org_type_counts'] = self.org_type_counts() elif sender == UserRoleLink: - self.data['enablers_mentors_count'] = self.enablers_mentors_count + self.data['enablers_mentors_count'] = self.enablers_mentors_count() elif sender == InterestGroup: - self.data['ig_count'] = self.interest_groups_count + self.data['ig_count'] = self.interest_groups_count() elif sender == LearningCircle: - self.data['learning_circle_count'] = self.learning_circles_count - - def receive(self, text_data): - - @receiver(post_save, sender=User) - @receiver(post_save, sender=LearningCircle) - @receiver(post_save, sender=InterestGroup) - @receiver(post_save, sender=UserRoleLink) - @receiver(post_save, sender=Organization) - @receiver(post_delete, sender=User) - @receiver(post_delete, sender=LearningCircle) - @receiver(post_delete, sender=InterestGroup) - @receiver(post_delete, sender=UserRoleLink) - @receiver(post_delete, sender=Organization) - def db_signals(sender, instance, created=None, *args, **kwargs): - if created: - self.get_based_on_sender(sender) - async_to_sync(self.channel_layer.group_send)( - self.group_name, - {"type": "send_data", "data": self.data} - ) - - if created == None: - self.get_based_on_sender(sender) - async_to_sync(self.channel_layer.group_send)( - self.group_name, - {"type": "send_data", "data": self.data} - ) + self.data['learning_circle_count'] = self.learning_circles_count() + + +landing_stats = LandingStats() + +class GlobalCount(WebsocketConsumer): + data = {} + group_name = "landing_stats" + + def connect(self): + async_to_sync(self.channel_layer.group_add)( + self.group_name, + self.channel_name + ) + self.accept() + + landing_stats.get_data(None) + self.data = landing_stats.data + + self.send(text_data=json.dumps(self.data)) + + def disconnect(self, code): + self.channel_layer.group_discard(self.group_name, self.channel_name) def send_data(self, event): self.send(text_data=json.dumps(event['data'])) +channel_layer = get_channel_layer() +@receiver(post_save, sender=User) +@receiver(post_save, sender=LearningCircle) +@receiver(post_save, sender=InterestGroup) +@receiver(post_save, sender=UserRoleLink) +@receiver(post_save, sender=Organization) +@receiver(post_delete, sender=User) +@receiver(post_delete, sender=LearningCircle) +@receiver(post_delete, sender=InterestGroup) +@receiver(post_delete, sender=UserRoleLink) +@receiver(post_delete, sender=Organization) +def db_signals(sender, instance, created=None, *args, **kwargs): + if created or created == None: + landing_stats.get_data(sender) + data = landing_stats.data + async_to_sync(channel_layer.group_send)( + "landing_stats", + {"type": "send_data", "data": data} + )