Beispiel #1
0
        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))
                {
                    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);

                    if (countyResults.IsSuccess)
                    {
                        Console.WriteLine($"Added {county.Name}");
                        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);
            }

            return(electionInfo);
        }
Beispiel #2
0
        private async Task <LiveElectionInfo> GetCandidatesFromDb(ElectionResultsQuery query, Ballot ballot,
                                                                  ApplicationDbContext dbContext)
        {
            LiveElectionInfo liveElectionInfo = new LiveElectionInfo();

            if (ballot.Election.Live)
            {
                if (query.Division == ElectionDivision.National)
                {
                    return(await _appCache.GetOrAddAsync(
                               $"{ballot.BallotType}-national", () => _resultsCrawler.AggregateNationalResults(query, ballot),
                               DateTimeOffset.Now.AddMinutes(_settings.CsvCacheInMinutes)));
                }
                if (query.Division == ElectionDivision.Diaspora)
                {
                    return(await _appCache.GetOrAddAsync(
                               $"{ballot.BallotType}-diaspora", () => _resultsCrawler.AggregateDiasporaResults(query, ballot),
                               DateTimeOffset.Now.AddMinutes(_settings.CsvCacheInMinutes)));
                }
                if (query.Division == ElectionDivision.Diaspora_Country)
                {
                    return(await _appCache.GetOrAddAsync(
                               $"{ballot.BallotType}-diaspora_country-{query.CountryId}", () => _resultsCrawler.ImportCountryResults(query, ballot),
                               DateTimeOffset.Now.AddMinutes(_settings.CsvCacheInMinutes)));
                }
                var county = await dbContext.Counties.FirstOrDefaultAsync(c => c.CountyId == query.CountyId);

                if (county.CountyId.IsCapitalCity())
                {
                    int?index = null;
                    if (query.Division == ElectionDivision.Locality)
                    {
                        var sector = await dbContext.Localities.FirstOrDefaultAsync(l => l.LocalityId == query.LocalityId);

                        index = int.Parse(sector.Name.Split(" ").Last());
                    }
                    return(await _resultsCrawler.ImportCapitalCityResults(ballot, index));
                }

                if (query.Division == ElectionDivision.Locality)
                {
                    return(await _resultsCrawler.ImportLocalityResults(ballot, query));
                }
                var url = _urlBuilder.GetFileUrl(ballot.BallotType, query.Division, county?.ShortName,
                                                 query.LocalityId);
                if (url.IsFailure)
                {
                    return(LiveElectionInfo.Default);
                }
                var result = await _appCache.GetOrAddAsync(
                    $"{url}", () => _resultsCrawler.Import(url.Value),
                    DateTimeOffset.Now.AddMinutes(_settings.CsvCacheInMinutes));

                if (result.IsSuccess)
                {
                    return(result.Value);
                }
                return(LiveElectionInfo.Default);
            }
            if (ballot.Election.Category == ElectionCategory.Local && query.CountyId.GetValueOrDefault().IsCapitalCity() == false)
            {
                if (!ballot.AllowsDivision(query.Division, query.LocalityId.GetValueOrDefault()) && !ballot.Election.Live)
                {
                    var aggregatedVotes = await RetrieveAggregatedVotes(dbContext, query, ballot);

                    liveElectionInfo.Candidates = aggregatedVotes;
                    liveElectionInfo.Aggregated = true;
                    return(liveElectionInfo);
                }
            }

            var results = await GetCandidateResultsFromQueryAndBallot(query, ballot, dbContext);

            liveElectionInfo.Candidates = results;
            return(liveElectionInfo);
        }
Beispiel #3
0
        private async Task <LiveElectionInfo> GetCandidatesFromDb(ElectionResultsQuery query, Ballot ballot,
                                                                  ApplicationDbContext dbContext)
        {
            LiveElectionInfo liveElectionInfo = new LiveElectionInfo();

            if (ballot.Election.Live && ballot.AllowsDivision(query.Division, query.LocalityId.GetValueOrDefault()))
            {
                try
                {
                    int?siruta;
                    var county = await _territoryRepository.GetCountyById(query.CountyId);

                    if (query.LocalityId.GetValueOrDefault().IsCapitalCity() && query.Division == ElectionDivision.County)
                    {
                        siruta = null;
                    }
                    else
                    {
                        var locality = await _territoryRepository.GetLocalityById(query.LocalityId);

                        if (locality.IsFailure)
                        {
                            return(LiveElectionInfo.Default);
                        }
                        siruta = locality.IsSuccess ? locality.Value?.Siruta : null;
                    }
                    if (county.IsFailure)
                    {
                        return(LiveElectionInfo.Default);
                    }

                    var countyShortName = county.IsSuccess ? county.Value.ShortName : null;
                    var url             = _liveElectionUrlBuilder.GetFileUrl(ballot.BallotType, query.Division, countyShortName, siruta);
                    if (url.IsFailure)
                    {
                        return(LiveElectionInfo.Default);
                    }
                    liveElectionInfo = await _csvDownloaderJob.GetCandidatesFromUrl(url.Value);

                    var candidates = liveElectionInfo.Candidates;
                    var parties    = await dbContext.Parties.ToListAsync();

                    var candidatesForThisElection = await GetCandidateResultsFromQueryAndBallot(query, ballot, dbContext);

                    var dbCandidates = new List <CandidateResult>();
                    if (candidates == null)
                    {
                        liveElectionInfo.Candidates = dbCandidates;
                        return(liveElectionInfo);
                    }
                    foreach (var candidate in candidates)
                    {
                        dbCandidates.Add(PopulateCandidateData(candidatesForThisElection, candidate, parties, ballot));
                    }

                    liveElectionInfo.Candidates = dbCandidates;
                    return(liveElectionInfo);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Probably there are no votes ");
                    Console.WriteLine(e);
                    return(new LiveElectionInfo());
                }
            }
            if (ballot.Election.Category == ElectionCategory.Local && query.CountyId.GetValueOrDefault().IsCapitalCity() == false)
            {
                if (!ballot.AllowsDivision(query.Division, query.LocalityId.GetValueOrDefault()) && !ballot.Election.Live)
                {
                    var aggregatedVotes = await RetrieveAggregatedVotes(query, ballot);

                    liveElectionInfo.Candidates = aggregatedVotes;
                    return(liveElectionInfo);
                }
            }

            var results = await GetCandidateResultsFromQueryAndBallot(query, ballot, dbContext);

            liveElectionInfo.Candidates = results;
            return(liveElectionInfo);
        }