using Microsoft.AspNetCore.Mvc; using MP.Data; using MP.IOC.Data; using NLog; using System.Diagnostics; using System.Reflection; namespace MP.IOC.Controllers { [Route("api/[controller]")] [ApiController] public class BenchController : ControllerBase { #region Public Constructors public BenchController(IConfiguration configuration, MpDataService DataService) { Log.Info("Starting BenchController"); _configuration = configuration; DService = DataService; Log.Info("Avviata BenchController"); } #endregion Public Constructors #region Public Methods /// /// Conteggio chiavi REDIS dato pattern api/Bench/CountKeys /// /// /// [HttpGet] [Route("CountKeys")] [Route("CNTKEY")] public string CountKeys(string? id) { string answ = "ND"; if (id == null) id = ""; Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); // conto quanti oggetti ho in memoria REDIS... string groupHash = ""; // DataLayer.mHash(""); if (id.Length > 0) { groupHash += id + ":"; } groupHash += "*"; answ = $"Trovate {DService.RedisCountKey(groupHash)} Hash per {groupHash}"; stopWatch.Stop(); // Get the elapsed time as a TimeSpan value. TimeSpan ts = stopWatch.Elapsed; // accodo tempo! answ += $"{Environment.NewLine}Elapsed: {ts.TotalMilliseconds}ms"; // ritorno return answ; } /// /// Bench recupero dati macchina api/Bench/DtMac?id=SIMUL_01 /// /// /// [HttpGet("DatiMacc")] public async Task DatiMacc(string id) { string answ = "ND"; Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); // se id nullo --> KO! if (id == null) { answ = "KO"; } else { answ = ""; try { Dictionary valori = await DService.mDatiMacchineAsync(id); foreach (var item in valori) { answ += $"{item.Key}|{item.Value}{Environment.NewLine}"; } } catch { } } stopWatch.Stop(); // Get the elapsed time as a TimeSpan value. TimeSpan ts = stopWatch.Elapsed; // accodo tempo! answ += $"{Environment.NewLine}Elapsed: {ts.TotalMilliseconds}ms"; // ritorno return answ; } /// Recupera riga traduzione microstato x processing da HASH table /// api/Bench/FindSMI?id=60&idxMS=3&valore=1 [HttpGet("FindSMI")] public async Task FindSMI(int? id, int? idxMS, int? valore) { string answ = "ND"; Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); // se id nullo --> KO! if (id == null) { answ = "KO"; } else { // recupero dati x sapere quale famiglia SMI è necessaria e quale stato / segnale si // sia ricevuto int idxFamIn = Convert.ToInt32(id); int idxMicroStato = Convert.ToInt32(idxMS); int valIOB = Convert.ToInt32(valore); // recupero microstato macchina da chiave relativa answ = ""; try { var fiHASH = Utils.GetHashSMI(idxFamIn); string outVal = ""; bool trovato = DService.RedisKeyPresent(fiHASH); if (!trovato) { // ricarico tabella! KeyValuePair[] valori = await DService.StateMachInByKeyAsync(idxFamIn); answ = string.Format("Ricaricata SMI per famiglia {0}
", fiHASH); } // recupero singolo valore (stringa) x chiave outVal = await DService.ValoreSmiAsync(idxFamIn, idxMicroStato, valIOB); // mostro output answ += $"idxFamIN: {idxFamIn} | idxMS: {idxMicroStato} | valIOB: {valIOB} | out: {outVal}"; } catch { } } stopWatch.Stop(); // Get the elapsed time as a TimeSpan value. TimeSpan ts = stopWatch.Elapsed; // accodo tempo! answ += $"{Environment.NewLine}Total Time Elapsed: {ts.TotalMilliseconds}ms"; // ritorno return answ; } /// /// Test verifica insert enabled x macchina /// /// /// [HttpGet("InsEnab")] public async Task InsEnab(string id) { string answ = "ND"; Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); // se id nullo --> KO! if (id == null) { answ = "KO"; } else { // recupero microstato macchina da chiave relativa var insEnab = await DService.IobInsEnabAsync(id); answ = $"Macchina {id}, insEnabled {insEnab}{Environment.NewLine}"; } stopWatch.Stop(); // Get the elapsed time as a TimeSpan value. TimeSpan ts = stopWatch.Elapsed; // accodo tempo! answ += $"{Environment.NewLine}Total Time Elapsed: {ts.TotalMilliseconds}ms"; // ritorno return answ; } /// /// pulizia dati api/Bench/RClean?id=100 /// /// /// [HttpGet("RClean")] public string RClean(int? id) { string answ = "ND"; Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); // se id nullo --> KO! if (id == null) { answ = "KO"; } else { string chiave; KeyValuePair[] valori = new KeyValuePair[2]; try { // svuoto i precedenti hash... CICLO! for (int i = 0; i < id; i++) { chiave = string.Format("test:{0}", i); DService.RedisDelKey(chiave); } answ = "OK"; } catch { } } stopWatch.Stop(); // Get the elapsed time as a TimeSpan value. TimeSpan ts = stopWatch.Elapsed; // accodo tempo! answ += $"{Environment.NewLine}Elapsed: {ts.TotalMilliseconds}ms"; // ritorno return answ; } //// GET: IOB (è un check alive) //public string Index() //{ // return "OK"; //} /// /// Setup dati di test hash api/Bench/RSetup?id=100 /// /// /// [HttpGet("RSetup")] public string RSetup(int? id) { string answ = "ND"; Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); // se id nullo --> KO! if (id == null) { answ = "KO"; } else { string chiave; KeyValuePair[] valori = new KeyValuePair[2]; try { // salvo il datasetet come insieme di hash in redis... for (int i = 0; i < id; i++) { chiave = string.Format("test:{0}", i); valori[0] = new KeyValuePair("numero", i.ToString()); valori[1] = new KeyValuePair("doppio", (i * 2).ToString()); DService.RedisSetHash(chiave, valori); } answ = "OK"; } catch { } } stopWatch.Stop(); // Get the elapsed time as a TimeSpan value. TimeSpan ts = stopWatch.Elapsed; // accodo tempo! answ += $"{Environment.NewLine}Elapsed: {ts.TotalMilliseconds}ms"; // ritorno return answ; } /// /// Recupero singolo valore hash api/Bench/RSingleHash?id=50 /// /// /// [HttpGet("RSingleHash")] public string RSingleHash(int? id) { string answ = "ND"; Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); // se id nullo --> KO! if (id == null) { answ = "KO"; } else { answ = ""; string chiave; KeyValuePair[] valori = new KeyValuePair[2]; try { // ora restituisco record completo dell'hash con ID indicato chiave = string.Format("test:{0}", id); valori = DService.RedisGetHash(chiave); foreach (var item in valori) { answ += string.Format("{0}|{1}
", item.Key, item.Value); } } catch { } } stopWatch.Stop(); // Get the elapsed time as a TimeSpan value. TimeSpan ts = stopWatch.Elapsed; // accodo tempo! answ += $"{Environment.NewLine}Elapsed: {ts.TotalMilliseconds}ms"; // ritorno return answ; } /// /// Lettura tab StateMachineIngressi /// /// /// [HttpGet("StateMachineIn")] public async Task StateMachineIn(int? id) { string answ = "ND"; long splitTime = 0; Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); // se id nullo --> KO! if (id == null) { answ = "KO"; } else { int idxFamIn = Convert.ToInt32(id); answ = ""; try { KeyValuePair[] valori = await DService.StateMachInByKeyAsync(idxFamIn); splitTime = stopWatch.ElapsedMilliseconds; foreach (var item in valori) { answ += $"{item.Key}|{item.Value}{Environment.NewLine}"; } } catch { } } stopWatch.Stop(); // Get the elapsed time as a TimeSpan value. TimeSpan ts = stopWatch.Elapsed; // accodo tempo! answ += $"{Environment.NewLine}ReadTime: {splitTime}ms
Total Time Elapsed: {ts.TotalMilliseconds}ms"; // ritorno return answ; } [HttpGet("version")] public string version() { var version = Assembly .GetExecutingAssembly() .GetName() .Version? .ToString() ?? "unknown"; return version; } #endregion Public Methods #region Protected Properties /// /// Dataservice x accesso DB /// protected MpDataService DService { get; set; } #endregion Protected Properties #region Private Fields private static IConfiguration _configuration = null!; private static Logger Log = LogManager.GetCurrentClassLogger(); #endregion Private Fields } }