Bozza servizi REST richiesta calcolo PROD
This commit is contained in:
@@ -1,11 +1,10 @@
|
||||
using EgwCoreLib.Lux.Core.RestPayload;
|
||||
using EgwCoreLib.Lux.Data;
|
||||
using EgwCoreLib.Lux.Data.DbModel.Config;
|
||||
using EgwCoreLib.Lux.Data.Services;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using EgwMultiEngineManager.Data;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using Newtonsoft.Json;
|
||||
using NLog;
|
||||
using StackExchange.Redis;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Lux.API.Controllers
|
||||
@@ -16,11 +15,9 @@ namespace Lux.API.Controllers
|
||||
{
|
||||
#region Public Constructors
|
||||
|
||||
public ProdController(IConfiguration config, IRedisService redisService, ImageCacheService imgServ)
|
||||
public ProdController(ProdService prodService)
|
||||
{
|
||||
_config = config;
|
||||
_redisService = redisService;
|
||||
chPub = _config.GetValue<string>("ServerConf:ChannelPub") ?? "";
|
||||
PService = prodService;
|
||||
}
|
||||
|
||||
#endregion Public Constructors
|
||||
@@ -44,6 +41,155 @@ namespace Lux.API.Controllers
|
||||
return Ok("OK");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Chiamata GET:
|
||||
/// - fornisce il job da eseguire dalla coda (SE presente)
|
||||
/// GET: api/Prod/getjob/ABC012345
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet("getjob/{id}")]
|
||||
public async Task<ActionResult<string>> GetJob(string id)
|
||||
{
|
||||
var result = await PService.GetJob(id);
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Chiamata GET:
|
||||
/// - fornisce il primo job da eseguire dalla coda (SE presente)
|
||||
/// - viene registrato come "in corso" e spostato dalla coda richiesta
|
||||
/// GET: api/Prod/getnext
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet("getnext")]
|
||||
public async Task<ActionResult<string>> GetNext()
|
||||
{
|
||||
var result = await PService.GetNext();
|
||||
return Ok(result);
|
||||
#if false
|
||||
Stopwatch sw = new Stopwatch();
|
||||
sw.Start();
|
||||
string result = "";
|
||||
// prendo dalla coda primo job (rimuovendolo...)
|
||||
var rawReq = await _redisService.QueuePopAsync(queueKey);
|
||||
if (rawReq.HasValue)
|
||||
{
|
||||
string reqUid = $"{rawReq}";
|
||||
// metto UID in coda running
|
||||
_redisService.QueuePush(queueKey, (RedisValue)reqUid);
|
||||
// FixMe ToDo !!!: salvataggio data-ora per indicare avvio calcolo...
|
||||
|
||||
// recupero richiesta serializzata
|
||||
string currKey = $"{redisOrderReqKey}:{reqUid.Replace("/", ":")}";
|
||||
var rawRes = await _redisService.GetAsync(currKey);
|
||||
if (!string.IsNullOrEmpty(rawRes))
|
||||
{
|
||||
result = rawRes;
|
||||
}
|
||||
}
|
||||
sw.Stop();
|
||||
Log.Info($"GetNext | {sw.Elapsed.TotalMilliseconds:N3} ms");
|
||||
return Ok(result);
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Chiamata GET: num richieste in coda (tot)
|
||||
/// GET: api/Prod/alive
|
||||
/// </summary>
|
||||
/// <param name="id">id oggetto</param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("queue")]
|
||||
public async Task<IActionResult> QueueLen()
|
||||
{
|
||||
var result = await PService.QueueLen();
|
||||
return Ok(result);
|
||||
#if false
|
||||
Stopwatch sw = new Stopwatch();
|
||||
sw.Start();
|
||||
long numWaiting = await _redisService.QueueCountAsync(queueKey);
|
||||
sw.Stop();
|
||||
Log.Info($"QueueLen | {sw.Elapsed.TotalMilliseconds:N3} ms");
|
||||
return Ok(numWaiting);
|
||||
#endif
|
||||
}
|
||||
|
||||
#endregion Public Methods
|
||||
|
||||
#region Private Fields
|
||||
|
||||
private static Logger Log = LogManager.GetCurrentClassLogger();
|
||||
private ProdService PService;
|
||||
|
||||
#endregion Private Fields
|
||||
|
||||
#if false
|
||||
[HttpPost("enqueue")]
|
||||
/// <summary>
|
||||
/// Accodamento richiesta di calcolo prod
|
||||
/// </summary>
|
||||
/// <param name="reqType">Tipo richiesta</param>
|
||||
/// <param name="reqUid">UID (riga ordine)</param>
|
||||
/// <param name="currRequest">Contenuto della richiesta come QuestionDTO</param>
|
||||
/// <returns></returns>
|
||||
public async Task<bool> EnqueueRequest(string reqType, string reqUid, CalcRequestDTO currRequest)
|
||||
{
|
||||
bool done = false;
|
||||
int nId = 1;
|
||||
// salvo su cache x successivo reinvio da currRequest
|
||||
QuestionDTO calcRequest = new QuestionDTO(nId, currRequest.EnvType, currRequest.DictExec);
|
||||
// salvo in cache contenuto della richiesta x UID
|
||||
string currKey = $"{redisOrderReqKey}:{reqUid.Replace("/", ":")}";
|
||||
done = await _redisService.SetAsync(currKey, calcRequest.sProcessArgs);
|
||||
// accodo la nuova richiesta
|
||||
//RedisKey queueKey = (RedisKey)$"{redisBaseKey}:OrderQueue:{reqType}";
|
||||
_redisService.QueuePush(queueKey, (RedisValue)reqUid);
|
||||
// dizionario richieste: è il serializzato dell'elenco degli UID da calcolare...
|
||||
var currList = await _redisService.QueueListAllAsync(queueKey);
|
||||
Dictionary<string, string> calcDict = new Dictionary<string, string>();
|
||||
calcDict.Add("ReqLen", $"{calcDict.Count}");
|
||||
string listReq = JsonConvert.SerializeObject(currList);
|
||||
calcDict.Add("ReqList", listReq);
|
||||
// preparo richiesta di calcolo x UID da inviare
|
||||
QuestionDTO chRequest = new QuestionDTO(nId, currRequest.EnvType, calcDict);
|
||||
// invio sul channel redis della richiesta di processing
|
||||
await _redisService.PublishAsync(chPub, chRequest.sProcessArgs);
|
||||
// ritorno
|
||||
return done;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Chiamata GET: dizionario stato richieste
|
||||
/// GET: api/Prod/alive
|
||||
/// </summary>
|
||||
/// <param name="id">id oggetto</param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("queue-status")]
|
||||
public async Task<IActionResult> QueueStatus()
|
||||
{
|
||||
Stopwatch sw = new Stopwatch();
|
||||
sw.Start();
|
||||
List<string>? listEstim = new List<string>();
|
||||
List<string>? listOptim = new List<string>();
|
||||
Dictionary<string, int> queueStatus = new Dictionary<string, int>();
|
||||
// cerco in redis...
|
||||
RedisValue rawEstim = await _redisDb.StringGetAsync($"{redisBaseKey}:EstimReq");
|
||||
if (rawEstim.HasValue)
|
||||
{
|
||||
listEstim = JsonConvert.DeserializeObject<List<string>>($"{rawEstim}");
|
||||
}
|
||||
RedisValue rawOptim = await _redisDb.StringGetAsync($"{redisBaseKey}:OptimReq");
|
||||
if (rawOptim.HasValue)
|
||||
{
|
||||
listOptim = JsonConvert.DeserializeObject<List<string>>($"{rawOptim}");
|
||||
}
|
||||
// simulo status...
|
||||
queueStatus.Add("estimation", listEstim?.Count ?? 0);
|
||||
queueStatus.Add("optimization", listOptim?.Count ?? 0);
|
||||
sw.Stop();
|
||||
Log.Info($"QueueStatus | {sw.Elapsed.TotalMilliseconds:N3} ms");
|
||||
return Ok(queueStatus);
|
||||
}
|
||||
/// <summary>
|
||||
/// Chiamata GET:
|
||||
/// - elenco delle richieste di stima da eseguire
|
||||
@@ -72,16 +218,34 @@ namespace Lux.API.Controllers
|
||||
Log.Info($"EstimationRequestQueue | {sw.Elapsed.TotalMilliseconds:N3} ms");
|
||||
return Ok(listReq);
|
||||
}
|
||||
/// <summary>
|
||||
/// Chiamata GET:
|
||||
/// - elenco delle richieste di ottimizzazione/nesting da eseguire
|
||||
/// - vengono registrate come "passate" al calcolo alla data-ora della richiesta
|
||||
/// GET: api/Prod/estimation
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet("optmization")]
|
||||
public async Task<ActionResult<List<EstimReqPayloadDTO>>> OptimitionRequestQueue()
|
||||
{
|
||||
Stopwatch sw = new Stopwatch();
|
||||
sw.Start();
|
||||
var listReq = new List<EstimReqPayloadDTO>();
|
||||
// vado a recuperare da REDIS elenco degli ordini NON ancora associati ad 1/+ prod
|
||||
|
||||
#endregion Public Methods
|
||||
// opzione 1: restituisco TUTTI ordini NON ancora eseguiti
|
||||
// opzione 2: restituisco dall'inizio solo max(n) non ancora eseguiti? (es primi 5 ordini)
|
||||
|
||||
#region Private Fields
|
||||
// genero elenco degli ordini e per ogni ordine aggiungo il Dict<ItemTag, OrderRowUid>
|
||||
await Task.Delay(100);
|
||||
|
||||
private static Logger Log = LogManager.GetCurrentClassLogger();
|
||||
private readonly IRedisService _redisService;
|
||||
private readonly string chPub = "";
|
||||
private IConfiguration _config;
|
||||
// opzione 1: per tutti gli ordini ritornato registro data-ora invio e tolgo dalla coda...
|
||||
// opzione 2: aspetto conferma dal sistema che li ha presi in carico e registro data-ora...
|
||||
|
||||
#endregion Private Fields
|
||||
sw.Stop();
|
||||
Log.Info($"EstimationRequestQueue | {sw.Elapsed.TotalMilliseconds:N3} ms");
|
||||
return Ok(listReq);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user