public async Task <Result <LiveResultsResponse> > GetElectionResults(ResultsQuery resultsQuery) { try { if (string.IsNullOrWhiteSpace(resultsQuery.Source)) { var data = await CombineAllSources(resultsQuery); return(Result.Ok(CreateLiveResultsResponse(data))); } var response = await _resultsRepository.Get(resultsQuery.ElectionId, resultsQuery.Source, FileType.Results.ConvertEnumToString()); if (response.IsSuccess) { var electionResultsData = JsonConvert.DeserializeObject <ElectionResultsData>(response.Value.StatisticsJson); if (string.IsNullOrWhiteSpace(resultsQuery.County)) { return(Result.Ok(CreateLiveResultsResponse(electionResultsData))); } foreach (var candidate in electionResultsData.Candidates) { candidate.Votes = candidate.Counties[resultsQuery.County]; } return(Result.Ok(CreateLiveResultsResponse(electionResultsData))); } } catch (Exception e) { Console.WriteLine(e); throw; } return(Result.Ok(new LiveResultsResponse())); }
private async Task <ElectionResultsData> CombineAllSources(ResultsQuery resultsQuery) { var electionGetResult = _electionConfigurationSource.GetElectionById(resultsQuery.ElectionId); if (electionGetResult.IsFailure) { Log.LogWarning($"Could not retrieve election with id {resultsQuery.ElectionId} due to error {electionGetResult.Error}"); return(ElectionResultsData.Default); } var availableSources = electionGetResult.Value.Files.Where(f => f.Active && f.FileType == FileType.Results).Select(f => f.Name).ToList(); var data = new ElectionResultsData(); if (availableSources.Count == 0) { return(CreateEmptyElectionResultsData(resultsQuery, data)); } foreach (var source in availableSources) { var resultsResponse = await _resultsRepository.Get(resultsQuery.ElectionId, source, FileType.Results.ConvertEnumToString()); if (resultsResponse.IsSuccess) { var deserializedData = JsonConvert.DeserializeObject <ElectionResultsData>(resultsResponse.Value.StatisticsJson); data = StatisticsAggregator.CombineResults(data, deserializedData); } } if (data?.Candidates?.Count == 0) { return(CreateEmptyElectionResultsData(resultsQuery, data)); } return(data); }
public async Task <IActionResult> GetStudyResults(Guid id, [FromQuery] ResultsQuery query) { if (id == Guid.Empty) { return(BadRequest("Id cannot be empty")); } var results = await _repository.GetResultsAsync(id, query); return(Ok(results)); }
private ElectionResultsData CreateEmptyElectionResultsData(ResultsQuery resultsQuery, ElectionResultsData data) { var currentElectionResult = _electionConfigurationSource.GetElectionById(resultsQuery.ElectionId); if (currentElectionResult.IsSuccess) { data.Candidates = currentElectionResult.Value.Candidates; return(data); } else { Log.LogWarning(currentElectionResult.Error); return(ElectionResultsData.Default); } }
public async Task <ActionResult <LiveResultsResponse> > GetResults([FromQuery] string electionId = Consts.FirstElectionRound, string source = null, string county = null, FileType fileType = FileType.Results) { try { var resultsQuery = new ResultsQuery(fileType, source, county, electionId); var key = resultsQuery.ToString(); var result = await _appCache.GetOrAddAsync( key, () => _resultsAggregator.GetElectionResults(resultsQuery), DateTimeOffset.Now.AddSeconds(_config.Value.IntervalInSeconds)); if (result.IsFailure) { _appCache.Remove(key); Log.LogWarning(result.Error); return(BadRequest(result.Error)); } var voteCountStatisticsResult = await _appCache.GetOrAddAsync( Consts.ResultsCountKey + electionId, () => _resultsAggregator.GetVoteCountStatistics(electionId), DateTimeOffset.Now.AddSeconds(_config.Value.IntervalInSeconds)); if (voteCountStatisticsResult.IsSuccess) { result.Value.TotalCountedVotes = voteCountStatisticsResult.Value.TotalCountedVotes; result.Value.PercentageCounted = voteCountStatisticsResult.Value.Percentage; } if (electionId == Consts.SecondElectionRound && string.IsNullOrWhiteSpace(source) && string.IsNullOrWhiteSpace(county)) { result.Value.PercentageCounted = 100; result.Value.CanceledVotes = 182648; if (voteCountStatisticsResult.IsSuccess) { result.Value.TotalCountedVotes = voteCountStatisticsResult.Value.TotalCountedVotes - result.Value.CanceledVotes; } result.Value.Candidates[0].Votes = 6509135; result.Value.Candidates[0].Percentage = (decimal)66.09; result.Value.Candidates[1].Votes = 3339922; result.Value.Candidates[1].Percentage = (decimal)33.91; } return(result.Value); } catch (Exception e) { Log.LogError(e, "Exception encountered while retrieving results"); throw; } }
private async Task <ElectionResultsData> CombineAllSources(ResultsQuery resultsQuery) { var files = _electionConfigurationSource.GetListOfFilesWithElectionResults(); var availableSources = files.Where(f => f.Active && f.FileType == FileType.Results).Select(f => f.Name); var data = new ElectionResultsData(); foreach (var source in availableSources) { var resultsResponse = await _resultsRepository.Get(resultsQuery.ElectionId, source, FileType.Results.ConvertEnumToString()); if (resultsResponse.IsSuccess) { var deserializedData = JsonConvert.DeserializeObject <ElectionResultsData>(resultsResponse.Value.StatisticsJson); data = StatisticsAggregator.CombineResults(data, deserializedData); } } return(data); }
public async Task <PagedResult <Result> > GetResultsAsync(Guid studyId, ResultsQuery query) { var filter = Builders <Study> .Filter.Eq(x => x.Id, studyId); var result = await Collection.Find(filter).Project(x => x.Results).FirstOrDefaultAsync(); if (query.StationId != 0 && query.SensorId != 0) { return(result.Where(x => x.StationId == query.StationId && x.SensorId == query.SensorId) .OrderByDescending(x => x.Created) .Paginate(query)); } if (query.StationId != 0) { return(result.Where(x => x.StationId == query.StationId).OrderByDescending(x => x.Created) .Paginate(query)); } return(result.OrderByDescending(x => x.Created).Paginate(query)); }
public async Task <ActionResult <LiveResultsResponse> > GetResults([FromQuery] string electionId, string source = null, string county = null, FileType fileType = FileType.Results) { try { var resultsQuery = new ResultsQuery(fileType, source, county, electionId); var key = resultsQuery.ToString(); var result = await _appCache.GetOrAddAsync( key, () => _resultsAggregator.GetElectionResults(resultsQuery), DateTimeOffset.Now.AddSeconds(_config.Value.IntervalInSeconds)); if (result.IsFailure) { _appCache.Remove(key); _logger.LogError(result.Error); return(BadRequest(result.Error)); } return(result.Value); } catch (Exception e) { _logger.LogError(e, "Exception encountered while retrieving results"); throw; } }