Re-organised statistic endpoints and services/helpers

This commit is contained in:
2024-12-13 20:35:22 +01:00
parent d15c5326ed
commit edc91229e1
13 changed files with 218 additions and 191 deletions

View File

@@ -0,0 +1,49 @@
using System.Runtime.InteropServices;
using MoonCore.Attributes;
namespace MoonlightServers.Daemon.Helpers;
[Singleton]
public class HostSystemHelper
{
public string GetOsName()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
// Windows platform detected
var osVersion = Environment.OSVersion.Version;
return $"Windows {osVersion.Major}.{osVersion.Minor}.{osVersion.Build}";
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
var releaseRaw = File
.ReadAllLines("/etc/os-release")
.FirstOrDefault(x => x.StartsWith("PRETTY_NAME="));
if (string.IsNullOrEmpty(releaseRaw))
return "Linux (unknown release)";
var release = releaseRaw
.Replace("PRETTY_NAME=", "")
.Replace("\"", "");
if (string.IsNullOrEmpty(release))
return "Linux (unknown release)";
return release;
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
// macOS platform detected
var osVersion = Environment.OSVersion.Version;
return $"Shitty macOS {osVersion.Major}.{osVersion.Minor}.{osVersion.Build}";
}
// Unknown platform
return "Unknown";
}
}

View File

@@ -0,0 +1,36 @@
using System.Diagnostics;
using MoonCore.Attributes;
namespace MoonlightServers.Daemon.Helpers;
[Singleton]
public class OwnProcessHelper
{
public long GetMemoryUsage()
{
var process = Process.GetCurrentProcess();
var bytes = process.PrivateMemorySize64;
return bytes;
}
public TimeSpan GetUptime()
{
var process = Process.GetCurrentProcess();
var uptime = DateTime.Now - process.StartTime;
return uptime;
}
public int CpuUsage()
{
var process = Process.GetCurrentProcess();
var cpuTime = process.TotalProcessorTime;
var wallClockTime = DateTime.UtcNow - process.StartTime.ToUniversalTime();
var cpuUsage = (int)(100.0 * cpuTime.TotalMilliseconds / wallClockTime.TotalMilliseconds /
Environment.ProcessorCount);
return cpuUsage;
}
}