Skip to content

Commit

Permalink
AD-136 Added TotalBallotsHashed to ElectionResults model
Browse files Browse the repository at this point in the history
  • Loading branch information
morrisonbrett committed Oct 26, 2024
1 parent c0556db commit eaea264
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
7 changes: 7 additions & 0 deletions TrueVote.Api/Models/ElectionModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,13 @@ public class ElectionResults
[JsonProperty(nameof(TotalBallots), Required = Required.Always)]
public required int TotalBallots { get; set; }

[Required]
[Description("Total number of ballots hashed")]
[Range(0, int.MaxValue)]
[JsonPropertyName("TotalBallotsHashed")]
[JsonProperty(nameof(TotalBallotsHashed), Required = Required.Always)]
public required int TotalBallotsHashed { get; set; }

[Required]
[Description("List of Race Results")]
[DataType("List<RaceResult>")]
Expand Down
14 changes: 7 additions & 7 deletions TrueVote.Api/Services/Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,23 +97,19 @@ public async Task<IReadOnlyList<AccessCodeModel>> GetElectionAccessCodesByAccess

public async Task<ElectionResults> GetElectionResultsByElectionId([GraphQLName("ElectionId")] string ElectionId)
{
// Step 1: Fetch Ballots with the given ElectionId
var ballots = await _trueVoteDbContext.Ballots.Where(b => b.ElectionId == ElectionId).ToListAsync();

if (ballots.Count == 0)
{
return new ElectionResults
{
ElectionId = ElectionId,
TotalBallots = 0,
TotalBallotsHashed = 0,
Races = []
};
}

// Step 2: Count total ballots
var totalBallots = ballots.Count;

// Step 3: Aggregate results across all ballots
// Aggregate results across all ballots
var raceResults = ballots.SelectMany(b => b.Election.Races.Select(r => new { Ballot = b, Race = r }))
.GroupBy(br => new { br.Race.RaceId, br.Race.Name })
.Select(g => new RaceResult
Expand All @@ -132,10 +128,14 @@ public async Task<ElectionResults> GetElectionResultsByElectionId([GraphQLName("
})
.ToList();

// TODO Optimize this. Across large data sets this is a big .Contains (IN clause). Use a batching pattern or reporting server, etc.
var ballotHashes = await _trueVoteDbContext.BallotHashes.Where(bh => ballots.Select(b => b.BallotId).Contains(bh.BallotId)).ToListAsync();

return new ElectionResults
{
ElectionId = ElectionId,
TotalBallots = totalBallots,
TotalBallots = ballots.Count,
TotalBallotsHashed = ballotHashes.Count,
Races = raceResults
};
}
Expand Down
4 changes: 2 additions & 2 deletions scripts/sample_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@
"Name": "Presidential General Election - Santa Barbara County, California",
"Description": "Election for the great state of California. All eligibile California voters, use this election to make your voice heard!",
"HeaderImageUrl": "/static/sample-elections/ca_secretary_of_state.png",
"StartDate": "2024-10-22",
"EndDate": "2024-11-05",
"StartDate": "2024-10-22 13:00:00",
"EndDate": "2024-11-06 07:59:59",
"BaseRaces": [
{
"Name": "President and Vice President",
Expand Down

0 comments on commit eaea264

Please sign in to comment.