From e6ba2cce627dd13e379a1de0e88abbfb93554029 Mon Sep 17 00:00:00 2001 From: sbwalker Date: Mon, 31 Mar 2025 13:16:35 -0400 Subject: [PATCH] fix #5194 - improve performance of retrieving scheduled job logs --- Oqtane.Client/Modules/Admin/Jobs/Edit.razor | 2 +- Oqtane.Client/Modules/Admin/Jobs/Log.razor | 8 +++----- Oqtane.Client/Services/Interfaces/IJobLogService.cs | 5 +++-- Oqtane.Client/Services/JobLogService.cs | 5 ++--- Oqtane.Server/Controllers/JobLogController.cs | 6 +++--- Oqtane.Server/Infrastructure/Jobs/HostedServiceBase.cs | 3 +-- .../Repository/Interfaces/IJobLogRepository.cs | 3 ++- Oqtane.Server/Repository/JobLogRepository.cs | 10 +++++++++- 8 files changed, 24 insertions(+), 18 deletions(-) diff --git a/Oqtane.Client/Modules/Admin/Jobs/Edit.razor b/Oqtane.Client/Modules/Admin/Jobs/Edit.razor index 848ab8e5..1d359f66 100644 --- a/Oqtane.Client/Modules/Admin/Jobs/Edit.razor +++ b/Oqtane.Client/Modules/Admin/Jobs/Edit.razor @@ -45,7 +45,7 @@
- +
diff --git a/Oqtane.Client/Modules/Admin/Jobs/Log.razor b/Oqtane.Client/Modules/Admin/Jobs/Log.razor index 7dbc6a3f..f5453315 100644 --- a/Oqtane.Client/Modules/Admin/Jobs/Log.razor +++ b/Oqtane.Client/Modules/Admin/Jobs/Log.razor @@ -44,14 +44,12 @@ else private async Task GetJobLogs() { - _jobLogs = await JobLogService.GetJobLogsAsync(); - + var jobId = -1; if (PageState.QueryString.ContainsKey("id")) { - _jobLogs = _jobLogs.Where(item => item.JobId == Int32.Parse(PageState.QueryString["id"])).ToList(); + jobId = int.Parse(PageState.QueryString["id"]); } - - _jobLogs = _jobLogs.OrderByDescending(item => item.JobLogId).ToList(); + _jobLogs = await JobLogService.GetJobLogsAsync(jobId); } private string DisplayStatus(bool isExecuting, bool? succeeded) diff --git a/Oqtane.Client/Services/Interfaces/IJobLogService.cs b/Oqtane.Client/Services/Interfaces/IJobLogService.cs index 3f56473b..e6a10a20 100644 --- a/Oqtane.Client/Services/Interfaces/IJobLogService.cs +++ b/Oqtane.Client/Services/Interfaces/IJobLogService.cs @@ -10,10 +10,11 @@ namespace Oqtane.Services public interface IJobLogService { /// - /// Return a list of all entries + /// Return a list of entries /// + /// /// - Task> GetJobLogsAsync(); + Task> GetJobLogsAsync(int jobId); /// /// Return a entry for the given Id diff --git a/Oqtane.Client/Services/JobLogService.cs b/Oqtane.Client/Services/JobLogService.cs index 04c9c534..0d1d3359 100644 --- a/Oqtane.Client/Services/JobLogService.cs +++ b/Oqtane.Client/Services/JobLogService.cs @@ -15,10 +15,9 @@ namespace Oqtane.Services private string Apiurl => CreateApiUrl("JobLog"); - public async Task> GetJobLogsAsync() + public async Task> GetJobLogsAsync(int jobId) { - List joblogs = await GetJsonAsync>(Apiurl); - return joblogs.OrderBy(item => item.StartDate).ToList(); + return await GetJsonAsync>($"{Apiurl}?jobid={jobId}"); } public async Task GetJobLogAsync(int jobLogId) diff --git a/Oqtane.Server/Controllers/JobLogController.cs b/Oqtane.Server/Controllers/JobLogController.cs index 5f711e4f..dac2db68 100644 --- a/Oqtane.Server/Controllers/JobLogController.cs +++ b/Oqtane.Server/Controllers/JobLogController.cs @@ -17,12 +17,12 @@ namespace Oqtane.Controllers _jobLogs = jobLogs; } - // GET: api/ + // GET: api/?jobid=x [HttpGet] [Authorize(Roles = RoleNames.Host)] - public IEnumerable Get() + public IEnumerable Get(string jobid) { - return _jobLogs.GetJobLogs(); + return _jobLogs.GetJobLogs(int.Parse(jobid)); } // GET api//5 diff --git a/Oqtane.Server/Infrastructure/Jobs/HostedServiceBase.cs b/Oqtane.Server/Infrastructure/Jobs/HostedServiceBase.cs index 7f7f440a..655c7b20 100644 --- a/Oqtane.Server/Infrastructure/Jobs/HostedServiceBase.cs +++ b/Oqtane.Server/Infrastructure/Jobs/HostedServiceBase.cs @@ -172,8 +172,7 @@ namespace Oqtane.Infrastructure jobs.UpdateJob(job); // trim the job log - List logs = jobLogs.GetJobLogs().Where(item => item.JobId == job.JobId) - .OrderByDescending(item => item.JobLogId).ToList(); + List logs = jobLogs.GetJobLogs(job.JobId).ToList(); for (int i = logs.Count; i > job.RetentionHistory; i--) { jobLogs.DeleteJobLog(logs[i - 1].JobLogId); diff --git a/Oqtane.Server/Repository/Interfaces/IJobLogRepository.cs b/Oqtane.Server/Repository/Interfaces/IJobLogRepository.cs index d5858da5..2c092e7a 100644 --- a/Oqtane.Server/Repository/Interfaces/IJobLogRepository.cs +++ b/Oqtane.Server/Repository/Interfaces/IJobLogRepository.cs @@ -1,4 +1,4 @@ -using System.Collections.Generic; +using System.Collections.Generic; using Oqtane.Models; namespace Oqtane.Repository @@ -6,6 +6,7 @@ namespace Oqtane.Repository public interface IJobLogRepository { IEnumerable GetJobLogs(); + IEnumerable GetJobLogs(int jobId); JobLog AddJobLog(JobLog jobLog); JobLog UpdateJobLog(JobLog jobLog); JobLog GetJobLog(int jobLogId); diff --git a/Oqtane.Server/Repository/JobLogRepository.cs b/Oqtane.Server/Repository/JobLogRepository.cs index 440a483d..ee234c27 100644 --- a/Oqtane.Server/Repository/JobLogRepository.cs +++ b/Oqtane.Server/Repository/JobLogRepository.cs @@ -1,4 +1,4 @@ -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; using Microsoft.EntityFrameworkCore; using Oqtane.Models; @@ -15,9 +15,17 @@ namespace Oqtane.Repository } public IEnumerable GetJobLogs() + { + return GetJobLogs(-1); + } + + public IEnumerable GetJobLogs(int jobId) { return _db.JobLog + .AsNoTracking() + .Where(item => item.JobId == jobId || jobId == -1) .Include(item => item.Job) // eager load jobs + .OrderByDescending(item => item.JobLogId) .ToList(); }