-
Notifications
You must be signed in to change notification settings - Fork 306
Expand file tree
/
Copy pathSystemLogsChatDataQuery.cs
More file actions
56 lines (45 loc) · 2.18 KB
/
SystemLogsChatDataQuery.cs
File metadata and controls
56 lines (45 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using CleanArchitecture.Blazor.Application.Features.SystemLogs.Caching;
using CleanArchitecture.Blazor.Application.Features.SystemLogs.DTOs;
namespace CleanArchitecture.Blazor.Application.Features.SystemLogs.Queries.ChatData;
public class SystemLogsTimeLineChatDataQuery : ICacheableRequest<List<SystemLogTimeLineDto>>
{
public DateTime LastDateTime { get; set; } = DateTime.Now.AddDays(-60);
public string CacheKey => SystemLogsCacheKey.GetChartDataCacheKey(LastDateTime.ToString());
public IEnumerable<string>? Tags => SystemLogsCacheKey.Tags;
}
public class SystemLogsChatDataQueryHandler : IRequestHandler<SystemLogsTimeLineChatDataQuery, List<SystemLogTimeLineDto>>
{
private readonly IApplicationDbContext _context;
private readonly IStringLocalizer<SystemLogsChatDataQueryHandler> _localizer;
public SystemLogsChatDataQueryHandler(
IApplicationDbContext context,
IStringLocalizer<SystemLogsChatDataQueryHandler> localizer
)
{
_context = context;
_localizer = localizer;
}
public async Task<List<SystemLogTimeLineDto>> Handle(SystemLogsTimeLineChatDataQuery request,
CancellationToken cancellationToken)
{
var data = await _context.SystemLogs.Where(x => x.TimeStamp >= request.LastDateTime)
.GroupBy(x => new { x.TimeStamp.Date })
.Select(x => new { x.Key.Date, Total = x.Count() })
.OrderBy(x => x.Date)
.ToListAsync(cancellationToken);
List<SystemLogTimeLineDto> result = new();
DateTime end = new(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0);
var start = request.LastDateTime.Date;
while (start <= end)
{
var item = data.FirstOrDefault(x => x.Date == start.Date);
result.Add(item != null
? new SystemLogTimeLineDto { dt = item.Date, total = item.Total }
: new SystemLogTimeLineDto { dt = start, total = 0 });
start = start.AddDays(1);
}
return result.OrderBy(x => x.dt).ToList();
}
}