Skip to content

Commit c772ccd

Browse files
authored
Add new view to Dashboard: Most viewed pages #TECH-185 (#5)
1 parent d2144da commit c772ccd

3 files changed

Lines changed: 132 additions & 20 deletions

File tree

Neolution.OrchardCoreModules.PageViewStats/Controllers/DashboardController.cs

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
namespace Neolution.OrchardCoreModules.PageViewStats.Controllers;
22

33
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
46
using System.Threading.Tasks;
57
using Microsoft.AspNetCore.Authorization;
68
using Microsoft.AspNetCore.Mvc;
79
using Neolution.OrchardCoreModules.PageViewStats.Models;
810
using Neolution.OrchardCoreModules.PageViewStats.Services;
911
using Neolution.OrchardCoreModules.PageViewStats.ViewModels;
1012
using OrchardCore.Admin;
13+
using OrchardCore.Autoroute.Models;
1114
using OrchardCore.ContentManagement;
1215
using OrchardCore.Data;
1316
using OrchardCore.Settings;
@@ -46,20 +49,58 @@ public async Task<ActionResult> Index(int history)
4649
return Forbid();
4750
}
4851

49-
if (history > 30 || history <= 0)
50-
{
51-
// Limit history to maximum number of 30 days
52-
history = 30;
53-
}
54-
5552
var settings = await this.siteService.GetSiteSettingsAsync();
5653
var tz = TimeZoneInfo.FindSystemTimeZoneById(settings.TimeZoneId);
5754
var now = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, tz);
5855
var today = DateOnly.FromDateTime(now);
5956

60-
var pageViews = await this.repository.LoadPageViewsAsync(today.AddDays(history * -1), today);
57+
IList<DailyArchive> pageViews;
58+
if (history == 0)
59+
{
60+
pageViews = await this.repository.LoadAllPageViewsAsync();
61+
}
62+
else
63+
{
64+
if (history > 30 || history <= 1)
65+
{
66+
// Limit history to maximum number of 30 days
67+
history = 30;
68+
}
69+
70+
pageViews = await this.repository.LoadPageViewsAsync(today.AddDays(history * -1), today);
71+
}
6172

62-
var viewModel = new DashboardViewModel { History = history, PageViews = pageViews };
73+
var contentItems = await this.contentManager.GetAsync(pageViews.SelectMany(x => x.PageViews).Select(x => x.ContentItemId));
74+
var contentItemList = contentItems.ToList();
75+
var pageViewsPerSite = new List<PageViewsPerContentItem>();
76+
foreach (var groupedItems in pageViews.SelectMany(x => x.PageViews).GroupBy(x => x.ContentItemId))
77+
{
78+
var pageView = contentItemList.FirstOrDefault(x => x.ContentItemId == groupedItems.Key);
79+
if (pageView == null)
80+
{
81+
continue;
82+
}
83+
84+
var item = new PageViewsPerContentItem
85+
{
86+
ContentItemId = groupedItems.Key,
87+
DisplayText = pageView.DisplayText,
88+
Route = pageView.Get<AutoroutePart>("AutoroutePart"),
89+
BotViews = groupedItems.Sum(g => g.BotViews),
90+
UniqueVisitors = groupedItems.Sum(g => g.UniqueVisitors),
91+
TotalViews = groupedItems.Sum(g => g.TotalViews),
92+
};
93+
94+
pageViewsPerSite.Add(item);
95+
}
96+
97+
98+
var viewModel = new DashboardViewModel
99+
{
100+
History = history,
101+
PageViewsByDay = pageViews,
102+
PageViewsBySite = pageViewsPerSite
103+
};
63104

64105
return View(viewModel);
65106
}

Neolution.OrchardCoreModules.PageViewStats/ViewModels/DashboardViewModel.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
public class DashboardViewModel
77
{
88
public int History { get; set; }
9-
public IList<DailyArchive> PageViews { get; set; }
9+
public IList<DailyArchive> PageViewsByDay { get; set; }
10+
public List<PageViewsPerContentItem> PageViewsBySite { get; set; }
1011
}
Lines changed: 81 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,46 @@
11
@model Neolution.OrchardCoreModules.PageViewStats.ViewModels.DashboardViewModel
22

3+
@functions
4+
{
5+
string GetDropDownButtonText()
6+
{
7+
return Model.History > 0 ? $"Last {Model.History} days" : "Since ever";
8+
}
9+
}
10+
311
<div class="row">
412
<div class="col">
513
<h1>Page View Statistics</h1>
6-
<div class="form-group d-inline-flex float-right mb-1">
14+
</div>
15+
</div>
16+
17+
<ul class="nav nav-tabs">
18+
<li class="nav-item pr-md-2">
19+
<a class="nav-link active" id="byday-tab" data-bs-toggle="tab" href="#byday" role="tab" aria-controls="byday" aria-selected="true">@T["By day"]</a>
20+
</li>
21+
<li class="nav-item pr-md-2">
22+
<a class="nav-link" id="bysite-tab" data-bs-toggle="tab" href="#bysite" role="tab" aria-controls="bysite">@T["By site"]</a>
23+
</li>
24+
</ul>
25+
26+
<div class="tab-content" id="tabs">
27+
28+
<div class="tab-pane fade show active" id="byday" role="tabpanel" aria-labelledby="byday-tab">
29+
<div class="form-group d-inline-flex float-end mb-1">
730
<div class="btn-group">
831
<div class="dropdown order-md-1">
932
<button type="button" class="btn btn-secondary btn-sm dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
10-
Last @Model.History days
33+
@GetDropDownButtonText()
1134
</button>
1235
<ul class="dropdown-menu">
13-
<li><a class="dropdown-item" href="?history=7">Last week</a></li>
14-
<li><a class="dropdown-item" href="?history=14">Last two weeks</a></li>
15-
<li><a class="dropdown-item" href="?history=21">Last three weeks</a></li>
16-
<li><a class="dropdown-item" href="?history=30">Last month</a></li>
36+
<li><a class="dropdown-item" href="?history=7#byday">Last week</a></li>
37+
<li><a class="dropdown-item" href="?history=14#byday">Last two weeks</a></li>
38+
<li><a class="dropdown-item" href="?history=21#byday">Last three weeks</a></li>
39+
<li><a class="dropdown-item" href="?history=30#byday">Last month</a></li>
1740
</ul>
1841
</div>
1942
</div>
2043
</div>
21-
</div>
22-
</div>
23-
<div class="row">
24-
<div class="col">
2544
<table class="table table-sm table-striped table-hover">
2645
<thead>
2746
<tr>
@@ -33,7 +52,7 @@
3352
</tr>
3453
</thead>
3554
<tbody>
36-
@foreach (var item in Model.PageViews)
55+
@foreach (var item in Model.PageViewsByDay)
3756
{
3857
<tr>
3958
<td>@item.Id</td>
@@ -46,4 +65,55 @@
4665
</tbody>
4766
</table>
4867
</div>
68+
<div class="tab-pane fade" id="bysite" role="tabpanel" aria-labelledby="bysite-tab">
69+
<div class="form-group d-inline-flex float-end mb-1">
70+
<div class="btn-group">
71+
<div class="dropdown order-md-1">
72+
<button type="button" class="btn btn-secondary btn-sm dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
73+
@GetDropDownButtonText()
74+
</button>
75+
<ul class="dropdown-menu">
76+
<li><a class="dropdown-item" href="?history=7#bysite">Last week</a></li>
77+
<li><a class="dropdown-item" href="?history=14#bysite">Last two weeks</a></li>
78+
<li><a class="dropdown-item" href="?history=21#bysite">Last three weeks</a></li>
79+
<li><a class="dropdown-item" href="?history=30#bysite">Last month</a></li>
80+
<li><a class="dropdown-item" href="?history=0#bysite">Since ever</a></li>
81+
</ul>
82+
</div>
83+
</div>
84+
</div>
85+
<table class="table table-sm table-striped table-hover">
86+
<thead>
87+
<tr>
88+
<th>ContentItemId</th>
89+
<th>Page Title</th>
90+
<th>Route</th>
91+
<th>Views</th>
92+
<th>Bot Views</th>
93+
<th>Unique Visitors</th>
94+
</tr>
95+
</thead>
96+
<tbody>
97+
@foreach (var item in Model.PageViewsBySite)
98+
{
99+
<tr>
100+
<td>@item.ContentItemId</td>
101+
<td>@item.DisplayText</td>
102+
<td>/@item.Route.Path</td>
103+
<td>@item.TotalViews</td>
104+
<td>@item.BotViews</td>
105+
<td>@item.UniqueVisitors</td>
106+
</tr>
107+
}
108+
</tbody>
109+
</table>
110+
</div>
49111
</div>
112+
113+
<script at="Foot">
114+
$('document').ready(function () {
115+
let selectedTab = window.location.hash;
116+
let tabTrigger = new bootstrap.Tab($('.nav-link[href="' + selectedTab + '"]:first'));
117+
tabTrigger.show();
118+
});
119+
</script>

0 commit comments

Comments
 (0)