Skip to content

Commit

Permalink
lc dashboard fix
Browse files Browse the repository at this point in the history
  • Loading branch information
adnankattekaden committed Nov 15, 2023
1 parent dff4d90 commit 93a66be
Showing 1 changed file with 49 additions and 26 deletions.
75 changes: 49 additions & 26 deletions api/common/common_views.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import json

import requests
from django.db import models
from django.db.models import Count, Subquery, OuterRef
from django.db.models import Sum, F, Case, When, Value, CharField, Count, Q
from django.db.models.functions import Coalesce
from rest_framework.views import APIView
from django.db import models

from db.learning_circle import LearningCircle
from db.learning_circle import UserCircleLink
from db.organization import Organization
Expand All @@ -14,42 +15,64 @@
from utils.types import IntegrationType, OrganizationType, RoleType
from utils.utils import CommonUtils
from .serializer import StudentInfoSerializer, CollegeInfoSerializer
from django.db.models import Count, Subquery, OuterRef


class LcDashboardAPI(APIView):
def get(self, request):
date = request.query_params.get("date")
if date:
learning_circle_count = LearningCircle.objects.filter(
created_at__gt=date
).count()
learning_circle_count = LearningCircle.objects.filter(created_at__gt=date).count()
total_no_enrollment = UserCircleLink.objects.filter(accepted=True, created_at__gt=date).count()
user_circle_link_count = UserCircleLink.objects.filter(created_at__gt=date, circle=OuterRef('pk')).values(
'circle_id').annotate(
total_users=Count('id')).values('total_users')

query = InterestGroup.objects.annotate(
total_circles=Count("learning_circle_ig", distinct=True),
total_users=Subquery(user_circle_link_count, output_field=models.IntegerField())
).values("name", "total_circles", "total_users")

circle_count_by_ig = (
query.values("name")
.order_by("name")
.annotate(
total_circles=Count("learning_circle_ig", distinct=True),
total_users=Count("learning_circle_ig__user_circle_link_circle", distinct=True),
)
)
unique_user_count = (
UserCircleLink.objects.filter(created_at__gt=date, accepted=True)
.values("user")
.distinct()
.count()
)
else:
learning_circle_count = LearningCircle.objects.all().count()
total_no_enrollment = UserCircleLink.objects.filter(accepted=True).count()

total_no_enrollment = UserCircleLink.objects.filter(accepted=True).count()
user_circle_link_count = UserCircleLink.objects.filter(circle=OuterRef('pk')).values('circle_id').annotate(
total_users=Count('id')).values('total_users')
user_circle_link_count = UserCircleLink.objects.filter(circle=OuterRef('pk')).values('circle_id').annotate(
total_users=Count('id')).values('total_users')

query = InterestGroup.objects.annotate(
total_circles=Count("learning_circle_ig", distinct=True),
total_users=Subquery(user_circle_link_count, output_field=models.IntegerField())
).values("name", "total_circles", "total_users")

circle_count_by_ig = (
query.values("name")
.order_by("name")
.annotate(
query = InterestGroup.objects.annotate(
total_circles=Count("learning_circle_ig", distinct=True),
total_users=Count("learning_circle_ig__user_circle_link_circle", distinct=True),
total_users=Subquery(user_circle_link_count, output_field=models.IntegerField())
).values("name", "total_circles", "total_users")

circle_count_by_ig = (
query.values("name")
.order_by("name")
.annotate(
total_circles=Count("learning_circle_ig", distinct=True),
total_users=Count("learning_circle_ig__user_circle_link_circle", distinct=True),
)
)
)

unique_user_count = (
UserCircleLink.objects.filter(accepted=True)
.values("user")
.distinct()
.count()
)
unique_user_count = (
UserCircleLink.objects.filter(accepted=True)
.values("user")
.distinct()
.count()
)

return CustomResponse(
response={
Expand Down

0 comments on commit 93a66be

Please sign in to comment.