diff --git a/src/ElectionResults.API/Dockerfile b/src/ElectionResults.API/Dockerfile index e16db29..5cfd91b 100644 --- a/src/ElectionResults.API/Dockerfile +++ b/src/ElectionResults.API/Dockerfile @@ -1,20 +1,25 @@ -#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging. +#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging. -FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base +FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base +USER app WORKDIR /app -EXPOSE 80 -EXPOSE 443 +EXPOSE 8080 +EXPOSE 8081 -FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build +FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build +ARG BUILD_CONFIGURATION=Release WORKDIR /src COPY ["ElectionResults.API/ElectionResults.API.csproj", "ElectionResults.API/"] -RUN dotnet restore "ElectionResults.API/ElectionResults.API.csproj" +COPY ["ElectionResults.Core/ElectionResults.Core.csproj", "ElectionResults.Core/"] +COPY ["ElectionResults.Importer/ElectionResults.Importer.csproj", "ElectionResults.Importer/"] +RUN dotnet restore "./ElectionResults.API/ElectionResults.API.csproj" COPY . . WORKDIR "/src/ElectionResults.API" -RUN dotnet build "ElectionResults.API.csproj" -c Release -o /app/build +RUN dotnet build "./ElectionResults.API.csproj" -c $BUILD_CONFIGURATION -o /app/build FROM build AS publish -RUN dotnet publish "ElectionResults.API.csproj" -c Release -o /app/publish +ARG BUILD_CONFIGURATION=Release +RUN dotnet publish "./ElectionResults.API.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false FROM base AS final WORKDIR /app diff --git a/src/ElectionResults.API/ElectionResults.API.csproj b/src/ElectionResults.API/ElectionResults.API.csproj index 3a5890d..f4efc64 100644 --- a/src/ElectionResults.API/ElectionResults.API.csproj +++ b/src/ElectionResults.API/ElectionResults.API.csproj @@ -1,31 +1,29 @@  - - netcoreapp3.1 + net8.0 0a7d747b-d86b-44b1-8e0f-69bc4c568730 Linux - - - - - - + + + + + + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive - - - - - - - + all runtime; build; native; contentfiles; analyzers; buildtransitive - + + + @@ -38,4 +36,4 @@ - + \ No newline at end of file diff --git a/src/ElectionResults.API/Import/TurnoutCrawler.cs b/src/ElectionResults.API/Import/TurnoutCrawler.cs index 6574287..a0cfa26 100644 --- a/src/ElectionResults.API/Import/TurnoutCrawler.cs +++ b/src/ElectionResults.API/Import/TurnoutCrawler.cs @@ -67,10 +67,8 @@ private async Task ProcessStream(Stream csvStream) var csvContent = await ReadCsvContent(csvStream); TextReader sr = new StringReader(csvContent); var csvParser = new CsvReader(sr, CultureInfo.CurrentCulture); - csvParser.Configuration.HeaderValidated = null; - csvParser.Configuration.MissingFieldFound = null; var turnouts = csvParser.GetRecords().ToList(); - using (var dbContext = _serviceProvider.CreateScope().ServiceProvider.GetService()) + await using (var dbContext = _serviceProvider.CreateScope().ServiceProvider.GetService()) { EntityFrameworkManager.ContextFactory = context => dbContext; _dbCounties = await dbContext.Counties.ToListAsync(); diff --git a/src/ElectionResults.API/Startup.cs b/src/ElectionResults.API/Startup.cs index e0a8ff5..6fa64b1 100644 --- a/src/ElectionResults.API/Startup.cs +++ b/src/ElectionResults.API/Startup.cs @@ -48,13 +48,11 @@ public void ConfigureServices(IServiceCollection services) services .AddMvc() - .SetCompatibilityVersion(CompatibilityVersion.Latest) - .AddJsonOptions(options => { - options.JsonSerializerOptions.IgnoreNullValues = true; + options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull; options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter(new SnakeCaseNamingPolicy())); - }); ; + }); services.AddLazyCache(); RegisterDependencies(services, Configuration); services.AddSwaggerGen(c => @@ -63,7 +61,7 @@ public void ConfigureServices(IServiceCollection services) }); services.AddDbContextPool(options => { - options.UseMySQL(Configuration["ConnectionStrings:DefaultConnection"] + options.UseMySQL(Configuration["ConnectionStrings:DefaultConnection"]! ); }); @@ -81,7 +79,7 @@ public void ConfigureServices(IServiceCollection services) }); }); - if (Configuration["ScheduleTaskEnabled"].ToLower().Equals("true")) //excuse the primitive syntax + if (Configuration["ScheduleTaskEnabled"]!.ToLower().Equals("true")) //excuse the primitive syntax services.AddHostedService(); } @@ -151,7 +149,7 @@ private static void MigrateDatabase(ApplicationDbContext context) } catch (Exception e) { - Console.WriteLine(3); + Console.WriteLine(e); } } } diff --git a/src/ElectionResults.Core/ElectionResults.Core.csproj b/src/ElectionResults.Core/ElectionResults.Core.csproj index 6de84de..50eab1a 100644 --- a/src/ElectionResults.Core/ElectionResults.Core.csproj +++ b/src/ElectionResults.Core/ElectionResults.Core.csproj @@ -1,29 +1,29 @@  - - netcoreapp3.1 - + + net8.0 + - - - - - - - - - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - - - - - - + + + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + - + \ No newline at end of file diff --git a/src/ElectionResults.Core/Extensions/AsyncEx.cs b/src/ElectionResults.Core/Extensions/AsyncEx.cs deleted file mode 100644 index 3078616..0000000 --- a/src/ElectionResults.Core/Extensions/AsyncEx.cs +++ /dev/null @@ -1,45 +0,0 @@ -using System; -using System.Collections.Concurrent; -using System.Collections.Generic; -using System.Threading; -using System.Threading.Tasks; - -namespace ElectionResults.Core.Extensions -{ - public static class AsyncEx - { - public static async Task ParallelForEachAsync(this IEnumerable source, Func asyncAction, int maxDegreeOfParallelism = 10) - { - var semaphoreSlim = new SemaphoreSlim(maxDegreeOfParallelism); - var tcs = new TaskCompletionSource(); - var exceptions = new ConcurrentBag(); - bool addingCompleted = false; - - foreach (T item in source) - { - await semaphoreSlim.WaitAsync(); - asyncAction(item).ContinueWith(t => - { - semaphoreSlim.Release(); - - if (t.Exception != null) - { - exceptions.Add(t.Exception); - } - - if (Volatile.Read(ref addingCompleted) && semaphoreSlim.CurrentCount == maxDegreeOfParallelism) - { - tcs.TrySetResult(null); - } - }); - } - - Volatile.Write(ref addingCompleted, true); - await tcs.Task; - if (exceptions.Count > 0) - { - throw new AggregateException(exceptions); - } - } - } -} \ No newline at end of file diff --git a/src/ElectionResults.Core/Migrations/20200924201346_AddedElectionTables.cs b/src/ElectionResults.Core/Migrations/20200924201346_AddedElectionTables.cs index 7c7ca05..1772835 100644 --- a/src/ElectionResults.Core/Migrations/20200924201346_AddedElectionTables.cs +++ b/src/ElectionResults.Core/Migrations/20200924201346_AddedElectionTables.cs @@ -1,6 +1,6 @@ using System; using Microsoft.EntityFrameworkCore.Migrations; -using MySql.Data.EntityFrameworkCore.Metadata; +using MySql.EntityFrameworkCore.Metadata; namespace ElectionResults.Core.Migrations { diff --git a/src/ElectionResults.Core/Migrations/20200928172740_AddedWinnersTable.cs b/src/ElectionResults.Core/Migrations/20200928172740_AddedWinnersTable.cs index 1b72635..d70d1e0 100644 --- a/src/ElectionResults.Core/Migrations/20200928172740_AddedWinnersTable.cs +++ b/src/ElectionResults.Core/Migrations/20200928172740_AddedWinnersTable.cs @@ -1,5 +1,5 @@ using Microsoft.EntityFrameworkCore.Migrations; -using MySql.Data.EntityFrameworkCore.Metadata; +using MySql.EntityFrameworkCore.Metadata; namespace ElectionResults.Core.Migrations { diff --git a/src/ElectionResults.Core/Scheduler/CsvDownloaderJob.cs b/src/ElectionResults.Core/Scheduler/CsvDownloaderJob.cs index f88f57a..41c2057 100644 --- a/src/ElectionResults.Core/Scheduler/CsvDownloaderJob.cs +++ b/src/ElectionResults.Core/Scheduler/CsvDownloaderJob.cs @@ -43,9 +43,10 @@ public async Task GetCandidatesFromUrl(string url) return liveElectionInfo; } - public async Task DownloadFiles() + public Task DownloadFiles() { - // await DownloadCandidates(); + return Task.CompletedTask; + // await DownloadCandidates(); } private async Task DownloadCandidates() @@ -257,13 +258,10 @@ private async Task DownloadFile(string url) private async Task ExtractCandidatesFromCsv(Stream csvStream) { - List candidates; var csvContent = await ReadCsvContent(csvStream); TextReader sr = new StringReader(csvContent); var csvParser = new CsvReader(sr, CultureInfo.CurrentCulture); - csvParser.Configuration.HeaderValidated = null; - csvParser.Configuration.MissingFieldFound = null; - candidates = await GetCandidates(csvParser); + var candidates = await GetCandidates(csvParser); var nullVotes = 0; var total = 0; var voted = 0; @@ -332,10 +330,8 @@ private async Task ProcessStream(Stream csvStream) var csvContent = await ReadCsvContent(csvStream); TextReader sr = new StringReader(csvContent); var csvParser = new CsvReader(sr, CultureInfo.CurrentCulture); - csvParser.Configuration.HeaderValidated = null; - csvParser.Configuration.MissingFieldFound = null; var turnouts = csvParser.GetRecords().ToList(); - using (var dbContext = _serviceProvider.CreateScope().ServiceProvider.GetService()) + await using (var dbContext = _serviceProvider.CreateScope().ServiceProvider.GetService()) { EntityFrameworkManager.ContextFactory = context => dbContext; var localities = await dbContext.Localities.ToListAsync(); diff --git a/src/ElectionResults.Core/Scheduler/CsvIndexes.cs b/src/ElectionResults.Core/Scheduler/CsvIndexes.cs index a34375a..3dfbc3c 100644 --- a/src/ElectionResults.Core/Scheduler/CsvIndexes.cs +++ b/src/ElectionResults.Core/Scheduler/CsvIndexes.cs @@ -17,7 +17,7 @@ public class CsvIndexes public int CountryNameIndex { get; set; } public int CandidatesIndex { get; set; } public int NullVotesIndex2 { get; set; } - public CsvMode CsvMode { get; set; } + private CsvMode CsvMode { get; set; } public CsvIndexes(CsvMode csvMode) { if (csvMode == CsvMode.National || csvMode == CsvMode.Diaspora) @@ -29,7 +29,8 @@ public CsvIndexes(CsvMode csvMode) CsvMode = csvMode; } - public void SetIndexesForNationalResults() + + private void SetIndexesForNationalResults() { EligibleVotersIndex = 12; TotalVotesIndex = 16; @@ -40,7 +41,7 @@ public void SetIndexesForNationalResults() CandidatesIndex = 25; } - public void SetIndexesForCorrespondenceResults() + private void SetIndexesForCorrespondenceResults() { EligibleVotersIndex = 12; TotalVotesIndex = 13; @@ -52,7 +53,7 @@ public void SetIndexesForCorrespondenceResults() CandidatesIndex = 22; } - public async Task Map(CsvReader csvParser) + public Task Map(CsvReader csvParser) { var index = 0; while (index < CandidatesIndex) @@ -124,6 +125,8 @@ public async Task Map(CsvReader csvParser) } index++; } + + return Task.CompletedTask; } } } \ No newline at end of file diff --git a/src/ElectionResults.Core/Scheduler/ResultsCrawler.cs b/src/ElectionResults.Core/Scheduler/ResultsCrawler.cs index 8852999..7434206 100644 --- a/src/ElectionResults.Core/Scheduler/ResultsCrawler.cs +++ b/src/ElectionResults.Core/Scheduler/ResultsCrawler.cs @@ -422,13 +422,10 @@ public async Task GetCandidatesFromUrl(string url, CsvIndexes private async Task ExtractCandidatesFromCsv(Stream csvStream, CsvIndexes csvIndexes) { - List candidates; var csvContent = await ReadCsvContent(csvStream); TextReader sr = new StringReader(csvContent); var csvParser = new CsvReader(sr, CultureInfo.CurrentCulture); - csvParser.Configuration.HeaderValidated = null; - csvParser.Configuration.MissingFieldFound = null; - candidates = await GetCandidates(csvParser, csvIndexes); + var candidates = await GetCandidates(csvParser, csvIndexes); var nullVotes = 0; var total = 0; var voted = 0; @@ -479,8 +476,6 @@ private async Task> ExtractCandidateResultsFromCsv(Stream c var csvContent = await ReadCsvContent(csvStream); TextReader sr = new StringReader(csvContent); var csvParser = new CsvReader(sr, CultureInfo.CurrentCulture); - csvParser.Configuration.HeaderValidated = null; - csvParser.Configuration.MissingFieldFound = null; candidates = await GetCandidates(csvParser, csvIndexes); while (true) { diff --git a/src/ElectionResults.Importer/ElectionResults.Importer.csproj b/src/ElectionResults.Importer/ElectionResults.Importer.csproj index 911f0fb..f95a842 100644 --- a/src/ElectionResults.Importer/ElectionResults.Importer.csproj +++ b/src/ElectionResults.Importer/ElectionResults.Importer.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net8.0