Update saveDataItem: reinserito metodo mongoDB
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
using MP.Core.Objects;
|
||||
|
||||
namespace MP.Core.DTO
|
||||
{
|
||||
public class MtcSetupDto
|
||||
{
|
||||
#region Public Properties
|
||||
|
||||
public List<MachDataItem> DataItems { get; set; } = new();
|
||||
public string IdxMacchina { get; set; } = "";
|
||||
|
||||
#endregion Public Properties
|
||||
}
|
||||
}
|
||||
@@ -1,18 +1,16 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using MongoDB.Driver;
|
||||
using MP.Core.Conf;
|
||||
using MP.Core.DTO;
|
||||
using MP.Core.Objects;
|
||||
using MP.Data.MgModels;
|
||||
using Newtonsoft.Json;
|
||||
using NLog;
|
||||
using NLog.Fluent;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using MongoDB.Driver;
|
||||
using MongoDB.Bson;
|
||||
using MP.Data.MgModels;
|
||||
using System.IO;
|
||||
using MP.Core.Conf;
|
||||
using Newtonsoft.Json;
|
||||
using static MP.Data.MgModels.RecipeModel;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MP.Data.Controllers
|
||||
{
|
||||
@@ -112,6 +110,34 @@ namespace MP.Data.Controllers
|
||||
return answ;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Salva i dataItems della macchina indicata in MongoDB
|
||||
/// </summary>
|
||||
/// <param name="idxMacchina"></param>
|
||||
/// <param name="dataItems"></param>
|
||||
/// <returns></returns>
|
||||
public bool SaveMachineDataItems(string idxMacchina, List<MachDataItem> dataItems)
|
||||
{
|
||||
bool answ = false;
|
||||
var collMtcSetup = mongoDb.GetCollection<MtcSetupDto>("MtcSetup");
|
||||
// compongo filtro ricerca e metodo update
|
||||
var filter = Builders<MtcSetupDto>.Filter.Eq(u => u.IdxMacchina, idxMacchina);
|
||||
// chiamo update: cerco riga, se c'è aggiorno sennò creo
|
||||
MtcSetupDto newDoc = new MtcSetupDto()
|
||||
{
|
||||
IdxMacchina = idxMacchina,
|
||||
DataItems = dataItems
|
||||
};
|
||||
// elimino se ci fosse già...
|
||||
collMtcSetup.DeleteMany(filter);
|
||||
// inserisco ex novo!
|
||||
collMtcSetup.InsertOne(newDoc);
|
||||
answ = true;
|
||||
return answ;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Init ricetta dato PODL + conf
|
||||
/// </summary>
|
||||
|
||||
@@ -3,6 +3,7 @@ using MP.Data.DbModels;
|
||||
using Newtonsoft.Json;
|
||||
using NLog;
|
||||
using StackExchange.Redis;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Diagnostics;
|
||||
@@ -283,12 +284,82 @@ namespace MP.Data.Services
|
||||
#region Private Methods
|
||||
|
||||
/// <summary>
|
||||
/// Esegue flush memoria _redisConn dato pat2Flush
|
||||
/// Esegue flush memoria _redisConn dato pattern
|
||||
/// </summary>
|
||||
/// <param name="pat2Flush"></param>
|
||||
/// <param name="pattern"></param>
|
||||
/// <returns></returns>
|
||||
private async Task<bool> ExecFlushRedisPattern(RedisValue pat2Flush)
|
||||
private async Task<bool> ExecFlushRedisPattern(RedisValue pattern)
|
||||
{
|
||||
Log.Debug($"Richiesta flush pattern: {pattern}");
|
||||
|
||||
// 1. Target ONLY master (le replica sono in read-only)
|
||||
var master = _redisConn.GetEndPoints()
|
||||
.Where(ep => _redisConn.GetServer(ep).IsConnected && !_redisConn.GetServer(ep).IsReplica)
|
||||
.FirstOrDefault();
|
||||
|
||||
if (master == null)
|
||||
{
|
||||
Log.Warn($"Nessun master Redis raggiungibile per il pattern {pattern}");
|
||||
return false;
|
||||
}
|
||||
|
||||
// 2. Flush intero DB se richiesto
|
||||
if (pattern.ToString() == "*")
|
||||
{
|
||||
Log.Debug($"Full DB reset da pattern {pattern}");
|
||||
if (master != null)
|
||||
{
|
||||
_redisConn.GetServer(master).FlushDatabase(_redisDb.Database);
|
||||
Log.Info($"Flush database {_redisDb.Database} completato");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
// altrimenti faccio ciclo!
|
||||
var server = _redisConn.GetServer(master);
|
||||
var db = _redisConn.GetDatabase(_redisDb.Database);
|
||||
const int batchSize = 500;
|
||||
var batch = new List<RedisKey>(batchSize);
|
||||
int deletedCount = 0;
|
||||
|
||||
try
|
||||
{
|
||||
// KeysAsync usa SCAN automaticamente quando i risultati sono grandi
|
||||
await foreach (var key in server.KeysAsync(
|
||||
database: _redisDb.Database,
|
||||
pattern: pattern.ToString(),
|
||||
pageSize: batchSize))
|
||||
{
|
||||
batch.Add(key);
|
||||
if (batch.Count >= batchSize)
|
||||
{
|
||||
// Esecuzione batch in parallelo controllato
|
||||
await Task.WhenAll(batch.Select(k => db.KeyDeleteAsync(k)));
|
||||
batch.Clear();
|
||||
deletedCount += batchSize;
|
||||
}
|
||||
}
|
||||
|
||||
// Restanti
|
||||
if (batch.Count > 0)
|
||||
{
|
||||
await Task.WhenAll(batch.Select(k => db.KeyDeleteAsync(k)));
|
||||
deletedCount += batch.Count;
|
||||
}
|
||||
|
||||
Log.Info("Flush pattern {Pattern}: eliminate {Count} chiavi", pattern, deletedCount);
|
||||
return true;
|
||||
}
|
||||
catch (RedisConnectionException ex)
|
||||
{
|
||||
Log.Error(ex, "Connessione Redis persa durante il flush di {pattern}", pattern);
|
||||
return false;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex, "Errore imprevisto nel flush pattern {pattern}", pattern);
|
||||
throw;
|
||||
}
|
||||
#if false
|
||||
bool answ = false;
|
||||
var masterEndpoint = _redisConn.GetEndPoints()
|
||||
.Where(ep => _redisConn.GetServer(ep).IsConnected && !_redisConn.GetServer(ep).IsReplica)
|
||||
@@ -320,7 +391,8 @@ namespace MP.Data.Services
|
||||
}
|
||||
answ = true;
|
||||
}
|
||||
return answ;
|
||||
return answ;
|
||||
#endif
|
||||
}
|
||||
|
||||
#endregion Private Methods
|
||||
|
||||
@@ -1153,8 +1153,9 @@ namespace MP.IOC.Controllers
|
||||
string answ = "NO";
|
||||
try
|
||||
{
|
||||
// recupero IP del client remoto
|
||||
var agent = Request.Headers["User-Agent"].ToString();
|
||||
// recupero IP del client remoto, se vuoto IOC
|
||||
string agent = Request.Headers["User-Agent"].ToString() ?? "IOC";
|
||||
agent = string.IsNullOrWhiteSpace(agent) ? "IOC" : agent;
|
||||
//var ipv4 = HttpContext.Connection.RemoteIpAddress?.ToString();
|
||||
var ipv4 = HttpContext.Connection.RemoteIpAddress?.MapToIPv4().ToString();
|
||||
|
||||
|
||||
@@ -3350,27 +3350,6 @@ namespace MP.IOC.Data
|
||||
/// </summary>
|
||||
/// <param name="pattern"></param>
|
||||
/// <returns></returns>
|
||||
#if false
|
||||
public async Task<bool> RedisFlushPatternAsync(RedisValue pattern)
|
||||
{
|
||||
bool answ = false;
|
||||
var listEndpoints = redisConnAdmin.GetEndPoints();
|
||||
foreach (var endPoint in listEndpoints)
|
||||
{
|
||||
var server = redisConnAdmin.GetServer(endPoint);
|
||||
if (server != null)
|
||||
{
|
||||
var keyList = server.Keys(redisDb.Database, pattern);
|
||||
foreach (var item in keyList)
|
||||
{
|
||||
await redisDb.KeyDeleteAsync(item);
|
||||
}
|
||||
answ = true;
|
||||
}
|
||||
}
|
||||
return answ;
|
||||
}
|
||||
#endif
|
||||
public async Task<bool> RedisFlushPatternAsync(RedisValue pattern)
|
||||
{
|
||||
Log.Debug($"Richiesta flush pattern: {pattern}");
|
||||
@@ -3536,28 +3515,16 @@ namespace MP.IOC.Data
|
||||
/// <returns></returns>
|
||||
public async Task<bool> RemRebootLogAddAsync(RemoteRebootLogModel newRec)
|
||||
{
|
||||
bool fatto = false;
|
||||
#if false
|
||||
// insert del record
|
||||
fatto = await IocDbController.RemRebootLogAddAsync(newRec);
|
||||
// pulizia record vecchi
|
||||
int num2keep = 5;
|
||||
string confVal = await tryGetConfig("IO_NumReboot2Keep");
|
||||
if (!string.IsNullOrEmpty(confVal))
|
||||
{
|
||||
int.TryParse(confVal, out num2keep);
|
||||
}
|
||||
fatto = await IocDbController.RemRebootLogKeepLastAsync(num2keep);
|
||||
#endif
|
||||
// insert del record + pulizia
|
||||
|
||||
string confVal = await tryGetConfig("IO_NumReboot2Keep");
|
||||
int num2keep = int.TryParse(confVal, out int n) ? n : 5;
|
||||
fatto = await IocDbController.RemRebootLogAddAndCleanAsync(newRec, num2keep);
|
||||
|
||||
// svuota cache
|
||||
var currKey = $"{Utils.redisRemRebLog}:*";
|
||||
await RedisFlushPatternAsync(currKey);
|
||||
// insert del record + pulizia
|
||||
bool fatto = await IocDbController.RemRebootLogAddAndCleanAsync(newRec, num2keep);
|
||||
if (fatto)
|
||||
{
|
||||
// svuota cache
|
||||
var currKey = $"{Utils.redisRemRebLog}:*";
|
||||
await RedisFlushPatternAsync(currKey);
|
||||
}
|
||||
return fatto;
|
||||
}
|
||||
|
||||
@@ -3865,16 +3832,23 @@ namespace MP.IOC.Data
|
||||
public async Task<bool> SaveDataItemsAsync(string id, List<MachDataItem> dataList)
|
||||
{
|
||||
bool answ = false;
|
||||
|
||||
if (useFactory)
|
||||
if (mongoController != null)
|
||||
{
|
||||
await using var scope = _scopeFactory.CreateAsyncScope();
|
||||
var mtcService = scope.ServiceProvider.GetRequiredService<IMtcSetupService>();
|
||||
answ = await mtcService.ReplaceMachineDataAsync(id, dataList);
|
||||
answ = mongoController.SaveMachineDataItems(id, dataList);
|
||||
}
|
||||
else
|
||||
{
|
||||
answ = await MtcService.ReplaceMachineDataAsync(id, dataList);
|
||||
// modalità con SqlSb (no mongo)
|
||||
if (useFactory)
|
||||
{
|
||||
await using var scope = _scopeFactory.CreateAsyncScope();
|
||||
var mtcService = scope.ServiceProvider.GetRequiredService<IMtcSetupService>();
|
||||
answ = await mtcService.ReplaceMachineDataAsync(id, dataList);
|
||||
}
|
||||
else
|
||||
{
|
||||
answ = await MtcService.ReplaceMachineDataAsync(id, dataList);
|
||||
}
|
||||
}
|
||||
|
||||
return answ;
|
||||
@@ -4944,28 +4918,5 @@ namespace MP.IOC.Data
|
||||
|
||||
#endregion Private Methods
|
||||
|
||||
#if false
|
||||
public bool RedisSetKey(string valKey, string redVal)
|
||||
{
|
||||
bool answ = redisDb.StringSet(Utils.RedKeyHash(valKey), redVal);
|
||||
return answ;
|
||||
}
|
||||
public async Task<bool> RedisSetKeyAsync(string valKey, string redVal)
|
||||
{
|
||||
bool answ = await redisDb.StringSetAsync(Utils.RedKeyHash(valKey), redVal);
|
||||
return answ;
|
||||
}
|
||||
|
||||
public bool RedisSetKey(RedisKey valKey, string redVal, int TTL_sec)
|
||||
{
|
||||
bool answ = redisDb.StringSet(Utils.RedKeyHash(valKey), redVal, TimeSpan.FromSeconds(TTL_sec));
|
||||
return answ;
|
||||
}
|
||||
public async Task<bool> RedisSetKeyAsync(RedisKey valKey, string redVal, int TTL_sec)
|
||||
{
|
||||
bool answ = await redisDb.StringSetAsync(Utils.RedKeyHash(valKey), redVal, TimeSpan.FromSeconds(TTL_sec));
|
||||
return answ;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Version>6.16.2604.2711</Version>
|
||||
<Version>6.16.2604.2714</Version>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<body>
|
||||
<i>Modulo MP-IOC </i>
|
||||
<h4>Versione: 6.16.2604.2711</h4>
|
||||
<h4>Versione: 6.16.2604.2714</h4>
|
||||
<br /> Note di rilascio:
|
||||
<ul>
|
||||
<li>
|
||||
|
||||
@@ -1 +1 @@
|
||||
6.16.2604.2711
|
||||
6.16.2604.2714
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<item>
|
||||
<version>6.16.2604.2711</version>
|
||||
<version>6.16.2604.2714</version>
|
||||
<url>https://nexus.steamware.net/repository/SWS/MP-IOC/stable/LAST/MP.IOC.zip</url>
|
||||
<changelog>https://nexus.steamware.net/repository/SWS/MP-IOC/stable/LAST/ChangeLog.html</changelog>
|
||||
<mandatory>false</mandatory>
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<RootNamespace>MP.SPEC</RootNamespace>
|
||||
<Version>6.16.2604.712</Version>
|
||||
<Version>6.16.2604.2711</Version>
|
||||
<UserSecretsId>1800a78a-6ff1-40f9-b490-87fb8bfc1394</UserSecretsId>
|
||||
<SatelliteResourceLanguages>en</SatelliteResourceLanguages>
|
||||
</PropertyGroup>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<body>
|
||||
<i>Modulo MAPOSPEC </i>
|
||||
<h4>Versione: 6.16.2604.712</h4>
|
||||
<h4>Versione: 6.16.2604.2711</h4>
|
||||
<br /> Note di rilascio:
|
||||
<ul>
|
||||
<li>
|
||||
|
||||
@@ -1 +1 @@
|
||||
6.16.2604.712
|
||||
6.16.2604.2711
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<item>
|
||||
<version>6.16.2604.712</version>
|
||||
<version>6.16.2604.2711</version>
|
||||
<url>https://nexus.steamware.net/repository/SWS/MP-SPEC/stable/LAST/MP.SPEC.zip</url>
|
||||
<changelog>https://nexus.steamware.net/repository/SWS/MP-SPEC/stable/LAST/ChangeLog.html</changelog>
|
||||
<mandatory>false</mandatory>
|
||||
|
||||
Reference in New Issue
Block a user