From c8120b3388c58ccd40df1db942d3238238294dc8 Mon Sep 17 00:00:00 2001 From: Josh Date: Sun, 12 Feb 2023 23:37:26 -0600 Subject: [PATCH] Remove InfluxDB and add /api/v1/playerCount (#664) * Add /playerCount api * Modify /playerCount api response * Remove all InfluxDB components --- .../Controllers/StatisticsEndpoints.cs | 37 +++++++- .../Responses/PlayerCountResponse.cs | 13 +++ .../InfluxDBConfiguration.cs | 16 ---- .../Configuration/ServerConfiguration.cs | 3 +- ProjectLighthouse/Helpers/InfluxHelper.cs | 90 ------------------- ProjectLighthouse/Logging/LogArea.cs | 1 - .../Logging/Loggers/InfluxLogger.cs | 23 ----- ProjectLighthouse/ProjectLighthouse.csproj | 3 +- ProjectLighthouse/StartupTasks.cs | 7 -- 9 files changed, 51 insertions(+), 142 deletions(-) create mode 100644 ProjectLighthouse.Servers.API/Responses/PlayerCountResponse.cs delete mode 100644 ProjectLighthouse/Configuration/ConfigurationCategories/InfluxDBConfiguration.cs delete mode 100644 ProjectLighthouse/Helpers/InfluxHelper.cs delete mode 100644 ProjectLighthouse/Logging/Loggers/InfluxLogger.cs diff --git a/ProjectLighthouse.Servers.API/Controllers/StatisticsEndpoints.cs b/ProjectLighthouse.Servers.API/Controllers/StatisticsEndpoints.cs index b1dd3e46..a72ab4ac 100644 --- a/ProjectLighthouse.Servers.API/Controllers/StatisticsEndpoints.cs +++ b/ProjectLighthouse.Servers.API/Controllers/StatisticsEndpoints.cs @@ -1,6 +1,6 @@ using LBPUnion.ProjectLighthouse.Helpers; +using LBPUnion.ProjectLighthouse.PlayerData; using LBPUnion.ProjectLighthouse.Servers.API.Responses; -using LBPUnion.ProjectLighthouse.Types; using Microsoft.AspNetCore.Mvc; namespace LBPUnion.ProjectLighthouse.Servers.API.Controllers; @@ -36,4 +36,39 @@ public class StatisticsEndpoints : ApiEndpointController TeamPicks = await StatisticsHelper.TeamPickCount(this.database), } ); + + private static readonly List gameVersions = new() + { + GameVersion.LittleBigPlanet1, + GameVersion.LittleBigPlanet2, + GameVersion.LittleBigPlanet3, + GameVersion.LittleBigPlanetVita, + GameVersion.LittleBigPlanetPSP, + }; + + /// + /// Get player counts for each individual title + /// + /// An instance of PlayerCountResponse + [HttpGet("playerCount")] + [ProducesResponseType(typeof(PlayerCountResponse), StatusCodes.Status200OK)] + public async Task GetPlayerCounts() + { + List gameList = new(); + foreach (GameVersion version in gameVersions) + { + gameList.Add(new PlayerCountObject + { + Game = version.ToString(), + PlayerCount = await StatisticsHelper.RecentMatchesForGame(this.database, version), + }); + } + PlayerCountResponse response = new() + { + TotalPlayerCount = await StatisticsHelper.RecentMatches(this.database), + Games = gameList, + }; + + return this.Ok(response); + } } \ No newline at end of file diff --git a/ProjectLighthouse.Servers.API/Responses/PlayerCountResponse.cs b/ProjectLighthouse.Servers.API/Responses/PlayerCountResponse.cs new file mode 100644 index 00000000..27e82e30 --- /dev/null +++ b/ProjectLighthouse.Servers.API/Responses/PlayerCountResponse.cs @@ -0,0 +1,13 @@ +namespace LBPUnion.ProjectLighthouse.Servers.API.Responses; + +public class PlayerCountObject +{ + public string Game { get; set; } = ""; + public int PlayerCount { get; set; } +} + +public class PlayerCountResponse +{ + public int TotalPlayerCount { get; set; } + public List Games { get; set; } = new(); +} \ No newline at end of file diff --git a/ProjectLighthouse/Configuration/ConfigurationCategories/InfluxDBConfiguration.cs b/ProjectLighthouse/Configuration/ConfigurationCategories/InfluxDBConfiguration.cs deleted file mode 100644 index c3aad3f2..00000000 --- a/ProjectLighthouse/Configuration/ConfigurationCategories/InfluxDBConfiguration.cs +++ /dev/null @@ -1,16 +0,0 @@ -namespace LBPUnion.ProjectLighthouse.Configuration.ConfigurationCategories; - -public class InfluxDBConfiguration -{ - public bool InfluxEnabled { get; set; } - - /// - /// Whether or not to log to InfluxDB. - /// - public bool LoggingEnabled { get; set; } - - public string Organization { get; set; } = "lighthouse"; - public string Bucket { get; set; } = "lighthouse"; - public string Token { get; set; } = ""; - public string Url { get; set; } = "http://localhost:8086"; -} \ No newline at end of file diff --git a/ProjectLighthouse/Configuration/ServerConfiguration.cs b/ProjectLighthouse/Configuration/ServerConfiguration.cs index 3c078d1f..8e174108 100644 --- a/ProjectLighthouse/Configuration/ServerConfiguration.cs +++ b/ProjectLighthouse/Configuration/ServerConfiguration.cs @@ -11,7 +11,7 @@ public class ServerConfiguration : ConfigurationBase // This is so Lighthouse can properly identify outdated configurations and update them with newer settings accordingly. // If you are modifying anything here, this value MUST be incremented. // Thanks for listening~ - public override int ConfigVersion { get; set; } = 17; + public override int ConfigVersion { get; set; } = 18; public override string ConfigName { get; set; } = "lighthouse.yml"; public string WebsiteListenUrl { get; set; } = "http://localhost:10060"; @@ -36,7 +36,6 @@ public class ServerConfiguration : ConfigurationBase public DigestKeyConfiguration DigestKey { get; set; } = new(); public DiscordIntegrationConfiguration DiscordIntegration { get; set; } = new(); public GoogleAnalyticsConfiguration GoogleAnalytics { get; set; } = new(); - public InfluxDBConfiguration InfluxDB { get; set; } = new(); public MailConfiguration Mail { get; set; } = new(); public UserGeneratedContentLimitConfiguration UserGeneratedContentLimits { get; set; } = new(); public WebsiteConfiguration WebsiteConfiguration { get; set; } = new(); diff --git a/ProjectLighthouse/Helpers/InfluxHelper.cs b/ProjectLighthouse/Helpers/InfluxHelper.cs deleted file mode 100644 index 2486556f..00000000 --- a/ProjectLighthouse/Helpers/InfluxHelper.cs +++ /dev/null @@ -1,90 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics.CodeAnalysis; -using System.Threading; -using System.Threading.Tasks; -using InfluxDB.Client; -using InfluxDB.Client.Writes; -using LBPUnion.ProjectLighthouse.Configuration; -using LBPUnion.ProjectLighthouse.Extensions; -using LBPUnion.ProjectLighthouse.Logging; -using LBPUnion.ProjectLighthouse.PlayerData; - -namespace LBPUnion.ProjectLighthouse.Helpers; - -public static class InfluxHelper -{ - public static readonly InfluxDBClient Client = new(url: ServerConfiguration.Instance.InfluxDB.Url, - token: ServerConfiguration.Instance.InfluxDB.Token); - - private static readonly List gameVersions = new() - { - GameVersion.LittleBigPlanet1, - GameVersion.LittleBigPlanet2, - GameVersion.LittleBigPlanet3, - GameVersion.LittleBigPlanetVita, - GameVersion.LittleBigPlanetPSP, - }; - - private static async void Log() - { - try - { - await using Database database = new(); - using WriteApi writeApi = Client.GetWriteApi(); - PointData point = PointData.Measurement("lighthouse") - .Field("playerCount", await StatisticsHelper.RecentMatches(database)) - .Field("slotCount", await StatisticsHelper.SlotCount(database)) - .Field("userCount", await StatisticsHelper.UserCount(database)) - .Field("photoCount", await StatisticsHelper.PhotoCount(database)); - - foreach (GameVersion gameVersion in gameVersions) - { - PointData gamePoint = PointData.Measurement("lighthouse") - .Tag("game", gameVersion.ToString()) - .Field("playerCountGame", await StatisticsHelper.RecentMatchesForGame(database, gameVersion)); - - writeApi.WritePoint(gamePoint, ServerConfiguration.Instance.InfluxDB.Bucket, ServerConfiguration.Instance.InfluxDB.Organization); - } - - writeApi.WritePoint(point, ServerConfiguration.Instance.InfluxDB.Bucket, ServerConfiguration.Instance.InfluxDB.Organization); - - writeApi.Flush(); - } - catch(Exception e) - { - Logger.Error("Exception while logging: ", LogArea.InfluxDB); - Logger.Error(e.ToDetailedException(), LogArea.InfluxDB); - } - } - - [SuppressMessage("ReSharper", "FunctionNeverReturns")] - public static async Task StartLogging() - { - await Client.ReadyAsync(); - Logger.Success("InfluxDB is now ready.", LogArea.InfluxDB); - Thread t = new - ( - delegate() - { - while (true) - { - try - { - Log(); - } - catch(Exception e) - { - Logger.Error("Exception while running log thread: ", LogArea.InfluxDB); - Logger.Error(e.ToDetailedException(), LogArea.InfluxDB); - } - - Thread.Sleep(60000); - } - } - ); - t.IsBackground = true; - t.Name = "InfluxDB Logger"; - t.Start(); - } -} \ No newline at end of file diff --git a/ProjectLighthouse/Logging/LogArea.cs b/ProjectLighthouse/Logging/LogArea.cs index a0fa18c7..09593884 100644 --- a/ProjectLighthouse/Logging/LogArea.cs +++ b/ProjectLighthouse/Logging/LogArea.cs @@ -13,7 +13,6 @@ public enum LogArea Database, Filter, HTTP, - InfluxDB, Match, Photos, Resources, diff --git a/ProjectLighthouse/Logging/Loggers/InfluxLogger.cs b/ProjectLighthouse/Logging/Loggers/InfluxLogger.cs deleted file mode 100644 index a5685d32..00000000 --- a/ProjectLighthouse/Logging/Loggers/InfluxLogger.cs +++ /dev/null @@ -1,23 +0,0 @@ -using InfluxDB.Client; -using InfluxDB.Client.Writes; -using LBPUnion.ProjectLighthouse.Configuration; -using LBPUnion.ProjectLighthouse.Helpers; - -namespace LBPUnion.ProjectLighthouse.Logging.Loggers; - -public class InfluxLogger : ILogger -{ - public void Log(LogLine line) - { - string channel = string.IsNullOrEmpty(line.Area) ? "" : $"[{line.Area}] "; - - string level = $"{$"{channel} {line}".TrimEnd()}"; - string content = line.Message; - - using WriteApi writeApi = InfluxHelper.Client.GetWriteApi(); - - PointData point = PointData.Measurement("lighthouseLog").Field("level", level).Field("content", content); - - writeApi.WritePoint(point, ServerConfiguration.Instance.InfluxDB.Bucket, ServerConfiguration.Instance.InfluxDB.Organization); - } -} \ No newline at end of file diff --git a/ProjectLighthouse/ProjectLighthouse.csproj b/ProjectLighthouse/ProjectLighthouse.csproj index b6ecbfe0..45c9b50c 100644 --- a/ProjectLighthouse/ProjectLighthouse.csproj +++ b/ProjectLighthouse/ProjectLighthouse.csproj @@ -13,7 +13,6 @@ - @@ -27,7 +26,7 @@ - + diff --git a/ProjectLighthouse/StartupTasks.cs b/ProjectLighthouse/StartupTasks.cs index 65152873..948b62e2 100644 --- a/ProjectLighthouse/StartupTasks.cs +++ b/ProjectLighthouse/StartupTasks.cs @@ -63,13 +63,6 @@ public static class StartupTasks migrateDatabase(database).Wait(); - if (ServerConfiguration.Instance.InfluxDB.InfluxEnabled) - { - Logger.Info("Influx logging is enabled. Starting influx logging...", LogArea.Startup); - InfluxHelper.StartLogging().Wait(); - if (ServerConfiguration.Instance.InfluxDB.LoggingEnabled) Logger.Instance.AddLogger(new InfluxLogger()); - } - Logger.Debug ( "This is a debug build, so performance may suffer! " +