-
-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathVisitCountPerPage.razor
More file actions
126 lines (112 loc) · 4.17 KB
/
Copy pathVisitCountPerPage.razor
File metadata and controls
126 lines (112 loc) · 4.17 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
@using System.Linq.Expressions
@using LinkDotNet.Blog.Domain
@using LinkDotNet.Blog.Infrastructure
@using LinkDotNet.Blog.Infrastructure.Persistence
@using System.Collections.Immutable
@using System.Diagnostics
@using Microsoft.Extensions.Logging
@inject IRepository<BlogPost> BlogPostRepository
@inject IRepository<BlogPostRecord> BlogPostRecordRepository
@inject ILogger<VisitCountPerPage> Logger;
<div class="card">
<div class="card-header">Blog Post Visit Counter</div>
<div class="card-body">
<div class="row">
<DateRangeSelector FilterChanged="RefreshVisitCount"></DateRangeSelector>
@if (visitData is not null)
{
<p id="total-clicks">@visitData.Sum(c => c.ClickCount) clicks in total</p>
}
else
{
<p id="total-clicks">Loading...</p>
}
<table class="table table-striped table-hover">
<tbody>
<tr>
<th>Title</th>
<th>Clicks</th>
<th>Likes</th>
</tr>
@if (visitData is not null)
{
@foreach (var date in visitData)
{
<tr>
<td><a class="link" href="blogPost/@date.Id">@date.Title</a></td>
<td>@date.ClickCount</td>
<td>@date.Likes</td>
</tr>
}
}
else
{
<Loading></Loading>
}
</tbody>
</table>
</div>
</div>
</div>
@code {
private Filter filter = new();
private IReadOnlyCollection<VisitCountPageData> visitData = [];
protected override async Task OnInitializedAsync()
{
visitData = await LoadBlogPostInformationAsync();
}
private static async ValueTask<IPagedList<BlogPostVisitData>> GetRelevantBlogPostsAsync(
IRepository<BlogPost> blogPostRepository,
Filter filter)
{
Expression<Func<BlogPost, bool>>? blogPostFilter = filter.EndDate.HasValue
? bp => bp.UpdatedDate.Date <= filter.EndDate.Value.ToDateTime(default)
: null;
return await blogPostRepository.GetAllByProjectionAsync(
s => new BlogPostVisitData(s.Id, s.Title, s.Likes),
filter: blogPostFilter,
orderBy: o => o.UpdatedDate);
}
private static async Task<IPagedList<BlogPostRecord>> GetRelevantBlogPostRecords(
IRepository<BlogPostRecord> blogPostRecordRepository,
Filter filter)
{
return await blogPostRecordRepository.GetAllAsync(
ur => (!filter.StartDate.HasValue || ur.DateClicked >= filter.StartDate)
&& (!filter.EndDate.HasValue || ur.DateClicked <= filter.EndDate),
orderBy: o => o.DateClicked);
}
private async Task<IReadOnlyCollection<VisitCountPageData>> LoadBlogPostInformationAsync()
{
var stopwatch = Stopwatch.StartNew();
var blogPosts = await GetRelevantBlogPostsAsync(BlogPostRepository, filter);
var userRecords = await GetRelevantBlogPostRecords(BlogPostRecordRepository, filter);
var time = stopwatch.ElapsedMilliseconds;
Logger.LogDebug("Retrieving data took {ElapsedMilliseconds}ms", time);
var blogPostDictionary = blogPosts.ToDictionary(bp => bp.Id, bp => bp);
var loadBlogPostInformation = userRecords
.GroupBy(s => s.BlogPostId)
.Where(s => blogPostDictionary.ContainsKey(s.Key))
.Select(s => new
{
Count = s.Sum(l => l.Clicks),
BlogPost = blogPostDictionary[s.Key]
})
.OrderByDescending(s => s.Count)
.Select(s => new VisitCountPageData
{
Id = s.BlogPost.Id,
Likes = s.BlogPost.Likes,
Title = s.BlogPost.Title,
ClickCount = s.Count,
}).ToImmutableArray();
Logger.LogDebug("Processing data took {ElapsedMilliseconds}ms", stopwatch.ElapsedMilliseconds - time);
return loadBlogPostInformation;
}
private async Task RefreshVisitCount(Filter newBeginning)
{
filter = newBeginning;
visitData = await LoadBlogPostInformationAsync();
}
private record struct BlogPostVisitData(string Id, string Title, int Likes);
}