From bd4fa827f2487a0b4216afb02782054c0f13c016 Mon Sep 17 00:00:00 2001 From: Hironsan Date: Thu, 6 Jan 2022 16:27:14 +0900 Subject: [PATCH] Remove StatisticsAPI --- backend/api/tests/api/test_statistics.py | 75 +----------------------- backend/api/urls.py | 5 -- backend/api/views/statistics.py | 61 +------------------ 3 files changed, 3 insertions(+), 138 deletions(-) diff --git a/backend/api/tests/api/test_statistics.py b/backend/api/tests/api/test_statistics.py index 14d3836e..ca8e8dc3 100644 --- a/backend/api/tests/api/test_statistics.py +++ b/backend/api/tests/api/test_statistics.py @@ -1,80 +1,9 @@ -from django.conf import settings -from django.contrib.auth.models import User from model_mommy import mommy from rest_framework import status from rest_framework.reverse import reverse -from rest_framework.test import APITestCase -from ...models import DOCUMENT_CLASSIFICATION, Example -from .utils import (CRUDMixin, TestUtilsMixin, assign_user_to_role, - create_default_roles, make_doc, make_label, - prepare_project, remove_all_role_mappings) - - -class TestStatisticsAPI(APITestCase, TestUtilsMixin): - - @classmethod - def setUpTestData(cls): - cls.super_user_name = 'super_user_name' - cls.super_user_pass = 'super_user_pass' - cls.other_user_name = 'other_user_name' - cls.other_user_pass = 'other_user_pass' - create_default_roles() - # Todo: change super_user to project_admin. - super_user = User.objects.create_superuser(username=cls.super_user_name, - password=cls.super_user_pass, - email='fizz@buzz.com') - - other_user = User.objects.create_user(username=cls.other_user_name, - password=cls.other_user_pass, - email='bar@buzz.com') - - cls.project = mommy.make( - _model='TextClassificationProject', - project_type=DOCUMENT_CLASSIFICATION, - users=[super_user] - ) - doc1 = mommy.make('Example', project=cls.project) - doc2 = mommy.make('Example', project=cls.project) - mommy.make('ExampleState', example=doc1, confirmed_by=super_user) - mommy.make('Category', example=doc1, user=super_user) - mommy.make('Category', example=doc2, user=other_user) - cls.url = reverse(viewname='statistics', args=[cls.project.id]) - cls.doc = Example.objects.filter(project=cls.project) - - assign_user_to_role(project_member=other_user, project=cls.project, - role_name=settings.ROLE_ANNOTATOR) - - @classmethod - def doCleanups(cls): - remove_all_role_mappings() - - def test_returns_exact_progress(self): - self.client.login(username=self.super_user_name, - password=self.super_user_pass) - response = self.client.get(self.url, format='json') - self.assertEqual(response.data['total'], 2) - self.assertEqual(response.data['remaining'], 1) - - def test_returns_user_count(self): - self.client.login(username=self.super_user_name, - password=self.super_user_pass) - response = self.client.get(self.url, format='json') - self.assertIn('label', response.data) - self.assertIsInstance(response.data['label'], dict) - - def test_returns_label_count(self): - self.client.login(username=self.super_user_name, - password=self.super_user_pass) - response = self.client.get(self.url, format='json') - self.assertIn('user', response.data) - self.assertIsInstance(response.data['user'], dict) - - def test_returns_partial_response(self): - self.client.login(username=self.super_user_name, - password=self.super_user_pass) - response = self.client.get(f'{self.url}?include=user', format='json') - self.assertEqual(list(response.data.keys()), ['user']) +from ...models import DOCUMENT_CLASSIFICATION +from .utils import CRUDMixin, make_doc, make_label, prepare_project class TestMemberProgress(CRUDMixin): diff --git a/backend/api/urls.py b/backend/api/urls.py index cd6da57c..45d30fcf 100644 --- a/backend/api/urls.py +++ b/backend/api/urls.py @@ -27,11 +27,6 @@ urlpatterns_project = [ view=export_dataset.DownloadAPI.as_view(), name='download-dataset' ), - path( - route='statistics', - view=statistics.StatisticsAPI.as_view(), - name='statistics' - ), path( route='progress', view=statistics.ProgressAPI.as_view(), diff --git a/backend/api/views/statistics.py b/backend/api/views/statistics.py index cde819ef..4cafb801 100644 --- a/backend/api/views/statistics.py +++ b/backend/api/views/statistics.py @@ -1,7 +1,5 @@ import abc -from django.conf import settings -from django.db.models import Count from django.shortcuts import get_object_or_404 from rest_framework import status from rest_framework.permissions import IsAuthenticated @@ -9,67 +7,10 @@ from rest_framework.response import Response from rest_framework.views import APIView from ..models import (Annotation, Category, DocType, Example, ExampleState, - Label, Project, RoleMapping, Span, SpanType) + Label, Project, Span, SpanType) from ..permissions import IsInProjectReadOnlyOrAdmin -class StatisticsAPI(APIView): - pagination_class = None - permission_classes = [IsAuthenticated & IsInProjectReadOnlyOrAdmin] - - def get(self, request, *args, **kwargs): - p = get_object_or_404(Project, pk=self.kwargs['project_id']) - - include = set(request.GET.getlist('include')) - response = {} - - if not include or 'label' in include: - label_count, user_count = self.label_per_data(p) - response['label'] = label_count - # TODO: Make user_label count chart - response['user_label'] = user_count - - if not include or 'total' in include or 'remaining' in include or 'user' in include: - progress = self.progress() - response.update(progress) - - if not include or 'confirmed_count' in include: - confirmed_count = self.confirmed_count(p) - response['confirmed_count'] = confirmed_count - - if include: - response = {key: value for (key, value) in response.items() if key in include} - - return Response(response) - - def progress(self): - project = get_object_or_404(Project, pk=self.kwargs['project_id']) - examples = Example.objects.filter(project=self.kwargs['project_id']).values('id') - total = examples.count() - done = ExampleState.objects.count_done(examples) - done_by_user = ExampleState.objects.measure_member_progress(examples, project.users.all()) - remaining = total - done - return {'total': total, 'remaining': remaining, 'user': done_by_user} - - def label_per_data(self, project): - return {}, {} - - def confirmed_count(self, project): - confirmed_count = { - settings.ROLE_ANNOTATOR: 0, - settings.ROLE_ANNOTATION_APPROVER: 0, - settings.ROLE_PROJECT_ADMIN: 0, - } - # Todo: convert to one query - count_by_user = ExampleState.objects.filter(example__project=project)\ - .values('confirmed_by')\ - .annotate(total=Count('confirmed_by')) - for record in count_by_user: - mapping = RoleMapping.objects.get(project=project, user=record['confirmed_by']) - confirmed_count[mapping.role.name] += record['total'] - return confirmed_count - - class ProgressAPI(APIView): permission_classes = [IsAuthenticated & IsInProjectReadOnlyOrAdmin]