From 68c1151191b3fd3b5fad882d5e53896b619a2cd3 Mon Sep 17 00:00:00 2001 From: RaidMax Date: Tue, 14 Sep 2021 18:12:20 -0500 Subject: [PATCH] add tooltip timestamp to max concurrent players --- Application/Misc/ServerDataViewer.cs | 42 +++++++++++++------ SharedLibraryCore/Dtos/IW4MAdminInfo.cs | 4 +- .../Interfaces/IServerDataViewer.cs | 2 +- WebfrontCore/Controllers/HomeController.cs | 8 ++-- WebfrontCore/Views/Home/Index.cshtml | 2 +- WebfrontCore/wwwroot/js/server.js | 9 ++++ 6 files changed, 49 insertions(+), 18 deletions(-) diff --git a/Application/Misc/ServerDataViewer.cs b/Application/Misc/ServerDataViewer.cs index 0dc84b193..a0043579f 100644 --- a/Application/Misc/ServerDataViewer.cs +++ b/Application/Misc/ServerDataViewer.cs @@ -10,7 +10,6 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using SharedLibraryCore; using SharedLibraryCore.Dtos; -using SharedLibraryCore.Helpers; using SharedLibraryCore.Interfaces; using ILogger = Microsoft.Extensions.Logging.ILogger; @@ -20,14 +19,14 @@ namespace IW4MAdmin.Application.Misc public class ServerDataViewer : IServerDataViewer { private readonly ILogger _logger; - private readonly IDataValueCache _snapshotCache; + private readonly IDataValueCache _snapshotCache; private readonly IDataValueCache _serverStatsCache; private readonly IDataValueCache> _clientHistoryCache; private readonly TimeSpan? _cacheTimeSpan = Utilities.IsDevelopment ? TimeSpan.FromSeconds(1) : (TimeSpan?) TimeSpan.FromMinutes(1); - public ServerDataViewer(ILogger logger, IDataValueCache snapshotCache, + public ServerDataViewer(ILogger logger, IDataValueCache snapshotCache, IDataValueCache serverStatsCache, IDataValueCache> clientHistoryCache) { @@ -37,7 +36,7 @@ namespace IW4MAdmin.Application.Misc _clientHistoryCache = clientHistoryCache; } - public async Task MaxConcurrentClientsAsync(long? serverId = null, TimeSpan? overPeriod = null, + public async Task<(int?, DateTime?)> MaxConcurrentClientsAsync(long? serverId = null, TimeSpan? overPeriod = null, CancellationToken token = default) { _snapshotCache.SetCacheItem(async (snapshots, cancellationToken) => @@ -45,26 +44,45 @@ namespace IW4MAdmin.Application.Misc var oldestEntry = overPeriod.HasValue ? DateTime.UtcNow - overPeriod.Value : DateTime.UtcNow.AddDays(-1); - var maxClients = 0; + + int? maxClients; + DateTime? maxClientsTime; if (serverId != null) { - maxClients = await snapshots.Where(snapshot => snapshot.ServerId == serverId) + var clients = await snapshots.Where(snapshot => snapshot.ServerId == serverId) .Where(snapshot => snapshot.CapturedAt >= oldestEntry) - .MaxAsync(snapshot => (int?) snapshot.ClientCount, cancellationToken) ?? 0; + .OrderByDescending(snapshot => snapshot.ClientCount) + .Select(snapshot => new + { + snapshot.ClientCount, + snapshot.CapturedAt + }) + .FirstOrDefaultAsync(cancellationToken); + + maxClients = clients?.ClientCount; + maxClientsTime = clients?.CapturedAt; } else { - maxClients = await snapshots.Where(snapshot => snapshot.CapturedAt >= oldestEntry) + var clients = await snapshots.Where(snapshot => snapshot.CapturedAt >= oldestEntry) .GroupBy(snapshot => snapshot.PeriodBlock) - .Select(grp => grp.Sum(snapshot => (int?) snapshot.ClientCount)) - .MaxAsync(cancellationToken) ?? 0; + .Select(grp => new + { + ClientCount = grp.Sum(snapshot => (int?) snapshot.ClientCount), + Time = grp.Max(snapshot => (DateTime?) snapshot.CapturedAt) + }) + .OrderByDescending(snapshot => snapshot.ClientCount) + .FirstOrDefaultAsync(cancellationToken); + + maxClients = clients?.ClientCount; + maxClientsTime = clients?.Time; } _logger.LogDebug("Max concurrent clients since {Start} is {Clients}", oldestEntry, maxClients); - return maxClients; + return (maxClients, maxClientsTime); }, nameof(MaxConcurrentClientsAsync), _cacheTimeSpan); try @@ -74,7 +92,7 @@ namespace IW4MAdmin.Application.Misc catch (Exception ex) { _logger.LogError(ex, "Could not retrieve data for {Name}", nameof(MaxConcurrentClientsAsync)); - return 0; + return (null, null); } } diff --git a/SharedLibraryCore/Dtos/IW4MAdminInfo.cs b/SharedLibraryCore/Dtos/IW4MAdminInfo.cs index df0569366..0dab1eb96 100644 --- a/SharedLibraryCore/Dtos/IW4MAdminInfo.cs +++ b/SharedLibraryCore/Dtos/IW4MAdminInfo.cs @@ -1,4 +1,5 @@ -using static SharedLibraryCore.Server; +using System; +using static SharedLibraryCore.Server; namespace SharedLibraryCore.Dtos { @@ -9,6 +10,7 @@ namespace SharedLibraryCore.Dtos public int TotalOccupiedClientSlots { get; set; } public int TotalAvailableClientSlots { get; set; } public int MaxConcurrentClients { get; set; } + public DateTime MaxConcurrentClientsTime { get; set; } /// /// specifies the game name filter diff --git a/SharedLibraryCore/Interfaces/IServerDataViewer.cs b/SharedLibraryCore/Interfaces/IServerDataViewer.cs index 53df55534..652e948c1 100644 --- a/SharedLibraryCore/Interfaces/IServerDataViewer.cs +++ b/SharedLibraryCore/Interfaces/IServerDataViewer.cs @@ -19,7 +19,7 @@ namespace SharedLibraryCore.Interfaces /// how far in the past to search /// CancellationToken /// - Task MaxConcurrentClientsAsync(long? serverId = null, TimeSpan? overPeriod = null, CancellationToken token = default); + Task<(int?, DateTime?)> MaxConcurrentClientsAsync(long? serverId = null, TimeSpan? overPeriod = null, CancellationToken token = default); /// /// Gets the total number of clients connected and total clients connected in the given time frame diff --git a/WebfrontCore/Controllers/HomeController.cs b/WebfrontCore/Controllers/HomeController.cs index dffe6231d..73fd4695f 100644 --- a/WebfrontCore/Controllers/HomeController.cs +++ b/WebfrontCore/Controllers/HomeController.cs @@ -1,4 +1,5 @@ -using Microsoft.AspNetCore.Diagnostics; +using System; +using Microsoft.AspNetCore.Diagnostics; using Microsoft.AspNetCore.Mvc; using SharedLibraryCore; using SharedLibraryCore.Dtos; @@ -33,7 +34,7 @@ namespace WebfrontCore.Controllers ViewBag.Keywords = Localization["WEBFRONT_KEWORDS_HOME"]; var servers = Manager.GetServers().Where(_server => !game.HasValue || _server.GameName == game); - var maxConcurrentClients = await _serverDataViewer.MaxConcurrentClientsAsync(token: cancellationToken); + var (clientCount, time) = await _serverDataViewer.MaxConcurrentClientsAsync(token: cancellationToken); var (count, recentCount) = await _serverDataViewer.ClientCountsAsync(token: cancellationToken); var model = new IW4MAdminInfo() @@ -42,7 +43,8 @@ namespace WebfrontCore.Controllers TotalOccupiedClientSlots = servers.SelectMany(_server => _server.GetClientsAsList()).Count(), TotalClientCount = count, RecentClientCount = recentCount, - MaxConcurrentClients = maxConcurrentClients, + MaxConcurrentClients = clientCount ?? 0, + MaxConcurrentClientsTime = time ?? DateTime.UtcNow, Game = game, ActiveServerGames = Manager.GetServers().Select(_server => _server.GameName).Distinct().ToArray() }; diff --git a/WebfrontCore/Views/Home/Index.cshtml b/WebfrontCore/Views/Home/Index.cshtml index 9ac9c0254..2ce89dbd6 100644 --- a/WebfrontCore/Views/Home/Index.cshtml +++ b/WebfrontCore/Views/Home/Index.cshtml @@ -11,7 +11,7 @@
@Html.Raw(formatTranslation("WEBFRONT_HOME_CLIENTS_ONLINE", Model.TotalOccupiedClientSlots, Model.TotalAvailableClientSlots))
-
+
@Html.Raw(formatTranslation("WEBFRONT_HOME_MAX_CONCURRENT_CLIENTS", Model.MaxConcurrentClients.ToString("#,##0")))
diff --git a/WebfrontCore/wwwroot/js/server.js b/WebfrontCore/wwwroot/js/server.js index 5deda0c8f..7dcb1d234 100644 --- a/WebfrontCore/wwwroot/js/server.js +++ b/WebfrontCore/wwwroot/js/server.js @@ -95,6 +95,15 @@ $(document).ready(function () { historyChart.render(); charts[serverId] = historyChart; }); + + $('.moment-date').each((index, element) => { + const title = $(element).attr('title'); + + if (title !== undefined) { + const date = new Date(title); + $(element).attr('title', moment.utc(date).calendar()); + } + }); }); setInterval(refreshClientActivity, 2000);