From a83bec5e57675d1d4ba19b3172021b8764e4c409 Mon Sep 17 00:00:00 2001 From: Marcel Baumgartner Date: Mon, 24 Apr 2023 01:16:49 +0200 Subject: [PATCH] Fixed statistics system --- .../Statistics/StatisticsCaptureService.cs | 75 ++++++--- Moonlight/Shared/Views/Admin/Statistics.razor | 154 +++++++++++------- Moonlight/defaultstorage/configs/config.json | 4 + 3 files changed, 150 insertions(+), 83 deletions(-) diff --git a/Moonlight/App/Services/Statistics/StatisticsCaptureService.cs b/Moonlight/App/Services/Statistics/StatisticsCaptureService.cs index 3bfd9ca..139ece7 100644 --- a/Moonlight/App/Services/Statistics/StatisticsCaptureService.cs +++ b/Moonlight/App/Services/Statistics/StatisticsCaptureService.cs @@ -1,47 +1,78 @@ -using Moonlight.App.Database; +using Logging.Net; +using Moonlight.App.Database; +using Moonlight.App.Database.Entities; using Moonlight.App.Repositories; +using Moonlight.App.Services.Sessions; namespace Moonlight.App.Services.Statistics; public class StatisticsCaptureService { - private readonly DataContext DataContext; - private readonly ConfigService ConfigService; - private readonly StatisticsRepository StatisticsRepository; private readonly IServiceScopeFactory ServiceScopeFactory; - private readonly WebSpaceService WebSpaceService; + private readonly DateTimeService DateTimeService; private readonly PeriodicTimer Timer; - public StatisticsCaptureService(IServiceScopeFactory serviceScopeFactory, ConfigService configService) + public StatisticsCaptureService(IServiceScopeFactory serviceScopeFactory, ConfigService configService, DateTimeService dateTimeService) { ServiceScopeFactory = serviceScopeFactory; - var provider = ServiceScopeFactory.CreateScope().ServiceProvider; - - DataContext = provider.GetRequiredService(); - ConfigService = configService; - StatisticsRepository = provider.GetRequiredService(); - WebSpaceService = provider.GetRequiredService(); + DateTimeService = dateTimeService; - var config = ConfigService.GetSection("Moonlight").GetSection("Statistics"); + var config = configService + .GetSection("Moonlight") + .GetSection("Statistics"); + if(!config.GetValue("Enabled")) return; - - var _period = config.GetValue("Wait"); - var period = TimeSpan.FromMinutes(_period); + + var period = TimeSpan.FromMinutes(config.GetValue("Wait")); Timer = new(period); + Logger.Info("Starting statistics system"); Task.Run(Run); } private async Task Run() { - while (await Timer.WaitForNextTickAsync()) + try { - StatisticsRepository.Add("statistics.usersCount", DataContext.Users.Count()); - StatisticsRepository.Add("statistics.serversCount", DataContext.Servers.Count()); - StatisticsRepository.Add("statistics.domainsCount", DataContext.Domains.Count()); - StatisticsRepository.Add("statistics.webspacesCount", DataContext.WebSpaces.Count()); - StatisticsRepository.Add("statistics.databasesCount", DataContext.Databases.Count()); + while (await Timer.WaitForNextTickAsync()) + { + Logger.Warn("Creating statistics"); + + using var scope = ServiceScopeFactory.CreateScope(); + + var statisticsRepo = scope.ServiceProvider.GetRequiredService>(); + var usersRepo = scope.ServiceProvider.GetRequiredService>(); + var serversRepo = scope.ServiceProvider.GetRequiredService>(); + var domainsRepo = scope.ServiceProvider.GetRequiredService>(); + var webspacesRepo = scope.ServiceProvider.GetRequiredService>(); + var databasesRepo = scope.ServiceProvider.GetRequiredService>(); + var sessionService = scope.ServiceProvider.GetRequiredService(); + + void AddEntry(string chart, int value) + { + statisticsRepo!.Add(new StatisticsData() + { + Chart = chart, + Value = value, + Date = DateTimeService.GetCurrent() + }); + } + + AddEntry("usersCount", usersRepo.Get().Count()); + AddEntry("serversCount", serversRepo.Get().Count()); + AddEntry("domainsCount", domainsRepo.Get().Count()); + AddEntry("webspacesCount", webspacesRepo.Get().Count()); + AddEntry("databasesCount", databasesRepo.Get().Count()); + AddEntry("sessionsCount", sessionService.GetAll().Length); + } + + Logger.Log("Statistics are weird"); + } + catch (Exception e) + { + Logger.Error("An unexpected error occured while capturing statistics"); + Logger.Error(e); } } } \ No newline at end of file diff --git a/Moonlight/Shared/Views/Admin/Statistics.razor b/Moonlight/Shared/Views/Admin/Statistics.razor index 49c5f43..d2645e7 100644 --- a/Moonlight/Shared/Views/Admin/Statistics.razor +++ b/Moonlight/Shared/Views/Admin/Statistics.razor @@ -1,94 +1,125 @@ @page "/admin/statistics" + @using Moonlight.App.Models.Misc @using Moonlight.App.Services.Statistics @using Moonlight.App.Database.Entities @using ApexCharts +@using Moonlight.App.Helpers +@using Moonlight.App.Services @inject StatisticsViewService StatisticsViewService +@inject SmartTranslateService SmartTranslateService
- + + + + +
- + - @foreach (var charts in Charts.Chunk(2)) - { -
- @foreach (var chart in charts) - { -
-
-
-
- @chart.Header -
-
-
- - - + @foreach (var charts in Charts.Chunk(2)) + { +
+ @foreach (var chart in charts) + { +
+
+
+
+ @chart.Key
+
+ + + +
- } -
- } +
+ } +
+ } -@code { +@code +{ private StatisticsTimeSpan StatisticsTimeSpan = StatisticsTimeSpan.Day; private LazyLoader Loader; - private List<(string Header, StatisticsData[] Data)> Charts = new(); + private Dictionary Charts = new(); - private int bind + private int TimeSpanBind { - get { return (int) StatisticsTimeSpan; } - set { StatisticsTimeSpan = (StatisticsTimeSpan) value; - Task.Run(async() => await Loader.Reload()); + get => (int)StatisticsTimeSpan; + set + { + StatisticsTimeSpan = (StatisticsTimeSpan)value; + Task.Run(async () => await Loader.Reload()); } } - private async Task Load(LazyLoader loader) + private Task Load(LazyLoader loader) { Charts.Clear(); - - Charts.Add(("Servers", StatisticsViewService.GetData("statistics.serversCount", StatisticsTimeSpan))); - Charts.Add(("Users", StatisticsViewService.GetData("statistics.usersCount", StatisticsTimeSpan))); - Charts.Add(("Domains", StatisticsViewService.GetData("statistics.domainsCount", StatisticsTimeSpan))); - Charts.Add(("Databases", StatisticsViewService.GetData("statistics.databasesCount", StatisticsTimeSpan))); - Charts.Add(("Websites", StatisticsViewService.GetData("statistics.websitesCount", StatisticsTimeSpan))); - } - - private string FormatDate(DateTime e) - { - string i2s(int i) - { - if (i.ToString().Length < 2) - return "0" + i; - return i.ToString(); - } - - return $"{i2s(e.Day)}.{i2s(e.Month)}.{e.Year} {i2s(e.Hour)}:{i2s(e.Minute)}"; + + Charts.Add( + SmartTranslateService.Translate("Servers"), + StatisticsViewService.GetData("serversCount", StatisticsTimeSpan) + ); + + Charts.Add( + SmartTranslateService.Translate("Users"), + StatisticsViewService.GetData("usersCount", StatisticsTimeSpan) + ); + + Charts.Add( + SmartTranslateService.Translate("Domains"), + StatisticsViewService.GetData("domainsCount", StatisticsTimeSpan) + ); + + Charts.Add( + SmartTranslateService.Translate("Databases"), + StatisticsViewService.GetData("databasesCount", StatisticsTimeSpan) + ); + + Charts.Add( + SmartTranslateService.Translate("Webspaces"), + StatisticsViewService.GetData("webspacesCount", StatisticsTimeSpan) + ); + + Charts.Add( + SmartTranslateService.Translate("Sessions"), + StatisticsViewService.GetData("sessionsCount", StatisticsTimeSpan) + ); + + return Task.CompletedTask; } private ApexChartOptions GenerateOptions() @@ -121,8 +152,9 @@ } }; } - - private async Task OnChartRendered() + + private Task OnChartRendered() { + return Task.CompletedTask; } } \ No newline at end of file diff --git a/Moonlight/defaultstorage/configs/config.json b/Moonlight/defaultstorage/configs/config.json index b0a2a22..72fb99b 100644 --- a/Moonlight/defaultstorage/configs/config.json +++ b/Moonlight/defaultstorage/configs/config.json @@ -78,6 +78,10 @@ "DiscordNotifications": { "Enable": false, "WebHook": "" + }, + "Statistics": { + "Enabled": true, + "Wait": 15 } } } \ No newline at end of file