From d03179997a4ecf6cf71add73b16ee0d38c3922ab Mon Sep 17 00:00:00 2001 From: jvyden Date: Wed, 9 Mar 2022 17:34:25 -0500 Subject: [PATCH] Log recent matches for individual games to influx (#230) --- ProjectLighthouse/Helpers/InfluxHelper.cs | 36 ++++++++++++++++--- ProjectLighthouse/Helpers/StatisticsHelper.cs | 5 +++ 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/ProjectLighthouse/Helpers/InfluxHelper.cs b/ProjectLighthouse/Helpers/InfluxHelper.cs index 3876a2b5..5fc66bfc 100644 --- a/ProjectLighthouse/Helpers/InfluxHelper.cs +++ b/ProjectLighthouse/Helpers/InfluxHelper.cs @@ -1,9 +1,12 @@ +using System; +using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using InfluxDB.Client; using InfluxDB.Client.Writes; using Kettu; using LBPUnion.ProjectLighthouse.Logging; +using LBPUnion.ProjectLighthouse.Types; using LBPUnion.ProjectLighthouse.Types.Settings; namespace LBPUnion.ProjectLighthouse.Helpers; @@ -12,6 +15,15 @@ public static class InfluxHelper { public static readonly InfluxDBClient Client = InfluxDBClientFactory.Create(ServerSettings.Instance.InfluxUrl, ServerSettings.Instance.InfluxToken); + private static readonly List gameVersions = new() + { + GameVersion.LittleBigPlanet1, + GameVersion.LittleBigPlanet2, + GameVersion.LittleBigPlanet3, + GameVersion.LittleBigPlanetVita, + GameVersion.LittleBigPlanetPSP, + }; + public static async void Log() { using WriteApi writeApi = Client.GetWriteApi(); @@ -19,6 +31,15 @@ public static class InfluxHelper .Field("playerCount", await StatisticsHelper.RecentMatches()) .Field("slotCount", await StatisticsHelper.SlotCount()); + foreach (GameVersion gameVersion in gameVersions) + { + PointData gamePoint = PointData.Measurement("lighthouse") + .Tag("game", gameVersion.ToString()) + .Field("playerCountGame", await StatisticsHelper.RecentMatchesForGame(gameVersion)); + + writeApi.WritePoint(ServerSettings.Instance.InfluxBucket, ServerSettings.Instance.InfluxOrg, gamePoint); + } + writeApi.WritePoint(ServerSettings.Instance.InfluxBucket, ServerSettings.Instance.InfluxOrg, point); writeApi.Flush(); @@ -34,10 +55,17 @@ public static class InfluxHelper { while (true) { - #pragma warning disable CS4014 - Log(); - #pragma warning restore CS4014 -// Logger.Log("Logged.", LoggerLevelInflux.Instance); + try + { + Log(); + } + catch(Exception e) + { + Logger.Log("Exception while logging: ", LoggerLevelInflux.Instance); + + foreach (string line in e.ToString().Split("\n")) Logger.Log(line, LoggerLevelInflux.Instance); + } + Thread.Sleep(60000); } } diff --git a/ProjectLighthouse/Helpers/StatisticsHelper.cs b/ProjectLighthouse/Helpers/StatisticsHelper.cs index 3cd876ba..aab77a92 100644 --- a/ProjectLighthouse/Helpers/StatisticsHelper.cs +++ b/ProjectLighthouse/Helpers/StatisticsHelper.cs @@ -1,5 +1,6 @@ using System.Linq; using System.Threading.Tasks; +using LBPUnion.ProjectLighthouse.Types; using Microsoft.EntityFrameworkCore; namespace LBPUnion.ProjectLighthouse.Helpers; @@ -10,6 +11,10 @@ public static class StatisticsHelper public static async Task RecentMatches() => await database.LastContacts.Where(l => TimestampHelper.Timestamp - l.Timestamp < 300).CountAsync(); + public static async Task RecentMatchesForGame + (GameVersion gameVersion) + => await database.LastContacts.Where(l => TimestampHelper.Timestamp - l.Timestamp < 300 && l.GameVersion == gameVersion).CountAsync(); + public static async Task SlotCount() => await database.Slots.CountAsync(); public static async Task UserCount() => await database.Users.CountAsync(u => !u.Banned);