huge commit for advanced stats feature.

broke data out into its own library.
may be breaking changes with existing plugins
This commit is contained in:
RaidMax
2021-03-22 11:09:25 -05:00
parent db2e1deb2f
commit c5375b661b
505 changed files with 13671 additions and 3271 deletions

View File

@ -0,0 +1,192 @@
using Microsoft.AspNetCore.Mvc;
using SharedLibraryCore;
using SharedLibraryCore.Database.Models;
using SharedLibraryCore.Dtos;
using SharedLibraryCore.Dtos.Meta.Responses;
using SharedLibraryCore.Interfaces;
using SharedLibraryCore.QueryHelper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Data.Models;
using IW4MAdmin.Plugins.Stats.Config;
using WebfrontCore.ViewComponents;
namespace WebfrontCore.Controllers
{
public class ClientController : BaseController
{
private readonly IMetaService _metaService;
private readonly IConfigurationHandler<StatsConfiguration> _configurationHandler;
public ClientController(IManager manager, IMetaService metaService,
IConfigurationHandler<StatsConfiguration> configurationHandler) : base(manager)
{
_metaService = metaService;
_configurationHandler = configurationHandler;
}
public async Task<IActionResult> ProfileAsync(int id, MetaType? metaFilterType)
{
var client = await Manager.GetClientService().Get(id);
if (client == null)
{
return NotFound();
}
var activePenalties = (await Manager.GetPenaltyService().GetActivePenaltiesAsync(client.AliasLinkId, client.IPAddress));
var tag = await _metaService.GetPersistentMeta(EFMeta.ClientTag, client);
if (tag?.LinkedMeta != null)
{
client.SetAdditionalProperty(EFMeta.ClientTag, tag.LinkedMeta.Value);
}
var displayLevelInt = (int)client.Level;
var displayLevel = client.Level.ToLocalizedLevelName();
if (!Authorized && client.Level.ShouldHideLevel())
{
displayLevelInt = (int)Data.Models.Client.EFClient.Permission.User;
displayLevel = Data.Models.Client.EFClient.Permission.User.ToLocalizedLevelName();
}
displayLevel = string.IsNullOrEmpty(client.Tag) ? displayLevel : $"{displayLevel} ({client.Tag})";
var clientDto = new PlayerInfo()
{
Name = client.Name,
Level = displayLevel,
LevelInt = displayLevelInt,
ClientId = client.ClientId,
IPAddress = client.IPAddressString,
NetworkId = client.NetworkId,
Meta = new List<InformationResponse>(),
Aliases = client.AliasLink.Children
.Select(_alias => _alias.Name)
.GroupBy(_alias => _alias.StripColors())
// we want the longest "duplicate" name
.Select(_grp => _grp.OrderByDescending(_name => _name.Length).First())
.Distinct()
.OrderBy(a => a)
.ToList(),
IPs = client.AliasLink.Children
.Where(i => i.IPAddress != null)
.OrderByDescending(i => i.DateAdded)
.Select(i => i.IPAddress.ConvertIPtoString())
.Prepend(client.CurrentAlias.IPAddress.ConvertIPtoString())
.Distinct()
.ToList(),
HasActivePenalty = activePenalties.Any(_penalty => _penalty.Type != EFPenalty.PenaltyType.Flag),
Online = Manager.GetActiveClients().FirstOrDefault(c => c.ClientId == client.ClientId) != null,
TimeOnline = (DateTime.UtcNow - client.LastConnection).HumanizeForCurrentCulture(),
LinkedAccounts = client.LinkedAccounts,
MetaFilterType = metaFilterType
};
var meta = await _metaService.GetRuntimeMeta<InformationResponse>(new ClientPaginationRequest
{
ClientId = client.ClientId,
Before = DateTime.UtcNow
}, MetaType.Information);
var gravatar = await _metaService.GetPersistentMeta("GravatarEmail", client);
if (gravatar != null)
{
clientDto.Meta.Add(new InformationResponse()
{
Key = "GravatarEmail",
Type = MetaType.Other,
Value = gravatar.Value
});
}
clientDto.ActivePenalty = activePenalties.OrderByDescending(_penalty => _penalty.Type).FirstOrDefault();
clientDto.Meta.AddRange(Authorized ? meta : meta.Where(m => !m.IsSensitive));
string strippedName = clientDto.Name.StripColors();
ViewBag.Title = strippedName.Substring(strippedName.Length - 1).ToLower()[0] == 's' ?
strippedName + "'" :
strippedName + "'s";
ViewBag.Title += " " + Localization["WEBFRONT_CLIENT_PROFILE_TITLE"];
ViewBag.Description = $"Client information for {strippedName}";
ViewBag.Keywords = $"IW4MAdmin, client, profile, {strippedName}";
ViewBag.UseNewStats = _configurationHandler.Configuration().EnableAdvancedMetrics;
return View("Profile/Index", clientDto);
}
public async Task<IActionResult> PrivilegedAsync()
{
var admins = (await Manager.GetClientService().GetPrivilegedClients())
.OrderByDescending(_client => _client.Level)
.ThenBy(_client => _client.Name);
var adminsDict = new Dictionary<EFClient.Permission, IList<ClientInfo>>();
foreach (var admin in admins)
{
if (!adminsDict.ContainsKey(admin.Level))
{
adminsDict.Add(admin.Level, new List<ClientInfo>());
}
adminsDict[admin.Level].Add(new ClientInfo()
{
Name = admin.Name,
ClientId = admin.ClientId
});
}
ViewBag.Title = Localization["WEBFRONT_CLIENT_PRIVILEGED_TITLE"];
ViewBag.Description = Localization["WEBFRONT_DESCRIPTION_PRIVILEGED"];
ViewBag.Keywords = Localization["WEBFRONT_KEYWORDS_PRIVILEGED"];
return View("Privileged/Index", adminsDict);
}
public async Task<IActionResult> FindAsync(string clientName)
{
if (string.IsNullOrWhiteSpace(clientName))
{
return StatusCode(400);
}
var clientsDto = await Manager.GetClientService().FindClientsByIdentifier(clientName);
foreach (var client in clientsDto)
{
if (!Authorized && ((Data.Models.Client.EFClient.Permission)client.LevelInt).ShouldHideLevel())
{
client.LevelInt = (int)Data.Models.Client.EFClient.Permission.User;
client.Level = Data.Models.Client.EFClient.Permission.User.ToLocalizedLevelName();
}
}
ViewBag.Title = $"{clientsDto.Count} {Localization["WEBFRONT_CLIENT_SEARCH_MATCHING"]} \"{clientName}\"";
return View("Find/Index", clientsDto);
}
public async Task<IActionResult> Meta(int id, int count, int offset, long? startAt, MetaType? metaFilterType)
{
var request = new ClientPaginationRequest
{
ClientId = id,
Count = count,
Offset = offset,
Before = DateTime.FromFileTimeUtc(startAt ?? DateTime.UtcNow.ToFileTimeUtc())
};
var meta = await ProfileMetaListViewComponent.GetClientMeta(_metaService, metaFilterType, Client.Level, request);
if (!meta.Any())
{
return Ok();
}
return View("Components/ProfileMetaList/_List", meta);
}
}
}

View File

@ -0,0 +1,38 @@
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using SharedLibraryCore;
using SharedLibraryCore.Configuration;
using SharedLibraryCore.Interfaces;
using Stats.Dtos;
namespace WebfrontCore.Controllers
{
[Route("clientstatistics")]
public class ClientStatisticsController : BaseController
{
private IResourceQueryHelper<StatsInfoRequest, AdvancedStatsInfo> _queryHelper;
private readonly DefaultSettings _defaultConfig;
public ClientStatisticsController(IManager manager,
IResourceQueryHelper<StatsInfoRequest, AdvancedStatsInfo> queryHelper,
IConfigurationHandler<DefaultSettings> configurationHandler) : base(manager)
{
_queryHelper = queryHelper;
_defaultConfig = configurationHandler.Configuration();
}
[HttpGet("{id:int}/advanced")]
public async Task<IActionResult> Advanced(int id, [FromQuery] string serverId)
{
ViewBag.Config = _defaultConfig.GameStrings;
var hitInfo = await _queryHelper.QueryResource(new StatsInfoRequest
{
ClientId = id,
ServerEndpoint = serverId
});
return View("~/Views/Client/Statistics/Advanced.cshtml", hitInfo.Results.First());
}
}
}

View File

@ -0,0 +1,213 @@
using IW4MAdmin.Plugins.Stats;
using IW4MAdmin.Plugins.Stats.Helpers;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using SharedLibraryCore;
using SharedLibraryCore.Dtos;
using SharedLibraryCore.Dtos.Meta.Responses;
using SharedLibraryCore.Interfaces;
using Stats.Dtos;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using ILogger = Microsoft.Extensions.Logging.ILogger;
using Data.Abstractions;
using IW4MAdmin.Plugins.Stats.Config;
namespace IW4MAdmin.Plugins.Web.StatsWeb.Controllers
{
public class StatsController : BaseController
{
private readonly ILogger _logger;
private readonly IManager _manager;
private readonly IResourceQueryHelper<ChatSearchQuery, MessageResponse> _chatResourceQueryHelper;
private readonly ITranslationLookup _translationLookup;
private readonly IDatabaseContextFactory _contextFactory;
private readonly IConfigurationHandler<StatsConfiguration> _configurationHandler;
public StatsController(ILogger<StatsController> logger, IManager manager, IResourceQueryHelper<ChatSearchQuery,
MessageResponse> resourceQueryHelper, ITranslationLookup translationLookup,
IDatabaseContextFactory contextFactory,
IConfigurationHandler<StatsConfiguration> configurationHandler) : base(manager)
{
_logger = logger;
_manager = manager;
_chatResourceQueryHelper = resourceQueryHelper;
_translationLookup = translationLookup;
_contextFactory = contextFactory;
_configurationHandler = configurationHandler;
}
[HttpGet]
public IActionResult TopPlayersAsync()
{
ViewBag.Title = Utilities.CurrentLocalization.LocalizationIndex["WEBFRONT_STATS_INDEX_TITLE"];
ViewBag.Description = Utilities.CurrentLocalization.LocalizationIndex["WEBFRONT_STATS_INDEX_DESC"];
ViewBag.Servers = _manager.GetServers()
.Select(_server => new ServerInfo() {Name = _server.Hostname, ID = _server.EndPoint});
ViewBag.Localization = _translationLookup;
return View("~/Views/Client/Statistics/Index.cshtml");
}
[HttpGet]
public async Task<IActionResult> GetTopPlayersAsync(int count, int offset, long? serverId = null)
{
// this prevents empty results when we really want aggregate
if (serverId == 0)
{
serverId = null;
}
var server = _manager.GetServers().FirstOrDefault(_server => _server.EndPoint == serverId);
if (server != null)
{
serverId = StatManager.GetIdForServer(server);
}
var results = _configurationHandler.Configuration().EnableAdvancedMetrics
? await Plugin.Manager.GetNewTopStats(offset, count, serverId)
: await Plugin.Manager.GetTopStats(offset, count, serverId);
// this returns an empty result so we know to stale the loader
if (results.Count == 0 && offset > 0)
{
return Ok();
}
ViewBag.UseNewStats = _configurationHandler.Configuration().EnableAdvancedMetrics;
return View("~/Views/Client/Statistics/Components/TopPlayers/_List.cshtml", results);
}
[HttpGet]
public async Task<IActionResult> GetMessageAsync(string serverId, long when)
{
var whenTime = DateTime.FromFileTimeUtc(when);
var whenUpper = whenTime.AddMinutes(5);
var whenLower = whenTime.AddMinutes(-5);
var messages = await _chatResourceQueryHelper.QueryResource(new ChatSearchQuery()
{
ServerId = serverId,
SentBefore = whenUpper,
SentAfter = whenLower
});
return View("~/Views/Client/_MessageContext.cshtml", messages.Results.ToList());
}
[HttpGet("Message/Find")]
public async Task<IActionResult> FindMessage([FromQuery] string query)
{
ViewBag.Localization = _translationLookup;
ViewBag.EnableColorCodes = _manager.GetApplicationSettings().Configuration().EnableColorCodes;
ViewBag.Query = query;
ViewBag.QueryLimit = 100;
ViewBag.Title = _translationLookup["WEBFRONT_STATS_MESSAGES_TITLE"];
ViewBag.Error = null;
ViewBag.IsFluid = true;
ChatSearchQuery searchRequest = null;
try
{
searchRequest = query.ParseSearchInfo(int.MaxValue, 0);
}
catch (ArgumentException e)
{
_logger.LogWarning(e, "Could not parse chat message search query {query}", query);
ViewBag.Error = e;
}
catch (FormatException e)
{
_logger.LogWarning(e, "Could not parse chat message search query filter format {query}", query);
ViewBag.Error = e;
}
var result = searchRequest != null ? await _chatResourceQueryHelper.QueryResource(searchRequest) : null;
return View("~/Views/Client/Message/Find.cshtml", result);
}
[HttpGet("Message/FindNext")]
public async Task<IActionResult> FindNextMessages([FromQuery] string query, [FromQuery] int count,
[FromQuery] int offset)
{
ChatSearchQuery searchRequest;
try
{
searchRequest = query.ParseSearchInfo(count, offset);
}
catch (ArgumentException e)
{
_logger.LogWarning(e, "Could not parse chat message search query {query}", query);
throw;
}
catch (FormatException e)
{
_logger.LogWarning(e, "Could not parse chat message search query filter format {query}", query);
throw;
}
var result = await _chatResourceQueryHelper.QueryResource(searchRequest);
return PartialView("~/Views/Client/Message/_Item.cshtml", result.Results);
}
[HttpGet]
[Authorize]
public async Task<IActionResult> GetAutomatedPenaltyInfoAsync(int penaltyId)
{
await using var context = _contextFactory.CreateContext(false);
var penalty = await context.Penalties
.Select(_penalty => new
{_penalty.OffenderId, _penalty.PenaltyId, _penalty.When, _penalty.AutomatedOffense})
.FirstOrDefaultAsync(_penalty => _penalty.PenaltyId == penaltyId);
if (penalty == null)
{
return NotFound();
}
// todo: this can be optimized
var iqSnapshotInfo = context.ACSnapshots
.Where(s => s.ClientId == penalty.OffenderId)
.Include(s => s.LastStrainAngle)
.Include(s => s.HitOrigin)
.Include(s => s.HitDestination)
.Include(s => s.CurrentViewAngle)
.Include(s => s.PredictedViewAngles)
.ThenInclude(_angles => _angles.Vector)
.OrderBy(s => s.When)
.ThenBy(s => s.Hits);
var penaltyInfo = await iqSnapshotInfo.ToListAsync();
if (penaltyInfo.Count > 0)
{
return View("~/Views/Client/_PenaltyInfo.cshtml", penaltyInfo);
}
// we want to show anything related to the automated offense
else
{
return View("~/Views/Client/_MessageContext.cshtml", new List<MessageResponse>
{
new MessageResponse()
{
ClientId = penalty.OffenderId,
Message = penalty.AutomatedOffense,
When = penalty.When
}
});
}
}
}
}