Files

261 lines
7.8 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 Properties
/// <summary>
/// Cache Redis abilitata o meno
/// </summary>
[Parameter]
public bool RedisEnabled { get; set; } = false;
#endregion Public Properties
#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 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(TaskListTable.ActionParam action)
{
await TService.TaskListMove(action.CurrRec, action.GoUp);
detRecord = null;
currRecord = null;
await ReloadData();
}
protected async Task DoReset(bool force)
{
detRecord = null;
currRecord = null;
await TService.FlushCacheAsync();
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;
isLoading = true;
detRecord = selRec;
await ReloadData();
isLoading = false;
}
protected async Task ForceAll(bool force)
{
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 ForceUpdate(bool doForce)
{
currRecord = null;
await ReloadData();
}
protected override async Task OnInitializedAsync()
{
isLoading = true;
MaxVal = 2;
await advStep(1);
await ReloadData();
await advStep(2);
isLoading = false;
}
protected void ResetData()
{
TService.rollBackEdit(currRecord);
currRecord = null;
}
protected double righDiv(double num, double den)
{
if (den == 0)
{
den = 1;
}
double answ = num / den;
return answ;
}
#endregion Protected Methods
#region Private Fields
private double currVal = 0;
private Dictionary<int, List<TaskListModel>> ListRecords = new Dictionary<int, List<TaskListModel>>();
private int MaxVal = 10;
private double nextVal = 0;
private List<TaskListModel> SearchRecords = new List<TaskListModel>();
#endregion Private Fields
#region Private Properties
private TaskListModel? currRecord { get; set; } = null;
private TaskListModel? detRecord { get; set; } = null;
private int expTimeMsec { get; set; } = 30000;
private bool isLoading { get; set; } = false;
private string SearchVal { get; set; } = "";
private Task2ExeType typeSel { get; set; } = Task2ExeType.ALL;
#endregion Private Properties
#region Private Methods
private async Task ReloadData()
{
SearchRecords = await TService.TaskListAll(TypeSel, RedisEnabled, SearchVal);
// suddivido per gruppo e genero relativo display raggruppato
ListRecords = SearchRecords
.OrderBy(x => x.Group)
.ThenBy(x => x.Ordinal)
.GroupBy(x => x.Group)
.ToDictionary(g => g.Key, g => g.ToList());
}
#endregion Private Methods
}
}