From 6d575da0b7056782e1f604d8b3cc8267e5ad28ff Mon Sep 17 00:00:00 2001 From: "Samuele E. Locatelli (W11-AI)" Date: Tue, 7 Apr 2026 18:46:28 +0200 Subject: [PATCH] Modifica upsert dati --- .../Repository/Utils/StatsAggrRepository.cs | 38 ++++++++++++++++++- .../Repository/Utils/StatsDetailRepository.cs | 37 +++++++++++++++++- MP.IOC/Services/MetricsDbFlushService.cs | 2 - 3 files changed, 71 insertions(+), 6 deletions(-) diff --git a/MP.Data/Repository/Utils/StatsAggrRepository.cs b/MP.Data/Repository/Utils/StatsAggrRepository.cs index 8867c657..1d521b31 100644 --- a/MP.Data/Repository/Utils/StatsAggrRepository.cs +++ b/MP.Data/Repository/Utils/StatsAggrRepository.cs @@ -82,9 +82,43 @@ namespace MP.Data.Repository.Utils } // ora preparo inserimento massivo - await dbCtx + var existingRecords = await dbCtx .DbSetStatsAggr - .AddRangeAsync(listRecords); + .Where(x => listRecords.Select(r => r.Hour).Contains(x.Hour) && + listRecords.Select(r => r.Type).Contains(x.Type)) + .ToListAsync(); + + if (existingRecords.Count > 0) + { + var existingHours = existingRecords.Select(r => new { r.Hour, r.Type }).ToHashSet(); + + var recordsToUpdate = listRecords.Where(r => + existingHours.Contains(new { Hour = r.Hour, Type = r.Type })).ToList(); + + var recordsToAdd = listRecords.Where(r => + !existingHours.Contains(new { Hour = r.Hour, Type = r.Type })).ToList(); + + foreach (var updateRec in recordsToUpdate) + { + var existing = existingRecords.First(x => x.Hour == updateRec.Hour && x.Type == updateRec.Type); + existing.RequestCount = updateRec.RequestCount; + existing.AvgDuration = updateRec.AvgDuration; + existing.MaxDuration = updateRec.MaxDuration; + existing.MinDuration = updateRec.MinDuration; + existing.NoReply = updateRec.NoReply; + } + + if (recordsToAdd.Count > 0) + { + await dbCtx.DbSetStatsAggr.AddRangeAsync(recordsToAdd); + } + } + else + { + await dbCtx + .DbSetStatsAggr + .AddRangeAsync(listRecords); + } // salvo! answ = await dbCtx.SaveChangesAsync(); diff --git a/MP.Data/Repository/Utils/StatsDetailRepository.cs b/MP.Data/Repository/Utils/StatsDetailRepository.cs index 63f9f0aa..f687ac45 100644 --- a/MP.Data/Repository/Utils/StatsDetailRepository.cs +++ b/MP.Data/Repository/Utils/StatsDetailRepository.cs @@ -103,9 +103,42 @@ namespace MP.Data.Repository.Utils } // ora preparo inserimento massivo - await dbCtx + var existingRecords = await dbCtx .DbSetStatsDet - .AddRangeAsync(listRecords); + .Where(x => listRecords.Select(r => new { r.Hour, r.Environment, r.Type }).Contains(new { Hour = x.Hour, Environment = x.Environment, Type = x.Type })) + .ToListAsync(); + + if (existingRecords.Count > 0) + { + var existingKeys = existingRecords.Select(r => new { r.Hour, r.Environment, r.Type }).ToHashSet(); + + var recordsToUpdate = listRecords.Where(r => + existingKeys.Contains(new { Hour = r.Hour, Environment = r.Environment, Type = r.Type })).ToList(); + + var recordsToAdd = listRecords.Where(r => + !existingKeys.Contains(new { Hour = r.Hour, Environment = r.Environment, Type = r.Type })).ToList(); + + foreach (var updateRec in recordsToUpdate) + { + var existing = existingRecords.First(x => x.Hour == updateRec.Hour && x.Environment == updateRec.Environment && x.Type == updateRec.Type); + existing.RequestCount = updateRec.RequestCount; + existing.AvgDuration = updateRec.AvgDuration; + existing.MaxDuration = updateRec.MaxDuration; + existing.MinDuration = updateRec.MinDuration; + existing.NoReply = updateRec.NoReply; + } + + if (recordsToAdd.Count > 0) + { + await dbCtx.DbSetStatsDet.AddRangeAsync(recordsToAdd); + } + } + else + { + await dbCtx + .DbSetStatsDet + .AddRangeAsync(listRecords); + } // salvo! answ = await dbCtx.SaveChangesAsync(); diff --git a/MP.IOC/Services/MetricsDbFlushService.cs b/MP.IOC/Services/MetricsDbFlushService.cs index 45152ae4..1050ef60 100644 --- a/MP.IOC/Services/MetricsDbFlushService.cs +++ b/MP.IOC/Services/MetricsDbFlushService.cs @@ -35,8 +35,6 @@ namespace MP.IOC.Services protected override async Task ExecuteAsync(CancellationToken stoppingToken) { - await RecoverHistoricalMetricsAsync(); - var interval = _config.GetValue("RouteMan:FlushIntervalSeconds", FlushIntervalSeconds); while (!stoppingToken.IsCancellationRequested)