feat: 新增请求访问统计功能

This commit is contained in:
橙子
2024-08-14 22:23:54 +08:00
parent b619204c5e
commit 9af98089f2
6 changed files with 135 additions and 92 deletions

View File

@@ -5,6 +5,7 @@ using Quartz;
using Volo.Abp.BackgroundWorkers.Quartz;
using Volo.Abp.Caching;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Entities;
using Yi.Framework.Bbs.Domain.Entities;
using Yi.Framework.Bbs.Domain.Shared.Caches;
using Yi.Framework.Bbs.Domain.Shared.Enums;
@@ -15,7 +16,7 @@ namespace Yi.Framework.Bbs.Application.Jobs;
public class AccessLogStoreJob : QuartzBackgroundWorkerBase
{
private readonly ISqlSugarRepository<AccessLogAggregateRoot> _repository;
/// <summary>
/// 缓存前缀
/// </summary>
@@ -46,10 +47,13 @@ public class AccessLogStoreJob : QuartzBackgroundWorkerBase
_repository = repository;
JobDetail = JobBuilder.Create<AccessLogStoreJob>().WithIdentity(nameof(AccessLogStoreJob))
.Build();
//每分钟执行一次
Trigger = TriggerBuilder.Create().WithIdentity(nameof(AccessLogStoreJob))
.WithCronSchedule("* * * * *")
.WithCronSchedule("0 * * * * ?")
.Build();
}
public override async Task Execute(IJobExecutionContext context)
@@ -64,9 +68,20 @@ public class AccessLogStoreJob : QuartzBackgroundWorkerBase
var entity = await _repository._DbQueryable.Where(x => x.AccessLogType == AccessLogTypeEnum.Request)
.Where(x => x.CreationTime.Date == DateTime.Today)
.FirstAsync();
// _repository._Db.Storageable(list2).ExecuteCommandAsync();
if (entity is not null)
{
entity.Number = number+1;
await _repository.UpdateAsync(entity);
}
else
{
await _repository.InsertAsync((new AccessLogAggregateRoot() { Number = number,AccessLogType = AccessLogTypeEnum.Request}));
}
//删除前一天的缓存
await RedisClient.DelAsync($"{CacheKeyPrefix}:{AccessLogCacheConst.Key}:{DateTime.Now.Date.AddDays(-1)}");
}
}
}

View File

@@ -55,9 +55,9 @@ namespace Yi.Framework.Bbs.Application.Services
/// </summary>
/// <param name="AccessLogType"></param>
/// <returns></returns>
public async Task<List<AccessLogDto>> GetListAsync([FromQuery] AccessLogTypeEnum AccessLogType)
public async Task<List<AccessLogDto>> GetListAsync([FromQuery] AccessLogTypeEnum accessLogType)
{
var entities = await _repository._DbQueryable.Where(x => x.AccessLogType == AccessLogType)
var entities = await _repository._DbQueryable.Where(x => x.AccessLogType == accessLogType)
.Where(x => x.CreationTime >= DateTime.Now.AddMonths(-3))
.OrderBy(x => x.CreationTime).ToListAsync();
var output = entities.Adapt<List<AccessLogDto>>();
@@ -73,11 +73,11 @@ namespace Yi.Framework.Bbs.Application.Services
public async Task AccessAsync()
{
//可判断http重复防止同一ip多次访问
var last = await _repository._DbQueryable.OrderByDescending(x => x.CreationTime).FirstAsync();
var last = await _repository._DbQueryable.Where(x=>x.AccessLogType==AccessLogTypeEnum.HomeClick).OrderByDescending(x => x.CreationTime).FirstAsync();
if (last is null || last.CreationTime.Date != DateTime.Today)
{
await _repository.InsertAsync(new AccessLogAggregateRoot());
await _repository.InsertAsync(new AccessLogAggregateRoot(){AccessLogType=AccessLogTypeEnum.HomeClick});
}
else
{
@@ -90,10 +90,10 @@ namespace Yi.Framework.Bbs.Application.Services
/// 获取当前周首页点击数据
/// </summary>
/// <returns></returns>
public async Task<AccessLogDto[]> GetWeekAsync()
public async Task<AccessLogDto[]> GetWeekAsync([FromQuery] AccessLogTypeEnum accessLogType)
{
var lastSeven = await _repository._DbQueryable
.Where(x => x.AccessLogType == AccessLogTypeEnum.HomeClick)
.Where(x => x.AccessLogType == accessLogType)
.OrderByDescending(x => x.CreationTime).ToPageListAsync(1, 7);
return WeekTimeHandler(lastSeven.ToArray());