示例#1
0
        public async Task <Result <ElectionResultsData> > Parse(ElectionResultsData electionResultsData, string csvContent,
                                                                ElectionResultsFile file)
        {
            try
            {
                if (electionResultsData == null)
                {
                    electionResultsData = new ElectionResultsData();
                }
                var electionsConfigResponse = _electionConfigurationSource.GetElectionById(file.ElectionId);
                var electionsConfig         = electionsConfigResponse.Value;
                var pollingStations         = await CalculateVotesByCounty(csvContent, electionsConfig, file);

                var sumOfVotes = electionResultsData.Candidates.Sum(c => c.Votes);
                electionResultsData.Candidates = StatisticsAggregator.CalculatePercentagesForCandidates(electionResultsData.Candidates, sumOfVotes);
                var counties = pollingStations.GroupBy(g => g.County).Select(c => c.Key).OrderBy(c => c).ToList();
                foreach (var candidate in electionResultsData.Candidates)
                {
                    foreach (var county in counties)
                    {
                        var votesForCandidate = pollingStations.Where(p => p.County == county)
                                                .Sum(p => p.Candidates.FirstOrDefault(c => c.Id == candidate.Id)?.Votes ?? 0);
                        candidate.Counties[county] = votesForCandidate;
                    }
                }

                return(Result.Ok(electionResultsData));
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return(Result.Failure <ElectionResultsData>(e.Message));
            }
        }
示例#2
0
 public Task <Result <ElectionResultsData> > Parse(ElectionResultsData electionResultsData, string csvContent,
                                                   ElectionResultsFile file)
 {
     WasInvoked = true;
     electionResultsData.Candidates = new List <CandidateConfig>();
     return(Task.FromResult(Result.Ok(electionResultsData)));
 }
        public static ElectionStatistics BuildElectionStatistics(ElectionResultsFile file, ElectionResultsData electionResultsData)
        {
            var electionStatistics = new ElectionStatistics();

            electionStatistics.Id             = $"{DateTime.MaxValue.Ticks - DateTime.UtcNow.Ticks:D19}";
            electionStatistics.Type           = file.FileType.ConvertEnumToString();
            electionStatistics.Source         = file.ResultsSource;
            electionStatistics.Timestamp      = file.Timestamp;
            electionStatistics.StatisticsJson = JsonConvert.SerializeObject(electionResultsData);
            electionStatistics.ElectionId     = file.ElectionId;
            return(electionStatistics);
        }
示例#4
0
        public async Task <Result <VoteMonitoringStats> > GetVoteMonitoringStats(ElectionResultsFile monitoringJson)
        {
            var httpClient = new HttpClient();
            var json       = await httpClient.GetStringAsync(monitoringJson.URL);

            var response = JsonConvert.DeserializeObject <List <MonitoringInfo> >(json);

            return(Result.Ok(new VoteMonitoringStats
            {
                Statistics = response,
                ElectionId = monitoringJson.ElectionId
            }));
        }
示例#5
0
        public async Task ProcessStream(Stream csvStream, ElectionResultsFile file)
        {
            var csvContent = await ReadCsvContent(csvStream);

            var aggregationResult = await _statisticsAggregator.RetrieveElectionData(csvContent, file);

            if (aggregationResult.IsSuccess)
            {
                var electionStatistics = FileNameParser.BuildElectionStatistics(file, aggregationResult.Value);
                Log.LogInformation($"Inserting results from {file.Name} with timestamp {electionStatistics.Timestamp}");
                await _resultsRepository.InsertResults(electionStatistics);
            }
        }
示例#6
0
        public virtual async Task <Result <VoterTurnout> > GetVoterTurnoutFromBEC(ElectionResultsFile turnoutJson)
        {
            try
            {
                var httpClient   = new HttpClient();
                var voterTurnout = await GetVoterTurnoutByUrl(httpClient, turnoutJson.URL);

                voterTurnout.ElectionId = turnoutJson.ElectionId;
                return(Result.Ok(voterTurnout));
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return(Result.Failure <VoterTurnout>(e.Message));
            }
        }
        public async Task ProcessFile(ElectionResultsFile file)
        {
            try
            {
                var stream = await DownloadFile(file.URL);

                var uploadResponse = await UploadFileToStorage(stream, file.Name);

                if (uploadResponse.IsSuccess)
                {
                    stream.Seek(0, SeekOrigin.Begin);
                    await _fileProcessor.ProcessStream(stream, file);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
示例#8
0
 private async Task ProcessCsv(ElectionResultsFile file, long timestamp)
 {
     file.Name =
         $"{file.ResultsType.ConvertEnumToString()}_{file.ResultsLocation.ConvertEnumToString()}_{timestamp}.csv";
     await _bucketUploader.UploadFromUrl(file);
 }
 protected override Task PopulateCandidatesListWithVotes(string csvContent, ElectionResultsData electionResultsData, ElectionResultsFile file)
 {
     return(Task.CompletedTask);
 }
示例#10
0
        public async Task UploadFromUrl(ElectionResultsFile file)
        {
            var stream = await DownloadFile(file.URL);

            await UploadFileToStorage(stream, file.Name);
        }
 public Task <Result <ElectionResultsData> > Parse(ElectionResultsData electionResultsData, string csvContent,
                                                   ElectionResultsFile file)
 {
     return(Task.FromResult(Result.Ok(electionResultsData)));
 }
        public async Task <Result <ElectionResultsData> > RetrieveElectionData(string csvContent, ElectionResultsFile file)
        {
            _logger.LogInformation($"Retrieving data from csv");
            try
            {
                var electionResults = new ElectionResultsData();
                foreach (var csvParser in CsvParsers)
                {
                    await csvParser.Parse(electionResults, csvContent, file);
                }

                return(Result.Ok(electionResults));
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Failed to process data from csv");
                _logger.LogInformation(csvContent.Substring(0, 1000));
                return(Result.Failure <ElectionResultsData>(e.Message));
            }
        }
示例#13
0
        protected virtual async Task <List <PollingStation> > CalculateVotesByCounty(string csvContent,
                                                                                     Election election, ElectionResultsFile file)
        {
            var csvParser       = new CsvParser(new StringReader(csvContent));
            var pollingStations = new List <PollingStation>();
            var headers         = (await csvParser.ReadAsync()).ToList();

            do
            {
                var rowValues = await csvParser.ReadAsync();

                if (rowValues == null)
                {
                    break;
                }
                var pollingStation = new PollingStation();
                pollingStation.County = rowValues[1].StartsWith("SECTOR") ? "BUCUREȘTI" : rowValues[1];
                pollingStation.Name   = rowValues[4];
                foreach (var candidate in election.Candidates)
                {
                    var votes = int.Parse(rowValues[headers.IndexOf(file.Prefix + candidate.CsvId)]);
                    pollingStation.Candidates.Add(new CandidateConfig
                    {
                        Id    = candidate.CsvId,
                        Votes = votes
                    });
                    pollingStation.TotalVotes += votes;
                }
                pollingStations.Add(pollingStation);
            } while (true);

            return(pollingStations);
        }
        public async Task <Result <ElectionResultsData> > Parse(ElectionResultsData electionResultsData, string csvContent, ElectionResultsFile file)
        {
            if (electionResultsData == null)
            {
                electionResultsData = new ElectionResultsData();
            }
            var electionsConfigResponse = _electionConfigurationSource.GetElectionById(file.ElectionId);

            if (electionsConfigResponse.IsFailure)
            {
                return(Result.Failure <ElectionResultsData>(electionsConfigResponse.Error));
            }

            var electionsConfig = electionsConfigResponse.Value;

            electionResultsData.Candidates = electionsConfig.Candidates.Select(c => new CandidateConfig
            {
                Id       = c.CsvId,
                ImageUrl = c.ImageUrl,
                Name     = c.Name
            }).ToList();

            await PopulateCandidatesListWithVotes(csvContent, electionResultsData, file);

            var sumOfVotes = electionResultsData.Candidates.Sum(c => c.Votes);

            electionResultsData.Candidates = StatisticsAggregator.CalculatePercentagesForCandidates(electionResultsData.Candidates, sumOfVotes);

            return(Result.Ok(electionResultsData));
        }
        protected virtual async Task PopulateCandidatesListWithVotes(string csvContent,
                                                                     ElectionResultsData electionResultsData, ElectionResultsFile file)
        {
            try
            {
                var csvParser          = new CsvParser(new StringReader(csvContent));
                var headers            = (await csvParser.ReadAsync()).ToList();
                var totalCanceledVotes = 0;
                do
                {
                    var rowValues = await csvParser.ReadAsync();

                    if (rowValues == null)
                    {
                        break;
                    }
                    var canceledVotesColumn = "d";
                    if (file.ResultsSource == "mail")
                    {
                        canceledVotesColumn = "d2";
                    }
                    foreach (var candidate in electionResultsData.Candidates)
                    {
                        var votes = int.Parse(rowValues[headers.IndexOf(file.Prefix + candidate.Id)]);
                        candidate.Votes += votes;
                    }
                    var canceledVotes = int.Parse(rowValues[headers.IndexOf(canceledVotesColumn)]);
                    totalCanceledVotes += canceledVotes;
                } while (true);

                if (file.ElectionId == Consts.SecondElectionRound)
                {
                    Console.WriteLine($"Canceled votes for {file.URL} is {totalCanceledVotes}");
                }
                electionResultsData.CanceledVotes = totalCanceledVotes;
            }
            catch (Exception e)
            {
                Log.LogError(e, $"Couldn't populate list with votes for file {file.URL} and election {file.ElectionId}");
            }
        }
示例#16
0
        protected virtual async Task PopulateCandidatesListWithVotes(string csvContent,
                                                                     List <CandidateConfig> candidates, ElectionResultsFile file)
        {
            try
            {
                var csvParser = new CsvParser(new StringReader(csvContent));
                var headers   = (await csvParser.ReadAsync()).ToList();
                do
                {
                    var rowValues = await csvParser.ReadAsync();

                    if (rowValues == null)
                    {
                        break;
                    }
                    foreach (var candidate in candidates)
                    {
                        var votes = int.Parse(rowValues[headers.IndexOf(file.Prefix + candidate.Id)]);
                        candidate.Votes += votes;
                    }
                } while (true);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
示例#17
0
        public async Task <Result <ElectionResultsData> > Parse(ElectionResultsData electionResultsData, string csvContent, ElectionResultsFile file)
        {
            if (electionResultsData == null)
            {
                electionResultsData = new ElectionResultsData();
            }
            var electionsConfig = DeserializeElectionsConfig();

            electionResultsData.Candidates = electionsConfig.Candidates.Select(c => new CandidateConfig
            {
                Id       = c.CsvId,
                ImageUrl = c.ImageUrl,
                Name     = c.Name
            }).ToList();

            await PopulateCandidatesListWithVotes(csvContent, electionResultsData.Candidates, file);

            var sumOfVotes = electionResultsData.Candidates.Sum(c => c.Votes);

            electionResultsData.Candidates = StatisticsAggregator.CalculatePercentagesForCandidates(electionResultsData.Candidates, sumOfVotes);

            return(Result.Ok(electionResultsData));
        }