Files
mapo-core/MP.Stats/Pages/TaskScheduler.razor.cs
T
2024-04-03 09:10:26 +02:00

348 lines
9.3 KiB
C#

using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using MP.Stats.Data;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using System;
using System.Linq;
using StackExchange.Redis;
using MP.Data.DatabaseModels;
using static MP.Data.Objects.Enums;
using Microsoft.AspNetCore.DataProtection;
namespace MP.Stats.Pages
{
public partial class TaskScheduler : ComponentBase, IDisposable
{
#region Public Methods
public string checkSelect(int TaskId)
{
string answ = "";
if (currRecord != null)
{
try
{
answ = (currRecord.TaskId == TaskId) ? "table-info" : "";
}
catch
{ }
}
else if (detRecord != null)
{
answ = (detRecord.TaskId == TaskId) ? "table-info" : "";
}
return answ;
}
public void Dispose()
{
MessageService.EA_SearchUpdated -= OnSeachUpdated;
}
public async void OnSeachUpdated()
{
await InvokeAsync(() =>
{
Task task = UpdateData();
StateHasChanged();
});
}
#endregion Public Methods
#region Protected Fields
protected string fileName = "TaskList.csv";
#endregion Protected Fields
#region Protected Properties
[Inject]
protected IJSRuntime JSRuntime { get; set; }
protected string mainCss
{
get => detRecord == null ? "col-12" : "col-6";
}
[Inject]
protected MessageService MessageService { get; set; }
[Inject]
protected NavigationManager NavManager { get; set; }
[Inject]
protected MpStatsService StatService { get; set; }
protected int totalCount { get; set; } = 0;
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", Descript = "Descrizione", DtLastExec = DateTime.Today, DtNextExec = DateTime.Today.AddDays(1) };
await ReloadData();
}
protected async Task doCancel()
{
currRecord = null;
detRecord = null;
await ReloadData();
}
protected async Task doEdit(TaskListModel selRec)
{
currRecord = selRec;
await ReloadData();
}
protected async Task DoFilter(SelectData newFilter)
{
clearFile();
SearchRecords = null;
ListRecords = null;
currFilter = newFilter;
await ReloadData();
}
protected async Task doReset()
{
detRecord = null;
currRecord = null;
await StatService.FlushAll();
await ReloadData();
}
private double currVal = 0;
private double nextVal = 0;
private int MaxVal = 10;
/// <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 doRun(TaskListModel selRec)
{
// SE non è ancora scaduto chiedo conferma
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 StatService.ExecuteTask(selRec.TaskId);
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()
{
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 StatService.ExecuteTask(taskRec.TaskId);
}
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 override async Task OnInitializedAsync()
{
clearFile();
numRecord = 10;
MessageService.ShowSearch = false;
MessageService.PageName = "Task Scheduler";
MessageService.PageIcon = "oi oi-clock";
MessageService.EA_SearchUpdated += OnSeachUpdated;
await ReloadData();
}
protected void ResetData()
{
clearFile();
StatService.rollBackEdit(currRecord);
currRecord = null;
}
protected async Task ResetFilter(SelectData newFilter)
{
clearFile();
currRecord = null;
SearchRecords = null;
ListRecords = null;
currFilter = SelectData.Init(5, 7);
await ReloadData();
}
protected double righDiv(double num, double den)
{
if (den == 0)
{
den = 1;
}
double answ = num / den;
return answ;
}
protected double simVal(double minVal, double maxVal)
{
Random rnd = new Random();
return rnd.NextDouble() * (maxVal - minVal) + minVal;
}
protected async Task UpdateData()
{
currRecord = null;
await ReloadData();
}
#endregion Protected Methods
#region Private Fields
private List<TaskListModel> ListRecords;
private List<TaskListModel> SearchRecords;
#endregion Private Fields
#region Private Properties
private SelectData currFilter
{
get
{
return MessageService.ODL_Filter;
}
set
{
MessageService.ODL_Filter = value;
}
}
private int currPage { get; set; } = 1;
private TaskListModel currRecord { get; set; } = null;
private TaskListModel detRecord { get; set; } = null;
private string fullPath
{
get => $"{Directory.GetCurrentDirectory()}\\temp\\{fileName}";
}
private bool isLoading { get; set; } = false;
private int expTimeMsec { get; set; } = 30000;
private int numRecord { get; set; } = 10;
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 clearFile()
{
await Task.Run(() => File.Delete(fullPath));
}
private async Task ExportCsv()
{
isLoading = true;
// salvo davvero!
await MP.Data.Utils.SaveToCsv(SearchRecords, fullPath, ';');
isLoading = false;
}
private async Task ReloadData()
{
SearchRecords = await StatService.TaskListAll(TypeSel, "");
totalCount = SearchRecords.Count;
ListRecords = SearchRecords.Skip(numRecord * (currPage - 1)).Take(numRecord).ToList();
}
#endregion Private Methods
}
}