using k8s.Models; using Microsoft.AspNetCore.Components; using NLog; using WebDoorCreator.Data.DbModels; using WebDoorCreator.Data.DTO; using WebDoorCreator.Data.Services; using WebDoorCreator.UI.Data; namespace WebDoorCreator.UI.Components.FilesMan { public partial class CompoCompare { #region Public Properties [Parameter] public CompoSelectFilter actFilter { get; set; } = new CompoSelectFilter(); [Parameter] public EventCallback updateRecordCount { get; set; } #endregion Public Properties #region Public Methods public static string getFolder(string key) { string answ = ""; if (key.Contains("\\")) { answ = key.Split("\\")[1]; } return answ; } public static string getType(string key) { string answ = ""; if (key.Contains("\\")) { answ = key.Split("\\")[0]; } return answ; } #endregion Public Methods #region Protected Fields protected static Logger Log = LogManager.GetCurrentClassLogger(); protected List tempDOT = new List(); #endregion Protected Fields #region Protected Properties protected bool _showAdd { get; set; } = true; protected bool _showRem { get; set; } = true; protected bool _showUpd { get; set; } = true; protected KeyValuePair actFile { get; set; } = new KeyValuePair(); [Inject] protected IConfiguration configuration { get; set; } = null!; protected double expTime { get; set; } = 6; protected string filtBtnCssAdd { get => showAdd ? "btn-primary" : "btn-secondary"; } protected string filtBtnCssRem { get => showRem ? "btn-danger" : "btn-secondary"; } protected string filtBtnCssUpd { get => showUpd ? "btn-warning" : "btn-secondary"; } protected bool isDrawing { get; set; } = false; protected bool isLoading { get; set; } = false; protected List ListType { get; set; } = new List(); protected int numSteps { get; set; } = 4; protected bool show { get; set; } = false; protected bool showAdd { get => _showAdd; set => _showAdd = value; } protected bool showBtnCur { get => NumFileCur > 0; } protected bool showBtnNew { get => NumFileNew > 0; } protected bool showRem { get => _showRem; set => _showRem = value; } protected bool showUpd { get => _showUpd; set => _showUpd = value; } protected string tplMissing { get => numTplMissing >= 0 ? $"{numTplMissing}" : "???"; } protected string txtDx { get; set; } = ""; protected string txtSx { get; set; } = ""; protected string TypeSel { get => _typeSel; set { if (_typeSel != value) { _typeSel = value; ReloadData().ConfigureAwait(false); //var pUpd = Task.Run(async () => //{ // await InvokeAsync(ReloadData); //}); //pUpd.Wait(); } } } [Inject] protected WebDoorCreatorService WDCService { get; set; } = null!; [Inject] protected WDCUserService WDCUService { get; set; } = null!; #endregion Protected Properties #region Protected Methods protected string btnCssClass(Core.Enum.fileStatus fStat, bool doAct) { string answ = ""; if (fStat == Core.Enum.fileStatus.add && doAct) { answ = "btn-primary"; } else if (fStat == Core.Enum.fileStatus.mod && doAct) { answ = "btn-warning"; } else if (fStat == Core.Enum.fileStatus.rem && doAct) { answ = "btn-danger"; } else { answ = "btn-secondary"; } return answ; } protected string btnIcon(Core.Enum.fileStatus fStat) { string answ = ""; if (fStat == Core.Enum.fileStatus.add || fStat == Core.Enum.fileStatus.mod) { answ = "fa-solid fa-circle-arrow-right"; } else if (fStat == Core.Enum.fileStatus.rem) { answ = "fa-solid fa-square-minus"; } return answ; } protected string btnTxt(Core.Enum.fileStatus fStat) { string answ = ""; if (fStat == Core.Enum.fileStatus.add) { answ = "A"; } else if (fStat == Core.Enum.fileStatus.mod) { answ = "U"; } else if (fStat == Core.Enum.fileStatus.rem) { answ = "D"; } return answ; } protected async Task changeDoAct(KeyValuePair currObj) { await Task.Delay(1); files2Chk[currObj.Key].action = !files2Chk[currObj.Key].action; } protected async Task doSave() { isDrawing = true; isLoading = true; sendDataVal = 0; await Task.Delay(1); var updTask1 = Task.Run(() => { files2Save = searchRecords.Where(x => x.Value.action).ToDictionary(x => x.Key, x => x.Value); foreach (var item in files2Save) { string dir2chk = ""; var fileDirs = item.Key.Split("\\"); foreach (var dir in fileDirs.SkipLast(1)) { dir2chk += $"\\{dir}"; if (!Directory.Exists($"{rootPathCur}{dir2chk}")) { Directory.CreateDirectory($"{rootPathCur}{dir2chk}"); } } if (item.Value.status == Core.Enum.fileStatus.add) { File.Copy($"{rootPathNew}\\{item.Key}", $"{rootPathCur}\\{item.Key}"); Log.Info($"Add new Compo: {rootPathNew}\\{item.Key}"); } else if (item.Value.status == Core.Enum.fileStatus.mod) { ReplaceFile($"{rootPathNew}\\{item.Key}", $"{rootPathCur}\\{item.Key}"); Log.Info($"Updated Compo: {rootPathNew}\\{item.Key}"); } else if (item.Value.status == Core.Enum.fileStatus.rem) { File.Delete($"{rootPathCur}\\{item.Key}"); Log.Info($"Removed Compo: {rootPathNew}\\{item.Key}"); } } } ); await updTask1; sendDataVal = 30; var updTask2 = Task.Run(async () => { await WDCService.ListValuesLuaNgeInsert(rootPathCur); }); await updTask2; Log.Info($"DB Updated for Compo changes!"); sendDataVal = 45; // svuoto cache! await WDCService.FlushCustomPattern("Cache"); sendDataVal = 50; await doScan(true, true); sendDataVal = 90; } /// /// Scansione e comparazione directory COMPO /// /// /// /// protected async Task doScan(bool scanCur, bool scanNew) { isDrawing = true; isLoading = true; files2Chk.Clear(); searchRecords.Clear(); stats2show.Clear(); stats2show.Add(Core.Enum.fileStatus.add); stats2show.Add(Core.Enum.fileStatus.mod); stats2show.Add(Core.Enum.fileStatus.rem); ListType = new List(); Log.Info($"Component scan started by user {WDCUService.userId}"); sendDataVal = 0; var updTask1 = Task.Run(async () => await WDCService.populateHws() ); await updTask1; searchRecords = new Dictionary(); tempDOT.Clear(); // gestione files nuovi var newFiles = await getCurFiles(rootPathNew, !scanNew); NumFileNew = newFiles.Count; sendDataVal = 30; // gestione files cur var curFiles = await getCurFiles(rootPathCur, !scanCur); NumFileCur = curFiles.Count; sendDataVal = 60; foreach (var item in newFiles) { if (curFiles.ContainsKey(item.Key)) { if (curFiles[item.Key].FileMD5 != item.Value.FileMD5) { item.Value.status = Core.Enum.fileStatus.mod; checkItemType(item); } curFiles.Remove(item.Key); } else { item.Value.status = Core.Enum.fileStatus.add; checkItemType(item); } searchRecords.Add(item.Key, item.Value); } sendDataVal = 90; foreach (var item in curFiles) { item.Value.status = Core.Enum.fileStatus.rem; searchRecords.Add(item.Key, item.Value); checkItemType(item); } searchRecords = searchRecords.Where(x => x.Value.status != Core.Enum.fileStatus.none).ToDictionary(x => x.Key, x => x.Value); await ReloadData(); sendDataVal = 100; isLoading = false; isDrawing = false; if (searchRecords.Count == 0) { scanMsg = "THE DIRECTORIES HAVE NO DIFFERENCES"; } Log.Info($"Component scan COMPLETED"); } protected async Task filtBtnToggle(Core.Enum.fileStatus fileStat) { switch (fileStat) { case Core.Enum.fileStatus.add: showAdd = !showAdd; if (showAdd && !stats2show.Contains(Core.Enum.fileStatus.add)) { stats2show.Add(Core.Enum.fileStatus.add); } else if (!showAdd && stats2show.Contains(Core.Enum.fileStatus.add)) { stats2show.Remove(Core.Enum.fileStatus.add); } break; case Core.Enum.fileStatus.mod: showUpd = !showUpd; if (showUpd && !stats2show.Contains(Core.Enum.fileStatus.mod)) { stats2show.Add(Core.Enum.fileStatus.mod); } else if (!showUpd && stats2show.Contains(Core.Enum.fileStatus.mod)) { stats2show.Remove(Core.Enum.fileStatus.mod); } break; case Core.Enum.fileStatus.rem: showRem = !showRem; if (showRem && !stats2show.Contains(Core.Enum.fileStatus.rem)) { stats2show.Add(Core.Enum.fileStatus.rem); } else if (!showRem && stats2show.Contains(Core.Enum.fileStatus.rem)) { stats2show.Remove(Core.Enum.fileStatus.rem); } break; } await ReloadData(); } /// /// Sistema i componenti missing creando file (vuoti) corrispondenti a quanto trovato sul DB /// /// protected async Task fixMissing() { isLoading = true; bool done = await WDCService.CompoFixMissing(rootPathCur); numTplMissing = await WDCService.CompoCountMissing(rootPathCur); isLoading = false; } protected override async Task OnParametersSetAsync() { await ReloadData(); } protected async Task ReloadData() { isDrawing = true; await Task.Delay(1); var filtData = searchRecords .Where(x => stats2show.Contains(x.Value.status) && (TypeSel == "*" || x.Key.StartsWith(TypeSel))) .ToList(); totalCount = filtData.Count; files2Chk = filtData .Skip(numRecord * (currPage - 1)) .Take(numRecord) .ToDictionary(x => x.Key, x => x.Value); //await InvokeAsync(() => StateHasChanged()); isDrawing = false; } protected void ReplaceFile(string FileToMoveAndDelete, string FileToReplace) { File.Replace(FileToMoveAndDelete, FileToReplace, null); } protected void selectAll(bool selUnsel) { foreach (var item in searchRecords) { item.Value.action = selUnsel; } } protected async Task showDiff(KeyValuePair currFile, Core.Enum.fileStatus cStatus) { await Task.Delay(1); actFile = currFile; txtSx = ""; txtDx = ""; // in base al tipo di modifica leggo... switch (cStatus) { case Core.Enum.fileStatus.add: txtSx = File.ReadAllText($"{rootPathNew}\\{currFile.Key}"); break; case Core.Enum.fileStatus.mod: txtSx = File.ReadAllText($"{rootPathNew}\\{currFile.Key}"); txtDx = File.ReadAllText($"{rootPathCur}\\{currFile.Key}"); break; case Core.Enum.fileStatus.rem: txtDx = File.ReadAllText($"{rootPathCur}\\{currFile.Key}"); break; case Core.Enum.fileStatus.none: default: break; } } /// /// verifica numero template missing x eventuale fix con creazione nuovi file vuoti /// /// protected async Task updateNumMissing() { isLoading = true; numTplMissing = await WDCService.CompoCountMissing(rootPathCur); isLoading = false; } #endregion Protected Methods #region Private Fields private Dictionary files2Chk = new Dictionary(); private Dictionary files2Save = new Dictionary(); private string scanMsg = "CLICK TO SCAN"; private Dictionary searchRecords = new Dictionary(); private int sendDataMaxVal = 100; private int sendDataVal = 0; private List stats2show = new List(); private string titleMsg = "Scanning directories"; #endregion Private Fields #region Private Properties private bool _selUns { get; set; } = false; private int _totalCount { get; set; } = -1; private string _typeSel { get; set; } = "*"; private int currPage { get => actFilter.CurrPage; set { selUns = false; actFilter.CurrPage = value; } } /// /// Numero files CURRENT /// private int NumFileCur { get; set; } = 0; /// /// Numero files NEW /// private int NumFileNew { get; set; } = 0; private int numRecord { get => actFilter.NumRec; set => actFilter.NumRec = value; } private int numTplMissing { get; set; } = -1; private string rootPathCur { get => configuration.GetValue("CompoBaseDirs:CurrCompoDir"); } private string rootPathNew { get => configuration.GetValue("CompoBaseDirs:NewCompoDir"); } private bool selUns { get => _selUns; set { _selUns = value; selectAll(_selUns); } } private int totalCount { get => _totalCount; set { if (_totalCount != value) { _totalCount = value; updateRecordCount.InvokeAsync(value); } } } #endregion Private Properties #region Private Methods private void checkItemType(KeyValuePair item) { string type = getType(item.Key); if (!ListType.Contains(type)) { ListType.Add(type); } } /// /// Recupera i dati files current, /// /// Path di ricerca /// Usa cache Redis vs lettura da filesystem /// private async Task> getCurFiles(string rootPath, bool useCache) { var updTask = Task.Run(async () => await WDCService.scanSrcDestDir(rootPath, useCache) ); var listFiles = await updTask; return listFiles; } #endregion Private Methods } }