Skip to content

Commit

Permalink
Added Version value for IdEntity
Browse files Browse the repository at this point in the history
  • Loading branch information
Aragas committed Oct 2, 2023
1 parent 8b679ce commit de64146
Show file tree
Hide file tree
Showing 10 changed files with 185 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public async Task<IActionResult> CrashUploadAsync(CancellationToken ct)
Converters = { new JsonStringEnumConverter() }
}), ct);

idEntity = new IdEntity { FileId = default!, CrashReportId = id, Created = DateTimeOffset.UtcNow, };
idEntity = new IdEntity { FileId = default!, CrashReportId = id, Version = version, Created = DateTimeOffset.UtcNow, };
await _dbContext.Set<IdEntity>().AddAsync(idEntity, ct);
await _dbContext.Set<FileEntity>().AddAsync(new FileEntity { Id = idEntity, DataCompressed = compressedHtmlStream.ToArray(), }, ct);
if (version >= 13) await _dbContext.Set<JsonEntity>().AddAsync(new JsonEntity { Id = idEntity, CrashReportCompressed = compressedJsonStream.ToArray(), }, ct);
Expand Down Expand Up @@ -118,7 +118,7 @@ public async Task<IActionResult> CrashUploadAsync([FromBody] CrashReportUploadBo
await using var compressedHtmlStream = await _gZipCompressor.CompressAsync(html.AsStream(), ct);
await using var compressedJsonStream = await _gZipCompressor.CompressAsync(json.AsStream(), ct);

idEntity = new IdEntity { FileId = default!, CrashReportId = body.CrashReport.Id, Created = DateTimeOffset.UtcNow, };
idEntity = new IdEntity { FileId = default!, CrashReportId = body.CrashReport.Id, Version = body.CrashReport.Version, Created = DateTimeOffset.UtcNow, };
await _dbContext.Set<IdEntity>().AddAsync(idEntity, ct);
await _dbContext.Set<JsonEntity>().AddAsync(new JsonEntity { Id = idEntity, CrashReportCompressed = compressedJsonStream.ToArray(), }, ct);
await _dbContext.Set<FileEntity>().AddAsync(new FileEntity { Id = idEntity, DataCompressed = compressedHtmlStream.ToArray(), }, ct);
Expand Down
10 changes: 5 additions & 5 deletions src/BUTR.CrashReportServer/Controllers/ReportController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,19 +144,19 @@ public async Task<IActionResult> Delete(string filename)
public ActionResult<IAsyncEnumerable<string>> GetAllFilenames() => Ok(_dbContext.Set<IdEntity>().Select(x => x.FileId));

[Authorize]
[HttpPost("GetFilenameDates")]
[ProducesResponseType(typeof(FilenameDate[]), StatusCodes.Status200OK, "application/json")]
[HttpPost("GetMetadata")]
[ProducesResponseType(typeof(FileMetadata[]), StatusCodes.Status200OK, "application/json")]
[ProducesResponseType(typeof(void), StatusCodes.Status500InternalServerError, "application/problem+json")]
[ProducesResponseType(typeof(TLSError), StatusCodes.Status400BadRequest, "application/json")]
[HttpsProtocol(Protocol = SslProtocols.Tls13)]
public ActionResult<IEnumerable<FilenameDate>> GetFilenameDates(ICollection<string> filenames, CancellationToken ct)
public ActionResult<IEnumerable<FileMetadata>> GetFilenameDates(ICollection<string> filenames, CancellationToken ct)
{
var filenamesWithExtension = filenames.Select(Path.GetFileNameWithoutExtension).ToImmutableArray();

return Ok(_dbContext.Set<IdEntity>()
.Where(x => filenamesWithExtension.Contains(x.FileId))
.Select(x => new { x.FileId, x.Created })
.Select(x => new { x.FileId, x.Version, x.Created })
.AsAsyncEnumerable()
.Select(x => new FilenameDate(x.FileId, x.Created.ToUniversalTime().ToString("O"))));
.Select(x => new FileMetadata(x.FileId, x.Version, x.Created.ToUniversalTime())));
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace BUTR.CrashReportServer.Migrations
{
/// <inheritdoc />
public partial class IdEntityVersion : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<byte>(
name: "version",
table: "id_entity",
type: "INTEGER",
nullable: false,
defaultValue: (byte)0);
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "version",
table: "id_entity");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ partial class AppDbContextModelSnapshot : ModelSnapshot
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "7.0.4");
modelBuilder.HasAnnotation("ProductVersion", "7.0.11");

modelBuilder.Entity("BUTR.CrashReportServer.Models.Database.FileEntity", b =>
{
Expand Down Expand Up @@ -50,6 +50,10 @@ protected override void BuildModel(ModelBuilder modelBuilder)
.HasColumnType("TEXT")
.HasColumnName("created");
b.Property<byte>("Version")
.HasColumnType("INTEGER")
.HasColumnName("version");
b.HasKey("FileId");
b.HasIndex("CrashReportId");
Expand Down
5 changes: 5 additions & 0 deletions src/BUTR.CrashReportServer/Models/API/FileMetadata.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using System;

namespace BUTR.CrashReportServer.Models.API;

public sealed record FileMetadata(string File, byte Version, DateTimeOffset Date);
3 changes: 0 additions & 3 deletions src/BUTR.CrashReportServer/Models/API/FilenameDate.cs

This file was deleted.

1 change: 1 addition & 0 deletions src/BUTR.CrashReportServer/Models/Database/IdEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ public sealed record IdEntity : IEntity
{
public required string FileId { get; set; }
public required Guid CrashReportId { get; set; }
public required byte Version { get; set; }
public required DateTimeOffset Created { get; set; }
}
4 changes: 2 additions & 2 deletions src/BUTR.CrashReportServer/Services/DatabaseMigrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ await Parallel.ForEachAsync(Enumerable.Range(0, 4), options, async (_, ct2) =>
{
var dbContext = await dbContextPool.GetAsync(ct2);
var wrong = dbContext.Set<FileEntity>().AsNoTracking().Where(x => x.Id.CrashReportId == Guid.Empty).Take(1000);
var wrong = dbContext.Set<FileEntity>().AsNoTracking().Where(x => x.Id.Version == 0).Take(1000);
while (true)
{
var entities = wrong.ToArray();
Expand All @@ -74,7 +74,7 @@ await Parallel.ForEachAsync(Enumerable.Range(0, 4), options, async (_, ct2) =>
{
sb.AppendLine($"""
UPDATE id_entity
SET crash_report_id = '{id}'
SET version = '{version}'
WHERE file_id = '{entity.Id.FileId}';
""");
}
Expand Down
45 changes: 29 additions & 16 deletions src/BUTR.CrashReportServer/Utils/CrashReportRawParser.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using BUTR.CrashReport.Models;

using SQLitePCL;

using System;
using System.Buffers;
using System.Buffers.Text;
Expand All @@ -15,25 +17,26 @@ public static class CrashReportRawParser
{
private static readonly byte[] Newline = "\n"u8.ToArray();
private static readonly byte[] ReportMarkerStart = "<report id='"u8.ToArray();
private static readonly byte[] ReportIdMarkerEnd = "' "u8.ToArray();
private static readonly byte[] ReportIdMarkerEnd = "'"u8.ToArray();
private static readonly byte[] ReportIdNoVersionMarkerEnd = "'>"u8.ToArray();
private static readonly byte[] ReportVersionMarkerStart = "' version='"u8.ToArray();
private static readonly byte[] ReportVersionMarkerEnd = "' />"u8.ToArray();
private static readonly byte[] ReportVersionMarkerEnd = "'>"u8.ToArray();
private static readonly byte[] ReportVersionMarkerEnd2 = "' >"u8.ToArray();
private static readonly byte[] JsonModelMarkerStart = "<div id='json-model-data' class='headers-container'>"u8.ToArray();
private static readonly byte[] JsonModelMarkerEnd = "</div>"u8.ToArray();

public static async Task<(bool, Guid, byte, CrashReportModel?)> TryReadCrashReportDataAsync(PipeReader reader)
{
var crashRreportId = Guid.Empty;
var crashRreportVersion = (byte) 0;
var crashReportVersion = (byte) 0;
var crashReportModel = default(CrashReportModel);

while (true)
{
var result = await reader.ReadAsync();
var buffer = result.Buffer;

while (!result.IsCompleted && !TryReadCrashReportData(ref buffer, out crashRreportId, out crashRreportVersion))
while (!result.IsCompleted && !TryReadCrashReportData(ref buffer, out crashRreportId, out crashReportVersion))
{
reader.AdvanceTo(buffer.Start, buffer.End);
}
Expand All @@ -55,9 +58,10 @@ public static class CrashReportRawParser
}

var isValid = crashRreportId != Guid.Empty &&
((crashRreportVersion > 12 && crashReportModel is not null) || (crashRreportVersion <= 12 && crashReportModel is null));
crashReportVersion > 0 &&
((crashReportVersion > 12 && crashReportModel is not null) || (crashReportVersion <= 12 && crashReportModel is null));

return (isValid, crashRreportId, crashRreportVersion, crashReportModel);
return (isValid, crashRreportId, crashReportVersion, crashReportModel);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand All @@ -72,6 +76,24 @@ private static bool TryReadCrashReportData(ref ReadOnlySequence<byte> buffer, ou
buffer = buffer.Slice(reader.Position);

if (line.IndexOf(ReportMarkerStart) is not (var idxIdStart and not -1)) return false;
var line2 = line.utf8_span_to_string();

if (line.Slice(idxIdStart + ReportMarkerStart.Length).IndexOf(ReportIdMarkerEnd) is not (var idxIdEnd and not -1)) return false;

if (!Utf8Parser.TryParse(line.Slice(idxIdStart + ReportMarkerStart.Length, idxIdEnd), out crashReportId, out _)) return false;

if (line.IndexOf(ReportVersionMarkerStart) is var idxVersionStart and not -1)
{
var idxEnd = -1;
if (line.Slice(idxVersionStart + ReportVersionMarkerStart.Length).IndexOf(ReportVersionMarkerEnd) is var idxVersionEnd and not -1) idxEnd = idxVersionEnd;
if (line.Slice(idxVersionStart + ReportVersionMarkerStart.Length).IndexOf(ReportVersionMarkerEnd2) is var idxVersionEnd2 and not -1) idxEnd = idxVersionEnd2;
if (idxEnd == -1) return false;

if (!Utf8Parser.TryParse(line.Slice(idxVersionStart + ReportVersionMarkerStart.Length, idxEnd), out version, out _)) return false;

return true;
}

if (line.Slice(idxIdStart + ReportMarkerStart.Length).IndexOf(ReportIdNoVersionMarkerEnd) is var idxNoVersionStart and not -1)
{
if (!Utf8Parser.TryParse(line.Slice(idxIdStart + ReportMarkerStart.Length, idxNoVersionStart), out crashReportId, out _)) return false;
Expand All @@ -80,16 +102,7 @@ private static bool TryReadCrashReportData(ref ReadOnlySequence<byte> buffer, ou
return true;
}

if (line.Slice(idxIdStart + ReportMarkerStart.Length).IndexOf(ReportIdMarkerEnd) is not (var idxIdEnd and not -1)) return false;

if (!Utf8Parser.TryParse(line.Slice(idxIdStart + ReportMarkerStart.Length, idxIdEnd), out crashReportId, out _)) return false;

if (line.IndexOf(ReportVersionMarkerStart) is not (var idxVersionStart and not -1)) return false;
if (line.Slice(idxVersionStart + ReportVersionMarkerStart.Length).IndexOf(ReportVersionMarkerEnd) is not (var idxVersionEnd and not -1)) return false;

if (!Utf8Parser.TryParse(line.Slice(idxVersionStart + ReportVersionMarkerStart.Length, idxVersionEnd), out version, out _)) return false;

return true;
return false;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down

0 comments on commit de64146

Please sign in to comment.