diff --git a/TrueVote.Api/Models/ElectionModel.cs b/TrueVote.Api/Models/ElectionModel.cs index 9189b0d..3c55e77 100644 --- a/TrueVote.Api/Models/ElectionModel.cs +++ b/TrueVote.Api/Models/ElectionModel.cs @@ -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")] diff --git a/TrueVote.Api/Services/Query.cs b/TrueVote.Api/Services/Query.cs index 36883d4..a688dac 100644 --- a/TrueVote.Api/Services/Query.cs +++ b/TrueVote.Api/Services/Query.cs @@ -97,23 +97,19 @@ public async Task> GetElectionAccessCodesByAccess public async Task 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 @@ -132,10 +128,14 @@ public async Task 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 }; } diff --git a/scripts/sample_data.json b/scripts/sample_data.json index 156e288..175a656 100644 --- a/scripts/sample_data.json +++ b/scripts/sample_data.json @@ -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",