Skip to content

Commit

Permalink
Fix data issues analyzer
Browse files Browse the repository at this point in the history
This resolves two issues:

1) The subcondition `date >= brexit_date and count != 705` would have been true for every vote of the 10th term. (This is related to the changes in 731663.)

2) If a vote had multiple issues, only one of them would be persisted, because multiple fragments with the same primary key would have been emitted. (I believe this was a bug since the inception of this code.)
  • Loading branch information
tillprochaska committed Aug 6, 2024
1 parent 932e454 commit 9bef327
Showing 1 changed file with 30 additions and 24 deletions.
54 changes: 30 additions & 24 deletions backend/howtheyvote/analysis/votes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import datetime
from collections import defaultdict
from collections.abc import Iterable, Iterator
from itertools import chain

from unidecode import unidecode

Expand Down Expand Up @@ -174,46 +173,53 @@ class VoteDataIssuesAnalyzer:
def __init__(self, vote: Vote):
self.vote = vote

def run(self) -> Iterator[Fragment]:
return chain(
def run(self) -> Fragment:
issues = [
self.member_votes_count(),
self.empty_titles(),
)
]

def empty_titles(self) -> Iterator[Fragment]:
if self.vote.title or self.vote.procedure_title:
return
issues = [issue for issue in issues if issue is not None]

yield Fragment(
return Fragment(
model="Vote",
source_id=self.vote.id,
source_name=type(self).__name__,
group_key=self.vote.id,
data={"issues": [DataIssue.EMPTY_TITLES]},
data={"issues": issues},
)

def member_votes_count(self) -> Iterator[Fragment]:
brexit_date = datetime.date(2020, 2, 1)
tenth_term_date = datetime.date(2024, 7, 16)
def empty_titles(self) -> DataIssue | None:
if not self.vote.title and not self.vote.procedure_title:
return DataIssue.EMPTY_TITLES

return None

def member_votes_count(self) -> DataIssue | None:
date = self.vote.timestamp.date()
count = len(self.vote.member_votes)

issues = []
# 9th term, pre Brexit
if (
date >= datetime.date(2019, 7, 2)
and date < datetime.date(2020, 2, 1)
and count != 751
):
return DataIssue.MEMBER_VOTES_COUNT_MISMATCH

# 9th term, post Brexit
if (
(date < brexit_date and count != 751)
or (date >= brexit_date and count != 705)
or (date >= tenth_term_date and count != 720)
date >= datetime.date(2020, 2, 1)
and date < datetime.date(2024, 7, 16)
and count != 705
):
issues = [DataIssue.MEMBER_VOTES_COUNT_MISMATCH]
return DataIssue.MEMBER_VOTES_COUNT_MISMATCH

yield Fragment(
model="Vote",
source_id=self.vote.id,
source_name=type(self).__name__,
group_key=self.vote.id,
data={"issues": issues},
)
# 10th term
if date >= datetime.date(2024, 7, 16) and count != 720:
return DataIssue.MEMBER_VOTES_COUNT_MISMATCH

return None


class VoteGroupsDataIssuesAnalyzer:
Expand Down

0 comments on commit 9bef327

Please sign in to comment.