Files
mapo-core/MP.Data/Services/LandDataService.cs
T
2025-07-04 10:54:30 +02:00

219 lines
7.7 KiB
C#

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.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MP.Data.Services
{
public class LandDataService : BaseServ
{
#region Public Constructors
public LandDataService(IConfiguration configuration) : base(configuration)
{
// conf DB
string connStr = _configuration.GetConnectionString("MP.Land");
if (string.IsNullOrEmpty(connStr))
{
Log.Error("ConnString empty!");
}
else
{
dbController = new Controllers.MpLandController(configuration);
StringBuilder sb = new StringBuilder();
sb.AppendLine($"LandService | MpLandController OK");
Log.Info(sb.ToString());
}
}
#endregion Public Constructors
#region Public Properties
public static Controllers.MpLandController dbController { get; set; } = null!;
#endregion Public Properties
#region Public Methods
/// <summary>
/// Restituisce info dimensione, tabelle e num righe DB
/// </summary>
/// <returns></returns>
public List<DbSizeModel> AllDbInfo()
{
// setup parametri costanti
string source = "DB";
Stopwatch sw = new Stopwatch();
sw.Start();
List<DbSizeModel> result = new List<DbSizeModel>();
// cerco in redis...
string currKey = $"{redisBaseKey}:DbInfo:ALL";
RedisValue rawData = redisDb.StringGet(currKey);
if (rawData.HasValue)
{
result = JsonConvert.DeserializeObject<List<DbSizeModel>>($"{rawData}");
source = "REDIS";
}
else
{
result = dbController.AllDbInfo();
// serializzo e salvo...
rawData = JsonConvert.SerializeObject(result);
redisDb.StringSet(currKey, rawData, UltraFastCache);
}
if (result == null)
{
result = new List<DbSizeModel>();
}
sw.Stop();
Log.Debug($"AllDbInfo | {source} | {sw.Elapsed.TotalMilliseconds}ms");
return result;
}
/// <summary>
/// Elenco INFO IOB da tab Macchine gestite + RemoteRebootLog
/// </summary>
/// <param name="UserName"></param>
/// <returns></returns>
public List<IobDTO> IobListAll()
{
string source = "DB";
List<IobDTO>? dbResult = new List<IobDTO>();
string currKey = $"{redisBaseKey}:IobList";
Stopwatch sw = new Stopwatch();
sw.Start();
string? rawData = redisDb.StringGet(currKey);
if (!string.IsNullOrEmpty(rawData) && rawData.Length > 2)
{
source = "REDIS";
var tempResult = JsonConvert.DeserializeObject<List<IobDTO>>(rawData);
dbResult = tempResult ?? new List<IobDTO>();
}
else
{
// recupero RRL missing
var listRRl = dbController.RemRebootLogGetLast();
var listRRlAdd = dbController.RemRebootLogGetLastNoMacc();
// recupero lista macchine
var ListMacch = dbController.MacchineGetAll();
// ...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 redis
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
redisDb.StringSet(currKey, rawData, UltraLongCache);
}
if (dbResult == null)
{
dbResult = new List<IobDTO>();
}
sw.Stop();
Log.Debug($"IobListAll | {source} | {sw.ElapsedMilliseconds} ms");
return dbResult;
}
/// <summary>
/// Recupera tutti i record di RemoteRebootLog
/// </summary>
/// <returns></returns>
public List<RemoteRebootLogModel> RemRebootLogGetAll()
{
// setup parametri costanti
string source = "DB";
Stopwatch sw = new Stopwatch();
sw.Start();
List<RemoteRebootLogModel> result = new List<RemoteRebootLogModel>();
// cerco in redis...
string currKey = $"{redisBaseKey}:RemRebLog:ALL";
RedisValue rawData = redisDb.StringGet(currKey);
//if (!string.IsNullOrEmpty($"{rawData}"))
if (rawData.HasValue)
{
result = JsonConvert.DeserializeObject<List<RemoteRebootLogModel>>($"{rawData}");
source = "REDIS";
}
else
{
result = dbController.RemRebootLogGetAll();
// serializzo e salvo...
rawData = JsonConvert.SerializeObject(result);
redisDb.StringSet(currKey, rawData, UltraFastCache);
}
if (result == null)
{
result = new List<RemoteRebootLogModel>();
}
sw.Stop();
Log.Debug($"RemRebootLogGetAll | {source} | {sw.Elapsed.TotalMilliseconds}ms");
return result;
}
/// <summary>
/// Recupera ultimo record x ogni IdxMacchina x avere ultimo attivo
/// </summary>
/// <returns></returns>
public List<RemoteRebootLogModel> RemRebootLogGetLast()
{
// setup parametri costanti
string source = "DB";
Stopwatch sw = new Stopwatch();
sw.Start();
List<RemoteRebootLogModel> result = new List<RemoteRebootLogModel>();
// cerco in redis...
string currKey = $"{redisBaseKey}:RemRebLog:LAST";
RedisValue rawData = redisDb.StringGet(currKey);
//if (!string.IsNullOrEmpty($"{rawData}"))
if (rawData.HasValue)
{
result = JsonConvert.DeserializeObject<List<RemoteRebootLogModel>>($"{rawData}");
source = "REDIS";
}
else
{
result = dbController.RemRebootLogGetLast();
// serializzo e salvo...
rawData = JsonConvert.SerializeObject(result);
redisDb.StringSet(currKey, rawData, UltraFastCache);
}
if (result == null)
{
result = new List<RemoteRebootLogModel>();
}
sw.Stop();
Log.Debug($"RemRebootLogGetLast | {source} | {sw.Elapsed.TotalMilliseconds}ms");
return result;
}
#endregion Public Methods
#region Private Fields
private static Logger Log = LogManager.GetCurrentClassLogger();
private string redisBaseKey = "MP:LAND:Cache";
#endregion Private Fields
}
}