Skip to content

Commit

Permalink
Merge pull request #1807 from gtech-mulearn/dev
Browse files Browse the repository at this point in the history
fix statistics
  • Loading branch information
adnankattekaden authored Dec 13, 2023
2 parents f007f08 + cf893ec commit 8a2441d
Showing 1 changed file with 64 additions and 68 deletions.
132 changes: 64 additions & 68 deletions api/common/common_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -16,109 +17,104 @@

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(
'role__title').annotate(role_count=Coalesce(Count('role__title'), 0))
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}
)

0 comments on commit 8a2441d

Please sign in to comment.