InfluxDB Logging support

This commit is contained in:
jvyden 2021-11-19 01:45:47 -05:00
commit 3c42c0cf55
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
7 changed files with 87 additions and 14 deletions

View file

@ -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<IActionResult> TotalPlayerCount()
{
int recentMatches = await this.database.LastMatches.Where(l => TimestampHelper.Timestamp - l.Timestamp < 60).CountAsync();
return this.Ok(recentMatches.ToString());
}
public async Task<IActionResult> TotalPlayerCount() => this.Ok((await StatisticsHelper.RecentMatches()).ToString()!);
[HttpGet("planetStats")]
public async Task<IActionResult> 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<IActionResult> TotalLevelCount() => this.Ok((await this.database.Slots.CountAsync()).ToString());
public async Task<IActionResult> TotalLevelCount() => this.Ok((await StatisticsHelper.SlotCount()).ToString());
}
}

View file

@ -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();
}
}
}

View file

@ -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<int> RecentMatches() => await database.LastMatches.Where(l => TimestampHelper.Timestamp - l.Timestamp < 60).CountAsync();
public static async Task<int> SlotCount() => await database.Slots.CountAsync();
public static async Task<int> MMPicksCount() => await database.Slots.CountAsync(s => s.TeamPick);
}
}

View file

@ -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
{

View file

@ -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);

View file

@ -9,6 +9,7 @@
<ItemGroup>
<PackageReference Include="BCrypt.Net-Next" Version="4.0.2"/>
<PackageReference Include="InfluxDB.Client" Version="3.1.0"/>
<PackageReference Include="JetBrains.Annotations" Version="2021.3.0"/>
<PackageReference Include="Kettu" Version="1.2.1"/>
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="6.0.0"/>

View file

@ -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; } = "";