45cb6b9f59
- aggiunta migrations - correzioni versione ef6 da ef8 - correzioni init varie
243 lines
8.3 KiB
C#
243 lines
8.3 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.ComponentModel.DataAnnotations;
|
|
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, IConnectionMultiplexer redConn) : base(configuration, redConn)
|
|
{
|
|
// 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 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 _redisConn...
|
|
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 _redisConn
|
|
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 _redisConn...
|
|
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 _redisConn...
|
|
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 Protected Methods
|
|
|
|
protected override void Dispose(bool disposing)
|
|
{
|
|
if (!_disposed)
|
|
{
|
|
if (disposing)
|
|
{
|
|
// Free managed resources here
|
|
dbController.Dispose();
|
|
}
|
|
|
|
// Free unmanaged resources here
|
|
_disposed = true;
|
|
}
|
|
|
|
// Call base class implementation.
|
|
base.Dispose(disposing);
|
|
}
|
|
|
|
#endregion Protected Methods
|
|
|
|
#region Private Fields
|
|
|
|
private static Logger Log = LogManager.GetCurrentClassLogger();
|
|
private bool _disposed = false;
|
|
private string redisBaseKey = "MP:LAND:Cache";
|
|
|
|
#endregion Private Fields
|
|
}
|
|
} |