Browse Source

Remove project from metrics view

pull/1644/head
Hironsan 2 years ago
parent
commit
f1720f49df
2 changed files with 7 additions and 8 deletions
  1. 8
      backend/api/managers.py
  2. 7
      backend/metrics/views.py

8
backend/api/managers.py

@ -3,22 +3,22 @@ from django.db.models import Count, Manager
class AnnotationManager(Manager): class AnnotationManager(Manager):
def calc_label_distribution(self, examples, users, labels):
def calc_label_distribution(self, examples, members, labels):
"""Calculate label distribution. """Calculate label distribution.
Args: Args:
examples: example queryset. examples: example queryset.
users: user queryset.
members: user queryset.
labels: label queryset. labels: label queryset.
Returns: Returns:
label distribution per user. label distribution per user.
Examples: Examples:
>>> self.calc_label_distribution(examples, users, labels)
>>> self.calc_label_distribution(examples, members, labels)
{'admin': {'positive': 10, 'negative': 5}} {'admin': {'positive': 10, 'negative': 5}}
""" """
distribution = {user.username: {label.text: 0 for label in labels} for user in users}
distribution = {member.username: {label.text: 0 for label in labels} for member in members}
items = self.filter(example_id__in=examples)\ items = self.filter(example_id__in=examples)\
.values('user__username', 'label__text')\ .values('user__username', 'label__text')\
.annotate(count=Count('label__text')) .annotate(count=Count('label__text'))

7
backend/metrics/views.py

@ -1,12 +1,11 @@
import abc import abc
from django.shortcuts import get_object_or_404
from rest_framework import status from rest_framework import status
from rest_framework.permissions import IsAuthenticated from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response from rest_framework.response import Response
from rest_framework.views import APIView from rest_framework.views import APIView
from api.models import Example, ExampleState, Project, Annotation, Label, Category, CategoryType, Span, SpanType
from api.models import Example, ExampleState, Annotation, Label, Category, CategoryType, Span, SpanType
from members.models import Member from members.models import Member
from members.permissions import IsInProjectReadOnlyOrAdmin from members.permissions import IsInProjectReadOnlyOrAdmin
@ -37,10 +36,10 @@ class LabelDistribution(abc.ABC, APIView):
label_type = Label label_type = Label
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
project = get_object_or_404(Project, pk=self.kwargs['project_id'])
labels = self.label_type.objects.filter(project=self.kwargs['project_id']) labels = self.label_type.objects.filter(project=self.kwargs['project_id'])
examples = Example.objects.filter(project=self.kwargs['project_id']).values('id') examples = Example.objects.filter(project=self.kwargs['project_id']).values('id')
data = self.model.objects.calc_label_distribution(examples, project.users.all(), labels)
members = Member.objects.filter(project=self.kwargs['project_id'])
data = self.model.objects.calc_label_distribution(examples, members, labels)
return Response(data=data, status=status.HTTP_200_OK) return Response(data=data, status=status.HTTP_200_OK)

Loading…
Cancel
Save