Exemplo n.º 1
0
        public async Task <LiveElectionInfo> ImportLocalityResults(Ballot ballot, ElectionResultsQuery query)
        {
            using (var dbContext = _serviceProvider.CreateScope().ServiceProvider.GetService <ApplicationDbContext>())
            {
                var county = await dbContext.Counties.FirstOrDefaultAsync(c => c.CountyId == query.CountyId);

                if (county == null)
                {
                    return(LiveElectionInfo.Default);
                }
                var url    = _liveElectionUrlBuilder.GetFileUrl(ballot.BallotType, ElectionDivision.County, county.ShortName, null);
                var stream = await _fileDownloader.Download(url.Value);

                var pollingSections = await ExtractCandidateResultsFromCsv(stream, new CsvIndexes(CsvMode.National));

                var locality =
                    await dbContext.Localities.FirstOrDefaultAsync(l => l.LocalityId == query.LocalityId);

                var sectionsForLocality       = pollingSections.Where(p => p.Siruta == locality.Siruta).ToList();
                LiveElectionInfo electionInfo = new LiveElectionInfo();
                foreach (var pollingSection in sectionsForLocality)
                {
                    electionInfo.ValidVotes += pollingSection.ValidVotes;
                    electionInfo.NullVotes  += pollingSection.NullVotes;
                }
                if (locality == null)
                {
                    return(LiveElectionInfo.Default);
                }

                var candidateResults = sectionsForLocality.SelectMany(s => s.Candidates).ToList();
                GroupResults(candidateResults, electionInfo);
                return(electionInfo);
            }
        }
        private void ImportLocalities(List <County> allCounties, List <Locality> allLocalities, List <Turnout> allTurnouts,
                                      List <PollingSection> pollingSectionsByCounty,
                                      Ballot ballot, ApplicationDbContext dbContext, List <Party> parties)
        {
            LiveElectionInfo localitiesElectionInfo = new LiveElectionInfo {
                Candidates = new List <CandidateResult>()
            };

            foreach (var county in allCounties.Where(c => c.CountyId != Consts.CapitalCity))
            {
                foreach (var locality in allLocalities.Where(l =>
                                                             l.CountyId == county.CountyId && allTurnouts.Any(t => t.LocalityId == l.LocalityId)))
                {
                    var sectionsForLocality       = pollingSectionsByCounty.Where(p => p.Siruta == locality.Siruta).ToList();
                    LiveElectionInfo electionInfo = new LiveElectionInfo();
                    foreach (var pollingSection in sectionsForLocality)
                    {
                        electionInfo.ValidVotes += pollingSection.ValidVotes;
                        electionInfo.NullVotes  += pollingSection.NullVotes;
                    }

                    var candidateResults = sectionsForLocality.SelectMany(s => s.Candidates).ToList();
                    localitiesElectionInfo.Candidates.AddRange(JsonConvert.DeserializeObject <List <CandidateResult> >(JsonConvert.SerializeObject(candidateResults)));
                    GroupResults(candidateResults, electionInfo);
                    var turnout =
                        allTurnouts.FirstOrDefault(t => t.LocalityId == locality.LocalityId && t.BallotId == ballot.BallotId);
                    UpdateResults(dbContext, electionInfo, turnout, parties);
                    localitiesElectionInfo.ValidVotes += electionInfo.ValidVotes;
                    localitiesElectionInfo.TotalVotes += electionInfo.TotalVotes;
                    localitiesElectionInfo.NullVotes  += electionInfo.NullVotes;
                }
            }
        }
        private async Task ImportCapitalCityFinalResults(Ballot ballot, List <Turnout> allTurnouts,
                                                         ApplicationDbContext dbContext,
                                                         List <Locality> allLocalities, List <Party> parties)
        {
            try
            {
                var sectors            = allLocalities.Where(l => l.CountyId == Consts.CapitalCity);
                var capitalCityResults = new LiveElectionInfo {
                    Candidates = new List <CandidateResult>()
                };
                foreach (var sector in sectors.Where(s => s.Name.ToLower().StartsWith("toate") == false))
                {
                    var index = int.Parse(sector.Name.Split(" ").Last());

                    var sectorResults = await ImportCapitalCityResults(ballot, index);

                    var sectorTurnout =
                        allTurnouts.FirstOrDefault(t => t.LocalityId == sector.LocalityId && t.BallotId == ballot.BallotId);
                    capitalCityResults.Candidates.AddRange(sectorResults.Candidates);
                    capitalCityResults.TotalVotes += sectorTurnout.TotalVotes;
                    capitalCityResults.ValidVotes += sectorResults.ValidVotes;
                    capitalCityResults.NullVotes  += sectorResults.NullVotes;
                    UpdateResults(dbContext, sectorResults, sectorTurnout, parties);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
        private async Task ImportDiasporaFinalResults(Ballot ballot, List <Turnout> allTurnouts,
                                                      ApplicationDbContext dbContext, List <Locality> allLocalities, List <Party> parties)
        {
            var allCountries = await dbContext.Countries.ToListAsync();

            var diasporaElectionInfo = new LiveElectionInfo {
                Candidates = new List <CandidateResult>()
            };

            foreach (var turnout in allTurnouts.Where(t => t.Division == ElectionDivision.Diaspora_Country))
            {
                var country      = allCountries.FirstOrDefault(c => c.Id == turnout.CountryId);
                var electionInfo = await ImportCountryResults(new ElectionResultsQuery
                {
                    CountryId = country.Id,
                    Division  = ElectionDivision.Diaspora_Country,
                    BallotId  = ballot.BallotId
                }, ballot);

                diasporaElectionInfo.Candidates.AddRange(JsonConvert.DeserializeObject <List <CandidateResult> >(JsonConvert.SerializeObject(electionInfo.Candidates)));
                UpdateResults(dbContext, electionInfo, turnout, parties);
                diasporaElectionInfo.ValidVotes += electionInfo.ValidVotes;
                diasporaElectionInfo.TotalVotes += turnout.TotalVotes;
                diasporaElectionInfo.NullVotes  += electionInfo.NullVotes;
            }

            GroupResults(diasporaElectionInfo.Candidates, diasporaElectionInfo);
            UpdateResults(dbContext, diasporaElectionInfo, allTurnouts.FirstOrDefault(d => d.Division == ElectionDivision.Diaspora && d.BallotId == ballot.BallotId), parties);
        }
Exemplo n.º 5
0
        private static void GroupResults(List <CandidateResult> results, LiveElectionInfo electionInfo)
        {
            var grouped          = results.GroupBy(c => c.Name).OrderByDescending(p => p.Sum(p => p.Votes)).ToList();
            var candidateResults = grouped.Select(g =>
            {
                var candidate   = g.FirstOrDefault();
                candidate.Votes = g.Sum(c => c.Votes);
                return(candidate);
            }).ToList();

            electionInfo.Candidates = candidateResults;
            electionInfo.TotalVotes = candidateResults.Sum(c => c.Votes);
        }
        private void UpdateResults(ApplicationDbContext dbContext, LiveElectionInfo electionInfo, Turnout turnout,
                                   List <Party> parties)
        {
            PrepareCandidates(electionInfo.Candidates, turnout, parties);

            if (turnout != null)
            {
                turnout.ValidVotes = electionInfo.Candidates.Sum(c => c.Votes);
                turnout.NullVotes  = electionInfo.NullVotes;
                dbContext.Update(turnout);
            }
            dbContext.CandidateResults.AddRange(electionInfo.Candidates);
        }
        public async Task <LiveElectionInfo> AggregateNationalResults(ElectionResultsQuery query, Ballot ballot)
        {
            var electionInfo = new LiveElectionInfo();

            using (var dbContext = _serviceProvider.CreateScope().ServiceProvider.GetService <ApplicationDbContext>())
            {
                var dbCounties = await dbContext.Counties.ToListAsync();

                var turnouts = await dbContext.Turnouts.Where(t => t.BallotId == ballot.BallotId).ToListAsync();

                var results            = new List <CandidateResult>();
                var capitalCityResults = await ImportCapitalCityResults(ballot);

                results.AddRange(capitalCityResults.Candidates);

                electionInfo.ValidVotes += capitalCityResults.ValidVotes;
                electionInfo.NullVotes  += capitalCityResults.NullVotes;

                foreach (var countyTurnout in turnouts.Where(t => t.Division == ElectionDivision.County && t.CountyId != Consts.CapitalCity))
                {
                    var county = dbCounties.First(c => c.CountyId == countyTurnout.CountyId);
                    if (county == null)
                    {
                        continue;
                    }
                    var url = _liveElectionUrlBuilder.GetFileUrl(ballot.BallotType, ElectionDivision.County, county.ShortName, null);

                    var countyResults = await Import(url.Value, new CsvIndexes(CsvMode.National));

                    if (countyResults.IsSuccess)
                    {
                        results.AddRange(countyResults.Value.Candidates);
                        electionInfo.ValidVotes += countyResults.Value.ValidVotes;
                        electionInfo.NullVotes  += countyResults.Value.NullVotes;
                    }
                }

                var diasporaResults = await AggregateDiasporaResults(query, ballot);

                electionInfo.ValidVotes += diasporaResults.ValidVotes;
                electionInfo.NullVotes  += diasporaResults.NullVotes;
                GroupResults(results.Concat(diasporaResults.Candidates).ToList(), electionInfo);
                //PrepareCandidates(electionInfo.Candidates, query, ballot);
                //await UpdateResults(dbContext, electionInfo.Candidates);
            }

            return(electionInfo);
        }
        private async Task <List <CandidateResult> > GetDiasporaResults(Result <string> url, Country country,
                                                                        LiveElectionInfo electionInfo, CsvIndexes csvIndexes)
        {
            var stream = await _fileDownloader.Download(url.Value);

            var pollingSections = await ExtractCandidateResultsFromCsv(stream, csvIndexes);

            var sectionsForLocality = pollingSections.Where(p => p.Country.NormalizeCountryName().EqualsIgnoringAccent(country.Name)).ToList();

            foreach (var pollingSection in sectionsForLocality)
            {
                electionInfo.ValidVotes += pollingSection.ValidVotes;
                electionInfo.NullVotes  += pollingSection.NullVotes;
            }

            return(sectionsForLocality.SelectMany(s => s.Candidates).ToList());
        }
Exemplo n.º 9
0
        public async Task <LiveElectionInfo> ImportCapitalCityResults(Ballot ballot, int?sector = null)
        {
            var electionInfo = new LiveElectionInfo();
            var results      = new List <CandidateResult>();

            if (sector != null)
            {
                await GetResultsForSector(ballot, sector.Value, results, electionInfo);
            }
            else
            {
                for (int sectorIndex = 1; sectorIndex <= 6; sectorIndex++)
                {
                    await GetResultsForSector(ballot, sectorIndex, results, electionInfo);
                }
            }
            GroupResults(results, electionInfo);

            return(electionInfo);
        }
Exemplo n.º 10
0
        public async Task <LiveElectionInfo> ImportCountryResults(ElectionResultsQuery query, Ballot ballot)
        {
            using (var dbContext = _serviceProvider.CreateScope().ServiceProvider.GetService <ApplicationDbContext>())
            {
                var country = await dbContext.Countries.FirstOrDefaultAsync(c => c.Id == query.CountryId);

                if (country == null)
                {
                    return(LiveElectionInfo.Default);
                }
                LiveElectionInfo electionInfo = new LiveElectionInfo();
                var url             = _liveElectionUrlBuilder.GetFileUrl(ballot.BallotType, ElectionDivision.Diaspora, null, null);
                var diasporaResults = await GetDiasporaResults(url, country, electionInfo, new CsvIndexes(CsvMode.Diaspora));

                var correspondenceUrl     = _liveElectionUrlBuilder.GetCorrespondenceUrl(ballot.BallotType, ElectionDivision.Diaspora);
                var correspondenceResults = await GetDiasporaResults(correspondenceUrl, country, electionInfo, new CsvIndexes(CsvMode.Correspondence));

                GroupResults(diasporaResults.Concat(correspondenceResults).ToList(), electionInfo);
                return(electionInfo);
            }
        }
Exemplo n.º 11
0
        public async Task <LiveElectionInfo> AggregateDiasporaResults(ElectionResultsQuery query, Ballot ballot)
        {
            var electionInfo = new LiveElectionInfo();

            var diasporaUrl     = _liveElectionUrlBuilder.GetFileUrl(ballot.BallotType, ElectionDivision.Diaspora, null, null);
            var diasporaResults = await _appCache.GetOrAddAsync(
                $"{diasporaUrl}", () => Import(diasporaUrl.Value, new CsvIndexes(CsvMode.Diaspora)),
                DateTimeOffset.Now.AddMinutes(_liveElectionSettings.CsvCacheInMinutes));

            electionInfo.ValidVotes += diasporaResults.Value.ValidVotes;
            electionInfo.NullVotes  += diasporaResults.Value.NullVotes;
            var url = _liveElectionUrlBuilder.GetCorrespondenceUrl(ballot.BallotType, ElectionDivision.Diaspora);
            var correspondenceResults = await _appCache.GetOrAddAsync(
                $"{url}", () => Import(url.Value, new CsvIndexes(CsvMode.Correspondence)),
                DateTimeOffset.Now.AddMinutes(_liveElectionSettings.CsvCacheInMinutes));

            electionInfo.ValidVotes += correspondenceResults.Value.ValidVotes;
            electionInfo.NullVotes  += correspondenceResults.Value.NullVotes;
            GroupResults(diasporaResults.Value.Candidates.Concat(correspondenceResults.Value.Candidates).ToList(), electionInfo);

            return(electionInfo);
        }
Exemplo n.º 12
0
        private async Task GetResultsForSector(Ballot ballot, int sectorIndex, List <CandidateResult> results, LiveElectionInfo electionInfo)
        {
            var url           = _liveElectionUrlBuilder.GetFileUrl(ballot.BallotType, ElectionDivision.County, $"s{sectorIndex}", null);
            var sectorResults = await Import(url.Value, new CsvIndexes(CsvMode.National));

            if (sectorResults.IsSuccess)
            {
                results.AddRange(sectorResults.Value.Candidates);
                electionInfo.ValidVotes += sectorResults.Value.ValidVotes;
                electionInfo.NullVotes  += sectorResults.Value.NullVotes;
            }
        }
Exemplo n.º 13
0
        private async Task <List <CandidateResult> > GetDiasporaResults(Result <string> url, Country country, LiveElectionInfo electionInfo)
        {
            List <CandidateResult> candidateResults = new List <CandidateResult>();
            var stream = await _fileDownloader.Download(url.Value);

            var pollingSections = await ExtractCandidateResultsFromCsv(stream);

            var sectionsForLocality = pollingSections.Where(p => p.Country.EqualsIgnoringAccent(country.Name)).ToList();

            foreach (var pollingSection in sectionsForLocality)
            {
                electionInfo.ValidVotes += pollingSection.ValidVotes;
                electionInfo.NullVotes  += pollingSection.NullVotes;
            }

            Console.WriteLine($"Added {country.Name}");

            candidateResults = sectionsForLocality.SelectMany(s => s.Candidates).ToList();
            return(candidateResults);
        }