using Microsoft.Extensions.Configuration; using MP.AppAuth.Controllers; using MP.Core.DTO; using MP.Data.Controllers; using MP.Data.DbModels; using MP.Data.DTO; using Newtonsoft.Json; using NLog; using StackExchange.Redis; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; using ZiggyCreatures.Caching.Fusion; namespace MP.Data.Services { public class LandDataService : BaseServ { #region Public Constructors public LandDataService( IConfiguration configuration, IConnectionMultiplexer redConn, IFusionCache cache, Repository.MpLand.IMpLandRepository mpLandRepository ) : base(configuration, cache, redConn) { _mpLandRepository = mpLandRepository; // conf DB string connStr = _configuration.GetConnectionString("MP.Land"); if (string.IsNullOrEmpty(connStr)) { Log.Error("ConnString empty!"); } else { StringBuilder sb = new StringBuilder(); sb.AppendLine($"LandService | MpLandRepository OK"); Log.Info(sb.ToString()); } } #endregion Public Constructors #region Public Methods /// /// Restituisce info dimensione, tabelle e num righe DB /// /// public async Task> AllDbInfoAsync() { // setup parametri costanti string source = "DB"; Stopwatch sw = new Stopwatch(); sw.Start(); List result = new List(); // cerco in _redisConn... string currKey = $"{redisBaseKey}:DbInfo:ALL"; RedisValue rawData = await _redisDb.StringGetAsync(currKey); if (rawData.HasValue) { result = JsonConvert.DeserializeObject>($"{rawData}"); source = "REDIS"; } else { result = await _mpLandRepository.AllDbInfoAsync(); // serializzo e salvo... rawData = JsonConvert.SerializeObject(result); await _redisDb.StringSetAsync(currKey, rawData, UltraFastCache); } if (result == null) { result = new List(); } sw.Stop(); Log.Debug($"AllDbInfoAsync | {source} | {sw.Elapsed.TotalMilliseconds}ms"); return result; } /// /// Elenco INFO IOB da tab Macchine gestite + RemoteRebootLog /// /// /// public async Task> IobListAllAsync() { string source = "DB"; List? dbResult = new List(); string currKey = $"{redisBaseKey}:IobList"; Stopwatch sw = new Stopwatch(); sw.Start(); var rawData = await _redisDb.StringGetAsync(currKey); if (rawData.HasValue) { source = "REDIS"; var tempResult = JsonConvert.DeserializeObject>(rawData); dbResult = tempResult ?? new List(); } else { // recupero RRL missing var listRRl = await _mpLandRepository.RemRebootLogGetLastAsync(); var listRRlAdd = await _mpLandRepository.RemRebootLogGetLastNoMaccAsync(); // recupero lista macchine var ListMacch = await _mpLandRepository.MacchineGetAllAsync(); // ...converto in DTO dbResult = ListMacch .Select(x => new IobDTO(x, IobInfo(x.IdxMacchina), MachIobConf(x.IdxMacchina))) .ToList(); // completo con RRL i mac address... // recupero record RemoteRebootLog mancanti e accodo var listExtra = listRRlAdd.Select(x => new IobDTO(x, IobInfo(x.IdxMacchina), MachIobConf(x.IdxMacchina))) .ToList(); dbResult.AddRange(listExtra); //ordino x idxmacchina... dbResult = dbResult.OrderBy(x => x.IdxMacchina).ToList(); // serializzo in cache _redisConn rawData = JsonConvert.SerializeObject(dbResult, JSSettings); await _redisDb.StringSetAsync(currKey, rawData, UltraLongCache); } if (dbResult == null) { dbResult = new List(); } sw.Stop(); Log.Debug($"IobListAllAsync | {source} | {sw.ElapsedMilliseconds} ms"); return dbResult; } /// /// Recupera tutti i record di RemoteRebootLog /// /// public List RemRebootLogGetAll() { // setup parametri costanti string source = "DB"; Stopwatch sw = new Stopwatch(); sw.Start(); List result = new List(); // cerco in _redisConn... string currKey = $"{redisBaseKey}:RemRebLog:ALL"; RedisValue rawData = _redisDb.StringGet(currKey); //if (!string.IsNullOrEmpty($"{rawData}")) if (rawData.HasValue) { result = JsonConvert.DeserializeObject>($"{rawData}"); source = "REDIS"; } else { result = _mpLandRepository.RemRebootLogGetAllAsync().GetAwaiter().GetResult(); // serializzo e salvo... rawData = JsonConvert.SerializeObject(result); _redisDb.StringSet(currKey, rawData, UltraFastCache); } if (result == null) { result = new List(); } sw.Stop(); Log.Debug($"RemRebootLogGetAll | {source} | {sw.Elapsed.TotalMilliseconds}ms"); return result; } /// /// Recupera ultimo record x ogni IdxMacchina x avere ultimo attivo /// /// public List RemRebootLogGetLast() { // setup parametri costanti string source = "DB"; Stopwatch sw = new Stopwatch(); sw.Start(); List result = new List(); // cerco in _redisConn... string currKey = $"{redisBaseKey}:RemRebLog:LAST"; RedisValue rawData = _redisDb.StringGet(currKey); //if (!string.IsNullOrEmpty($"{rawData}")) if (rawData.HasValue) { result = JsonConvert.DeserializeObject>($"{rawData}"); source = "REDIS"; } else { result = _mpLandRepository.RemRebootLogGetLastAsync().GetAwaiter().GetResult(); // serializzo e salvo... rawData = JsonConvert.SerializeObject(result); _redisDb.StringSet(currKey, rawData, UltraFastCache); } if (result == null) { result = new List(); } sw.Stop(); Log.Debug($"RemRebootLogGetLast | {source} | {sw.Elapsed.TotalMilliseconds}ms"); return result; } #endregion Public Methods #region Protected Methods protected override void Dispose(bool disposing) { if (!_disposed) { if (disposing) { // Free managed resources here } // Free unmanaged resources here _disposed = true; } // Call base class implementation. base.Dispose(disposing); } #endregion Protected Methods #region Private Fields private readonly Repository.MpLand.IMpLandRepository _mpLandRepository; private static Logger Log = LogManager.GetCurrentClassLogger(); private bool _disposed = false; private string redisBaseKey = "MP:LAND:Cache"; #endregion Private Fields } }