Skip to content

Commit

Permalink
Remove unused rest api for Project Stats.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rup-Narayan-Rajbanshi committed Sep 26, 2024
1 parent 1acc0b2 commit 52edd14
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 313 deletions.
225 changes: 0 additions & 225 deletions apps/project/tests/test_apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
from geo.models import Region
from project.tasks import (
_generate_project_viz_stats,
_generate_project_stats_cache,
)
from ary.models import AssessmentTemplate
from project.models import (
Expand Down Expand Up @@ -1374,230 +1373,6 @@ def test_project_memberships_in_particluar_project(self):
self.assertEqual(response.data['id'], project1.id)
self.assertNotIn('memberships', response.data) # `membership` field shouldnot be present

def test_project_summary_api(self):
user = self.create_user()

project1 = self.create_project(title='Project 1')
project2 = self.create_project(title='Project 2')
project3 = self.create_project(title='Project 3')
project4 = self.create_project(title='Project 4')
project5 = self.create_project(title='Project 5')
project1.add_member(user)
project2.add_member(user)
project3.add_member(user)
project4.add_member(user)
project5.add_member(user)

lead1 = self.create_lead(project=project1)
lead2 = self.create_lead(project=project1)
lead3 = self.create_lead(project=project2)
lead4 = self.create_lead(project=project3)
lead5 = self.create_lead(project=project4)
lead6 = self.create_lead(project=project4)
lead7 = self.create_lead(project=project5)
self.create_lead(project=project1)
self.create_lead(project=project5)

data = [
{
"lead": lead1,
"controlled": True,
"months": -3,
"days": -1
},
{
"lead": lead1,
"controlled": True,
"months": -2,
"days": -1
},
{
"lead": lead2,
"controlled": False,
"months": -3,
"days": -1
},
{
"lead": lead2,
"controlled": True,
"months": -3,
"days": -1
},
{
"lead": lead2,
"controlled": True,
"months": -3,
"days": -1
},
{
"lead": lead3,
"controlled": True,
"months": -1,
"days": -10
},
{
"lead": lead3,
"controlled": True,
"months": -1,
"days": -20
},
{
"lead": lead3,
"controlled": True,
"months": -1,
"days": -30
},
{
"lead": lead3,
"controlled": True,
"months": -1,
"days": -40
},
{
"lead": lead4,
"controlled": False,
"months": -3,
"days": -1
},
{
"lead": lead5,
"controlled": True,
"months": -3,
"days": -1,
},
{
"lead": lead5,
"controlled": True,
"months": -2,
"days": -1,
},
{
"lead": lead6,
"controlled": True,
"months": -3,
"days": -1
},
{
"lead": lead7,
"controlled": True,
"months": -3,
"days": 0,
},
]
now = timezone.now()
for item in data:
self.update_obj(
self.create_entry(lead=item['lead'], controlled=item['controlled'], created_by=user),
created_at=now + relativedelta(months=item['months'], days=item['days'])
)

# Run the caching process
_generate_project_stats_cache()

self.authenticate(user)
url = '/api/v1/projects-stat/summary/'
response = self.client.get(url)
self.assert_200(response)
self.assertEqual(response.data['projects_count'], 5)
self.assertEqual(response.data['total_leads_count'], 9)
self.assertEqual(response.data['total_leads_tagged_count'], 7)
self.assertEqual(response.data['total_leads_tagged_and_controlled_count'], 5)
self.assertEqual(len(response.data['recent_entries_activity']['projects']), 3)
self.assertEqual(response.data['recent_entries_activity']['projects'][0]['id'], project1.id)
self.assertEqual(response.data['recent_entries_activity']['projects'][0]['count'], 1)
self.assertEqual(response.data['recent_entries_activity']['projects'][1]['id'], project2.id)
self.assertEqual(response.data['recent_entries_activity']['projects'][1]['count'], 4)
self.assertEqual(len(response.data['recent_entries_activity']['activities']), 6)

def test_project_recent_api(self):
user = self.create_user()

project1 = self.create_project(title='Project 1')
project2 = self.create_project(title='Project 2')
project3 = self.create_project(title='Project 3')
project4 = self.create_project(title='Project 4')
project1.add_member(user)
project2.add_member(user)
project3.add_member(user)

lead1 = self.create_lead(project=project1, created_by=user)
lead2 = self.create_lead(project=project2, created_by=user)
self.create_entry(lead=lead1, controlled=False, created_by=user)
self.create_lead(project=project3, created_by=user)
self.create_lead(project=project4, created_by=user)

self.authenticate(user)
url = '/api/v1/projects-stat/recent/'
response = self.client.get(url)
self.assert_200(response)
self.assertEqual(len(response.data), 3)
self.assertEqual(response.data[0]['id'], project3.pk)
self.assertEqual(response.data[1]['id'], project1.pk)
self.assertEqual(response.data[2]['id'], project2.pk)

lead2.modified_by = user
lead2.save()
response = self.client.get(url)
self.assert_200(response)
self.assertEqual(len(response.data), 3)
self.assertEqual(response.data[0]['id'], project2.pk)

def test_project_stats_api(self):
user = self.create_user()

project1 = self.create_project(title='Project 1')
project2 = self.create_project(title='Project 2')
project3 = self.create_project(title='Project 3')
project1.add_member(user)
project2.add_member(user)

now = timezone.now()
lead1_1 = self.update_obj(self.create_lead(project=project1), created_at=now + relativedelta(months=-1))
lead1_2 = self.update_obj(self.create_lead(project=project1), created_at=now + relativedelta(months=-2))
lead1_3 = self.update_obj(self.create_lead(project=project1), created_at=now + relativedelta(months=-2))
lead1_4 = self.update_obj(self.create_lead(project=project1), created_at=now + relativedelta(months=-1))
self.update_obj(self.create_lead(project=project1), created_at=now + relativedelta(months=-1))

self.update_obj(self.create_entry(lead=lead1_1, controlled=False), created_at=now + relativedelta(months=-1))
self.update_obj(self.create_entry(lead=lead1_1, controlled=False), created_at=now + relativedelta(months=-1))
self.update_obj(self.create_entry(lead=lead1_2, controlled=True), created_at=now + relativedelta(months=-3))
self.update_obj(self.create_entry(lead=lead1_2, controlled=True), created_at=now + relativedelta(months=-2))
self.update_obj(self.create_entry(lead=lead1_2, controlled=True), created_at=now + relativedelta(months=-2))
self.update_obj(self.create_entry(lead=lead1_3, controlled=True), created_at=now + relativedelta(months=-3))
self.update_obj(self.create_entry(lead=lead1_3, controlled=True), created_at=now + relativedelta(months=-3))
self.create_entry(lead=lead1_3, controlled=True)
self.create_entry(lead=lead1_4, controlled=True)

lead2 = self.create_lead(project=project2)
lead3 = self.create_lead(project=project3)
self.create_entry(lead=lead2, controlled=False)
self.create_entry(lead=lead3, controlled=False)

# number_of_leads_tagged
lead1_2.status = lead1_1.status = Lead.Status.TAGGED
lead1_2.save(update_fields=('status',))
lead1_1.save(update_fields=('status',))

# Run the caching process
_generate_project_stats_cache()

self.authenticate(user)
url = '/api/v1/projects-stat/?involvement=my_projects'
response = self.client.get(url)
self.assert_200(response)
self.assertEqual(len(response.data['results']), 2)
# Check response for Project 1
project_1_data = next(
project for project in response.data['results'] if project['id'] == project1.pk
)
self.assertEqual(project_1_data['id'], project1.pk)
self.assertEqual(project_1_data['number_of_leads'], 5)
self.assertEqual(project_1_data['number_of_leads_tagged'], 2)
self.assertEqual(project_1_data['number_of_leads_tagged_and_controlled'], 1)
self.assertEqual(project_1_data['number_of_entries'], 9)
self.assertEqual(len(project_1_data['leads_activity']), 2)
self.assertEqual(len(project_1_data['entries_activity']), 3)

def test_project_stats_public_api(self):
normal_user = self.create_user()
admin_user = self.create_user()
Expand Down
85 changes: 0 additions & 85 deletions apps/project/views.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import logging
import uuid

from dateutil.relativedelta import relativedelta
import django_filters
from django.conf import settings
from django.http import Http404
from django.db import transaction, models
from django.utils import timezone
from django.utils.http import urlsafe_base64_decode
from django.utils.encoding import force_text
from django.contrib.postgres.fields.jsonb import KeyTextTransform
from django.db.models.functions import Cast
from django.template.response import TemplateResponse
from deep.permalinks import Permalink
from rest_framework.exceptions import PermissionDenied
Expand All @@ -35,7 +32,6 @@
IsProjectMember,
)
from deep.serializers import URLCachedFileField
from deep.paginations import SmallSizeSetPagination
from tabular.models import Field

from user.utils import send_project_join_request_emails
Expand Down Expand Up @@ -65,7 +61,6 @@
)
from .serializers import (
ProjectSerializer,
ProjectStatSerializer,
ProjectMembershipSerializer,
ProjectJoinRequestSerializer,
ProjectUserGroupSerializer,
Expand Down Expand Up @@ -665,86 +660,6 @@ def get_analysis(self, request, pk=None, version=None):
})


class ProjectStatViewSet(ProjectViewSet):
pagination_class = SmallSizeSetPagination

def get_serializer_class(self):
return ProjectStatSerializer

def get_queryset(self):
return get_filtered_projects(
self.request.user, self.request.GET,
annotate=True,
).prefetch_related(
'regions', 'organizations',
).select_related(
'created_by__profile', 'modified_by__profile'
)

@action(
detail=False,
permission_classes=[permissions.IsAuthenticated],
url_path='recent'
)
def get_recent_projects(self, request, *args, **kwargs):
# Only pull project data for which user is member of
qs = self.get_queryset().filter(Project.get_query_for_member(request.user))
recent_projects = Project.get_recent_active_projects(request.user, qs)
return response.Response(
self.get_serializer_class()(
recent_projects,
context=self.get_serializer_context(),
many=True,
).data
)

@action(
detail=False,
permission_classes=[permissions.IsAuthenticated],
url_path='summary'
)
def get_projects_summary(self, request, pk=None, version=None):
projects = Project.get_for_member(request.user)
# Lead stats
leads = Lead.objects.filter(project__in=projects)
total_leads_tagged_count = leads.annotate(entries_count=models.Count('entry')).filter(entries_count__gt=0).count()
total_leads_tagged_and_controlled_count = leads.annotate(
entries_count=models.Count('entry'),
controlled_entries_count=models.Count(
'entry', filter=models.Q(entry__controlled=True)
),
).filter(entries_count__gt=0, entries_count=models.F('controlled_entries_count')).count()
# Entries activity
recent_projects_id = list(
projects.annotate(
entries_count=Cast(KeyTextTransform('entries_activity', 'stats_cache'), models.IntegerField())
).filter(entries_count__gt=0).order_by('-entries_count').values_list('id', flat=True)[:3])
recent_entries = Entry.objects.filter(
project__in=recent_projects_id,
created_at__gte=(timezone.now() + relativedelta(months=-3))
)
recent_entries_activity = {
'projects': (
recent_entries.order_by().values('project')
.annotate(count=models.Count('*'))
.filter(count__gt=0)
.values('count', id=models.F('project'), title=models.F('project__title'))
),
'activities': (
recent_entries.order_by('project', 'created_at__date').values('project', 'created_at__date')
.annotate(count=models.Count('*'))
.values('project', 'count', date=models.Func(models.F('created_at__date'), function='DATE'))
),
}
return response.Response({
'projects_count': projects.count(),
'total_leads_count': leads.count(),
'total_leads_tagged_count': total_leads_tagged_count,
'total_leads_tagged_and_controlled_count': total_leads_tagged_and_controlled_count,
'recent_entries_activity': recent_entries_activity,
})


class ProjectMembershipViewSet(viewsets.ModelViewSet):
serializer_class = ProjectMembershipSerializer
permission_classes = [permissions.IsAuthenticated,
Expand Down
3 changes: 0 additions & 3 deletions deep/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
ProjectUserGroupViewSet,
ProjectOptionsView,
ProjectViewSet,
ProjectStatViewSet,
accept_project_confirm,
)
from geo.views import (
Expand Down Expand Up @@ -242,8 +241,6 @@
# Project routers
router.register(r'projects', ProjectViewSet,
basename='project')
router.register(r'projects-stat', ProjectStatViewSet,
basename='project-stat')
router.register(r'projects/(?P<project_id>\d+)/project-memberships', ProjectMembershipViewSet,
basename='project_membership')
router.register(r'projects/(?P<project_id>\d+)/project-usergroups', ProjectUserGroupViewSet,
Expand Down

0 comments on commit 52edd14

Please sign in to comment.