Exemplo n.º 1
0
    def profile_stats(self):
        from mresponse.moderations.models import Moderation

        current_window_start = datetime.combine(
            (now() - timedelta(days=STATS_ROLLING_WINDOW_SIZE)).date(),
            datetime.min.time(),
            pytz.UTC,
        )

        start_previous_window = current_window_start - timedelta(
            days=STATS_ROLLING_WINDOW_SIZE)
        end_previous_window = current_window_start
        current = Moderation.objects.filter(
            response__author=self.user,
            submitted_at__gte=current_window_start).aggregate(
                total_moderations_count=Count("id"),
                positive_in_tone_count=Count("id",
                                             filter=Q(positive_in_tone=True)),
                addressing_the_issue_count=Count(
                    "id", filter=Q(addressing_the_issue=True)),
                personal_count=Count("id", filter=Q(personal=True)),
            )

        previous = Moderation.objects.filter(
            response__author=self.user,
            submitted_at__lt=end_previous_window,
            submitted_at__gte=start_previous_window,
        ).aggregate(
            total_moderations_count=Count("id"),
            positive_in_tone_count=Count("id",
                                         filter=Q(positive_in_tone=True)),
            addressing_the_issue_count=Count(
                "id", filter=Q(addressing_the_issue=True)),
            personal_count=Count("id", filter=Q(personal=True)),
        )

        return dict(
            current_count=current["total_moderations_count"],
            previous_count=previous["total_moderations_count"],
            positive_in_tone_count=current["positive_in_tone_count"],
            positive_in_tone_change=change_calculation(
                previous["positive_in_tone_count"],
                current["positive_in_tone_count"]),
            addressing_the_issue_count=current["addressing_the_issue_count"],
            addressing_the_issue_change=change_calculation(
                previous["addressing_the_issue_count"],
                current["addressing_the_issue_count"],
            ),
            personal_count=current["personal_count"],
            personal_change=change_calculation(previous["personal_count"],
                                               current["personal_count"]),
        )
Exemplo n.º 2
0
 def test_value_1_is_less_than_value_2(self):
     self.assertEqual(change_calculation(1, 10), 9.0)
Exemplo n.º 3
0
 def test_value_1_is_zero(self):
     self.assertEqual(change_calculation(0, 10), None)
Exemplo n.º 4
0
 def test_value_1_is_greater_than_value_2(self):
     self.assertEqual(change_calculation(10, 1), -0.9)