Browse Source

Remove StatisticsAPI

pull/1619/head
Hironsan 3 years ago
parent
commit
bd4fa827f2
3 changed files with 3 additions and 138 deletions
  1. 75
      backend/api/tests/api/test_statistics.py
  2. 5
      backend/api/urls.py
  3. 61
      backend/api/views/statistics.py

75
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):

5
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(),

61
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]

Loading…
Cancel
Save