Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix maps stats to properly count the parents stats #561

Merged
merged 1 commit into from
Mar 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions ureport/polls/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from django.contrib.auth.models import User
from django.core.cache import cache
from django.db import connection, models
from django.db.models import Count, F, Sum
from django.db.models import Count, F, Q, Sum
from django.utils import timezone, translation
from django.utils.text import slugify
from django.utils.translation import ugettext_lazy as _
Expand Down Expand Up @@ -998,17 +998,28 @@ def calculate_results(self, segment=None):
osm_id = boundary.get("osm_id").upper()

categories_results = (
PollStats.objects.filter(org=org, question=self, location__id=boundary["id"])
PollStats.objects.filter(org=org, question=self)
.filter(
Q(location__id=boundary["id"])
| Q(location__parent__id=boundary["id"])
| Q(location__parent__parent__id=boundary["id"])
)
.exclude(category=None)
.values("category__category")
.annotate(label=F("category__category"), count=Sum("count"))
.values("label", "count")
)
categories_results_dict = {elt["label"]: elt["count"] for elt in categories_results}

unset_count_stats = PollStats.objects.filter(
org=org, question=self, category=None, location__id=boundary["id"]
).aggregate(Sum("count"))
unset_count_stats = (
PollStats.objects.filter(org=org, question=self, category=None)
.filter(
Q(location__id=boundary["id"])
| Q(location__parent__id=boundary["id"])
| Q(location__parent__parent__id=boundary["id"])
)
.aggregate(Sum("count"))
)
unset_count = unset_count_stats.get("count__sum", 0) or 0

for categorie_label in categories_label:
Expand Down
28 changes: 26 additions & 2 deletions ureport/polls/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import absolute_import, division, print_function, unicode_literals

import json
import uuid
from datetime import datetime, timedelta

import pytz
Expand Down Expand Up @@ -2934,7 +2935,7 @@ def test_poll_results_stats(self):
lagos_boundary = Boundary.objects.create(
org=self.nigeria,
osm_id="R-LAGOS",
name="NigerLagosia",
name="Lagos",
parent=nigeria_boundary,
level=1,
geometry='{"type":"MultiPolygon", "coordinates":[[1, 2]]}',
Expand All @@ -2958,7 +2959,13 @@ def test_poll_results_stats(self):
geometry='{"type":"MultiPolygon", "coordinates":[[1, 2]]}',
)

yes_category = PollResponseCategory.objects.create(question=self.poll_question, category="Yes")
rule_uuid = uuid.uuid4()
yes_category = PollResponseCategory.objects.create(
question=self.poll_question, category="Yes", rule_uuid=rule_uuid
)

rule_uuid = uuid.uuid4()
PollResponseCategory.objects.create(question=self.poll_question, category="No", rule_uuid=rule_uuid)

PollResult.objects.create(
org=self.nigeria,
Expand Down Expand Up @@ -3006,6 +3013,23 @@ def test_poll_results_stats(self):
self.assertEqual(PollStats.objects.filter(category=yes_category).aggregate(Sum("count"))["count__sum"], 2)
self.assertEqual(PollStats.objects.filter(location=ikeja_boundary).aggregate(Sum("count"))["count__sum"], 1)

self.assertEqual(
self.poll_question.calculate_results()[0]["categories"],
[{"count": 2, "label": "Yes"}, {"count": 0, "label": "No"}],
)
self.assertEqual(
self.poll_question.calculate_results(segment=dict(location="State"))[0]["categories"],
[{"count": 1, "label": "Yes"}, {"count": 0, "label": "No"}],
)
self.assertEqual(
self.poll_question.calculate_results(segment=dict(location="District", parent="R-LAGOS"))[0]["categories"],
[{"count": 1, "label": "Yes"}, {"count": 0, "label": "No"}],
)
self.assertEqual(
self.poll_question.calculate_results(segment=dict(location="Ward", parent="R-OYO"))[0]["categories"],
[{"count": 1, "label": "Yes"}, {"count": 0, "label": "No"}],
)


class PollsTasksTest(UreportTest):
def setUp(self):
Expand Down