public void combine_candidate_votes_by_id()
        {
            var localResults    = new ElectionResultsData();
            var diasporaResults = new ElectionResultsData();

            localResults.Candidates = new List <CandidateConfig>
            {
                new CandidateConfig {
                    Id = "L1", Votes = 1
                },
                new CandidateConfig {
                    Id = "L2", Votes = 2
                },
            };
            diasporaResults.Candidates = new List <CandidateConfig>
            {
                new CandidateConfig {
                    Id = "L2", Votes = 5
                },
                new CandidateConfig {
                    Id = "L1", Votes = 10
                },
            };
            var combinedVotes = StatisticsAggregator.CombineResults(localResults, diasporaResults);

            combinedVotes.Candidates[0].Id.Should().Be("L1");
            combinedVotes.Candidates[0].Votes.Should().Be(11);
            combinedVotes.Candidates[1].Id.Should().Be("L2");
            combinedVotes.Candidates[1].Votes.Should().Be(7);
        }
        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);
        }
Example #3
0
        private async Task <Result <ElectionResultsData> > GetResultsByType(ResultsType type, string location)
        {
            try
            {
                string resultsType          = type.ConvertEnumToString();
                var    localResultsResponse = await _resultsRepository.GetLatestResults(Consts.LOCAL, resultsType);

                var diasporaResultsResponse = await _resultsRepository.GetLatestResults(Consts.DIASPORA, resultsType);

                if (localResultsResponse.IsFailure || diasporaResultsResponse.IsFailure)
                {
                    return(Result.Failure <ElectionResultsData>("Failed to retrieve data"));
                }
                var localResultsData    = JsonConvert.DeserializeObject <ElectionResultsData>(localResultsResponse.Value.StatisticsJson);
                var diasporaResultsData = JsonConvert.DeserializeObject <ElectionResultsData>(diasporaResultsResponse.Value.StatisticsJson);
                var electionResultsData = StatisticsAggregator.CombineResults(localResultsData, diasporaResultsData);

                electionResultsData.Candidates.FirstOrDefault(c => c.Id == "g1").Votes  = 3485292;
                electionResultsData.Candidates.FirstOrDefault(c => c.Id == "g2").Votes  = 527098;
                electionResultsData.Candidates.FirstOrDefault(c => c.Id == "g3").Votes  = 1384450;
                electionResultsData.Candidates.FirstOrDefault(c => c.Id == "g4").Votes  = 357014;
                electionResultsData.Candidates.FirstOrDefault(c => c.Id == "g5").Votes  = 2051725;
                electionResultsData.Candidates.FirstOrDefault(c => c.Id == "g6").Votes  = 32787;
                electionResultsData.Candidates.FirstOrDefault(c => c.Id == "g7").Votes  = 30884;
                electionResultsData.Candidates.FirstOrDefault(c => c.Id == "g8").Votes  = 30850;
                electionResultsData.Candidates.FirstOrDefault(c => c.Id == "g9").Votes  = 27769;
                electionResultsData.Candidates.FirstOrDefault(c => c.Id == "g10").Votes = 815201;
                electionResultsData.Candidates.FirstOrDefault(c => c.Id == "g11").Votes = 39192;
                electionResultsData.Candidates.FirstOrDefault(c => c.Id == "g12").Votes = 244275;
                electionResultsData.Candidates.FirstOrDefault(c => c.Id == "g13").Votes = 48662;
                electionResultsData.Candidates.FirstOrDefault(c => c.Id == "g14").Votes = 141316;
                _totalCounted = electionResultsData.Candidates.Sum(c => c.Votes);
                if (string.IsNullOrWhiteSpace(location) == false)
                {
                    if (location == "TOTAL")
                    {
                        return(Result.Ok(electionResultsData));
                    }
                    if (location == "DSPR")
                    {
                        return(Result.Ok(diasporaResultsData));
                    }
                    if (location == "RO")
                    {
                        return(Result.Ok(localResultsData));
                    }
                    foreach (var candidate in electionResultsData.Candidates)
                    {
                        candidate.Votes = candidate.Counties[location];
                    }
                }
                return(Result.Ok(electionResultsData));
            }
            catch (Exception e)
            {
                _logger.LogError(e, $"Failed to retrieve results for type {type} and location {location}");
                return(Result.Failure <ElectionResultsData>(e.Message));
            }
        }
        public async Task <ElectionResultsData> GetResults(ResultsType type)
        {
            string resultsType = ConvertEnumToString(type);

            var localResults = await _resultsRepository.GetLatestResults(Consts.LOCAL, resultsType);

            var diasporaResults = await _resultsRepository.GetLatestResults(Consts.DIASPORA, resultsType);

            var localResultsData    = JsonConvert.DeserializeObject <ElectionResultsData>(localResults.StatisticsJson);
            var diasporaResultsData = JsonConvert.DeserializeObject <ElectionResultsData>(diasporaResults.StatisticsJson);

            return(StatisticsAggregator.CombineResults(localResultsData, diasporaResultsData));
        }
        private async Task <Result <ElectionResultsData> > GetResultsByType(ResultsType type, string location)
        {
            try
            {
                string resultsType          = type.ConvertEnumToString();
                var    localResultsResponse = await _resultsRepository.GetLatestResults(Consts.LOCAL, resultsType);

                var diasporaResultsResponse = await _resultsRepository.GetLatestResults(Consts.DIASPORA, resultsType);

                if (localResultsResponse.IsFailure || diasporaResultsResponse.IsFailure)
                {
                    return(Result.Failure <ElectionResultsData>("Failed to retrieve data"));
                }
                var localResultsData    = JsonConvert.DeserializeObject <ElectionResultsData>(localResultsResponse.Value.StatisticsJson);
                var diasporaResultsData = JsonConvert.DeserializeObject <ElectionResultsData>(diasporaResultsResponse.Value.StatisticsJson);
                var electionResultsData = StatisticsAggregator.CombineResults(localResultsData, diasporaResultsData);
                if (string.IsNullOrWhiteSpace(location) == false)
                {
                    if (location == "TOTAL")
                    {
                        return(Result.Ok(electionResultsData));
                    }
                    if (location == "DSPR")
                    {
                        return(Result.Ok(diasporaResultsData));
                    }
                    if (location == "RO")
                    {
                        return(Result.Ok(localResultsData));
                    }
                    foreach (var candidate in electionResultsData.Candidates)
                    {
                        candidate.Votes = candidate.Counties[location];
                    }
                }
                return(Result.Ok(electionResultsData));
            }
            catch (Exception e)
            {
                _logger.LogError(e, $"Failed to retrieve results for type {type} and location {location}");
                return(Result.Failure <ElectionResultsData>(e.Message));
            }
        }
        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);
        }