Bozza detail upsert da testare

This commit is contained in:
Samuele Locatelli
2026-04-09 07:10:14 +02:00
parent d83353bc82
commit c329437700
6 changed files with 112 additions and 20 deletions
@@ -65,6 +65,14 @@ namespace MP.Data.Repository.Utils
// 2. Se removeOld è true, manteniamo la logica originale (Eliminazione distruttiva)
if (removeOld)
{
// uso direttamente ExecuteDelete quando in EFCore8...
#if false
await dbCtx
.DbSetStatsAggr
.Where(x => x.Hour >= startDate && x.Hour <= endDate)
.ExecuteDeleteAsync();
#endif
var itemsToRemove = await dbCtx.DbSetStatsAggr
.Where(x => x.Hour >= minHour && x.Hour <= maxHour)
.ToListAsync();
@@ -126,6 +134,7 @@ namespace MP.Data.Repository.Utils
}
}
#if false
/// <inheritdoc />
public async Task<int> UpsertManyAsyncOrig(List<StatsAggregatedModel> listRecords, bool removeOld)
{
@@ -145,13 +154,6 @@ namespace MP.Data.Repository.Utils
DateTime startDate = firstRec.Hour;
DateTime endDate = lastRec.Hour;
// uso direttamente ExecuteDelete quando in EFCore8...
#if false
await dbCtx
.DbSetStatsAggr
.Where(x => x.Hour >= startDate && x.Hour <= endDate)
.ExecuteDeleteAsync();
#endif
var items = await dbCtx.DbSetStatsAggr
.Where(x => x.Hour >= startDate && x.Hour <= endDate)
.ToListAsync();
@@ -180,7 +182,8 @@ namespace MP.Data.Repository.Utils
await tx.RollbackAsync();
throw;
}
}
}
#endif
#endregion Public Methods
@@ -68,8 +68,102 @@ namespace MP.Data.Repository.Utils
return answ;
}
/// <inheritdoc />
public async Task<int> UpsertManyAsync(List<StatsDetailModel> listRecords, bool removeOld)
{
if (listRecords == null || !listRecords.Any()) return 0;
int answ = 0;
await using var dbCtx = await CreateContextAsync();
await using var tx = await dbCtx.Database.BeginTransactionAsync();
try
{
// 1. Calcolo del range temporale basato sulla lista in arrivo per ottimizzare la query SQL
var minHour = listRecords.Min(x => x.Hour);
var maxHour = listRecords.Max(x => x.Hour);
// 2. Gestione eliminazione distruttiva (se richiesto)
if (removeOld)
{
// uso direttamente ExecuteDelete quando in EFCore8...
#if false
await dbCtx
.DbSetStatsDet
.Where(x => x.Hour >= startDate && x.Hour <= endDate)
.ExecuteDeleteAsync();
#endif
var itemsToRemove = await dbCtx.DbSetStatsDet
.Where(x => x.Hour >= minHour && x.Hour <= maxHour)
.ToListAsync();
if (itemsToRemove.Any())
{
dbCtx.DbSetStatsDet.RemoveRange(itemsToRemove);
await dbCtx.SaveChangesAsync();
}
}
// 3. LOGICA DI UPSERT (Merge basato su Destination + Type + Hour)
// Recuperiamo dal DB solo i record che rientrano nel range temporale della lista in arrivo
var existingRecords = await dbCtx.DbSetStatsDet
.Where(x => x.Hour >= minHour && x.Hour <= maxHour)
.ToListAsync();
// Creiamo il dizionario di lookup con la chiave composta (Dest, Type, Hour)
var lookup = existingRecords.ToDictionary(
x => (x.Destination, x.Type, x.Hour),
x => x
);
foreach (var incoming in listRecords)
{
// Creiamo la chiave di ricerca basata sul record in arrivo
var key = (incoming.Destination, incoming.Type, incoming.Hour);
if (lookup.TryGetValue(key, out var existing))
{
// --- CASO: UPDATE ---
// Aggiorniamo i valori del record esistente con quelli nuovi
existing.RequestCount = incoming.RequestCount;
existing.AvgDuration = incoming.AvgDuration;
existing.MinDuration = incoming.MinDuration;
existing.MaxDuration = incoming.MaxDuration;
existing.NoReply = incoming.NoReply;
}
else
{
// --- CASO: INSERT ---
// Il record non esiste per questa combinazione, lo aggiungiamo
await dbCtx.DbSetStatsDet.AddAsync(incoming);
}
}
// 4. Salvataggio finale delle modifiche (Insert + Update)
answ = await dbCtx.SaveChangesAsync();
// Commit della transazione
await tx.CommitAsync();
// Pulizia memoria del ChangeTracker per evitare accumuli durante sessioni lunghe
dbCtx.ChangeTracker.Clear();
return answ;
}
catch (Exception ex)
{
await tx.RollbackAsync();
Log.Error(ex, "Error during StatsDetail UpsertManyAsync");
throw;
}
}
#if false
/// <inheritdoc />
public async Task<int> UpsertManyAsyncOrig(List<StatsDetailModel> listRecords, bool removeOld)
{
int answ = 0;
await using var dbCtx = await CreateContextAsync();
@@ -87,13 +181,7 @@ namespace MP.Data.Repository.Utils
DateTime startDate = firstRec.Hour;
DateTime endDate = lastRec.Hour;
// uso direttamente ExecuteDelete quando in EFCore8...
#if false
await dbCtx
.DbSetStatsDet
.Where(x => x.Hour >= startDate && x.Hour <= endDate)
.ExecuteDeleteAsync();
#endif
var items = await dbCtx.DbSetStatsDet
.Where(x => x.Hour >= startDate && x.Hour <= endDate)
.ToListAsync();
@@ -121,7 +209,8 @@ namespace MP.Data.Repository.Utils
await tx.RollbackAsync();
throw;
}
}
}
#endif
#endregion Public Methods
+1 -1
View File
@@ -1,6 +1,6 @@
<body>
<i>Modulo MP-IOC </i>
<h4>Versione: 6.16.2604.819</h4>
<h4>Versione: 6.16.2604.907</h4>
<br /> Note di rilascio:
<ul>
<li>
+1 -1
View File
@@ -1 +1 @@
6.16.2604.819
6.16.2604.907
+1 -1
View File
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<item>
<version>6.16.2604.819</version>
<version>6.16.2604.907</version>
<url>https://nexus.steamware.net/repository/SWS/MP-IOC/stable/LAST/MP.IOC.zip</url>
<changelog>https://nexus.steamware.net/repository/SWS/MP-IOC/stable/LAST/ChangeLog.html</changelog>
<mandatory>false</mandatory>
+1 -1
View File
@@ -290,7 +290,7 @@ namespace MP.IOC.Services
{
await using var scope = _scopeFactory.CreateAsyncScope();
var detailService = scope.ServiceProvider.GetRequiredService<IStatsDetailService>();
await detailService.UpsertManyAsync(detailRecordsToInsert, true);
await detailService.UpsertManyAsync(detailRecordsToInsert, false);
Log.Info($"[HOUR] Upserted {detailRecordsToInsert.Count} records to DB");
}