From f53fc4c53fae45f28c75789f3c9d7cea24b43568 Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Tue, 17 Sep 2024 17:56:44 +0200 Subject: [PATCH 1/3] Fix gestione bonifica file *.rul - non modifica micro stati x mappe ingressi - fix grafici vari (minori) --- Resources/VersNum.txt | 2 +- SMGen.Data/Services/SMGDataService.cs | 187 +++++++++++++++-- SMGen/Components/FilesList.razor.cs | 20 +- SMGen/Pages/FileFixIngr.razor | 16 ++ SMGen/Pages/FileFixIngr.razor.cs | 196 ++++++++++++++++++ SMGen/Pages/FilesFix.razor | 2 +- SMGen/Pages/SMEvent2State.razor | 2 +- SMGen/Pages/SMIn2Event.razor | 2 +- .../PublishProfiles/IIS-PROD.pubxml.user | 2 +- SMGen/SMGen.csproj | 2 +- SMGen/Shared/NavMenu.razor | 17 +- 11 files changed, 412 insertions(+), 36 deletions(-) create mode 100644 SMGen/Pages/FileFixIngr.razor create mode 100644 SMGen/Pages/FileFixIngr.razor.cs diff --git a/Resources/VersNum.txt b/Resources/VersNum.txt index 166cbfe..bb6ff80 100644 --- a/Resources/VersNum.txt +++ b/Resources/VersNum.txt @@ -1 +1 @@ -0.9.2408.1918 +0.9.2409.1717 diff --git a/SMGen.Data/Services/SMGDataService.cs b/SMGen.Data/Services/SMGDataService.cs index 656d977..d32a7a1 100644 --- a/SMGen.Data/Services/SMGDataService.cs +++ b/SMGen.Data/Services/SMGDataService.cs @@ -368,6 +368,142 @@ namespace SMGen.Data.Services private Dictionary Events_to_send { get; set; } = new Dictionary(); private List States { get; set; } = new List(); private List TranInList2add { get; set; } = new List(); + + + /// + /// Verifica su DB eventi e stati + /// + /// + /// + public async Task DoCheckUnusedEv(FilesClass currFile) + { + await Task.Delay(1); + States.Clear(); + Bits.Clear(); + Events_to_send.Clear(); + Rules.Clear(); + eventsAll.Clear(); + evOk.Clear(); + StatesAll.Clear(); + string lineOk = ""; + // recupero nome file x partire + string filePath = currFile.tempFileName; + FileLinesClass answ = new FileLinesClass(); + string[] lines = File.ReadAllLines(filePath, Encoding.UTF8); + try + { + foreach (var line in lines) + { + lineOk = line.Trim(); + if (!lineOk.StartsWith("#") && !string.IsNullOrEmpty(lineOk) && lineOk.Length >= 3) + { + if (lineOk.Contains("#")) + { + var lineSplit = lineOk.Split("#"); + lineOk = lineSplit[0]; + } + + var sz_tokens = lineOk.Split(":"); + + var sz_temp = sz_tokens[0].Trim(); + switch (sz_temp) + { + case "$DEFINITIONS": + b_rules_definition = false; + break; + + case "$NAME": + break; + + case "$IDX": + break; + + case "$STATE": + if (!StatesAll.ContainsKey(sz_tokens[2].Trim().ToUpper())) + { + StatesAll.Add(sz_tokens[2].Trim().ToUpper(), int.Parse(sz_tokens[1].Trim().ToUpper())); + } + break; + + case "$EVENT": + if (!Events_to_send.ContainsValue(int.Parse(sz_tokens[1].Trim().ToUpper()))) + { + Events_to_send.Add(sz_tokens[2].Trim().ToUpper(), int.Parse(sz_tokens[1].Trim().ToUpper())); + } + var newEvent = new AnagEventiModelTemp() + { + IdxTipo = int.Parse(sz_tokens[1].Trim().ToUpper()), + Nome = sz_tokens[2].Trim().ToUpper() + }; + break; + + case "$RULES": + b_rules_definition = true; + break; + case "$DO": + b_rules_definition = false; + + break; + default: + if (b_rules_definition) + { + var state = sz_temp.Trim().ToUpper(); + var event_to_send = sz_tokens[1].Trim().ToUpper(); + var next_state = sz_tokens[2].Trim().ToUpper(); + + var temp_rule = new RuleClass() + { + state = state, + event_to_send = event_to_send, + next_state = next_state + }; + Rules.Add(temp_rule); + States2Rules.Add(next_state); + } + break; + } + } + else + { + //linesChecked.Add(line, true); + } + } + foreach (var item in States2Rules) + { + if (!StatesAll2.ContainsKey(item)) + { + StatesAll2.Add(item, StatesAll[item]); + } + } + foreach (var ev in Events_to_send) + { + var rule2Ev = Rules.FirstOrDefault(x => x.event_to_send == ev.Key); + if (rule2Ev != null) + { + if (!evOk.ContainsKey(ev.Key) && !evOk.ContainsValue(ev.Value)) + { + evOk.Add(ev.Key, ev.Value); + } + } + } + } + catch (Exception exc) + { + Log.Error($"{exc}{Environment.NewLine}"); + } + + answ.file = currFile.tempFileName; + answ.statesOK = StatesAll2; + answ.eventsOK = evOk; + answ.lines = lines.ToList(); + return answ; + } + + /// + /// Verifica su DB eventi e stati + /// + /// + /// public async Task DoCheckUnusedEvSt(FilesClass currFile) { await Task.Delay(1); @@ -492,17 +628,15 @@ namespace SMGen.Data.Services return answ; } - - - /// /// Valuta un file di ruoles x ingressi 2 eventi e restituisce esito /// /// Path file *.rul da processare /// Indica se salvare sul DB + /// Indica se processare parte state (x state machine stati) /// /// - public async Task EvalIn2EvRuleFile(FilesClass currFile, bool saveToDb) + public async Task EvalIn2EvRuleFile(FilesClass currFile, bool saveToDb, bool doProcState) { await Task.Delay(1); Dictionary evSt2Change = new Dictionary(); @@ -637,7 +771,7 @@ namespace SMGen.Data.Services } } - evSt2Change = await modFile(currFile, false); + evSt2Change = await modFile(currFile, false, doProcState); if (evSt2Change.Count() == 0) { @@ -708,8 +842,14 @@ namespace SMGen.Data.Services return currFile; } - - public async Task> modFile(FilesClass file, bool doProc) + /// + /// Modifica il file in oggetto + /// + /// + /// + /// Necessaria modifica stati (x state machine stati) + /// + public async Task> modFile(FilesClass file, bool doProc, bool doProcState) { Dictionary evSt2Change = new Dictionary(); @@ -727,19 +867,22 @@ namespace SMGen.Data.Services { if (!line.StartsWith("$EVENT") && !line.StartsWith("#") && line.StartsWith("$STATE")) { - var lineSplit = line.Split(":"); - if (lineSplit.Count() >= 3) + if (doProcState) { - if (!statesFromDb.ContainsKey(int.Parse(lineSplit[1].Trim()))) + var lineSplit = line.Split(":"); + if (lineSplit.Count() >= 3) { - errMsg = $"Lo stato {lineSplit[1].Trim()}: {lineSplit[2].Trim()} non è presente in AnagraficaStati sul DB"; - file.errorMsgs.Add(errMsg); - file.isOk = false; - } - else if (statesFromDb[int.Parse(lineSplit[1].Trim())] != lineSplit[2].Trim()) - { - fileTxt = fileTxt.Replace(lineSplit[2].Trim(), statesFromDb[int.Parse(lineSplit[1].Trim())]); - evSt2Change.Add(lineSplit[2].Trim(), lineSplit[0].Trim()); + if (!statesFromDb.ContainsKey(int.Parse(lineSplit[1].Trim()))) + { + errMsg = $"Lo stato {lineSplit[1].Trim()}: {lineSplit[2].Trim()} non è presente in AnagraficaStati sul DB"; + file.errorMsgs.Add(errMsg); + file.isOk = false; + } + else if (statesFromDb[int.Parse(lineSplit[1].Trim())] != lineSplit[2].Trim()) + { + fileTxt = fileTxt.Replace(lineSplit[2].Trim(), statesFromDb[int.Parse(lineSplit[1].Trim())]); + evSt2Change.Add(lineSplit[2].Trim(), lineSplit[0].Trim()); + } } } } @@ -793,8 +936,12 @@ namespace SMGen.Data.Services /// /// Path file *.rul da processare /// Indica se salvare sul DB + /// + /// + /// + /// Indica se processare parte state (x state machine stati) /// - public async Task EvalIn2StateRuleFile(FilesClass currFile, bool saveToDb, bool calcItself, bool calcEmptyState, Core.Enum.ORDERTYPE orderType) + public async Task EvalIn2StateRuleFile(FilesClass currFile, bool saveToDb, bool calcItself, bool calcEmptyState, Core.Enum.ORDERTYPE orderType, bool doProcState) { await Task.Delay(1); Dictionary evSt2Change = new Dictionary(); @@ -940,7 +1087,7 @@ namespace SMGen.Data.Services Log.Error($"Eccezione durante la lettura del file {currFile.origFileName} alla riga {line}: {exc}{Environment.NewLine}"); } - evSt2Change = await modFile(currFile, false); + evSt2Change = await modFile(currFile, false, doProcState); if (evSt2Change.Count() == 0) { diff --git a/SMGen/Components/FilesList.razor.cs b/SMGen/Components/FilesList.razor.cs index eb05bd4..b9ecfaa 100644 --- a/SMGen/Components/FilesList.razor.cs +++ b/SMGen/Components/FilesList.razor.cs @@ -9,6 +9,9 @@ namespace SMGen.Components { protected FileLinesClass FileLines = new FileLinesClass(); + [Parameter] + public bool doProcState { get; set; } = true; + [Parameter] public bool calcEmptyState { get; set; } = false; @@ -118,7 +121,16 @@ namespace SMGen.Components await Task.Delay(1); if (FileLines.statesOK.Count <= 0 && FileLines.eventsOK.Count <= 0 && FileLines.lines.Count <= 0) { - FileLines = await SMGDService.DoCheckUnusedEvSt(currFile); + FileLines = await SMGDService.DoCheckUnusedEvSt(currFile); +#if false + if (doProcState) + { + } + else + { + FileLines = await SMGDService.DoCheckUnusedEv(currFile); + } +#endif } else { @@ -141,15 +153,15 @@ namespace SMGen.Components // chiamo esecuzione 1:1... if (hasBit) { - await SMGDService.EvalIn2EvRuleFile(item.Value, true); + await SMGDService.EvalIn2EvRuleFile(item.Value, true, doProcState); } else if (is2Chk) { - await SMGDService.modFile(item.Value, true); + await SMGDService.modFile(item.Value, true, doProcState); } else { - await SMGDService.EvalIn2StateRuleFile(item.Value, false, calcItSelf, calcEmptyState, orderType); + await SMGDService.EvalIn2StateRuleFile(item.Value, false, calcItSelf, calcEmptyState, orderType, doProcState); } item.Value.calcRunning = false; if (item.Value.isOk) diff --git a/SMGen/Pages/FileFixIngr.razor b/SMGen/Pages/FileFixIngr.razor new file mode 100644 index 0000000..9a7e3a8 --- /dev/null +++ b/SMGen/Pages/FileFixIngr.razor @@ -0,0 +1,16 @@ +@page "/FileFixIngr" + +
+ Fix Ingressi *.rul files +
+ + +
+ +
+
+ +
+
+ +
\ No newline at end of file diff --git a/SMGen/Pages/FileFixIngr.razor.cs b/SMGen/Pages/FileFixIngr.razor.cs new file mode 100644 index 0000000..becd0e6 --- /dev/null +++ b/SMGen/Pages/FileFixIngr.razor.cs @@ -0,0 +1,196 @@ +using EgwCoreLib.Razor; +using Microsoft.AspNetCore.Components.Forms; +using Microsoft.AspNetCore.Components; +using NLog; +using SMGen.Data.Data; +using SMGen.Data.Services; +using SMGen.Data; + +namespace SMGen.Pages +{ + public partial class FileFixIngr + { + #region Protected Fields + + protected static Logger Log = LogManager.GetCurrentClassLogger(); + protected SelectSMIn2EvParams currFilter = new SelectSMIn2EvParams(); + protected Dictionary eventsFromDb = new Dictionary(); + protected DataPager? pagerRulFix = null!; + protected Dictionary statesFromDb = new Dictionary(); + + #endregion Protected Fields + + #region Protected Properties + + protected Dictionary Files { get; set; } = new Dictionary(); + + protected int maxAllowedFiles { get; set; } = 100; + + protected string pathDir { get; set; } = ""; + + protected string pathFile { get; set; } = ""; + + protected int resetSucc { get; set; } = 0; + + [Inject] + protected SMGDataService SMGDService { get; set; } = null!; + + #endregion Protected Properties + + #region Protected Methods + + protected void ForceReload(int newNum) + { + numRecord = newNum; + } + + protected void ForceReloadPage(int newNum) + { + currPage = newNum; + } + + protected override async Task OnInitializedAsync() + { + // path cartella root (development) + pathDir = Path.Combine("Temp", "unsafe_uploads"); + // se la cartella non esistesse la creo + if (!Directory.Exists(pathDir)) + { + Directory.CreateDirectory(pathDir); + Log.Info($"Creato directory {pathDir}"); + } + + // svuoto cartella prima di partire + deleteOldFiles(pathDir); + await Task.Delay(1); + await SMGDService.ExecFlushRedisPattern(Core.Constants.FILES_TO_PROC); + } + + protected async Task pgResetReq(bool doReset) + { + if (doReset) + { + await Task.Delay(1); + if (pagerRulFix != null) + { + pagerRulFix.resetCurrPage(); + } + } + } + + protected async Task ReloadData() + { + Files = await SMGDService.FilesGetAll(); + } + + protected void UpdateTotCount(int newTotCount) + { + totalCount = newTotCount; + } + + #endregion Protected Methods + + #region Private Properties + + private int currPage + { + get => currFilter.CurrPage; + set => currFilter.CurrPage = value; + } + + private int numRecord + { + get => currFilter.NumRec; + set => currFilter.NumRec = value; + } + + private int totalCount + { + get => currFilter.TotCount; + set => currFilter.TotCount = value; + } + + #endregion Private Properties + + #region Private Methods + + /// + /// Procedee a bonificare la cartella di upload dei files + vecchi di 3 mesi + /// + private void deleteOldFiles(string dirPath) + { + // elenco files nella directory + string[] files = Directory.GetFiles(dirPath); + // li guardo tutti e se vecchi li elimino... + foreach (string file in files) + { + FileInfo fi = new FileInfo(file); + if (fi.LastAccessTime < DateTime.Now.AddMinutes(-10)) + { + fi.Delete(); + } + } + } + + private async Task LoadFiles(InputFileChangeEventArgs e) + { + List loadedFiles = new(); + long maxFileSize = 1024 * 1024; + loadedFiles.Clear(); + Files.Clear(); + + // svuoto cartella prima di partire + deleteOldFiles(pathDir); + + foreach (var file in e.GetMultipleFiles(maxAllowedFiles)) + { + try + { + loadedFiles.Add(file); + + //assegno un nome file randomico x sicurezza + var trustedFileNameForFileStorage = Path.GetRandomFileName(); + + //path del file da scrivere + pathFile = Path.Combine(pathDir, trustedFileNameForFileStorage); + + // creo file + using (FileStream fs = new(pathFile, FileMode.Create)) + { + // copio il contenuto del file + await file.OpenReadStream(maxFileSize).CopyToAsync(fs); + // scrivo log + Log.Info($"Salvato file temp {pathFile}"); + } + + if (file.Name.Contains(".rul")) + { + var newFIle = new FilesClass() + { + tempFileName = pathFile, + isOk = false, + origFileName = file.Name, + calcRunning = false, + DLoadFileName = "" + }; + + Files.Add(file.Name, newFIle); + } + } + catch (Exception exc) + { + Log.Error($"Errore durante salvataggio file temp {file.Name}: {exc}{Environment.NewLine}"); + } + } + if (Files != null && Files.Count > 0) + { + await SMGDService.FilesLoadRedis(Files); + } + resetSucc = 0; + await InvokeAsync(() => StateHasChanged()); + await ReloadData(); + } + + #endregion Private Methods + } +} \ No newline at end of file diff --git a/SMGen/Pages/FilesFix.razor b/SMGen/Pages/FilesFix.razor index 590e80d..89486a7 100644 --- a/SMGen/Pages/FilesFix.razor +++ b/SMGen/Pages/FilesFix.razor @@ -1,7 +1,7 @@ @page "/FilesFix"
- Files Fix + Fix Stati *.rul files
diff --git a/SMGen/Pages/SMEvent2State.razor b/SMGen/Pages/SMEvent2State.razor index 70f4e77..015d86e 100644 --- a/SMGen/Pages/SMEvent2State.razor +++ b/SMGen/Pages/SMEvent2State.razor @@ -1,6 +1,6 @@ @page "/SMEvent2State"
- Stati + Stati rul → csv
diff --git a/SMGen/Pages/SMIn2Event.razor b/SMGen/Pages/SMIn2Event.razor index 1b35073..2b5376c 100644 --- a/SMGen/Pages/SMIn2Event.razor +++ b/SMGen/Pages/SMIn2Event.razor @@ -1,7 +1,7 @@ @page "/SMIn2Event"
- Ingressi + Ingressi rul → csv
diff --git a/SMGen/Properties/PublishProfiles/IIS-PROD.pubxml.user b/SMGen/Properties/PublishProfiles/IIS-PROD.pubxml.user index 8ed7f0e..b685d7d 100644 --- a/SMGen/Properties/PublishProfiles/IIS-PROD.pubxml.user +++ b/SMGen/Properties/PublishProfiles/IIS-PROD.pubxml.user @@ -4,7 +4,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121. --> - True|2023-07-24T14:24:14.2770302Z;True|2023-07-24T16:23:20.6455643+02:00;True|2023-05-22T15:35:59.9735292+02:00;False|2023-05-22T15:33:39.4808724+02:00;True|2023-05-11T17:36:48.1468628+02:00; + True|2024-09-17T15:30:03.3866876Z||;True|2023-07-24T16:24:14.2770302+02:00||;True|2023-07-24T16:23:20.6455643+02:00||;True|2023-05-22T15:35:59.9735292+02:00||;False|2023-05-22T15:33:39.4808724+02:00||;True|2023-05-11T17:36:48.1468628+02:00||; AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAAHBJL+AjnSkKKQcJBrmvYDwAAAAACAAAAAAADZgAAwAAAABAAAABqHjtCI4r9HP4to5TPRtU/AAAAAASAAACgAAAAEAAAAGWTrj6ykvtUZnIZTp5COsMYAAAAHkol9Zhdo3QCFNYyIvyJViyIyNSW1oNCFAAAAEvXT2wDdsDBGFpVXvR5NVA172tk diff --git a/SMGen/SMGen.csproj b/SMGen/SMGen.csproj index 06aa72c..929e83b 100644 --- a/SMGen/SMGen.csproj +++ b/SMGen/SMGen.csproj @@ -5,7 +5,7 @@ enable enable State Machine Generator - 0.9.2408.1918 + 0.9.2409.1717 diff --git a/SMGen/Shared/NavMenu.razor b/SMGen/Shared/NavMenu.razor index e4fabda..90635a8 100644 --- a/SMGen/Shared/NavMenu.razor +++ b/SMGen/Shared/NavMenu.razor @@ -16,27 +16,32 @@
+