diff --git a/ProjectLighthouse/Controllers/StatisticsController.cs b/ProjectLighthouse/Controllers/StatisticsController.cs index 9deb12c4..eb945419 100644 --- a/ProjectLighthouse/Controllers/StatisticsController.cs +++ b/ProjectLighthouse/Controllers/StatisticsController.cs @@ -1,9 +1,7 @@ -using System.Linq; using System.Threading.Tasks; using LBPUnion.ProjectLighthouse.Helpers; using LBPUnion.ProjectLighthouse.Serialization; using Microsoft.AspNetCore.Mvc; -using Microsoft.EntityFrameworkCore; namespace LBPUnion.ProjectLighthouse.Controllers { @@ -20,18 +18,13 @@ namespace LBPUnion.ProjectLighthouse.Controllers [HttpGet("playersInPodCount")] [HttpGet("totalPlayerCount")] - public async Task TotalPlayerCount() - { - int recentMatches = await this.database.LastMatches.Where(l => TimestampHelper.Timestamp - l.Timestamp < 60).CountAsync(); - - return this.Ok(recentMatches.ToString()); - } + public async Task TotalPlayerCount() => this.Ok((await StatisticsHelper.RecentMatches()).ToString()!); [HttpGet("planetStats")] public async Task PlanetStats() { - int totalSlotCount = await this.database.Slots.CountAsync(); - int mmPicksCount = await this.database.Slots.CountAsync(s => s.TeamPick); + int totalSlotCount = await StatisticsHelper.SlotCount(); + int mmPicksCount = await StatisticsHelper.MMPicksCount(); return this.Ok ( @@ -41,6 +34,6 @@ namespace LBPUnion.ProjectLighthouse.Controllers } [HttpGet("planetStats/totalLevelCount")] - public async Task TotalLevelCount() => this.Ok((await this.database.Slots.CountAsync()).ToString()); + public async Task TotalLevelCount() => this.Ok((await StatisticsHelper.SlotCount()).ToString()); } } \ No newline at end of file diff --git a/ProjectLighthouse/Helpers/InfluxHelper.cs b/ProjectLighthouse/Helpers/InfluxHelper.cs new file mode 100644 index 00000000..cd5b844e --- /dev/null +++ b/ProjectLighthouse/Helpers/InfluxHelper.cs @@ -0,0 +1,48 @@ +using System.Threading; +using System.Threading.Tasks; +using InfluxDB.Client; +using InfluxDB.Client.Writes; +using Kettu; +using LBPUnion.ProjectLighthouse.Logging; +using LBPUnion.ProjectLighthouse.Types.Settings; + +namespace LBPUnion.ProjectLighthouse.Helpers +{ + public static class InfluxHelper + { + public static readonly InfluxDBClient Client = InfluxDBClientFactory.Create(ServerSettings.Instance.InfluxUrl, ServerSettings.Instance.InfluxToken); + + public static async void Log() + { + WriteApi writeApi = Client.GetWriteApi(); + PointData point = PointData.Measurement("lighthouse").Field("playerCount", await StatisticsHelper.RecentMatches()); + + writeApi.WritePoint(ServerSettings.Instance.InfluxBucket, ServerSettings.Instance.InfluxOrg, point); + + writeApi.Flush(); + } + + public static async Task StartLogging() + { + await Client.ReadyAsync(); + Logger.Log("InfluxDB is now ready.", LoggerLevelInflux.Instance); + Thread t = new Thread + ( + delegate() + { + while (true) + { + Thread.Sleep(5000); + #pragma warning disable CS4014 + Log(); + #pragma warning restore CS4014 + Logger.Log("Logged.", LoggerLevelInflux.Instance); + } + } + ); + t.IsBackground = true; + t.Name = "InfluxDB Logger"; + t.Start(); + } + } +} \ No newline at end of file diff --git a/ProjectLighthouse/Helpers/StatisticsHelper.cs b/ProjectLighthouse/Helpers/StatisticsHelper.cs new file mode 100644 index 00000000..99e42cab --- /dev/null +++ b/ProjectLighthouse/Helpers/StatisticsHelper.cs @@ -0,0 +1,17 @@ +using System.Linq; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; + +namespace LBPUnion.ProjectLighthouse.Helpers +{ + public static class StatisticsHelper + { + private static readonly Database database = new(); + + public static async Task RecentMatches() => await database.LastMatches.Where(l => TimestampHelper.Timestamp - l.Timestamp < 60).CountAsync(); + + public static async Task SlotCount() => await database.Slots.CountAsync(); + + public static async Task MMPicksCount() => await database.Slots.CountAsync(s => s.TeamPick); + } +} \ No newline at end of file diff --git a/ProjectLighthouse/Logging/LoggerLevels.cs b/ProjectLighthouse/Logging/LoggerLevels.cs index 3b169f6e..9d81a53b 100644 --- a/ProjectLighthouse/Logging/LoggerLevels.cs +++ b/ProjectLighthouse/Logging/LoggerLevels.cs @@ -57,6 +57,12 @@ namespace LBPUnion.ProjectLighthouse.Logging public override string Name => "Config"; } + public class LoggerLevelInflux : LoggerLevel + { + public static readonly LoggerLevelInflux Instance = new(); + public override string Name => "Influx"; + } + public class LoggerLevelAspNet : LoggerLevel { diff --git a/ProjectLighthouse/Program.cs b/ProjectLighthouse/Program.cs index 030f20d2..e03ba6f5 100644 --- a/ProjectLighthouse/Program.cs +++ b/ProjectLighthouse/Program.cs @@ -44,6 +44,12 @@ namespace LBPUnion.ProjectLighthouse Logger.Log("Migrating database...", LoggerLevelDatabase.Instance); MigrateDatabase(database); + if (ServerSettings.Instance.InfluxEnabled) + { + Logger.Log("Influx logging is enabled. Starting influx logging...", LoggerLevelStartup.Instance); + InfluxHelper.StartLogging(); + } + stopwatch.Stop(); Logger.Log($"Ready! Startup took {stopwatch.ElapsedMilliseconds}ms. Passing off control to ASP.NET...", LoggerLevelStartup.Instance); diff --git a/ProjectLighthouse/ProjectLighthouse.csproj b/ProjectLighthouse/ProjectLighthouse.csproj index 082c9930..b576a35c 100644 --- a/ProjectLighthouse/ProjectLighthouse.csproj +++ b/ProjectLighthouse/ProjectLighthouse.csproj @@ -9,6 +9,7 @@ + diff --git a/ProjectLighthouse/Types/Settings/ServerSettings.cs b/ProjectLighthouse/Types/Settings/ServerSettings.cs index 7b9b8c04..fcdc9a8f 100644 --- a/ProjectLighthouse/Types/Settings/ServerSettings.cs +++ b/ProjectLighthouse/Types/Settings/ServerSettings.cs @@ -68,7 +68,7 @@ namespace LBPUnion.ProjectLighthouse.Types.Settings [NotNull] public static ServerSettings Instance; - public const int CurrentConfigVersion = 2; + public const int CurrentConfigVersion = 3; [JsonPropertyName("ConfigVersionDoNotModifyOrYouWillBeSlapped")] public int ConfigVersion { get; set; } = CurrentConfigVersion; @@ -77,9 +77,11 @@ namespace LBPUnion.ProjectLighthouse.Types.Settings #endregion Meta - public string InfluxOrg { get; set; } = ""; - public string InfluxBucket { get; set; } = ""; + public bool InfluxEnabled { get; set; } = false; + public string InfluxOrg { get; set; } = "lighthouse"; + public string InfluxBucket { get; set; } = "lighthouse"; public string InfluxToken { get; set; } = ""; + public string InfluxUrl { get; set; } = "http://localhost:8086"; public string EulaText { get; set; } = "";