Skip to content

Commit

Permalink
feat(task-management): Use specification to query the list of job logs
Browse files Browse the repository at this point in the history
  • Loading branch information
colinin committed Sep 5, 2024
1 parent 3f0c410 commit 98e9f94
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 84 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using LINGYUN.Abp.TaskManagement.Permissions;
using Microsoft.AspNetCore.Authorization;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;

Expand Down Expand Up @@ -32,22 +34,38 @@ public async virtual Task<BackgroundJobLogDto> GetAsync(long id)

public async virtual Task<PagedResultDto<BackgroundJobLogDto>> GetListAsync(BackgroundJobLogGetListInput input)
{
var filter = new BackgroundJobLogFilter
{
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 specification = new BackgroundJobLogGetListSpecification(input);

var totalCount = await BackgroundJobLogRepository.GetCountAsync(specification);
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,
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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Specifications;

namespace LINGYUN.Abp.TaskManagement;

Expand All @@ -11,28 +11,24 @@ public interface IBackgroundJobLogRepository : IRepository<BackgroundJobLog, lon
/// <summary>
/// 获取过滤后的任务日志数量
/// </summary>
/// <param name="filter"></param>
/// <param name="jobId"></param>
/// <param name="specification"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<int> GetCountAsync(
BackgroundJobLogFilter filter,
string jobId = null,
ISpecification<BackgroundJobLog> specification,
CancellationToken cancellationToken = default);
/// <summary>
/// 获取过滤后的任务日志列表
/// </summary>
/// <param name="filter"></param>
/// <param name="jobId"></param>
/// <param name="specification"></param>
/// <param name="sorting"></param>
/// <param name="maxResultCount"></param>
/// <param name="skipCount"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<List<BackgroundJobLog>> GetListAsync(
BackgroundJobLogFilter filter,
string jobId = null,
string sorting = nameof(BackgroundJobLog.RunTime),
ISpecification<BackgroundJobLog> specification,
string sorting = $"{nameof(BackgroundJobLog.RunTime)} DESC",
int maxResultCount = 10,
int skipCount = 0,
CancellationToken cancellationToken = default);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.Specifications;

namespace LINGYUN.Abp.TaskManagement.EntityFrameworkCore;

Expand All @@ -21,45 +22,27 @@ public EfCoreBackgroundJobLogRepository(
}

public async virtual Task<int> GetCountAsync(
BackgroundJobLogFilter filter,
string jobId = null,
ISpecification<BackgroundJobLog> specification,
CancellationToken cancellationToken = default)
{
return await (await GetDbSetAsync())
.WhereIf(!jobId.IsNullOrWhiteSpace(), x => x.JobId.Equals(jobId))
.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)
.Where(specification.ToExpression())
.CountAsync(GetCancellationToken(cancellationToken));
}

public async virtual Task<List<BackgroundJobLog>> GetListAsync(
BackgroundJobLogFilter filter,
string jobId = null,
string sorting = nameof(BackgroundJobLog.RunTime),
ISpecification<BackgroundJobLog> specification,
string sorting = $"{nameof(BackgroundJobLog.RunTime)} DESC",
int maxResultCount = 10,
int skipCount = 0,
CancellationToken cancellationToken = default)
{
if (sorting.IsNullOrWhiteSpace())
{
sorting = $"{nameof(BackgroundJobLog.RunTime)}";
sorting = $"{nameof(BackgroundJobLog.RunTime)} DESC";
}
return await (await GetDbSetAsync())
.WhereIf(!jobId.IsNullOrWhiteSpace(), x => x.JobId.Equals(jobId))
.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)
.Where(specification.ToExpression())
.OrderBy(sorting)
.PageBy(skipCount, maxResultCount)
.ToListAsync(GetCancellationToken(cancellationToken));
Expand Down

0 comments on commit 98e9f94

Please sign in to comment.