381 lines
11 KiB
C#
381 lines
11 KiB
C#
using Microsoft.AspNetCore.Components;
|
|
using Microsoft.JSInterop;
|
|
using MP.TaskMan.Models;
|
|
using MP.TaskMan.Services;
|
|
using static MP.TaskMan.Objects.Enums;
|
|
|
|
namespace MP.TaskMan
|
|
{
|
|
public partial class TaskList : ComponentBase
|
|
{
|
|
#region Public Methods
|
|
|
|
protected string checkSelect(TaskListModel selRec)
|
|
{
|
|
string answ = "";
|
|
if (currRecord != null)
|
|
{
|
|
answ = (currRecord.Equals(selRec)) ? "table-info" : "";
|
|
}
|
|
else if (detRecord != null)
|
|
{
|
|
answ = detRecord.Equals(selRec) ? "table-info" : "";
|
|
}
|
|
#if false
|
|
// verifico se sia disabilitato...
|
|
if (!selRec.Enabled)
|
|
{
|
|
answ += " text-secondary text-opacity-50 textStriked";
|
|
}
|
|
#endif
|
|
return answ;
|
|
}
|
|
|
|
protected string checkDis(TaskListModel selRec)
|
|
{
|
|
string answ = "";
|
|
// verifico se sia disabilitato...
|
|
if (!selRec.Enabled)
|
|
{
|
|
answ += " text-secondary text-opacity-75 textStriked";
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
#endregion Public Methods
|
|
|
|
#region Protected Fields
|
|
|
|
protected string fileName = "TaskList.csv";
|
|
|
|
#endregion Protected Fields
|
|
|
|
#region Protected Properties
|
|
|
|
[Inject]
|
|
protected IJSRuntime JSRuntime { get; set; } = null!;
|
|
|
|
protected string mainCss
|
|
{
|
|
get => detRecord == null ? "col-12" : "col-6";
|
|
}
|
|
|
|
protected int maxOrdinal { get; set; } = 999;
|
|
protected int minOrdinal { get; set; } = 0;
|
|
protected int totalCount { get; set; } = 0;
|
|
|
|
[Inject]
|
|
protected TaskService TService { get; set; } = null!;
|
|
|
|
protected Task2ExeType TypeSel
|
|
{
|
|
get => typeSel;
|
|
set
|
|
{
|
|
if (typeSel != value)
|
|
{
|
|
typeSel = value;
|
|
var pUpd = Task.Run(async () =>
|
|
{
|
|
await ReloadData();
|
|
});
|
|
pUpd.Wait();
|
|
}
|
|
}
|
|
}
|
|
|
|
#endregion Protected Properties
|
|
|
|
#region Protected Methods
|
|
|
|
protected async Task addNew()
|
|
{
|
|
currRecord = new TaskListModel()
|
|
{
|
|
Name = "Nuovo Task",
|
|
TType = TypeSel,
|
|
Descript = "Descrizione Task",
|
|
DtLastExec = DateTime.Today,
|
|
DtNextExec = DateTime.Today.AddDays(1)
|
|
};
|
|
await ReloadData();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gestione display avanzamento step
|
|
/// </summary>
|
|
/// <param name="currStep"></param>
|
|
protected async Task advStep(int currStep)
|
|
{
|
|
currVal = currStep;
|
|
nextVal = currVal + 1;
|
|
await InvokeAsync(StateHasChanged);
|
|
}
|
|
|
|
protected string alCss(TaskListModel TaskRec)
|
|
{
|
|
return TaskRec.LastIsError ? "alert-danger" : "alert-success";
|
|
}
|
|
|
|
protected string btnCss(TaskListModel TaskRec)
|
|
{
|
|
string answ = DetailTaskId != null && DetailTaskId.TaskId == TaskRec.TaskId ? "btn-" : "btn-outline-";
|
|
answ += TaskRec.LastIsError ? "danger" : "success";
|
|
return answ;
|
|
}
|
|
|
|
protected async Task DoCancel()
|
|
{
|
|
currRecord = null;
|
|
detRecord = null;
|
|
await ReloadData();
|
|
}
|
|
|
|
protected async Task DoClone(TaskListModel selRec)
|
|
{
|
|
if (!await JSRuntime.InvokeAsync<bool>("confirm", $"Confermi di voler duplicare il record selezionato?"))
|
|
return;
|
|
currRecord = new TaskListModel()
|
|
{
|
|
Args = selRec.Args,
|
|
Name = $"Copia di {selRec.Name}",
|
|
Cad = selRec.Cad,
|
|
Command = selRec.Command,
|
|
Descript = $"Copia di {selRec.Descript}",
|
|
DtNextExec = DateTime.Today.AddDays(1),
|
|
DtLastExec = DateTime.Today.AddYears(-10),
|
|
Freq = selRec.Freq,
|
|
LastDuration = 0,
|
|
LastIsError = false,
|
|
LastResult = "",
|
|
TType = selRec.TType,
|
|
Ordinal = SearchRecords.Count + 1,
|
|
};
|
|
await ReloadData();
|
|
}
|
|
|
|
protected async Task DoEdit(TaskListModel selRec)
|
|
{
|
|
currRecord = selRec;
|
|
await ReloadData();
|
|
}
|
|
|
|
protected async Task DoMove(TaskListModel currRec, bool goUp)
|
|
{
|
|
await TService.TaskListMove(currRec, goUp);
|
|
detRecord = null;
|
|
currRecord = null;
|
|
await ReloadData();
|
|
}
|
|
|
|
protected async Task DoReset()
|
|
{
|
|
detRecord = null;
|
|
currRecord = null;
|
|
await TService.FlushCache();
|
|
await ReloadData();
|
|
}
|
|
|
|
protected async Task DoRun(TaskListModel selRec)
|
|
{
|
|
// SE non è ancora scaduto chiedo conferma
|
|
if (selRec.DtNextExec > DateTime.Now)
|
|
{
|
|
if (!await JSRuntime.InvokeAsync<bool>("confirm", $"Confermi esecuzione forzata task non scaduto?{Environment.NewLine}[{selRec.TaskId}]: {selRec.Name} - {selRec.Descript}{Environment.NewLine}Prossima schedulazione: {selRec.DtNextExec:yyyy-MM-dd HH:mm:ss}"))
|
|
return;
|
|
}
|
|
|
|
// imposto tempo atteso esecuzione da ultimo...
|
|
isLoading = true;
|
|
MaxVal = 4;
|
|
int currStep = 0;
|
|
await advStep(currStep);
|
|
expTimeMsec = (int)(1000 * selRec.LastDuration) / 4;
|
|
detRecord = null;
|
|
await advStep(currStep++);
|
|
await Task.Delay(100);
|
|
await advStep(currStep++);
|
|
// chiama esecuzione task
|
|
var result = await TService.ExecuteTask(selRec, false);
|
|
await advStep(currStep++);
|
|
isLoading = false;
|
|
await Task.Delay(100);
|
|
await advStep(currStep++);
|
|
await ReloadData();
|
|
}
|
|
|
|
protected async Task DoSelect(TaskListModel selRec)
|
|
{
|
|
detRecord = null;
|
|
currRecord = null;
|
|
DetailTaskId = null;
|
|
isLoading = true;
|
|
detRecord = selRec;
|
|
await ReloadData();
|
|
isLoading = false;
|
|
}
|
|
|
|
protected async Task ForceAll()
|
|
{
|
|
if (!await JSRuntime.InvokeAsync<bool>("confirm", $"Confermi esecuzione forzata di tutti i task?"))
|
|
return;
|
|
|
|
isLoading = true;
|
|
detRecord = null;
|
|
await Task.Delay(100);
|
|
foreach (var taskRec in SearchRecords)
|
|
{
|
|
var result = await TService.ExecuteTask(taskRec, false);
|
|
}
|
|
isLoading = false;
|
|
await Task.Delay(100);
|
|
await ReloadData();
|
|
}
|
|
|
|
protected async Task ForceReload(int newNum)
|
|
{
|
|
numRecord = newNum;
|
|
await ReloadData();
|
|
}
|
|
|
|
protected async Task ForceReloadPage(int newNum)
|
|
{
|
|
currPage = newNum;
|
|
await ReloadData();
|
|
}
|
|
|
|
protected async Task ForceUpdate(bool doForce)
|
|
{
|
|
currRecord = null;
|
|
await ReloadData();
|
|
}
|
|
|
|
protected string iconCss(TaskListModel TaskRec)
|
|
{
|
|
return TaskRec.LastIsError ? "fa-thumbs-down" : "fa-thumbs-up";
|
|
}
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
isLoading = true;
|
|
MaxVal = 2;
|
|
CleanupDir();
|
|
await advStep(1);
|
|
numRecord = 10;
|
|
await ReloadData();
|
|
await advStep(2);
|
|
isLoading = false;
|
|
}
|
|
|
|
protected void ResetData()
|
|
{
|
|
CleanupDir();
|
|
TService.rollBackEdit(currRecord);
|
|
currRecord = null;
|
|
}
|
|
|
|
protected double righDiv(double num, double den)
|
|
{
|
|
if (den == 0)
|
|
{
|
|
den = 1;
|
|
}
|
|
double answ = num / den;
|
|
return answ;
|
|
}
|
|
|
|
protected string TextReduce(string textOriginal, int maxChar)
|
|
{
|
|
string answ = textOriginal;
|
|
if (answ.Length > maxChar)
|
|
{
|
|
answ = $"{textOriginal.Substring(0, maxChar / 2)} ... {textOriginal.Substring(answ.Length - maxChar / 2)}";
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
protected void ToggleDetail(TaskListModel TaskRec)
|
|
{
|
|
if (DetailTaskId == null)
|
|
{
|
|
DetailTaskId = TaskRec;
|
|
}
|
|
else
|
|
{
|
|
DetailTaskId = (DetailTaskId.TaskId == TaskRec.TaskId) ? null : TaskRec;
|
|
}
|
|
}
|
|
|
|
#endregion Protected Methods
|
|
|
|
#region Private Fields
|
|
|
|
private double currVal = 0;
|
|
private TaskListModel? DetailTaskId = null;
|
|
private List<TaskListModel> ListRecords = new List<TaskListModel>();
|
|
private int MaxVal = 10;
|
|
private double nextVal = 0;
|
|
private List<TaskListModel> SearchRecords = new List<TaskListModel>();
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Private Properties
|
|
|
|
private int currPage { get; set; } = 1;
|
|
private TaskListModel? currRecord { get; set; } = null;
|
|
private TaskListModel? detRecord { get; set; } = null;
|
|
private int expTimeMsec { get; set; } = 30000;
|
|
|
|
private string fullPath
|
|
{
|
|
get => $"{Directory.GetCurrentDirectory()}\\temp\\{fileName}";
|
|
}
|
|
|
|
private bool isLoading { get; set; } = false;
|
|
private int numRecord { get; set; } = 10;
|
|
private string SearchVal { get; set; } = "";
|
|
private Task2ExeType typeSel { get; set; } = Task2ExeType.ND;
|
|
|
|
#endregion Private Properties
|
|
|
|
#region Private Methods
|
|
|
|
private string btnRunCss(DateTime dtNextExe)
|
|
{
|
|
DateTime adesso = DateTime.Now;
|
|
string answ = dtNextExe < adesso ? "btn-success" : "btn-warning";
|
|
return answ;
|
|
}
|
|
|
|
private async void CleanupDir()
|
|
{
|
|
var parentDir = Directory.GetParent(fullPath).FullName;
|
|
if (!Directory.Exists(parentDir))
|
|
{
|
|
Directory.CreateDirectory(parentDir);
|
|
}
|
|
await Task.Run(() => File.Delete(fullPath));
|
|
}
|
|
|
|
private async Task ExportCsv()
|
|
{
|
|
isLoading = true;
|
|
// salvo davvero!
|
|
await Egw.Core.Utils.SaveToCsv(SearchRecords, fullPath, ';');
|
|
isLoading = false;
|
|
}
|
|
|
|
private async Task ReloadData()
|
|
{
|
|
SearchRecords = await TService.TaskListAll(TypeSel, SearchVal);
|
|
totalCount = SearchRecords.Count;
|
|
var firstRec = SearchRecords.OrderBy(x => x.Ordinal).FirstOrDefault();
|
|
minOrdinal = firstRec != null ? firstRec.Ordinal : 0;
|
|
var lastRec = SearchRecords.OrderByDescending(x => x.Ordinal).FirstOrDefault();
|
|
maxOrdinal = lastRec != null ? lastRec.Ordinal : 9999;
|
|
ListRecords = SearchRecords.Skip(numRecord * (currPage - 1)).Take(numRecord).ToList();
|
|
}
|
|
|
|
#endregion Private Methods
|
|
}
|
|
} |