Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stats API #132

Merged
merged 3 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions apps/applicants/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,7 @@ class Meta:
]


class ApplicationStatsResponseSerializer(serializers.Serializer):
applied_jobs = serializers.IntegerField()
recruiter_actions = serializers.IntegerField()
shortlisted_jobs = serializers.IntegerField()
6 changes: 6 additions & 0 deletions apps/applicants/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
AllApplicantsOfCompany,
ApplyToJob,
UpdateApplicationStatus, GetAppliedJobs,
ApplicationStats
)

urlpatterns = [
Expand All @@ -15,4 +16,9 @@
UpdateApplicationStatus.as_view(),
name="updateapplicationstatus",
),
path(
"application/stats",
ApplicationStats.as_view(),
name="applicationstats"
)
]
23 changes: 23 additions & 0 deletions apps/applicants/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@
from rest_framework import exceptions, permissions, status
from rest_framework.response import Response
from rest_framework.views import APIView
from django.db.models import Count, Q

from apps.accounts.permissions import IsEmployer, IsJobSeeker, IsProfileCompleted
from apps.applicants.models import Applicants
from apps.applicants.serializers import (
ApplicantModelSerializer,
ApplyToJobSerializer,
UpdateApplicationStatusSerializer, AppliedJobSerializer,
ApplicationStatsResponseSerializer
)
from apps.jobs.models import Job
from apps.userprofile.models import UserProfile
from apps.utils.responses import InternalServerError


class AllApplicantsOfCompany(APIView):
Expand Down Expand Up @@ -123,3 +126,23 @@ def get(self, request):
AppliedJobSerializer(applicants, many=True).data,
status=status.HTTP_200_OK
)


class ApplicationStats(APIView):

permission_classes = [permissions.IsAuthenticated, IsJobSeeker]

@extend_schema(responses={200: ApplicationStatsResponseSerializer}, tags=["applications"])
def get(self, request):
try:
userprofile = UserProfile.objects.get(user=request.user)
counts = Applicants.objects.filter(user=userprofile).aggregate(
applied_jobs=Count('id'),
recruiter_actions=Count('id', filter=~Q(status='applied')),
shortlisted_jobs=Count('id', filter=Q(status='shortlisted'))
)

return Response(ApplicationStatsResponseSerializer(counts).data, status=status.HTTP_200_OK)
except Exception as e:
raise InternalServerError()

7 changes: 7 additions & 0 deletions apps/jobs/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,10 @@ class JobsCountByCategoriesSerializer(serializers.Serializer):
category = serializers.CharField()
count = serializers.CharField()


class CompanyStatsResponseSerializer(serializers.Serializer):
job_count = serializers.IntegerField()
applications_count = serializers.IntegerField()
reviewed_count = serializers.IntegerField()
shortlisted_count = serializers.IntegerField()

7 changes: 6 additions & 1 deletion apps/jobs/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from apps.jobs.views import (
CompanyViewSets, ContactUsViewSet,
JobViewSets
JobViewSets, CompanyStats
)

# app name for namespace
Expand All @@ -17,4 +17,9 @@

urlpatterns = [
path("", include(router.urls), name="Default"),
path(
"company/stats",
CompanyStats.as_view(),
name="companystats"
)
]
48 changes: 44 additions & 4 deletions apps/jobs/views.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@

from django.db import connection
from django.db.models import Count
import django_filters.rest_framework as df_filters
from django.db.models import BooleanField, Case, Value, When
from drf_spectacular.utils import extend_schema
from rest_framework import exceptions, parsers, status, viewsets, filters
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.permissions import IsAuthenticated
from django.db.models import BooleanField, Case, Value, When
from rest_framework import exceptions, parsers, status, viewsets, filters


from apps.accounts.permissions import Moderator
from apps.jobs.constants import response, values
from apps.userprofile.models import UserProfile
from apps.applicants.models import Applicants
from apps.jobs.models import Company, ContactMessage, Job
from apps.accounts.permissions import IsEmployer
from apps.jobs.serializers import CompanySerializer, ContactUsSerializer, JobSerializer, JobsCountByCategoriesSerializer
from apps.jobs.serializers import CompanySerializer, ContactUsSerializer, JobSerializer, JobsCountByCategoriesSerializer, CompanyStatsResponseSerializer
from apps.jobs.utils.validators import validationClass
from apps.utils.responses import InternalServerError
from apps.utils.pagination import DefaultPagination
Expand Down Expand Up @@ -403,3 +406,40 @@ def list(self, request, *args, **kwargs):
return super().list(request, *args, **kwargs)
else:
return super().list(request, *args, **kwargs)


class CompanyStats(APIView):

permission_classes = [IsAuthenticated, IsEmployer]

@extend_schema(tags=["company"], responses={200: CompanyStatsResponseSerializer})
def get(self, request):
"""Get company stats"""
try:
raw_query = """
SELECT
COUNT(DISTINCT(tj.job_id)) AS job_count,
COUNT(ta.id) AS applications_count,
COUNT(CASE WHEN ta.status != 'applied' THEN 1 ELSE NULL END) AS reviewed_count,
COUNT(CASE WHEN ta.status = 'shortlisted' THEN 1 ELSE NULL END) AS shortlisted_count
FROM tbl_job tj
JOIN tbl_applicants ta ON tj.job_id = ta.job_id
WHERE tj.employer_id = %s
"""

# Execute the raw SQL query
with connection.cursor() as cursor:
cursor.execute(raw_query, [str(request.user.id).replace("-", "")])
result = cursor.fetchone()

job_count, applications_count, reviewed_count, shortlisted_count = result
company_stats = CompanyStatsResponseSerializer({
"job_count": job_count,
"applications_count": applications_count,
"reviewed_count": reviewed_count,
"shortlisted_count": shortlisted_count
})

return Response(company_stats.data)
except Exception as e:
raise InternalServerError()
Loading