From 1c4921c9a894421f556bb3fcb871f554a9c9f336 Mon Sep 17 00:00:00 2001 From: Ludvig Hagmar <--global> Date: Mon, 14 Oct 2024 17:59:15 +0200 Subject: [PATCH 1/4] Fix bug in middleware and allow to filter statistics for logged in --- .../Controllers/StatisticsController.cs | 13 ++++- src/AKCore/Middlewares/MetricsMiddleware.cs | 1 + .../Statistics/StatisticsApp.vue | 50 +++++++++++++++++-- 3 files changed, 60 insertions(+), 4 deletions(-) diff --git a/src/AKCore/Controllers/StatisticsController.cs b/src/AKCore/Controllers/StatisticsController.cs index a3c56ab9..e54f0449 100644 --- a/src/AKCore/Controllers/StatisticsController.cs +++ b/src/AKCore/Controllers/StatisticsController.cs @@ -24,10 +24,21 @@ public ActionResult Index() } [Route("Model")] - public async Task GetModel() + public async Task GetModel(bool loggedIn = true, bool loggedOut = true) { + var all = loggedIn && loggedOut; + if(!loggedIn && !loggedOut) { + return Json(new + { + items = new List(), + dates = new List() + }); + } + + var dataItems = await db.RequestsDatas .Where(x => x.Created > DateTime.UtcNow.AddDays(-30)) + .Where(x => all || x.LoggedIn == loggedIn) .OrderBy(x => x.Created) .ToListAsync(); diff --git a/src/AKCore/Middlewares/MetricsMiddleware.cs b/src/AKCore/Middlewares/MetricsMiddleware.cs index f41b971a..31f24b54 100644 --- a/src/AKCore/Middlewares/MetricsMiddleware.cs +++ b/src/AKCore/Middlewares/MetricsMiddleware.cs @@ -68,6 +68,7 @@ private async Task SetupInterval() await metricsService.SaveMetrics(loggedOutRouteRequests, false, nowTime); } loggedInRouteRequests = []; + loggedOutRouteRequests = []; } catch { diff --git a/src/AKCore/Scripts/VueComponents/Statistics/StatisticsApp.vue b/src/AKCore/Scripts/VueComponents/Statistics/StatisticsApp.vue index 759d3a3f..c640f1c7 100644 --- a/src/AKCore/Scripts/VueComponents/Statistics/StatisticsApp.vue +++ b/src/AKCore/Scripts/VueComponents/Statistics/StatisticsApp.vue @@ -1,11 +1,33 @@  From 564414899136fe028406476d6e4ba243f0d2cf4d Mon Sep 17 00:00:00 2001 From: Ludvig Hagmar <--global> Date: Mon, 14 Oct 2024 18:15:07 +0200 Subject: [PATCH 2/4] Range stuff BE --- .../Controllers/StatisticsController.cs | 19 +++-- src/AKCore/Models/StatisticsModels.cs | 9 +- .../Statistics/PageViewGraph.vue | 84 +++++++++++++++++++ .../Statistics/StatisticsApp.vue | 81 ++---------------- 4 files changed, 111 insertions(+), 82 deletions(-) create mode 100644 src/AKCore/Scripts/VueComponents/Statistics/PageViewGraph.vue diff --git a/src/AKCore/Controllers/StatisticsController.cs b/src/AKCore/Controllers/StatisticsController.cs index e54f0449..db50a816 100644 --- a/src/AKCore/Controllers/StatisticsController.cs +++ b/src/AKCore/Controllers/StatisticsController.cs @@ -24,7 +24,7 @@ public ActionResult Index() } [Route("Model")] - public async Task GetModel(bool loggedIn = true, bool loggedOut = true) + public async Task GetModel(bool loggedIn = true, bool loggedOut = true, StatisticsRange range = StatisticsRange.Day) { var all = loggedIn && loggedOut; if(!loggedIn && !loggedOut) { @@ -35,9 +35,8 @@ public async Task GetModel(bool loggedIn = true, bool loggedOut = }); } - var dataItems = await db.RequestsDatas - .Where(x => x.Created > DateTime.UtcNow.AddDays(-30)) + .Where(x => x.Created > GetRangeCompare(range)) .Where(x => all || x.LoggedIn == loggedIn) .OrderBy(x => x.Created) .ToListAsync(); @@ -48,7 +47,8 @@ public async Task GetModel(bool loggedIn = true, bool loggedOut = .ToList(); var groupedItems = dataItems.GroupBy(x => x.Path) - .Select(x => new { Path = x.Key, Items = NormalizeItems(x, dates) }); + .Select(x => new { Path = x.Key, Items = NormalizeItems(x, dates) }) + .OrderByDescending(x=> x.Items.Sum(y=>y.Amount)); return Json(new { @@ -69,6 +69,15 @@ private static IEnumerable NormalizeItems(IEnumerable DateTime.UtcNow.AddDays(-1), + StatisticsRange.Week => DateTime.UtcNow.AddDays(-7), + StatisticsRange.Month => DateTime.UtcNow.AddDays(-30), + _ => DateTime.UtcNow.AddDays(-1) + }; -} + } diff --git a/src/AKCore/Models/StatisticsModels.cs b/src/AKCore/Models/StatisticsModels.cs index aa1e673e..9e5f0486 100644 --- a/src/AKCore/Models/StatisticsModels.cs +++ b/src/AKCore/Models/StatisticsModels.cs @@ -2,4 +2,11 @@ namespace AKCore.Models; -public record StatisticsItemModel(DateTime Created, int Amount); \ No newline at end of file +public record StatisticsItemModel(DateTime Created, int Amount); + +public enum StatisticsRange +{ + Day, + Week, + Month +} \ No newline at end of file diff --git a/src/AKCore/Scripts/VueComponents/Statistics/PageViewGraph.vue b/src/AKCore/Scripts/VueComponents/Statistics/PageViewGraph.vue new file mode 100644 index 00000000..a0ceab8c --- /dev/null +++ b/src/AKCore/Scripts/VueComponents/Statistics/PageViewGraph.vue @@ -0,0 +1,84 @@ + + + diff --git a/src/AKCore/Scripts/VueComponents/Statistics/StatisticsApp.vue b/src/AKCore/Scripts/VueComponents/Statistics/StatisticsApp.vue index c640f1c7..45416393 100644 --- a/src/AKCore/Scripts/VueComponents/Statistics/StatisticsApp.vue +++ b/src/AKCore/Scripts/VueComponents/Statistics/StatisticsApp.vue @@ -1,6 +1,7 @@