Re-organised statistic endpoints and services/helpers
This commit is contained in:
49
MoonlightServers.Daemon/Helpers/HostSystemHelper.cs
Normal file
49
MoonlightServers.Daemon/Helpers/HostSystemHelper.cs
Normal 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";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
36
MoonlightServers.Daemon/Helpers/OwnProcessHelper.cs
Normal file
36
MoonlightServers.Daemon/Helpers/OwnProcessHelper.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user