diff --git a/DiunaBI.API/Controllers/JobsController.cs b/DiunaBI.API/Controllers/JobsController.cs index cab4503..9666476 100644 --- a/DiunaBI.API/Controllers/JobsController.cs +++ b/DiunaBI.API/Controllers/JobsController.cs @@ -204,6 +204,79 @@ public class JobsController : Controller } } + // UI-friendly endpoints (JWT auth) + [HttpPost] + [Route("ui/schedule")] + public async Task ScheduleJobsUI([FromQuery] string? nameFilter = null) + { + try + { + var jobsCreated = await _jobScheduler.ScheduleAllJobsAsync(nameFilter); + + _logger.LogInformation("ScheduleJobsUI: Created {Count} jobs by user {UserId}", jobsCreated, User.Identity?.Name); + + return Ok(new + { + success = true, + jobsCreated, + message = $"Successfully scheduled {jobsCreated} jobs" + }); + } + catch (Exception ex) + { + _logger.LogError(ex, "ScheduleJobsUI: Error scheduling jobs"); + return BadRequest("An error occurred processing your request"); + } + } + + [HttpPost] + [Route("ui/schedule/imports")] + public async Task ScheduleImportJobsUI([FromQuery] string? nameFilter = null) + { + try + { + var jobsCreated = await _jobScheduler.ScheduleImportJobsAsync(nameFilter); + + _logger.LogInformation("ScheduleImportJobsUI: Created {Count} import jobs by user {UserId}", jobsCreated, User.Identity?.Name); + + return Ok(new + { + success = true, + jobsCreated, + message = $"Successfully scheduled {jobsCreated} import jobs" + }); + } + catch (Exception ex) + { + _logger.LogError(ex, "ScheduleImportJobsUI: Error scheduling import jobs"); + return BadRequest("An error occurred processing your request"); + } + } + + [HttpPost] + [Route("ui/schedule/processes")] + public async Task ScheduleProcessJobsUI() + { + try + { + var jobsCreated = await _jobScheduler.ScheduleProcessJobsAsync(); + + _logger.LogInformation("ScheduleProcessJobsUI: Created {Count} process jobs by user {UserId}", jobsCreated, User.Identity?.Name); + + return Ok(new + { + success = true, + jobsCreated, + message = $"Successfully scheduled {jobsCreated} process jobs" + }); + } + catch (Exception ex) + { + _logger.LogError(ex, "ScheduleProcessJobsUI: Error scheduling process jobs"); + return BadRequest("An error occurred processing your request"); + } + } + [HttpPost] [Route("{id:guid}/retry")] public async Task RetryJob(Guid id) diff --git a/DiunaBI.UI.Shared/Pages/Jobs/Index.razor b/DiunaBI.UI.Shared/Pages/Jobs/Index.razor index 319c92b..5edcc23 100644 --- a/DiunaBI.UI.Shared/Pages/Jobs/Index.razor +++ b/DiunaBI.UI.Shared/Pages/Jobs/Index.razor @@ -42,7 +42,33 @@ - + + + +
+ + Run All Jobs +
+
+ +
+ + Run All Imports +
+
+ +
+ + Run All Processes +
+
+
+ await JobService.ScheduleAllJobsAsync(), + "imports" => await JobService.ScheduleImportJobsAsync(), + "processes" => await JobService.ScheduleProcessJobsAsync(), + _ => (false, 0, "Unknown job type") + }; + + if (result.success) + { + Snackbar.Add($"{result.message} ({result.jobsCreated} jobs created)", Severity.Success); + await LoadJobs(); + } + else + { + Snackbar.Add(result.message, Severity.Error); + } + } + catch (Exception ex) + { + Console.WriteLine($"Scheduling jobs failed: {ex.Message}"); + Snackbar.Add($"Failed to schedule jobs: {ex.Message}", Severity.Error); + } + finally + { + isLoading = false; + } + } + private Color GetStatusColor(JobStatus status) { return status switch diff --git a/DiunaBI.UI.Shared/Services/JobService.cs b/DiunaBI.UI.Shared/Services/JobService.cs index 5535dc9..25fc40d 100644 --- a/DiunaBI.UI.Shared/Services/JobService.cs +++ b/DiunaBI.UI.Shared/Services/JobService.cs @@ -88,6 +88,89 @@ public class JobService return await response.Content.ReadFromJsonAsync(); } + + public async Task<(bool success, int jobsCreated, string message)> ScheduleAllJobsAsync(string? nameFilter = null) + { + try + { + var query = string.IsNullOrEmpty(nameFilter) ? "" : $"?nameFilter={Uri.EscapeDataString(nameFilter)}"; + var response = await _httpClient.PostAsync($"Jobs/ui/schedule{query}", null); + + if (!response.IsSuccessStatusCode) + { + var error = await response.Content.ReadAsStringAsync(); + return (false, 0, $"Failed to schedule jobs: {error}"); + } + + var json = await response.Content.ReadAsStringAsync(); + var result = JsonSerializer.Deserialize(json, _jsonOptions); + + var jobsCreated = result.GetProperty("jobsCreated").GetInt32(); + var message = result.GetProperty("message").GetString() ?? "Jobs scheduled"; + + return (true, jobsCreated, message); + } + catch (Exception ex) + { + Console.WriteLine($"Scheduling jobs failed: {ex.Message}"); + return (false, 0, $"Error: {ex.Message}"); + } + } + + public async Task<(bool success, int jobsCreated, string message)> ScheduleImportJobsAsync(string? nameFilter = null) + { + try + { + var query = string.IsNullOrEmpty(nameFilter) ? "" : $"?nameFilter={Uri.EscapeDataString(nameFilter)}"; + var response = await _httpClient.PostAsync($"Jobs/ui/schedule/imports{query}", null); + + if (!response.IsSuccessStatusCode) + { + var error = await response.Content.ReadAsStringAsync(); + return (false, 0, $"Failed to schedule import jobs: {error}"); + } + + var json = await response.Content.ReadAsStringAsync(); + var result = JsonSerializer.Deserialize(json, _jsonOptions); + + var jobsCreated = result.GetProperty("jobsCreated").GetInt32(); + var message = result.GetProperty("message").GetString() ?? "Import jobs scheduled"; + + return (true, jobsCreated, message); + } + catch (Exception ex) + { + Console.WriteLine($"Scheduling import jobs failed: {ex.Message}"); + return (false, 0, $"Error: {ex.Message}"); + } + } + + public async Task<(bool success, int jobsCreated, string message)> ScheduleProcessJobsAsync() + { + try + { + var response = await _httpClient.PostAsync("Jobs/ui/schedule/processes", null); + + if (!response.IsSuccessStatusCode) + { + var error = await response.Content.ReadAsStringAsync(); + return (false, 0, $"Failed to schedule process jobs: {error}"); + } + + var json = await response.Content.ReadAsStringAsync(); + var result = JsonSerializer.Deserialize(json, _jsonOptions); + + var jobsCreated = result.GetProperty("jobsCreated").GetInt32(); + var message = result.GetProperty("message").GetString() ?? "Process jobs scheduled"; + + return (true, jobsCreated, message); + } + catch (Exception ex) + { + Console.WriteLine($"Scheduling process jobs failed: {ex.Message}"); + return (false, 0, $"Error: {ex.Message}"); + } + } } public class JobStats