Skip to content

Commit

Permalink
Removed SQLite
Browse files Browse the repository at this point in the history
  • Loading branch information
Aragas committed Feb 25, 2024
1 parent da4f255 commit 8fcd90e
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 147 deletions.
76 changes: 0 additions & 76 deletions src/BUTR.CrashReportServer/Contexts/OldAppDbContext.cs

This file was deleted.

14 changes: 4 additions & 10 deletions src/BUTR.CrashReportServer/Controllers/CrashUploadController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public sealed record CrashReportUploadBody(CrashReportModel CrashReport, ICollec
private readonly CrashUploadOptions _options;
private readonly JsonSerializerOptions _jsonSerializerOptions;
private readonly AppDbContext _dbContext;
private readonly OldAppDbContext _dbContextOld;
private readonly GZipCompressor _gZipCompressor;
private readonly HexGenerator _hexGenerator;

Expand All @@ -48,7 +47,7 @@ public CrashUploadController(
AppDbContext dbContext,
GZipCompressor gZipCompressor,
HexGenerator hexGenerator,
IMeterFactory meterFactory, OldAppDbContext dbContextOld)
IMeterFactory meterFactory)
{
var meter = meterFactory.Create("BUTR.CrashReportServer.Controllers.CrashUploadController", "1.0.0");

Expand All @@ -58,7 +57,6 @@ public CrashUploadController(
_jsonSerializerOptions = jsonSerializerOptions.Value ?? throw new ArgumentNullException(nameof(jsonSerializerOptions));
_options = options.Value ?? throw new ArgumentNullException(nameof(options));
_dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
_dbContextOld = dbContextOld ?? throw new ArgumentNullException(nameof(dbContextOld));
_gZipCompressor = gZipCompressor ?? throw new ArgumentNullException(nameof(gZipCompressor));
_hexGenerator = hexGenerator ?? throw new ArgumentNullException(nameof(hexGenerator));
}
Expand All @@ -71,13 +69,9 @@ private string GenerateFileId(CancellationToken ct)
{
var fileIds = _hexGenerator.GetHex(count, 3);
var existing = _dbContext.IdEntities.Select(x => x.FileId).Where(x => fileIds.Contains(x)).ToHashSet();
var existing2 = _dbContextOld.IdEntities.Select(x => x.FileId).Where(x => fileIds.Contains(x)).ToHashSet();
//if (existing.Count == count) continue;
//fileId = existing.First(x => !fileIds.Contains(x));
fileIds.ExceptWith(existing);
fileIds.ExceptWith(existing2);
if (fileIds.Count == 0) continue;
return fileIds.First();
if (existing.Count == count) continue;
if (existing.Count == 0) return fileIds.First();
return existing.First(x => !fileIds.Contains(x));
}
return fileId;
}
Expand Down
36 changes: 7 additions & 29 deletions src/BUTR.CrashReportServer/Controllers/ReportController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,13 @@ public sealed record GetNewCrashReportsBody
private readonly ILogger _logger;
private readonly ReportOptions _options;
private readonly AppDbContext _dbContext;
private readonly OldAppDbContext _dbContextOld;
private readonly GZipCompressor _gZipCompressor;

public ReportController(ILogger<ReportController> logger, IOptionsSnapshot<ReportOptions> options, AppDbContext dbContext, OldAppDbContext dbContextOld, GZipCompressor gZipCompressor)
public ReportController(ILogger<ReportController> logger, IOptionsSnapshot<ReportOptions> options, AppDbContext dbContext, GZipCompressor gZipCompressor)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_options = options.Value ?? throw new ArgumentNullException(nameof(options));
_dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
_dbContextOld = dbContextOld ?? throw new ArgumentNullException(nameof(dbContextOld));
_gZipCompressor = gZipCompressor ?? throw new ArgumentNullException(nameof(gZipCompressor));
}

Expand Down Expand Up @@ -78,52 +76,32 @@ private async Task<IActionResult> GetHtml(string filename, CancellationToken ct)
if (ValidateRequest(ref filename) is { } errorResponse)
return errorResponse;

var file = await _dbContext.FileEntities.FirstOrDefaultAsync(x => x.Id!.FileId == filename, ct);
var fileOld = await _dbContextOld.FileEntities.FirstOrDefaultAsync(x => x.Id.FileId == filename, ct);
if (file is null && fileOld is null)
if (await _dbContext.FileEntities.FirstOrDefaultAsync(x => x.Id!.FileId == filename, ct) is not { } file)
return StatusCode(StatusCodes.Status404NotFound);

if (Request.GetTypedHeaders().AcceptEncoding.Any(x => x.Value.Equals("gzip", StringComparison.InvariantCultureIgnoreCase)))
{
Response.Headers.ContentEncoding = "gzip";
if (file is not null)
return File(file.DataCompressed, "text/html; charset=utf-8", true);
if (fileOld is not null)
return File(fileOld.DataCompressed, "text/html; charset=utf-8", true);
return File(file.DataCompressed, "text/html; charset=utf-8", true);
}
if (file is not null)
return File(await _gZipCompressor.DecompressAsync(file.DataCompressed, ct), "text/html; charset=utf-8", true);
if (fileOld is not null)
return File(await _gZipCompressor.DecompressAsync(fileOld.DataCompressed, ct), "text/html; charset=utf-8", true);

return StatusCode(StatusCodes.Status500InternalServerError);
return File(await _gZipCompressor.DecompressAsync(file.DataCompressed, ct), "text/html; charset=utf-8", true);
}

private async Task<IActionResult> GetJson(string filename, CancellationToken ct)
{
if (ValidateRequest(ref filename) is { } errorResponse)
return errorResponse;

var file = await _dbContext.JsonEntities.FirstOrDefaultAsync(x => x.Id!.FileId == filename, ct);
var fileOld = await _dbContextOld.JsonEntities.FirstOrDefaultAsync(x => x.Id.FileId == filename, ct);
if (file is null && fileOld is null)
if (await _dbContext.JsonEntities.FirstOrDefaultAsync(x => x.Id!.FileId == filename, ct) is not { } file)
return StatusCode(StatusCodes.Status404NotFound);

if (Request.GetTypedHeaders().AcceptEncoding.Any(x => x.Value.Equals("gzip", StringComparison.InvariantCultureIgnoreCase)))
{
Response.Headers.ContentEncoding = "gzip";
if (file is not null)
return File(await _gZipCompressor.CompressAsync(new MemoryStream(Encoding.UTF8.GetBytes(file.CrashReport)), ct), "application/json; charset=utf-8", true);
if (fileOld is not null)
return File(fileOld.CrashReportCompressed, "application/json; charset=utf-8", true);
return File(await _gZipCompressor.CompressAsync(new MemoryStream(Encoding.UTF8.GetBytes(file.CrashReport)), ct), "application/json; charset=utf-8", true);

}
if (file is not null)
return File(new MemoryStream(Encoding.UTF8.GetBytes(file.CrashReport)), "application/json; charset=utf-8", true);
if (fileOld is not null)
return File(await _gZipCompressor.DecompressAsync(fileOld.CrashReportCompressed, ct), "application/json; charset=utf-8", true);

return StatusCode(StatusCodes.Status500InternalServerError);
return File(new MemoryStream(Encoding.UTF8.GetBytes(file.CrashReport)), "application/json; charset=utf-8", true);
}

[AllowAnonymous]
Expand Down
8 changes: 0 additions & 8 deletions src/BUTR.CrashReportServer/Models/Database/OldFileEntity.cs

This file was deleted.

12 changes: 0 additions & 12 deletions src/BUTR.CrashReportServer/Models/Database/OldIdEntity.cs

This file was deleted.

8 changes: 0 additions & 8 deletions src/BUTR.CrashReportServer/Models/Database/OldJsonEntity.cs

This file was deleted.

6 changes: 4 additions & 2 deletions src/BUTR.CrashReportServer/Services/DatabaseMigrator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BUTR.CrashReportServer.Contexts;
/*
using BUTR.CrashReportServer.Contexts;
using BUTR.CrashReportServer.Models.Database;
using Microsoft.EntityFrameworkCore;
Expand Down Expand Up @@ -89,4 +90,5 @@ protected override async Task ExecuteAsync(CancellationToken ct)
GC.Collect();
}
}
}
}
*/
3 changes: 1 addition & 2 deletions src/BUTR.CrashReportServer/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,9 @@ public void ConfigureServices(IServiceCollection services)
services.AddSingleton<HexGenerator>();
services.AddSingleton<RecyclableMemoryStreamManager>();
services.AddSingleton<GZipCompressor>();
services.AddHostedService<DatabaseMigrator>();
//services.AddHostedService<DatabaseMigrator>();

services.AddDbContextFactory<AppDbContext>(x => x.UseNpgsql(_configuration.GetConnectionString("Main")));
services.AddDbContextFactory<OldAppDbContext>(x => x.UseSqlite(_configuration.GetConnectionString("Old")));

services.AddSwaggerGen(opt =>
{
Expand Down

0 comments on commit 8fcd90e

Please sign in to comment.