Implementato metodi x update DTO al ricevimento dati + riduzione inserimento

This commit is contained in:
Samuele Locatelli
2021-08-05 13:59:25 +02:00
parent 303d08a229
commit ff2b83164d
10 changed files with 207 additions and 38 deletions
+61 -22
View File
@@ -136,6 +136,19 @@ namespace GWMS.Data.Controllers
#region Public Methods
/// <summary>
/// Calcola DataOra arrotondata per eccesso secondo intervallo minuti indicato
/// </summary>
/// <param name="dateOrig"></param>
/// <param name="minInt"></param>
/// <returns></returns>
public DateTime DateRoundEnd(DateTime dateOrig, int minInt)
{
int minRound = (int)Math.Ceiling((double)dateOrig.Minute / minInt) * minInt;
DateTime roundDate = dateOrig.Date.AddHours(dateOrig.Hour).AddMinutes(minRound);
return roundDate;
}
public void Dispose()
{
// Clear database context
@@ -212,14 +225,14 @@ namespace GWMS.Data.Controllers
return dbResult;
}
public List<PlantDTO> GetPlantsDTO()
public List<PlantDTO> GetPlantsDTO(int maxRecords)
{
var plantList = dbCtx
.DbSetPlant
.ToList();
var dbResult = plantList
.Select(x => PlantDTO(x.PlantId))
.Select(x => PlantDTO(x.PlantId, maxRecords))
.ToList();
return dbResult;
@@ -334,9 +347,10 @@ namespace GWMS.Data.Controllers
/// <summary>
/// Recupero info PLANT modalità DTO
/// </summary>
/// <param name="PlantId"></param>
/// <param name="PlantId">Impianto</param>
/// <param name="PlantId">Max numero di record da recuperare</param>
/// <returns></returns>
public PlantDTO PlantDTO(int PlantId)
public PlantDTO PlantDTO(int PlantId, int maxRecords)
{
var currPlant = GetPlant(PlantId);
@@ -352,30 +366,35 @@ namespace GWMS.Data.Controllers
.DbSetPlantLog
.Where(x => x.FluxType == "Level" && x.PlantId == PlantId)
.OrderBy(x => x.DtEvent)
.Take(maxRecords)
.ToList();
var rawMainPressData = dbCtx
.DbSetPlantLog
.Where(x => x.FluxType == "MainPress" && x.PlantId == PlantId)
.OrderBy(x => x.DtEvent)
.Take(maxRecords)
.ToList();
var rawBHPressData = dbCtx
.DbSetPlantLog
.Where(x => x.FluxType == "PressBH" && x.PlantId == PlantId)
.OrderBy(x => x.DtEvent)
.Take(maxRecords)
.ToList();
var rawBLPressData = dbCtx
.DbSetPlantLog
.Where(x => x.FluxType == "PressBL" && x.PlantId == PlantId)
.OrderBy(x => x.DtEvent)
.Take(maxRecords)
.ToList();
var rawOrderData = dbCtx
.DbSetOrders
.Where(x => x.PlantId == PlantId)
.OrderBy(x => x.DtOrder)
.Take(maxRecords)
.ToList();
LevelTS = rawLevelData
@@ -397,6 +416,7 @@ namespace GWMS.Data.Controllers
PressTS.Add("BH", PressBHTS);
PressTS.Add("BL", PressBLTS);
double actLevel = LevelTS.Count > 0 ? LevelTS.OrderByDescending(x => x.DtEvent).Take(1).FirstOrDefault().ValDouble : 0;
double valMain = PressMainTS.Count > 0 ? PressMainTS.OrderByDescending(x => x.DtEvent).Take(1).FirstOrDefault().ValDouble : 0;
double valBH = PressBHTS.Count > 0 ? PressBHTS.OrderByDescending(x => x.DtEvent).Take(1).FirstOrDefault().ValDouble : 0;
double valBL = PressBLTS.Count > 0 ? PressBLTS.OrderByDescending(x => x.DtEvent).Take(1).FirstOrDefault().ValDouble : 0;
@@ -410,7 +430,7 @@ namespace GWMS.Data.Controllers
PlantId = PlantId,
PlantCode = currPlant.PlantCode,
PlantDesc = currPlant.PlantDesc,
LevelAct = currPlant.LevelAct,
LevelAct = actLevel,
LevelMax = currPlant.LevelMax,
PressAct = PressAct,
LevelTS = LevelTS,
@@ -421,36 +441,55 @@ namespace GWMS.Data.Controllers
}
/// <summary>
/// Inserimento di un novo record x plant log
/// Recupero da DB l'ultimo record per ogni flusso
/// </summary>
/// <returns></returns>
public List<PlantLogModel> PlantLogGetLastByFlux()
{
List<PlantLogModel> lastRec = dbCtx
.DbSetPlantLog
.OrderByDescending(o => o.DtEvent)
.GroupBy(g => g.FluxType)
.Select(s => s.First())
.ToList();
return lastRec;
}
/// <summary>
/// Inserimento di un set di record x plant log
/// </summary>
/// <param name="PlantLogModel">Record da inserire (senza ID...)</param>
/// <returns></returns>
public bool PlantLogInsertNew(List<PlantLogModel> newItems)
{
bool fatto = false;
//List<PlantLogModel> newList = new List<PlantLogModel>();
//foreach (var item in newItems)
//{
// newList.Add(new PlantLogModel()
// {
// DtEvent = item.DtEvent,
// FluxType = item.FluxType,
// PlantId = item.PlantId,
// ValNumber = item.ValNumber,
// ValString = item.ValString
// });
//}
try
{
dbCtx
.DbSetPlantLog
.AddRange(newItems);
//.AddRange(newList);
dbCtx.SaveChanges();
fatto = true;
#if false
// ogni 10 insert faccio chiamata x pulizia per le ultime 3h... FARE!!!!
Dictionary<string, int> flux2prox = new Dictionary<string, int>();
foreach (var item in newItems)
{
if (!flux2prox.ContainsKey(item.FluxType))
{
flux2prox.Add(item.FluxType, item.PlantId);
}
}
// ciclo x ogni flusso
foreach (var item in flux2prox)
{
DateTime adesso = DateTime.Now;
DateTime startTime = DateTime.Today.AddHours(adesso.Hour - 2);
string sqlQuery = $"CALL DecimateLog('{startTime}', {item.Value}, 15, '{item.Key}')";
var table = dbCtx.DbSetPlantLog.FromSqlRaw(sqlQuery);
}
#endif
}
catch (Exception exc)
{
-4
View File
@@ -26,8 +26,4 @@
<PackageReference Include="NLog" Version="4.7.10" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="5.0.0" />
</ItemGroup>
<ItemGroup>
<Folder Include="Resources\" />
</ItemGroup>
</Project>
+5 -3
View File
@@ -231,7 +231,9 @@ namespace GWMS.UI.Controllers
if (currPlant != null && currPlant.PlantId > 0)
{
double valDbl = 0;
double.TryParse(valore, out valDbl);
NumberStyles style = NumberStyles.Number;
CultureInfo culture = CultureInfo.CreateSpecificCulture("it-IT");
double.TryParse(valore.Replace(".", ","), style, culture, out valDbl);
// converto in plantLogModel...
PlantLogModel newItem = new PlantLogModel()
{
@@ -245,7 +247,7 @@ namespace GWMS.UI.Controllers
List<PlantLogModel> newData = new List<PlantLogModel>();
newData.Add(newItem);
// insert!
fatto = _DataService.PlantLogInsert(newData);
fatto = _DataService.PlantLogInsert(newData).Result;
}
}
catch (Exception exc)
@@ -283,7 +285,7 @@ namespace GWMS.UI.Controllers
// conversione dati
List<PlantLogModel> plData = rawData.fluxData.Select(jpl => _DataService.convertFluxToPL(currPlant.PlantId, jpl)).ToList();
//insert!
fatto = _DataService.PlantLogInsert(plData);
fatto = _DataService.PlantLogInsert(plData).Result;
}
}
if (fatto)
+1 -1
View File
@@ -76,7 +76,7 @@ namespace GWMS.UI.Controllers
// verifico ci sia valore
if (newItems != null)
{
fatto = _DataService.PlantLogInsert(newItems);
fatto = _DataService.PlantLogInsert(newItems).Result;
}
if (fatto)
{
+134 -4
View File
@@ -15,6 +15,7 @@ using NLog;
using GWMS.Data.DTO;
using GWMS.Data.DatabaseModels;
using static GWMS.Data.IobObjects;
using System.Globalization;
namespace GWMS.UI.Data
{
@@ -89,6 +90,88 @@ namespace GWMS.UI.Data
return new DistributedCacheEntryOptions().SetAbsoluteExpiration(DateTime.Now.AddSeconds(numSecAbsExp)).SetSlidingExpiration(TimeSpan.FromSeconds(numSecSliExp));
}
/// <summary>
/// Recupera PlantDTO e aggiorno valori attuali (se presente...)
/// ATTENZIONE: i dati sono sempre ricevuti PER SINGOLO PlantId!!!
/// </summary>
/// <param name="newItems"></param>
private async Task updateCurrDTO(List<PlantLogModel> newItems)
{
List<PlantDTO> dbResult = new List<PlantDTO>();
int PlantId = newItems.FirstOrDefault().PlantId;
string cacheKey = "DATA:PLANTS:ListDTO";
string rawData;
var redisDataList = await distributedCache.GetAsync(cacheKey);
if (redisDataList != null)
{
rawData = Encoding.UTF8.GetString(redisDataList);
dbResult = JsonConvert.DeserializeObject<List<PlantDTO>>(rawData);
// ora ciclo x ogni flusso/macchina l'ultimo record
List<PlantLogModel> lastByFlux = newItems
.OrderByDescending(x => x.DtEvent)
.GroupBy(g => g.FluxType)
.Select(s => s.First())
.ToList();
// aggiorno il DTO x i valori trovati...
var currDto = dbResult.Where(x => x.PlantId == PlantId).FirstOrDefault();
// verifico SE c'è Level
var lastLev = lastByFlux.Where(x => x.FluxType == "Level").FirstOrDefault();
if (lastLev != null)
{
currDto.LevelAct = lastLev.ValNumber;
}
// verifico SE c'è MainPress
var lastMain = lastByFlux.Where(x => x.FluxType == "MainPress").FirstOrDefault();
if (lastMain != null)
{
if (currDto.PressAct.ContainsKey("Main"))
{
currDto.PressAct["Main"] = lastMain.ValNumber;
}
else
{
currDto.PressAct.Add("Main", lastMain.ValNumber);
}
}
// verifico SE c'è PressBH
var lastBH = lastByFlux.Where(x => x.FluxType == "PressBH").FirstOrDefault();
if (lastBH != null)
{
if (currDto.PressAct.ContainsKey("BH"))
{
currDto.PressAct["BH"] = lastBH.ValNumber;
}
else
{
currDto.PressAct.Add("BH", lastBH.ValNumber);
}
}
// verifico SE c'è PressBL
var lastBL = lastByFlux.Where(x => x.FluxType == "PressBL").FirstOrDefault();
if (lastBL != null)
{
if (currDto.PressAct.ContainsKey("BL"))
{
currDto.PressAct["BL"] = lastBL.ValNumber;
}
else
{
currDto.PressAct.Add("BL", lastBL.ValNumber);
}
}
// salvo DTO!
rawData = JsonConvert.SerializeObject(dbResult);
redisDataList = Encoding.UTF8.GetBytes(rawData);
await distributedCache.SetAsync(cacheKey, redisDataList, cacheOpt(true));
}
}
#endregion Private Methods
#region Protected Methods
@@ -154,7 +237,9 @@ namespace GWMS.UI.Data
{
// cerco di ottenere un val in cifre
double valDbl = 0;
double.TryParse(origData.valore, out valDbl);
NumberStyles style = NumberStyles.Number;
CultureInfo culture = CultureInfo.CreateSpecificCulture("it-IT");
double.TryParse(origData.valore.Replace(".", ","), style, culture, out valDbl);
TimeSpan delta = origData.dtCurr.Subtract(origData.dtEve);
PlantLogModel answ = new PlantLogModel()
{
@@ -279,9 +364,52 @@ namespace GWMS.UI.Data
return await Task.FromResult(dbResult);
}
public bool PlantLogInsert(List<PlantLogModel> newItems)
public async Task<bool> PlantLogInsert(List<PlantLogModel> newItems)
{
return dbController.PlantLogInsertNew(newItems);
// aggiorno valori ACT x DTO
await updateCurrDTO(newItems);
// inserimento su DB
int IntervalMin = 60;
int.TryParse(_configuration["IntervalMin"], out IntervalMin);
List<PlantLogModel> lastValues = dbController.PlantLogGetLastByFlux();
List<PlantLogModel> item2insert = new List<PlantLogModel>();
// verifico i flussi presenti tra quelli ricevuti
List<string> fluxList = newItems
.GroupBy(g => g.FluxType)
.Select(s => s.First().FluxType)
.ToList();
foreach (var item in fluxList)
{
// cerco se c'è valore...
var lastInserted = lastValues.Where(x => x.FluxType == item).FirstOrDefault();
DateTime dateLimit = DateTime.Today.AddDays(-1);
if (lastInserted != null)
{
// per ogni flusso calcolo il valore minimo x inserimento (arrotondando a minInt)
dateLimit = dbController.DateRoundEnd(lastInserted.DtEvent, IntervalMin);
}
// cerco se ho record > valore minimo x ogni flusso ricevuto
List<PlantLogModel> insCandidates = newItems.Where(x => x.FluxType == item && x.DtEvent >= dateLimit).ToList();
while (insCandidates.Count > 0)
{
var newRec = insCandidates.First();
// il primo lo accodo da inserire
item2insert.Add(newRec);
// calcolo nuovo veto
dateLimit = dbController.DateRoundEnd(newRec.DtEvent, IntervalMin);
// ...e se ho record accodo
insCandidates = newItems.Where(x => x.FluxType == item && x.DtEvent >= dateLimit).ToList();
}
}
// faccio vero insert
return dbController.PlantLogInsertNew(item2insert);
//return dbController.PlantLogInsertNew(newItems);
}
public async Task<List<PlantDTO>> PlantsGetAll()
@@ -299,7 +427,9 @@ namespace GWMS.UI.Data
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
dbResult = dbController.GetPlantsDTO();
int maxRec = 100;
int.TryParse(_configuration["MaxLogRecord"], out maxRec);
dbResult = dbController.GetPlantsDTO(maxRec);
rawData = JsonConvert.SerializeObject(dbResult);
redisDataList = Encoding.UTF8.GetBytes(rawData);
await distributedCache.SetAsync(cacheKey, redisDataList, cacheOpt(true));
+1 -1
View File
@@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<Version>1.0.2108.0413</Version>
<Version>1.0.2108.0512</Version>
<UserSecretsId>95c9f021-52d1-4390-a670-5810b7b777b0</UserSecretsId>
</PropertyGroup>
+2
View File
@@ -24,6 +24,8 @@
"nKey": "PZZFRR",
"sKey": "M3T@n0"
},
"IntervalMin": 60,
"MaxLogRecord": 240,
"ZCodeUrl": "https://qrcode.steamware.net/",
"logo": "img/LogoPizzaferri.jpg"
}
+1 -1
View File
@@ -1,6 +1,6 @@
<body>
<i>GWMS - Gas Warehouse Management System</i>
<h4>Versione: 1.0.2108.0413</h4>
<h4>Versione: 1.0.2108.0512</h4>
<br /> Note di rilascio:
<ul>
<li>
+1 -1
View File
@@ -1 +1 @@
1.0.2108.0413
1.0.2108.0512
+1 -1
View File
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<item>
<version>1.0.2108.0413</version>
<version>1.0.2108.0512</version>
<url>http://nexus.steamware.net/repository/SWS/GWMS/stable/0/GWMS.UI.zip</url>
<changelog>http://nexus.steamware.net/repository/SWS/GWMS/stable/0/ChangeLog.html</changelog>
<mandatory>false</mandatory>