217836099c
- fix program.cs startup - fix calcolo dim DB - fix IOB count
243 lines
8.6 KiB
C#
243 lines
8.6 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;
|
|
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
|
|
|
|
/// <summary>
|
|
/// Restituisce info dimensione, tabelle e num righe DB
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<DbSizeModel>> AllDbInfoAsync()
|
|
{
|
|
// 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 = await _redisDb.StringGetAsync(currKey);
|
|
if (rawData.HasValue)
|
|
{
|
|
result = JsonConvert.DeserializeObject<List<DbSizeModel>>($"{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<DbSizeModel>();
|
|
}
|
|
sw.Stop();
|
|
Log.Debug($"AllDbInfoAsync | {source} | {sw.Elapsed.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco INFO IOB da tab Macchine gestite + RemoteRebootLog
|
|
/// </summary>
|
|
/// <param name="UserName"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<IobDTO>> IobListAllAsync()
|
|
{
|
|
string source = "DB";
|
|
List<IobDTO>? dbResult = new List<IobDTO>();
|
|
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<List<IobDTO>>(rawData);
|
|
dbResult = tempResult ?? new List<IobDTO>();
|
|
}
|
|
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<IobDTO>();
|
|
}
|
|
sw.Stop();
|
|
Log.Debug($"IobListAllAsync | {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 = _mpLandRepository.RemRebootLogGetAllAsync().GetAwaiter().GetResult();
|
|
// 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 = _mpLandRepository.RemRebootLogGetLastAsync().GetAwaiter().GetResult();
|
|
// 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
|
|
}
|
|
|
|
// 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
|
|
}
|
|
} |