Skip to content

Commit

Permalink
rework api file service db context to be short lived #406
Browse files Browse the repository at this point in the history
  • Loading branch information
myangelkamikaze committed Oct 14, 2023
1 parent 7c2c52d commit 46e1a45
Showing 1 changed file with 36 additions and 25 deletions.
61 changes: 36 additions & 25 deletions ElectronicObserver/Services/ApiFileService/ApiFileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
using ElectronicObserver.KancolleApi.Types.ApiReqMap.Start;
using ElectronicObserver.KancolleApi.Types.ApiReqMission.Result;
using ElectronicObserverTypes;
using ElectronicObserverTypes.Data;
using Microsoft.EntityFrameworkCore;

namespace ElectronicObserver.Services.ApiFileService;
Expand All @@ -28,10 +27,9 @@ public class ApiFileService : ObservableObject
{
private static int CurrentApiFileVersion => 1;

private ElectronicObserverContext Db { get; } = new();
private KCDatabase KcDatabase { get; }

private SortieRecord? SortieRecord { get; set; }
private int? CurrentSortieId { get; set; }

private static List<string> IgnoredApis { get; } = new()
{
Expand Down Expand Up @@ -74,6 +72,8 @@ private async Task SaveApiData(string apiName, string requestBody, string respon
{
if (IgnoredApis.Contains(apiName)) return;

await using ElectronicObserverContext db = new();

requestBody = FormatRequest(requestBody);

responseBody = TrimSvdata(responseBody);
Expand All @@ -97,25 +97,20 @@ private async Task SaveApiData(string apiName, string requestBody, string respon
Version = CurrentApiFileVersion,
};

await Db.ApiFiles.AddAsync(requestFile);
await Db.ApiFiles.AddAsync(responseFile);
await db.ApiFiles.AddAsync(requestFile);
await db.ApiFiles.AddAsync(responseFile);

await db.SaveChangesAsync();

await ProcessSortieData(requestFile, responseFile);
await ProcessExpeditionData(requestFile, responseFile);

await Db.SaveChangesAsync();
}

public async Task Add(string apiName, string requestBody, string responseBody)
{
await ApiProcessingChannel.Writer.WriteAsync(new(apiName, requestBody, responseBody));
}

public void SaveChanges()
{
Db.SaveChanges();
}

/// <summary>
/// Convert query params to json.
/// </summary>
Expand Down Expand Up @@ -175,18 +170,25 @@ private async Task ProcessSortieData(ApiFile requestFile, ApiFile responseFile)
{
if (requestFile.Name is "api_port/port")
{
if (SortieRecord is not null)
if (CurrentSortieId is int sortieId)
{
SortieIdsToProcess.Enqueue(SortieRecord.Id);
SortieIdsToProcess.Enqueue(sortieId);
}

SortieRecord = null;
CurrentSortieId = null;
return;
}

await using ElectronicObserverContext db = new();
SortieRecord? sortieRecord = CurrentSortieId switch
{
int id => await db.Sorties.FirstOrDefaultAsync(s => s.Id == id),
_ => null,
};

if (requestFile.Name is "api_req_map/start")
{
if (SortieRecord is not null)
if (sortieRecord is not null)
{
// todo: log bug - SortieRecord should always be null before a sortie starts
}
Expand Down Expand Up @@ -227,18 +229,21 @@ private async Task ProcessSortieData(ApiFile requestFile, ApiFile responseFile)
SortieFleetData fleetData = MakeSortieFleet(KcDatabase, fleetId, nodeSupportFleetId,
bossSupportFleetId, map);

SortieRecord = new()
sortieRecord = new()
{
World = response.ApiData.ApiMapareaId,
Map = response.ApiData.ApiMapinfoNo,
FleetData = fleetData,
MapData = mapData,
};

await Db.Sorties.AddAsync(SortieRecord);
await db.Sorties.AddAsync(sortieRecord);
await db.SaveChangesAsync();

CurrentSortieId = sortieRecord.Id;
}

if (SortieRecord is null)
if (sortieRecord is null)
{
// this should be all apis that are not related to a sortie
// apis related to sortie are all api calls that happen between
Expand All @@ -247,8 +252,10 @@ private async Task ProcessSortieData(ApiFile requestFile, ApiFile responseFile)
return;
}

SortieRecord.ApiFiles.Add(requestFile);
SortieRecord.ApiFiles.Add(responseFile);
sortieRecord.ApiFiles.Add(requestFile);
sortieRecord.ApiFiles.Add(responseFile);

await db.SaveChangesAsync();
}

private async Task ProcessExpeditionData(ApiFile requestFile, ApiFile responseFile)
Expand All @@ -272,6 +279,7 @@ private async Task ProcessExpeditionData(ApiFile requestFile, ApiFile responseFi
if (response is null) return;
if (!int.TryParse(request.ApiDeckId, out int fleetId)) return;

await using ElectronicObserverContext db = new();
IFleetData f = KCDatabase.Instance.Fleet[fleetId];

SortieFleet fleet = new()
Expand All @@ -289,10 +297,11 @@ private async Task ProcessExpeditionData(ApiFile requestFile, ApiFile responseFi
Fleet = fleet,
};

await Db.Expeditions.AddAsync(expedition);

expedition.ApiFiles.Add(requestFile);
expedition.ApiFiles.Add(responseFile);

await db.Expeditions.AddAsync(expedition);
await db.SaveChangesAsync();
}

private static bool ShouldIncludeFleet(KCDatabase kcDatabase, IFleetData fleet, int fleetId,
Expand Down Expand Up @@ -423,7 +432,9 @@ public async Task ProcessedApi(string apiName)
if (apiName is not "api_port/port") return;
if (!SortieIdsToProcess.TryDequeue(out int sortieId)) return;

SortieRecord? sortie = await Db.Sorties.FirstOrDefaultAsync(s => s.Id == sortieId);
await using ElectronicObserverContext db = new();

SortieRecord? sortie = await db.Sorties.FirstOrDefaultAsync(s => s.Id == sortieId);

if (sortie is null) return;

Expand All @@ -440,6 +451,6 @@ public async Task ProcessedApi(string apiName)
sortie.FleetAfterSortieData = MakeSortieFleet(KcDatabase, fleetId,
nodeSupportFleetId, bossSupportFleetId, map);

await Db.SaveChangesAsync();
await db.SaveChangesAsync();
}
}

0 comments on commit 46e1a45

Please sign in to comment.