Files
2024-06-20 12:02:43 +02:00

591 lines
18 KiB
C#

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<int> 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<DoorOpTypeModel> tempDOT = new List<DoorOpTypeModel>();
#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<string, FileDTO> actFile { get; set; } = new KeyValuePair<string, FileDTO>();
[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<string> ListType { get; set; } = new List<string>();
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<string, FileDTO> 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;
}
/// <summary>
/// Scansione e comparazione directory COMPO
/// </summary>
/// <param name="scanCur"></param>
/// <param name="scanNew"></param>
/// <returns></returns>
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<string>();
Log.Info($"Component scan started by user {WDCUService.userId}");
sendDataVal = 0;
var updTask1 = Task.Run(async () =>
await WDCService.populateHws()
);
await updTask1;
searchRecords = new Dictionary<string, FileDTO>();
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();
}
/// <summary>
/// Sistema i componenti missing creando file (vuoti) corrispondenti a quanto trovato sul DB
/// </summary>
/// <returns></returns>
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<string, FileDTO> 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;
}
}
/// <summary>
/// verifica numero template missing x eventuale fix con creazione nuovi file vuoti
/// </summary>
/// <returns></returns>
protected async Task updateNumMissing()
{
isLoading = true;
numTplMissing = await WDCService.CompoCountMissing(rootPathCur);
isLoading = false;
}
#endregion Protected Methods
#region Private Fields
private Dictionary<string, FileDTO> files2Chk = new Dictionary<string, FileDTO>();
private Dictionary<string, FileDTO> files2Save = new Dictionary<string, FileDTO>();
private string scanMsg = "CLICK TO SCAN";
private Dictionary<string, FileDTO> searchRecords = new Dictionary<string, FileDTO>();
private int sendDataMaxVal = 100;
private int sendDataVal = 0;
private List<Core.Enum.fileStatus> stats2show = new List<Core.Enum.fileStatus>();
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;
}
}
/// <summary>
/// Numero files CURRENT
/// </summary>
private int NumFileCur { get; set; } = 0;
/// <summary>
/// Numero files NEW
/// </summary>
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<string>("CompoBaseDirs:CurrCompoDir");
}
private string rootPathNew
{
get => configuration.GetValue<string>("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<string, FileDTO> item)
{
string type = getType(item.Key);
if (!ListType.Contains(type))
{
ListType.Add(type);
}
}
/// <summary>
/// Recupera i dati files current,
/// </summary>
/// <param name="rootPath">Path di ricerca</param>
/// <param name="useCache">Usa cache Redis vs lettura da filesystem</param>
/// <returns></returns>
private async Task<Dictionary<string, FileDTO>> getCurFiles(string rootPath, bool useCache)
{
var updTask = Task.Run(async () =>
await WDCService.scanSrcDestDir(rootPath, useCache)
);
var listFiles = await updTask;
return listFiles;
}
#endregion Private Methods
}
}