9055eaf73c
- aggiunta pagina operatori - completato fix
185 lines
7.9 KiB
C#
185 lines
7.9 KiB
C#
using Microsoft.Data.SqlClient;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using MP.Core.DTO;
|
|
using MP.Core.Objects;
|
|
using MP.Data.DbModels;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using static EgwCoreLib.Utils.DtUtils;
|
|
|
|
namespace MP.Data.Repository.FluxLog
|
|
{
|
|
public class FluxLogRepository : IFluxLogRepository
|
|
{
|
|
#region Public Constructors
|
|
|
|
public FluxLogRepository(IConfiguration configuration, IDbContextFactory<MoonPro_FluxContext> ctxFactoryFL)
|
|
{
|
|
_configuration = configuration;
|
|
_ctxFactoryFL = ctxFactoryFL;
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Methods
|
|
|
|
/// <inheritdoc />
|
|
public async Task<List<StatDedupDTO>> FluxLogDataReduxAsync(string idxMaccSel, List<string> fluxList, Periodo currPeriodo, Enums.ValSelection valMode, Enums.DataInterval intReq, int maxItem)
|
|
{
|
|
List<StatDedupDTO> procStats = new List<StatDedupDTO>();
|
|
Log.Info($"Inizio FluxLogDataReduxAsync | idxMaccSel: {idxMaccSel} | periodo: {currPeriodo.Inizio:yyyy-MM-dd} --> {currPeriodo.Fine:yyyy-MM-dd}");
|
|
TimeSpan step = TimeSpan.FromHours(1);
|
|
switch (intReq)
|
|
{
|
|
case Enums.DataInterval.minute:
|
|
step = TimeSpan.FromMinutes(1.00 / maxItem);
|
|
break;
|
|
|
|
case Enums.DataInterval.hour:
|
|
step = TimeSpan.FromHours(1.00 / maxItem);
|
|
break;
|
|
|
|
case Enums.DataInterval.day:
|
|
step = TimeSpan.FromDays(1.00 / maxItem);
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
|
|
var pIdxMacchina = new SqlParameter("@IdxMacchina", idxMaccSel);
|
|
var pOnlyTest = new SqlParameter("@OnlyTest", false);
|
|
|
|
await using var dbCtx = await _ctxFactoryFL.CreateDbContextAsync();
|
|
foreach (var item in fluxList)
|
|
{
|
|
Log.Info($"FluxLogDataReduxAsync | Flux: {item}");
|
|
int numRecProc = 0;
|
|
Stopwatch sw = new Stopwatch();
|
|
sw.Start();
|
|
var pCodFlux = new SqlParameter("@CodFlux", item);
|
|
DateTime dtCursStart = currPeriodo.Inizio;
|
|
DateTime dtCursEnd = dtCursStart.Add(step);
|
|
bool setCompleted = false;
|
|
while (!setCompleted)
|
|
{
|
|
var currFlux = await dbCtx
|
|
.DbSetFluxLog
|
|
.Where(x => (x.CodFlux == item) && (x.dtEvento >= dtCursStart && x.dtEvento < dtCursEnd) && (x.IdxMacchina == idxMaccSel))
|
|
.ToListAsync();
|
|
|
|
int numRec = currFlux.Count;
|
|
numRecProc += numRec;
|
|
if (numRec > maxItem)
|
|
{
|
|
List<Periodo> listPeriodi = new List<Periodo>();
|
|
|
|
switch (valMode)
|
|
{
|
|
case Enums.ValSelection.First:
|
|
var recStart = currFlux.Skip(1).FirstOrDefault();
|
|
listPeriodi.Add(new Periodo(recStart.dtEvento, dtCursEnd));
|
|
break;
|
|
|
|
case Enums.ValSelection.Last:
|
|
var recEnd = currFlux.LastOrDefault();
|
|
listPeriodi.Add(new Periodo(dtCursStart, recEnd.dtEvento));
|
|
break;
|
|
|
|
case Enums.ValSelection.Center:
|
|
int idx = 1;
|
|
var recCent = currFlux.Skip(idx / (maxItem + 1)).FirstOrDefault();
|
|
listPeriodi.Add(new Periodo(dtCursStart, recCent.dtEvento));
|
|
if (maxItem > 1)
|
|
{
|
|
for (int i = 2; i < maxItem; i++)
|
|
{
|
|
DateTime dtInizio = recCent.dtEvento;
|
|
recCent = currFlux.Skip(i / (maxItem + 1)).FirstOrDefault();
|
|
listPeriodi.Add(new Periodo(dtInizio, recCent.dtEvento));
|
|
}
|
|
}
|
|
listPeriodi.Add(new Periodo(recCent.dtEvento.AddSeconds(1), dtCursEnd));
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
|
|
foreach (var slot in listPeriodi)
|
|
{
|
|
var pDtStart = new SqlParameter("@DtStart", slot.Inizio);
|
|
var pDtEnd = new SqlParameter("@DtEnd", slot.Fine);
|
|
await dbCtx
|
|
.Database
|
|
.ExecuteSqlRawAsync("EXEC man.stp_ReduceFluxLog @IdxMacchina, @CodFlux, @DtStart, @DtEnd, @OnlyTest", pIdxMacchina, pCodFlux, pDtStart, pDtEnd, pOnlyTest);
|
|
}
|
|
}
|
|
|
|
dtCursStart = dtCursEnd;
|
|
dtCursEnd = dtCursStart.Add(step);
|
|
setCompleted = dtCursStart >= currPeriodo.Fine;
|
|
}
|
|
sw.Stop();
|
|
StatDedupDTO currStat = new StatDedupDTO()
|
|
{
|
|
IdxMacchina = idxMaccSel,
|
|
CodFlux = item,
|
|
Interval = intReq,
|
|
Num4Int = maxItem,
|
|
NumRec = numRecProc,
|
|
ProcTime = sw.Elapsed.TotalSeconds
|
|
};
|
|
procStats.Add(currStat);
|
|
}
|
|
Log.Info($"FINE FluxLogDataReduxAsync | idxMaccSel: {idxMaccSel} | periodo: {currPeriodo.Inizio:yyyy-MM-dd} --> {currPeriodo.Fine:yyyy-MM-dd}");
|
|
return procStats;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<List<FluxLogModel>> FluxLogGetLastFiltAsync(DateTime DtMax, DateTime DtMin, string IdxMacchina, string CodFlux, int MaxRec)
|
|
{
|
|
await using var dbCtx = await _ctxFactoryFL.CreateDbContextAsync();
|
|
return await dbCtx
|
|
.DbSetFluxLog
|
|
.AsNoTracking()
|
|
.Where(x => (x.dtEvento >= DtMin && x.dtEvento <= DtMax) && (IdxMacchina == "*" || x.IdxMacchina == IdxMacchina) && (CodFlux == "*" || x.CodFlux == CodFlux))
|
|
.OrderByDescending(x => x.dtEvento)
|
|
.Take(MaxRec)
|
|
.ToListAsync() ?? new();
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<List<ParetoFluxLogDTO>> FluxLogParetoAsync(string idxMacchina, DateTime dtFrom, DateTime dtTo)
|
|
{
|
|
await using var dbCtx = await _ctxFactoryFL.CreateDbContextAsync();
|
|
return await dbCtx
|
|
.DbSetFluxLog
|
|
.Where(x => (string.IsNullOrEmpty(idxMacchina) || x.IdxMacchina == idxMacchina) && (dtFrom <= x.dtEvento && x.dtEvento <= dtTo))
|
|
.AsNoTracking()
|
|
.GroupBy(x => x.CodFlux)
|
|
.Select(g => new ParetoFluxLogDTO() { IdxMacchina = idxMacchina, CodFlux = g.Key, Qty = g.Count() })
|
|
.OrderByDescending(x => x.Qty)
|
|
.ToListAsync() ?? new();
|
|
}
|
|
|
|
#endregion Public Methods
|
|
|
|
#region Protected Fields
|
|
|
|
protected readonly IDbContextFactory<MoonPro_FluxContext> _ctxFactoryFL;
|
|
|
|
#endregion Protected Fields
|
|
|
|
#region Private Fields
|
|
|
|
private static NLog.Logger Log = NLog.LogManager.GetCurrentClassLogger();
|
|
private readonly IConfiguration _configuration;
|
|
|
|
#endregion Private Fields
|
|
}
|
|
} |