-
Notifications
You must be signed in to change notification settings - Fork 26
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
Server endpoint for surfacing discrepancies #2026
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
from collections import Counter | ||
from collections import Counter, defaultdict | ||
import logging | ||
from typing import Dict, List, Optional, Mapping, cast as typing_cast | ||
import enum | ||
|
@@ -727,3 +727,90 @@ def get_discrepancy_counts_by_jurisdiction(election: Election): | |
for jurisdiction in election.jurisdictions | ||
} | ||
) | ||
|
||
|
||
DiscrepanciesByJurisdiction = Dict[str, Dict[str, Dict[str, Dict[str, int]]]] | ||
# DiscrepanciesByJurisdiction = { | ||
# jurisdictionID: { | ||
# batchName: { | ||
# contestID: { | ||
# reportedVotes: {choiceID: int}, | ||
# auditedVotes: {choiceID: int}, | ||
# discrepancies: {choiceID: int}, | ||
# } | ||
# } | ||
|
||
|
||
def create_nested_discrepancies_dict(): | ||
return defaultdict( | ||
lambda: defaultdict(lambda: defaultdict(lambda: defaultdict(int))) | ||
) | ||
|
||
|
||
@api.route("/election/<election_id>/discrepancy", methods=["GET"]) | ||
@restrict_access([UserType.AUDIT_ADMIN]) | ||
def get_discrepancies_by_jurisdiction(election: Election): | ||
current_round = get_current_round(election) | ||
if not current_round: | ||
raise Conflict("Audit not started") | ||
|
||
discrepancies_by_jurisdiction: DiscrepanciesByJurisdiction = {} | ||
|
||
if election.audit_type == AuditType.BATCH_COMPARISON: | ||
discrepancies_by_jurisdiction = ( | ||
get_batch_comparison_audit_discrepancies_by_jurisdiction( | ||
election, current_round.id | ||
) | ||
) | ||
else: | ||
raise Conflict("Discrepancies are only implemented for batch comparison audits") | ||
|
||
return jsonify(discrepancies_by_jurisdiction) | ||
nikhilb4a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def get_batch_comparison_audit_discrepancies_by_jurisdiction( | ||
election: Election, round_id: str | ||
): | ||
nikhilb4a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
discrepancies_by_jurisdiction = create_nested_discrepancies_dict() | ||
|
||
# TODO: Add support for combined batches | ||
batch_keys_in_round = set( | ||
SampledBatchDraw.query.filter_by(round_id=round_id) | ||
.join(Batch) | ||
.join(Jurisdiction) | ||
.with_entities(Jurisdiction.name, Batch.name) | ||
.all() | ||
) | ||
jurisdiction_name_to_id = dict( | ||
Jurisdiction.query.filter_by(election_id=election.id).with_entities( | ||
Jurisdiction.name, Jurisdiction.id | ||
) | ||
) | ||
|
||
for contest in list(election.contests): | ||
audited_batch_results = sampled_batch_results( | ||
contest, include_non_rla_batches=True | ||
) | ||
reported_batch_results = batch_tallies(contest) | ||
|
||
for batch_key, audited_batch_result in audited_batch_results.items(): | ||
if batch_key not in batch_keys_in_round: | ||
continue | ||
|
||
audited_contest_result = audited_batch_result[contest.id] | ||
reported_contest_result = reported_batch_results[batch_key][contest.id] | ||
vote_deltas = batch_vote_deltas( | ||
reported_contest_result, audited_contest_result | ||
) | ||
nikhilb4a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if not vote_deltas: | ||
continue | ||
|
||
jurisdiction_name, batch_name = batch_key | ||
jurisdiction_id = jurisdiction_name_to_id[jurisdiction_name] | ||
discrepancies_by_jurisdiction[jurisdiction_id][batch_name][contest.id] = { | ||
"reportedVotes": reported_contest_result, | ||
"auditedVotes": audited_contest_result, | ||
"discrepancies": vote_deltas, | ||
} | ||
|
||
return discrepancies_by_jurisdiction | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately, this logic will need to take into account combined batches. See the discrepancy counting endpoint above and/or |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on the way you're using this, I don't think you need the last two levels of
defaultdict
. I think this will suffice:Also maybe just inline it since it's only called in one place?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yeah, thanks. I initially separated it out anticipating I would also need to use it for the future ballot comparison discrepancy function, but with the shorter init then inline is fine