diff --git a/MP.Data/Controllers/MpStatsController.cs b/MP.Data/Controllers/MpStatsController.cs index 853d1c02..4a5b58ff 100644 --- a/MP.Data/Controllers/MpStatsController.cs +++ b/MP.Data/Controllers/MpStatsController.cs @@ -7,7 +7,10 @@ using System.Configuration; using NLog; using Microsoft.Extensions.Configuration; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Caching.Distributed; +using Microsoft.Extensions.Caching.Memory; using Microsoft.Data.SqlClient; +using Newtonsoft.Json; namespace MP.Data.Controllers { @@ -18,14 +21,18 @@ namespace MP.Data.Controllers private static IConfiguration _configuration; private static MoonPro_STATSContext dbCtx; private static NLog.Logger Log = LogManager.GetCurrentClassLogger(); + private readonly IDistributedCache distributedCache; + private readonly IMemoryCache memoryCache; #endregion Private Fields #region Public Constructors - public MpStatsController(IConfiguration configuration) + public MpStatsController(IConfiguration configuration, IMemoryCache memoryCache, IDistributedCache distributedCache) { _configuration = configuration; + this.memoryCache = memoryCache; + this.distributedCache = distributedCache; dbCtx = new MoonPro_STATSContext(configuration); Log.Info("Avviata classe MpStatsController.MpStatsController"); } @@ -228,21 +235,39 @@ namespace MP.Data.Controllers /// /// /// - public List StatTurniOeeGetAll(DateTime DataStart, DateTime DataEnd, string IdxMacchina, int IdxODL, string KeyRichiesta, string CodArticolo) + public async Task> StatTurniOeeGetAll(DateTime DataStart, DateTime DataEnd, string IdxMacchina, int IdxODL, string KeyRichiesta, string CodArticolo) { List dbResult = new List(); - var dataFrom = new SqlParameter("@dataFrom", DataStart); - var dataTo = new SqlParameter("@dataTo", DataEnd); - var idxMacchina = new SqlParameter("@idxMacchina", IdxMacchina); - var idxODL = new SqlParameter("@IdxODL", IdxODL); - var keyRichiesta = new SqlParameter("@KeyRichiesta", KeyRichiesta); - var codArticolo = new SqlParameter("@CodArticolo", CodArticolo); + var cacheKey = "oeeData"; + string rawData; + var redisCustomerList = await distributedCache.GetAsync(cacheKey); + if (redisCustomerList != null) + { + rawData = Encoding.UTF8.GetString(redisCustomerList); + dbResult = JsonConvert.DeserializeObject>(rawData); + } + else + { + var dataFrom = new SqlParameter("@dataFrom", DataStart); + var dataTo = new SqlParameter("@dataTo", DataEnd); + var idxMacchina = new SqlParameter("@idxMacchina", IdxMacchina); + var idxODL = new SqlParameter("@IdxODL", IdxODL); + var keyRichiesta = new SqlParameter("@KeyRichiesta", KeyRichiesta); + var codArticolo = new SqlParameter("@CodArticolo", CodArticolo); - dbResult = dbCtx - .DbSetTurniOee - .FromSqlRaw("EXEC stp_UI_TurniOee_GetByFilter @dataFrom,@dataTo,@idxMacchina,@IdxODL,@KeyRichiesta,@CodArticolo", dataFrom, dataTo, idxMacchina, idxODL, keyRichiesta, codArticolo) - .ToList(); + dbResult = dbCtx + .DbSetTurniOee + .FromSqlRaw("EXEC stp_UI_TurniOee_GetByFilter @dataFrom,@dataTo,@idxMacchina,@IdxODL,@KeyRichiesta,@CodArticolo", dataFrom, dataTo, idxMacchina, idxODL, keyRichiesta, codArticolo) + .ToList(); + + rawData = JsonConvert.SerializeObject(dbResult); + redisCustomerList = Encoding.UTF8.GetBytes(rawData); + var options = new DistributedCacheEntryOptions() + .SetAbsoluteExpiration(DateTime.Now.AddMinutes(10)) + .SetSlidingExpiration(TimeSpan.FromMinutes(2)); + await distributedCache.SetAsync(cacheKey, redisCustomerList, options); + } return dbResult; } diff --git a/MP.Stats/Data/MpStatsService.cs b/MP.Stats/Data/MpStatsService.cs index e2054459..16ee0e52 100644 --- a/MP.Stats/Data/MpStatsService.cs +++ b/MP.Stats/Data/MpStatsService.cs @@ -54,7 +54,7 @@ namespace MP.Stats.Data } else { - dbController = new MP.Data.Controllers.MpStatsController(configuration); + dbController = new MP.Data.Controllers.MpStatsController(configuration, memoryCache, distributedCache); StringBuilder sb = new StringBuilder(); sb.AppendLine($"DbController OK"); //sb.AppendLine($"CST: {dbController.CustomersCount()} | CNT: {dbController.CountersCount()} | BSK: {dbController.BasketsCount()} | NGT: {dbController.NegotiationsCount()} | DOC: {dbController.DocsCount()} | ITM: {dbController.ItemsCount()} | RES: {dbController.ResourcesCount()}"); @@ -107,28 +107,9 @@ namespace MP.Stats.Data return Task.FromResult(dbController.StatScartiGetAll(DataStart, DataEnd, IdxMacchina, IdxODL, KeyRichiesta, CodArticolo).ToArray()); } - public async Task StatTurniOeeGetAll(DateTime DataStart, DateTime DataEnd, string IdxMacchina, int IdxODL, string KeyRichiesta, string CodArticolo, string searchVal = "") + public Task> StatTurniOeeGetAll(DateTime DataStart, DateTime DataEnd, string IdxMacchina, int IdxODL, string KeyRichiesta, string CodArticolo, string searchVal = "") { - List answ = new List(); - var cacheKey = "oeeData"; - string serializedCustomerList; - var redisCustomerList = await distributedCache.GetAsync(cacheKey); - if (redisCustomerList != null) - { - serializedCustomerList = Encoding.UTF8.GetString(redisCustomerList); - answ = JsonConvert.DeserializeObject>(serializedCustomerList); - } - else - { - answ = dbController.StatTurniOeeGetAll(DataStart, DataEnd, IdxMacchina, IdxODL, KeyRichiesta, CodArticolo); - serializedCustomerList = JsonConvert.SerializeObject(answ); - redisCustomerList = Encoding.UTF8.GetBytes(serializedCustomerList); - var options = new DistributedCacheEntryOptions() - .SetAbsoluteExpiration(DateTime.Now.AddMinutes(10)) - .SetSlidingExpiration(TimeSpan.FromMinutes(2)); - await distributedCache.SetAsync(cacheKey, redisCustomerList, options); - } - return answ.ToArray(); + return dbController.StatTurniOeeGetAll(DataStart, DataEnd, IdxMacchina, IdxODL, KeyRichiesta, CodArticolo); } public Task StatTurniParetoGetAll(DateTime DataStart, DateTime DataEnd, string IdxMacchina, int IdxODL, string KeyRichiesta, string CodArticolo, string searchVal = "") diff --git a/MP.Stats/Pages/Oee.razor b/MP.Stats/Pages/Oee.razor index a2e0beff..f31be4fb 100644 --- a/MP.Stats/Pages/Oee.razor +++ b/MP.Stats/Pages/Oee.razor @@ -16,7 +16,7 @@ { @**@ } - @if (ListRecords == null) + @if (ListRecords == null || totalCount == 0 || ListRecords.Count() == 0) {
} @@ -66,6 +66,6 @@ } \ No newline at end of file diff --git a/MP.Stats/Pages/Oee.razor.cs b/MP.Stats/Pages/Oee.razor.cs index be601c8e..37330e82 100644 --- a/MP.Stats/Pages/Oee.razor.cs +++ b/MP.Stats/Pages/Oee.razor.cs @@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Components; using Microsoft.JSInterop; using MP.Stats.Data; using System; +using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; @@ -15,7 +16,7 @@ namespace MP.Stats.Pages private MP.Data.DatabaseModels.TurniOee[] ListRecords; - private MP.Data.DatabaseModels.TurniOee[] SearchRecords; + private List SearchRecords; #endregion Private Fields @@ -41,6 +42,19 @@ namespace MP.Stats.Pages [Inject] protected MpStatsService StatService { get; set; } + protected int totalCount + { + get + { + int answ = 0; + if (SearchRecords != null) + { + answ = SearchRecords.Count; + } + return answ; + } + } + #endregion Protected Properties #region Private Methods