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 = ReloadData(); 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"; } protected int maxOrdinal { get; set; } = 999; [Inject] protected MessageService MessageService { get; set; } protected int minOrdinal { get; set; } = 0; [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(); } /// /// Gestione display avanzamento step /// /// 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 doEdit(TaskListModel selRec) { currRecord = selRec; await ReloadData(); } protected async Task doClone(TaskListModel selRec) { if (!await JSRuntime.InvokeAsync("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.MinValue, Freq = selRec.Freq, LastDuration = 0, LastIsError = false, LastResult = "", TType = selRec.TType, Ordinal = SearchRecords.Count + 1, }; await ReloadData(); } protected async Task doMove(TaskListModel currRec, bool goUp) { await StatService.TaskListMove(currRec, goUp); detRecord = null; currRecord = null; await ReloadData(); } protected async Task doReset() { detRecord = null; currRecord = null; await StatService.FlushCache(); await ReloadData(); } protected async Task doRun(TaskListModel selRec) { // SE non è ancora scaduto chiedo conferma if (selRec.DtNextExec > DateTime.Now) { if (!await JSRuntime.InvokeAsync("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, 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() { if (!await JSRuntime.InvokeAsync("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, 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 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(); detRecord = null; currRecord = null; SearchRecords = null; ListRecords = null; await ReloadData(); } 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 List ListRecords; private int MaxVal = 10; private double nextVal = 0; private List SearchRecords; #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 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; 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 } }