You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
2.4 KiB

  1. from django.db.models import Count, Manager
  2. class AnnotationManager(Manager):
  3. def calc_label_distribution(self, examples, members, labels):
  4. """Calculate label distribution.
  5. Args:
  6. examples: example queryset.
  7. members: user queryset.
  8. labels: label queryset.
  9. Returns:
  10. label distribution per user.
  11. Examples:
  12. >>> self.calc_label_distribution(examples, members, labels)
  13. {'admin': {'positive': 10, 'negative': 5}}
  14. """
  15. distribution = {member.username: {label.text: 0 for label in labels} for member in members}
  16. items = self.filter(example_id__in=examples)\
  17. .values('user__username', 'label__text')\
  18. .annotate(count=Count('label__text'))
  19. for item in items:
  20. username = item['user__username']
  21. label = item['label__text']
  22. count = item['count']
  23. distribution[username][label] = count
  24. return distribution
  25. class ExampleManager(Manager):
  26. def bulk_create(self, objs, batch_size=None, ignore_conflicts=False):
  27. super().bulk_create(objs, batch_size=batch_size, ignore_conflicts=ignore_conflicts)
  28. uuids = [data.uuid for data in objs]
  29. examples = self.in_bulk(uuids, field_name='uuid')
  30. return [examples[uid] for uid in uuids]
  31. class ExampleStateManager(Manager):
  32. def count_done(self, examples, user=None):
  33. if user:
  34. queryset = self.filter(example_id__in=examples, confirmed_by=user)
  35. else:
  36. queryset = self.filter(example_id__in=examples)
  37. return queryset.distinct().values('example').count()
  38. def measure_member_progress(self, examples, members):
  39. done_count = self.filter(example_id__in=examples)\
  40. .values('confirmed_by__username')\
  41. .annotate(total=Count('confirmed_by'))
  42. response = {
  43. 'total': examples.count(),
  44. 'progress': [
  45. {
  46. 'user': obj['confirmed_by__username'],
  47. 'done': obj['total']
  48. } for obj in done_count
  49. ]
  50. }
  51. members_with_progress = {o['confirmed_by__username'] for o in done_count}
  52. for member in members:
  53. if member.username not in members_with_progress:
  54. response['progress'].append({
  55. 'user': member.username,
  56. 'done': 0
  57. })
  58. return response