diff --git a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/DataContainer.cs b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/DataContainer.cs new file mode 100644 index 0000000..e126ebb --- /dev/null +++ b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/DataContainer.cs @@ -0,0 +1,50 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; + +namespace LightReflectiveMirror.LoadBalancing +{ + // for stats + [Serializable] + public struct RelayServerInfo + { + public int ConnectedClients; + public int RoomCount; + public int PublicRoomCount; + public TimeSpan Uptime; + } + + [Serializable] + internal struct LoadBalancerStats + { + public int NodeCount; + public TimeSpan Uptime; + public long CCU; + public long TotalServerCount; + } + + // container for relay address info + [JsonObject(MemberSerialization.OptOut)] + public struct RelayAddress + { + public ushort Port; + public ushort EndpointPort; + public string Address; + [JsonIgnore] + public string EndpointAddress; + } + + [Serializable] + public struct Room + { + public int serverId; + public int hostId; + public string serverName; + public string serverData; + public bool isPublic; + public int maxPlayers; + public List clients; + + public RelayAddress relayInfo; + } +} diff --git a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint.cs b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint.cs index b30ea59..8fe741d 100644 --- a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint.cs +++ b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint.cs @@ -17,6 +17,17 @@ namespace LightReflectiveMirror.LoadBalancing [RestResource] public class Endpoint { + private LoadBalancerStats _stats + { + get => new() + { + NodeCount = Program.instance.availableRelayServers.Count, + Uptime = DateTime.Now - Program.startupTime, + CCU = Program.instance.GetTotalCCU(), + TotalServerCount = Program.instance.GetTotalServers(), + }; + } + /// /// Sent from an LRM server node /// adds it to the list if authenticated. @@ -136,6 +147,17 @@ namespace LightReflectiveMirror.LoadBalancing else await context.Response.SendResponseAsync(HttpStatusCode.NoContent); } + + /// + /// Returns stats. you're welcome + /// + /// + /// + [RestRoute("Get", "/api/stats/")] + public async Task GetStats(IHttpContext context) + { + await context.Response.SendResponseAsync(JsonConvert.SerializeObject(_stats)); + } } #region Startup diff --git a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Program.cs b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Program.cs index c814b8b..bf2f0c1 100644 --- a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Program.cs +++ b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Program.cs @@ -8,7 +8,7 @@ using System.Threading.Tasks; namespace LightReflectiveMirror.LoadBalancing { - class Program + partial class Program { /// /// Keeps track of all available relays. @@ -18,6 +18,7 @@ namespace LightReflectiveMirror.LoadBalancing private int _pingDelay = 10000; public static bool showDebugLogs = false; + public static DateTime startupTime; const string API_PATH = "/api/stats"; readonly string CONFIG_PATH = System.Environment.GetEnvironmentVariable("LRM_LB_CONFIG_PATH") ?? "config.json"; @@ -30,6 +31,7 @@ namespace LightReflectiveMirror.LoadBalancing { WriteTitle(); instance = this; + startupTime = DateTime.Now; if (!File.Exists(CONFIG_PATH)) { @@ -195,38 +197,4 @@ namespace LightReflectiveMirror.LoadBalancing } - // for stats - [Serializable] - public struct RelayServerInfo - { - public int ConnectedClients; - public int RoomCount; - public int PublicRoomCount; - public TimeSpan Uptime; - } - - // container for relay address info - [JsonObject(MemberSerialization.OptOut)] - public struct RelayAddress - { - public ushort Port; - public ushort EndpointPort; - public string Address; - [JsonIgnore] - public string EndpointAddress; - } - - [Serializable] - public struct Room - { - public int serverId; - public int hostId; - public string serverName; - public string serverData; - public bool isPublic; - public int maxPlayers; - public List clients; - - public RelayAddress relayInfo; - } } diff --git a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/ProgramExtra.cs b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/ProgramExtra.cs new file mode 100644 index 0000000..d5f6423 --- /dev/null +++ b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/ProgramExtra.cs @@ -0,0 +1,25 @@ +namespace LightReflectiveMirror.LoadBalancing +{ + partial class Program + { + public long GetTotalCCU() + { + long temp = 0; + + foreach (var item in availableRelayServers) + temp += item.Value.ConnectedClients; + + return temp; + } + + public long GetTotalServers() + { + int temp = 0; + + foreach (var item in availableRelayServers) + temp += item.Value.RoomCount; + + return temp; + } + } +}