Aggiunta modello dati statistiche e reposiitory/service per implementarli (da validare)

This commit is contained in:
Samuele E. Locatelli (W11-AI)
2026-04-04 18:23:52 +02:00
parent 80b87cad63
commit fb12ccc028
12 changed files with 719 additions and 3 deletions
@@ -0,0 +1,47 @@
// <Auto-Generated>
// This is here so CodeMaid doesn't reorganize this document
// </Auto-Generated>
namespace MP.Data.DbModels.Utils
{
/// <summary>
/// Classe statistiche impiego orario complessive
/// </summary>
[Table("stats_aggr")]
public class StatsAggregatedModel
{
[Key]
public int Id { get; set; }
/// <summary>
/// DataOra riferimento
/// </summary>
public DateTime Hour { get; set; }
/// <summary>
/// Num richieste complete/concluse (con ritorno)
/// </summary>
public long RequestCount { get; set; } = 0;
/// <summary>
/// Durata media esecuzione
/// </summary>
public double AvgDuration { get; set; } = 0;
/// <summary>
/// Durata massima esecuzione
/// </summary>
public double MaxDuration { get; set; } = 0;
/// <summary>
/// Durata 5 perc esecuzione
/// </summary>
public double Perc05Duration { get; set; } = 0;
/// <summary>
/// Durata 95 perc esecuzione
/// </summary>
public double Perc95Duration { get; set; } = 0;
/// <summary>
/// Durata massima esecuzione
/// </summary>
public double MaxDuration { get; set; } = 0;
/// <summary>
/// numero richieste senza risposta (num RUID avviati - num RUID chiusi)
/// </summary>
public long NoReply { get; set; } = 0;
}
}
@@ -0,0 +1,51 @@
// <Auto-Generated>
// This is here so CodeMaid doesn't reorganize this document
// </Auto-Generated>
namespace MP.Data.DbModels.Utils
{
/// <summary>
/// Classe statistiche impiego (orarie) di dettaglio (per envir/tipo)
/// </summary>
[Table("stats_detail")]
public class StatsDetailModel
{
[Key]
public int Id { get; set; }
/// <summary>
/// Ambiente
/// </summary>
public string Environment { get; set; } = "";
/// <summary>
/// Codice mode/submode
/// </summary>
public string Type { get; set; } = "";
/// <summary>
/// DataOra riferimento
/// </summary>
public DateTime Hour { get; set; }
/// <summary>
/// Num richieste complete/concluse (con ritorno)
/// </summary>
public long RequestCount { get; set; } = 0;
/// <summary>
/// Durata media esecuzione
/// </summary>
public double AvgDuration { get; set; } = 0;
/// <summary>
/// Durata 5 perc esecuzione
/// </summary>
public double Perc05Duration { get; set; } = 0;
/// <summary>
/// Durata 95 perc esecuzione
/// </summary>
public double Perc95Duration { get; set; } = 0;
/// <summary>
/// Durata massima esecuzione
/// </summary>
public double MaxDuration { get; set; } = 0;
/// <summary>
/// numero richieste senza risposta (num RUID avviati - num RUID chiusi)
/// </summary>
public long NoReply { get; set; } = 0;
}
}
+131
View File
@@ -0,0 +1,131 @@
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.Extensions.Configuration;
using MP.Core.DTO;
using MP.Data.DbModels;
using MP.Data.DTO;
using NLog;
#nullable disable
// <Auto-Generated>
// This is here so CodeMaid doesn't reorganize this document
// </Auto-Generated>
namespace MP.Data
{
public partial class MoonPro_UtilsContext : DbContext
{
#region Private Fields
private static NLog.Logger Log = LogManager.GetCurrentClassLogger();
private IConfiguration _configuration;
#endregion Private Fields
#region Public Constructors
/// <summary>
/// Indispensabile x prima generazione migrations EFCore
/// </summary>
[Obsolete("This constructor should never be used directly, and is only needed to generate entityframework stuff. Connection string can be adapted as pleased.")]
public MoonPro_UtilsContext()
{
}
public MoonPro_UtilsContext(IConfiguration configuration)
{
_configuration = configuration;
}
public MoonPro_UtilsContext(DbContextOptions<MoonPro_UtilsContext> options) : base(options)
{
}
#endregion Public Constructors
#region Public Properties
public virtual DbSet<StatsDetailModel> DbSetStatsDet { get; set; }
public virtual DbSet<StatsAggregatedModel> DbSetStatsAggr { get; set; }
#endregion Public Properties
#region Private Methods
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
#endregion Private Methods
#region Protected Methods
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
string connString = _configuration.GetConnectionString("MP.Utils");
if (!string.IsNullOrEmpty(connString))
{
optionsBuilder.UseSqlServer(connString);
}
else
{
optionsBuilder.UseSqlServer("Server=SQL2016DEV;Database=MoonPro_Utils;Trusted_Connection=True;");
}
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.HasAnnotation("Relational:Collation", "SQL_Latin1_General_CP1_CI_AS");
// indici tabelle stats
modelBuilder.Entity<StatsDetailModel>()
.HasIndex(x => new { x.Hour, x.Environment, x.Type })
.HasDatabaseName("idx_statsdet_hour_env_type")
.IsUnique();
modelBuilder.Entity<StatsAggregatedModel>()
.HasIndex(x => new { x.Hour })
.HasDatabaseName("idx_statsaggr_hour")
.IsUnique();
// modelBuilder.Entity<AnagArticoliModel>(entity =>
// {
// entity.HasKey(e => e.CodArticolo);
// entity.ToView("AnagArticoli");
// entity.Property(e => e.CodArticolo)
// .IsRequired()
// .HasMaxLength(50);
// entity.Property(e => e.DescArticolo)
// .IsRequired()
// .HasMaxLength(250);
// entity.Property(e => e.Disegno)
// .IsRequired()
// .HasMaxLength(50);
// entity.Property(e => e.Tipo)
// .IsRequired()
// .HasMaxLength(50);
// entity.Property(e => e.Azienda)
// .IsRequired()
// .HasMaxLength(50);
// });
// modelBuilder.Entity<ParetoFluxLogDTO>(entity =>
// {
// entity.HasKey(e => new { e.IdxMacchina, e.CodFlux });
// });
OnModelCreatingPartial(modelBuilder);
}
#endregion Protected Methods
}
}
@@ -0,0 +1,32 @@
using EgwCoreLib.Utils;
namespace MP.Data.Repository.Utils
{
/// <summary>
/// Interfaccia per le statistiche aggregate.
/// </summary>
public interface IStatsAggrRepository
{
/// <summary>
/// Recupera l'elenco delle statistiche aggregate per un periodo specifico.
/// </summary>
/// <param name="dtStart">La data di inizio del periodo.</param>
/// <param name="dtEnd">La data di fine del periodo.</param>
/// <returns>L'elenco delle statistiche aggregate ordinate cronologicamente.</returns>
Task<List<StatsAggregatedModel>> GetFiltAsync(DateTime dtStart, DateTime dtEnd);
/// <summary>
/// Recupera l'intervallo temporale disponibile nel database per le statistiche aggregate.
/// </summary>
/// <returns>L'intervallo di date (minima e massima ora presente).</returns>
Task<DtUtils.Periodo> GetRangeAsync();
/// <summary>
/// Inserisce o aggiorna in blocco le statistiche aggregate nel database.
/// </summary>
/// <param name="listRecords">L'elenco dei record da inserire.</param>
/// <param name="removeOld">Se true, elimina preventivamente i record nel periodo richiesto.</param>
/// <returns>Il numero di record inseriti.</returns>
Task<int> UpsertManyAsync(List<StatsAggregatedModel> listRecords, bool removeOld);
}
}
@@ -0,0 +1,36 @@
using EgwCoreLib.Utils;
namespace MP.Data.Repository.Utils
{
/// <summary>
/// Interfaccia per le statistiche di dettaglio.
/// </summary>
public interface IStatsDetailRepository
{
/// <summary>
/// Recupera l'elenco delle statistiche di dettaglio per un periodo specifico, con filtri opzionali.
/// </summary>
/// <param name="dtStart">La data di inizio del periodo.</param>
/// <param name="dtEnd">La data di fine del periodo.</param>
/// <param name="sEnvir">Filtro opzionale per l'ambiente (es. "DEV", "PROD").</param>
/// <param name="sType">Filtro opzionale per il tipo di statistica.</param>
/// <returns>L'elenco delle statistiche di dettaglio ordinate cronologicamente.</returns>
Task<List<StatsDetailModel>> GetFiltAsync(DateTime dtStart, DateTime dtEnd, string sEnvir = "", string sType = "");
/// <summary>
/// Recupera l'intervallo temporale disponibile nel database per le statistiche di dettaglio.
/// </summary>
/// <param name="sEnvir">Filtro opzionale per l'ambiente.</param>
/// <param name="sType">Filtro opzionale per il tipo.</param>
/// <returns>L'intervallo di date (minima e massima ora presente).</returns>
Task<DtUtils.Periodo> GetRangeAsync(string sEnvir, string sType);
/// <summary>
/// Inserisce o aggiorna in blocco le statistiche di dettaglio nel database.
/// </summary>
/// <param name="listRecords">L'elenco dei record da inserire.</param>
/// <param name="removeOld">Se true, elimina preventivamente i record nel periodo richiesto.</param>
/// <returns>Il numero di record inseriti.</returns>
Task<int> UpsertManyAsync(List<StatsDetailModel> listRecords, bool removeOld);
}
}
@@ -0,0 +1,93 @@
using EgwCoreLib.Utils;
namespace MP.Data.Repository.Utils
{
public class StatsAggrRepository : BaseRepository, IStatsAggrRepository
{
#region Public Constructors
public StatsAggrRepository(IDbContextFactory<DataLayerContext> ctxFactory) : base(ctxFactory)
{
}
#endregion Public Constructors
#region Public Methods
/// <inheritdoc />
public async Task<List<StatsAggregatedModel>> GetFiltAsync(DateTime dtStart, DateTime dtEnd)
{
await using var dbCtx = await CreateContextAsync();
return await dbCtx
.DbSetStatsAggr
.Where(x => x.Hour >= dtStart && x.Hour <= dtEnd)
.AsNoTracking()
.OrderBy(x => x.Hour)
.ToListAsync();
}
/// <inheritdoc />
public async Task<DtUtils.Periodo> GetRangeAsync()
{
await using var dbCtx = await CreateContextAsync();
DtUtils.Periodo answ = new DtUtils.Periodo(DtUtils.PeriodSet.Today);
var query = dbCtx.DbSetStatsAggr.AsQueryable();
var minHour = await query.MinAsync(x => x.Hour);
var maxHour = await query.MaxAsync(x => x.Hour);
answ.Inizio = minHour;
answ.Fine = maxHour;
// ritorno!
return answ;
}
/// <inheritdoc />
public async Task<int> UpsertManyAsync(List<StatsAggregatedModel> listRecords, bool removeOld)
{
int answ = 0;
await using var dbCtx = await CreateContextAsync();
await using var tx = await dbCtx.Database.BeginTransactionAsync();
try
{
// in primis se richiesto calcolo range periodo e svuoto...
if (removeOld)
{
var firstRec = listRecords.OrderBy(x => x.Hour).FirstOrDefault();
var lastRec = listRecords.OrderByDescending(x => x.Hour).FirstOrDefault();
if (firstRec != null && lastRec != null)
{
DateTime startDate = firstRec.Hour;
DateTime endDate = lastRec.Hour;
// uso direttamente ExecuteDelete
await dbCtx
.DbSetStatsAggr
.Where(x => x.Hour >= startDate && x.Hour <= endDate)
.ExecuteDeleteAsync();
}
}
// ora preparo inserimento massivo
await dbCtx
.DbSetStatsAggr
.AddRangeAsync(listRecords);
// salvo!
answ = await dbCtx.SaveChangesAsync();
// commit transazione
await tx.CommitAsync();
// libero memoria del changeTracker
dbCtx.ChangeTracker.Clear();
return answ;
}
catch
{
await tx.RollbackAsync();
throw;
}
}
#endregion Public Methods
}
}
@@ -0,0 +1,113 @@
using EgwCoreLib.Utils;
namespace MP.Data.Repository.Utils
{
public class StatsDetailRepository : BaseRepository, IStatsDetailRepository
{
#region Public Constructors
public StatsDetailRepository(IDbContextFactory<DataLayerContext> ctxFactory) : base(ctxFactory)
{
}
#endregion Public Constructors
#region Internal Methods
/// <inheritdoc />
public async Task<List<StatsDetailModel>> GetFiltAsync(DateTime dtStart, DateTime dtEnd, string sEnvir = "", string sType = "")
{
await using var dbCtx = await CreateContextAsync();
List<StatsDetailModel> answ = new List<StatsDetailModel>();
// recupero ed ordino per data-ora
var query = dbCtx.DbSetStatsDet
.Where(x => x.Hour >= dtStart && x.Hour <= dtEnd);
if (!string.IsNullOrEmpty(sEnvir))
query = query.Where(x => x.Environment == sEnvir);
if (!string.IsNullOrEmpty(sType))
query = query.Where(x => x.Type == sType);
answ = await query
.AsNoTracking()
.OrderBy(x => x.Hour)
.ThenBy(x => x.Environment)
.ThenBy(x => x.Type)
.ToListAsync();
return answ;
}
/// <inheritdoc />
public async Task<DtUtils.Periodo> GetRangeAsync(string sEnvir, string sType)
{
await using var dbCtx = await CreateContextAsync();
DtUtils.Periodo answ = new DtUtils.Periodo(DtUtils.PeriodSet.Today);
var query = dbCtx.DbSetStatsDet.AsQueryable();
if (!string.IsNullOrEmpty(sEnvir))
query = query.Where(x => x.Environment == sEnvir);
if (!string.IsNullOrEmpty(sType))
query = query.Where(x => x.Type == sType);
var minHour = await query.MinAsync(x => x.Hour);
var maxHour = await query.MaxAsync(x => x.Hour);
answ.Inizio = minHour;
answ.Fine = maxHour;
return answ;
}
/// <inheritdoc />
public async Task<int> UpsertManyAsync(List<StatsDetailModel> listRecords, bool removeOld)
{
int answ = 0;
await using var dbCtx = await CreateContextAsync();
await using var tx = await dbCtx.Database.BeginTransactionAsync();
try
{
// in primis se richiesto calcolo range periodo e svuoto...
if (removeOld)
{
var firstRec = listRecords.OrderBy(x => x.Hour).FirstOrDefault();
var lastRec = listRecords.OrderByDescending(x => x.Hour).FirstOrDefault();
if (firstRec != null && lastRec != null)
{
DateTime startDate = firstRec.Hour;
DateTime endDate = lastRec.Hour;
// uso direttamente ExecuteDelete
await dbCtx
.DbSetStatsDet
.Where(x => x.Hour >= startDate && x.Hour <= endDate)
.ExecuteDeleteAsync();
}
}
// ora preparo inserimento massivo
await dbCtx
.DbSetStatsDet
.AddRangeAsync(listRecords);
// salvo!
answ = await dbCtx.SaveChangesAsync();
// commit transazione
await tx.CommitAsync();
// libero memoria del changeTracker
dbCtx.ChangeTracker.Clear();
return answ;
}
catch
{
await tx.RollbackAsync();
throw;
}
}
#endregion Internal Methods
}
}
@@ -0,0 +1,31 @@
namespace MP.Data.Services.Utils
{
public interface IStatsAggrService
{
#region Public Methods
/// <summary>
/// Recupera l'elenco delle statistiche aggregate per un periodo specificato.
/// Utilizza la cache automaticamente.
/// </summary>
/// <param name="dtStart">Data inizio periodo</param>
/// <param name="dtEnd">Data fine periodo</param>
Task<List<StatsAggregatedModel>> GetFiltAsync(DateTime dtStart, DateTime dtEnd);
/// <summary>
/// Recupera il range di periodi valido per le chiamate aggregate.
/// Utilizza la cache automaticamente.
/// </summary>
Task<DtUtils.Periodo> GetRangeAsync();
/// <summary>
/// Inserisce o aggiorna in batch le statistiche aggregate nel database.
/// Opzionalmente elimina i record precedenti nel periodo specificato.
/// </summary>
/// <param name="listRecords">Elenco dei record da inserire/aggiornare</param>
/// <param name="removeOld">Se true elimina preventivamente i record nel periodo richiesto</param>
Task<int> UpsertManyAsync(List<StatsAggregatedModel> listRecords, bool removeOld);
#endregion Public Methods
}
}
@@ -0,0 +1,35 @@
namespace MP.Data.Services.Utils
{
public interface IStatsDetailService
{
#region Public Methods
/// <summary>
/// Recupera le statistiche di dettaglio per un periodo specificato, con filtri opzionali su ambiente e tipo.
/// Utilizza la cache automaticamente.
/// </summary>
/// <param name="dtStart">Data inizio periodo</param>
/// <param name="dtEnd">Data fine periodo</param>
/// <param name="sEnvir">Filtro opzionale per ambiente (default: vuoto = tutti)</param>
/// <param name="sType">Filtro opzionale per tipo (default: vuoto = tutti)</param>
Task<List<StatsDetailModel>> GetFiltAsync(DateTime dtStart, DateTime dtEnd, string sEnvir = "", string sType = "");
/// <summary>
/// Recupera il range di periodi valido per le chiamate di dettaglio.
/// Utilizza la cache automaticamente.
/// </summary>
/// <param name="sEnvir">Ambiente filtrato</param>
/// <param name="sType">Tipo filtrato</param>
Task<DtUtils.Periodo> GetRangeAsync(string sEnvir, string sType);
/// <summary>
/// Inserisce o aggiorna in batch le statistiche di dettaglio nel database.
/// Opzionalmente elimina i record precedenti nel periodo specificato.
/// </summary>
/// <param name="listRecords">Elenco dei record da inserire/aggiornare</param>
/// <param name="removeOld">Se true elimina preventivamente i record nel periodo richiesto</param>
Task<int> UpsertManyAsync(List<StatsDetailModel> listRecords, bool removeOld);
#endregion Public Methods
}
}
@@ -0,0 +1,74 @@
namespace MP.Data.Services.Utils
{
public class StatsAggrService : BaseServ, IStatsAggrService
{
#region Public Constructors
public StatsAggrService(
IConfiguration config,
IConnectionMultiplexer redis,
IStatsAggrRepository repo) : base(config, redis)
{
_className = "StatsAggr";
_repo = repo;
}
#endregion Public Constructors
#region Public Methods
/// <inheritdoc />
public async Task<List<StatsAggregatedModel>> GetFiltAsync(DateTime dtStart, DateTime dtEnd)
{
return await TraceAsync($"{_className}.GetFilt", async (activity) =>
{
return await GetOrSetCacheAsync(
$"{_redisBaseKey}:{_className}:DT:{dtStart:yyyyMMdd}:{dtEnd:yyyyMMdd}",
async () => await _repo.GetFiltAsync(dtStart, dtEnd),
UltraLongCache
);
});
}
/// <inheritdoc />
public async Task<DtUtils.Periodo> GetRangeAsync()
{
return await TraceAsync($"{_className}.GetRange", async (activity) =>
{
return await GetOrSetCacheAsync(
$"{_redisBaseKey}:{_className}:Range",
async () => await _repo.GetRangeAsync(),
UltraFastCache
);
});
}
/// <inheritdoc />
public async Task<int> UpsertManyAsync(List<StatsAggregatedModel> listRecords, bool removeOld)
{
return await TraceAsync($"{_className}.UpsertMany", async (activity) =>
{
string operation = "UpsertMany";
var success = await _repo.UpsertManyAsync(listRecords, removeOld);
activity?.SetTag("db.operation", operation);
if (success > 0)
{
await ClearCacheAsync($"{_redisBaseKey}:{_className}:*");
}
return success;
});
}
#endregion Public Methods
#region Private Fields
private readonly string _className;
private readonly IStatsAggrRepository _repo;
#endregion Private Fields
}
}
@@ -0,0 +1,74 @@
namespace MP.Data.Services.Utils
{
public class StatsDetailService : BaseServ, IStatsDetailService
{
#region Public Constructors
public StatsDetailService(
IConfiguration config,
IConnectionMultiplexer redis,
IStatsDetailRepository repo) : base(config, redis)
{
_className = "StatsDetail";
_repo = repo;
}
#endregion Public Constructors
#region Public Methods
/// <inheritdoc />
public async Task<List<StatsDetailModel>> GetFiltAsync(DateTime dtStart, DateTime dtEnd, string sEnvir = "", string sType = "")
{
return await TraceAsync($"{_className}.GetFilt", async (activity) =>
{
return await GetOrSetCacheAsync(
$"{_redisBaseKey}:{_className}:DT:{dtStart:yyyyMMdd}:{dtEnd:yyyyMMdd}",
async () => await _repo.GetFiltAsync(dtStart, dtEnd),
UltraLongCache
);
});
}
/// <inheritdoc />
public async Task<DtUtils.Periodo> GetRangeAsync(string sEnvir, string sType)
{
return await TraceAsync($"{_className}.GetRange", async (activity) =>
{
return await GetOrSetCacheAsync(
$"{_redisBaseKey}:{_className}:Range:{sEnvir}:{sType}",
async () => await _repo.GetRangeAsync(sEnvir, sType),
UltraFastCache
);
});
}
/// <inheritdoc />
public async Task<int> UpsertManyAsync(List<StatsDetailModel> listRecords, bool removeOld)
{
return await TraceAsync($"{_className}.UpsertMany", async (activity) =>
{
string operation = "UpsertMany";
var success = await _repo.UpsertManyAsync(listRecords, removeOld);
activity?.SetTag("db.operation", operation);
if (success > 0)
{
await ClearCacheAsync($"{_redisBaseKey}:{_className}:*");
}
return success;
});
}
#endregion Public Methods
#region Private Fields
private readonly string _className;
private readonly IStatsDetailRepository _repo;
#endregion Private Fields
}
}
+2 -3
View File
@@ -70,9 +70,7 @@
}
},
"RouteMan": {
"FlushIntervalSeconds": 10,
//"FlushIntervalSeconds": 60,
//"FlushIntervalSeconds": 300,
"FlushIntervalSeconds": 60,
"DefaultWeightOld": 100,
"DefaultWeightNew": 0
},
@@ -84,6 +82,7 @@
},
"ConnectionStrings": {
"MP.Data": "Server=SQL2016DEV;Database=MoonPro; User ID=sa;Password=keyhammer16; integrated security=False; MultipleActiveResultSets=True; App=MP.IOC;",
"MP.Utils": "Server=SQL2016DEV;Database=MoonPro_Utils; User ID=sa;Password=keyhammer16; integrated security=False; MultipleActiveResultSets=True; App=MP.IOC;",
"Redis": "redis.ufficio:26379,serviceName=devel,DefaultDatabase=5,connectTimeout=5000,syncTimeout=5000,asyncTimeout=5000,abortConnect=false,ssl=false",
"RedisAdmin": "redis.ufficio:26379,serviceName=devel,DefaultDatabase=5,connectTimeout=5000,syncTimeout=5000,asyncTimeout=5000,abortConnect=false,ssl=false,allowAdmin=true",
"mdbConnString": "mongodb://W2019-MONGODB:27017"