Browse Source

Merge pull request #1001 from colinin/TaskLogSpecification

feat(task-management): Use specification to query the list of job logs
pull/1010/head
yx lin 1 year ago
committed by GitHub
parent
commit
5de5e7ba86
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 42
      aspnet-core/modules/task-management/LINGYUN.Abp.TaskManagement.Application/LINGYUN/Abp/TaskManagement/BackgroundJobLogAppService.cs
  2. 32
      aspnet-core/modules/task-management/LINGYUN.Abp.TaskManagement.Application/System/Linq/Expressions/ExpressionFuncExtensions.cs
  3. 35
      aspnet-core/modules/task-management/LINGYUN.Abp.TaskManagement.Domain/LINGYUN/Abp/TaskManagement/BackgroundJobLogFilter.cs
  4. 18
      aspnet-core/modules/task-management/LINGYUN.Abp.TaskManagement.Domain/LINGYUN/Abp/TaskManagement/IBackgroundJobLogRepository.cs
  5. 31
      aspnet-core/modules/task-management/LINGYUN.Abp.TaskManagement.EntityFrameworkCore/LINGYUN/Abp/TaskManagement/EntityFrameworkCore/EfCoreBackgroundJobLogRepository.cs

42
aspnet-core/modules/task-management/LINGYUN.Abp.TaskManagement.Application/LINGYUN/Abp/TaskManagement/BackgroundJobLogAppService.cs

@ -1,6 +1,8 @@
using LINGYUN.Abp.TaskManagement.Permissions; using LINGYUN.Abp.TaskManagement.Permissions;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks; using System.Threading.Tasks;
using Volo.Abp.Application.Dtos; using Volo.Abp.Application.Dtos;
@ -32,22 +34,38 @@ public class BackgroundJobLogAppService : TaskManagementApplicationService, IBac
public async virtual Task<PagedResultDto<BackgroundJobLogDto>> GetListAsync(BackgroundJobLogGetListInput input) public async virtual Task<PagedResultDto<BackgroundJobLogDto>> GetListAsync(BackgroundJobLogGetListInput input)
{ {
var filter = new BackgroundJobLogFilter var specification = new BackgroundJobLogGetListSpecification(input);
{
BeginRunTime = input.BeginRunTime,
EndRunTime = input.EndRunTime,
HasExceptions = input.HasExceptions,
Filter = input.Filter,
Group = input.Group,
Name = input.Name,
Type = input.Type
};
var totalCount = await BackgroundJobLogRepository.GetCountAsync(filter, input.JobId); var totalCount = await BackgroundJobLogRepository.GetCountAsync(specification);
var backgroundJobLogs = await BackgroundJobLogRepository.GetListAsync( var backgroundJobLogs = await BackgroundJobLogRepository.GetListAsync(
filter, input.JobId, input.Sorting, input.MaxResultCount, input.SkipCount); specification, input.Sorting, input.MaxResultCount, input.SkipCount);
return new PagedResultDto<BackgroundJobLogDto>(totalCount, return new PagedResultDto<BackgroundJobLogDto>(totalCount,
ObjectMapper.Map<List<BackgroundJobLog>, List<BackgroundJobLogDto>>(backgroundJobLogs)); ObjectMapper.Map<List<BackgroundJobLog>, List<BackgroundJobLogDto>>(backgroundJobLogs));
} }
private class BackgroundJobLogGetListSpecification : Volo.Abp.Specifications.Specification<BackgroundJobLog>
{
protected BackgroundJobLogGetListInput Input { get; }
public BackgroundJobLogGetListSpecification(BackgroundJobLogGetListInput input)
{
Input = input;
}
public override Expression<Func<BackgroundJobLog, bool>> ToExpression()
{
Expression<Func<BackgroundJobLog, bool>> expression = _ => true;
return expression
.AndIf(!Input.JobId.IsNullOrWhiteSpace(), x => x.JobId.Equals(Input.JobId))
.AndIf(!Input.Type.IsNullOrWhiteSpace(), x => x.JobType.Contains(Input.Type))
.AndIf(!Input.Group.IsNullOrWhiteSpace(), x => x.JobGroup.Equals(Input.Group))
.AndIf(!Input.Name.IsNullOrWhiteSpace(), x => x.JobName.Equals(Input.Name))
.AndIf(!Input.Filter.IsNullOrWhiteSpace(), x => x.JobName.Contains(Input.Filter) ||
x.JobGroup.Contains(Input.Filter) || x.JobType.Contains(Input.Filter) || x.Message.Contains(Input.Filter))
.AndIf(Input.HasExceptions.HasValue, x => !string.IsNullOrWhiteSpace(x.Exception))
.AndIf(Input.BeginRunTime.HasValue, x => x.RunTime >= Input.BeginRunTime)
.AndIf(Input.EndRunTime.HasValue, x => x.RunTime <= Input.EndRunTime);
}
}
} }

32
aspnet-core/modules/task-management/LINGYUN.Abp.TaskManagement.Application/System/Linq/Expressions/ExpressionFuncExtensions.cs

@ -0,0 +1,32 @@
using Volo.Abp.Specifications;
namespace System.Linq.Expressions;
internal static class ExpressionFuncExtensions
{
public static Expression<Func<T, bool>> AndIf<T>(
this Expression<Func<T, bool>> first,
bool condition,
Expression<Func<T, bool>> second)
{
if (condition)
{
return ExpressionFuncExtender.And(first, second);
}
return first;
}
public static Expression<Func<T, bool>> OrIf<T>(
this Expression<Func<T, bool>> first,
bool condition,
Expression<Func<T, bool>> second)
{
if (condition)
{
return ExpressionFuncExtender.Or(first, second);
}
return first;
}
}

35
aspnet-core/modules/task-management/LINGYUN.Abp.TaskManagement.Domain/LINGYUN/Abp/TaskManagement/BackgroundJobLogFilter.cs

@ -1,35 +0,0 @@
using System;
namespace LINGYUN.Abp.TaskManagement;
public class BackgroundJobLogFilter
{
/// <summary>
/// 其他过滤条件
/// </summary>
public string Filter { get; set; }
/// <summary>
/// 存在异常
/// </summary>
public bool? HasExceptions { get; set; }
/// <summary>
/// 任务名称
/// </summary>
public string Name { get; set; }
/// <summary>
/// 任务分组
/// </summary>
public string Group { get; set; }
/// <summary>
/// 任务类型
/// </summary>
public string Type { get; set; }
/// <summary>
/// 开始触发时间
/// </summary>
public DateTime? BeginRunTime { get; set; }
/// <summary>
/// 结束触发时间
/// </summary>
public DateTime? EndRunTime { get; set; }
}

18
aspnet-core/modules/task-management/LINGYUN.Abp.TaskManagement.Domain/LINGYUN/Abp/TaskManagement/IBackgroundJobLogRepository.cs

@ -1,8 +1,8 @@
using System; using System.Collections.Generic;
using System.Collections.Generic;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories; using Volo.Abp.Domain.Repositories;
using Volo.Abp.Specifications;
namespace LINGYUN.Abp.TaskManagement; namespace LINGYUN.Abp.TaskManagement;
@ -11,28 +11,24 @@ public interface IBackgroundJobLogRepository : IRepository<BackgroundJobLog, lon
/// <summary> /// <summary>
/// 获取过滤后的任务日志数量 /// 获取过滤后的任务日志数量
/// </summary> /// </summary>
/// <param name="filter"></param> /// <param name="specification"></param>
/// <param name="jobId"></param>
/// <param name="cancellationToken"></param> /// <param name="cancellationToken"></param>
/// <returns></returns> /// <returns></returns>
Task<int> GetCountAsync( Task<int> GetCountAsync(
BackgroundJobLogFilter filter, ISpecification<BackgroundJobLog> specification,
string jobId = null,
CancellationToken cancellationToken = default); CancellationToken cancellationToken = default);
/// <summary> /// <summary>
/// 获取过滤后的任务日志列表 /// 获取过滤后的任务日志列表
/// </summary> /// </summary>
/// <param name="filter"></param> /// <param name="specification"></param>
/// <param name="jobId"></param>
/// <param name="sorting"></param> /// <param name="sorting"></param>
/// <param name="maxResultCount"></param> /// <param name="maxResultCount"></param>
/// <param name="skipCount"></param> /// <param name="skipCount"></param>
/// <param name="cancellationToken"></param> /// <param name="cancellationToken"></param>
/// <returns></returns> /// <returns></returns>
Task<List<BackgroundJobLog>> GetListAsync( Task<List<BackgroundJobLog>> GetListAsync(
BackgroundJobLogFilter filter, ISpecification<BackgroundJobLog> specification,
string jobId = null, string sorting = $"{nameof(BackgroundJobLog.RunTime)} DESC",
string sorting = nameof(BackgroundJobLog.RunTime),
int maxResultCount = 10, int maxResultCount = 10,
int skipCount = 0, int skipCount = 0,
CancellationToken cancellationToken = default); CancellationToken cancellationToken = default);

31
aspnet-core/modules/task-management/LINGYUN.Abp.TaskManagement.EntityFrameworkCore/LINGYUN/Abp/TaskManagement/EntityFrameworkCore/EfCoreBackgroundJobLogRepository.cs

@ -7,6 +7,7 @@ using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore; using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore; using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.Specifications;
namespace LINGYUN.Abp.TaskManagement.EntityFrameworkCore; namespace LINGYUN.Abp.TaskManagement.EntityFrameworkCore;
@ -21,45 +22,27 @@ public class EfCoreBackgroundJobLogRepository :
} }
public async virtual Task<int> GetCountAsync( public async virtual Task<int> GetCountAsync(
BackgroundJobLogFilter filter, ISpecification<BackgroundJobLog> specification,
string jobId = null,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
return await (await GetDbSetAsync()) return await (await GetDbSetAsync())
.WhereIf(!jobId.IsNullOrWhiteSpace(), x => x.JobId.Equals(jobId)) .Where(specification.ToExpression())
.WhereIf(!filter.Type.IsNullOrWhiteSpace(), x => x.JobType.Contains(filter.Type))
.WhereIf(!filter.Group.IsNullOrWhiteSpace(), x => x.JobGroup.Equals(filter.Group))
.WhereIf(!filter.Name.IsNullOrWhiteSpace(), x => x.JobName.Equals(filter.Name))
.WhereIf(!filter.Filter.IsNullOrWhiteSpace(), x => x.JobName.Contains(filter.Filter) ||
x.JobGroup.Contains(filter.Filter) || x.JobType.Contains(filter.Filter) || x.Message.Contains(filter.Filter))
.WhereIf(filter.HasExceptions.HasValue, x => !string.IsNullOrWhiteSpace(x.Exception))
.WhereIf(filter.BeginRunTime.HasValue, x => x.RunTime.CompareTo(filter.BeginRunTime.Value) >= 0)
.WhereIf(filter.EndRunTime.HasValue, x => x.RunTime.CompareTo(filter.EndRunTime.Value) <= 0)
.CountAsync(GetCancellationToken(cancellationToken)); .CountAsync(GetCancellationToken(cancellationToken));
} }
public async virtual Task<List<BackgroundJobLog>> GetListAsync( public async virtual Task<List<BackgroundJobLog>> GetListAsync(
BackgroundJobLogFilter filter, ISpecification<BackgroundJobLog> specification,
string jobId = null, string sorting = $"{nameof(BackgroundJobLog.RunTime)} DESC",
string sorting = nameof(BackgroundJobLog.RunTime),
int maxResultCount = 10, int maxResultCount = 10,
int skipCount = 0, int skipCount = 0,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
if (sorting.IsNullOrWhiteSpace()) if (sorting.IsNullOrWhiteSpace())
{ {
sorting = $"{nameof(BackgroundJobLog.RunTime)}"; sorting = $"{nameof(BackgroundJobLog.RunTime)} DESC";
} }
return await (await GetDbSetAsync()) return await (await GetDbSetAsync())
.WhereIf(!jobId.IsNullOrWhiteSpace(), x => x.JobId.Equals(jobId)) .Where(specification.ToExpression())
.WhereIf(!filter.Type.IsNullOrWhiteSpace(), x => x.JobType.Contains(filter.Type))
.WhereIf(!filter.Group.IsNullOrWhiteSpace(), x => x.JobGroup.Equals(filter.Group))
.WhereIf(!filter.Name.IsNullOrWhiteSpace(), x => x.JobName.Equals(filter.Name))
.WhereIf(!filter.Filter.IsNullOrWhiteSpace(), x => x.JobName.Contains(filter.Filter) ||
x.JobGroup.Contains(filter.Filter) || x.JobType.Contains(filter.Filter) || x.Message.Contains(filter.Filter))
.WhereIf(filter.HasExceptions.HasValue, x => !string.IsNullOrWhiteSpace(x.Exception))
.WhereIf(filter.BeginRunTime.HasValue, x => x.RunTime.CompareTo(filter.BeginRunTime.Value) >= 0)
.WhereIf(filter.EndRunTime.HasValue, x => x.RunTime.CompareTo(filter.EndRunTime.Value) <= 0)
.OrderBy(sorting) .OrderBy(sorting)
.PageBy(skipCount, maxResultCount) .PageBy(skipCount, maxResultCount)
.ToListAsync(GetCancellationToken(cancellationToken)); .ToListAsync(GetCancellationToken(cancellationToken));

Loading…
Cancel
Save