From edc91229e1eb6a63d9e9bb36e98bea28a454503b Mon Sep 17 00:00:00 2001 From: ChiaraBm Date: Fri, 13 Dec 2024 20:35:22 +0100 Subject: [PATCH] Re-organised statistic endpoints and services/helpers --- .../Services/NodeService.cs | 30 ++++-- .../Helpers/HostSystemHelper.cs | 49 +++++++++ .../Helpers/OwnProcessHelper.cs | 36 +++++++ .../StatisticsApplicationController.cs | 30 ++++++ .../Statistics/StatisticsDockerController.cs | 36 +++++++ .../Statistics/StatisticsHostController.cs | 28 +++++ .../Sys/SystemDataUsageController.cs | 33 ------ .../Controllers/Sys/SystemInfoController.cs | 33 ------ .../Services/SystemInfoService.cs | 101 ------------------ .../StatisticsApplicationResponse.cs | 8 ++ .../StatisticsDockerResponse.cs} | 8 +- .../Statistics/StatisticsHostResponse.cs | 6 ++ .../Http/Responses/Sys/SystemInfoResponse.cs | 11 -- 13 files changed, 218 insertions(+), 191 deletions(-) create mode 100644 MoonlightServers.Daemon/Helpers/HostSystemHelper.cs create mode 100644 MoonlightServers.Daemon/Helpers/OwnProcessHelper.cs create mode 100644 MoonlightServers.Daemon/Http/Controllers/Statistics/StatisticsApplicationController.cs create mode 100644 MoonlightServers.Daemon/Http/Controllers/Statistics/StatisticsDockerController.cs create mode 100644 MoonlightServers.Daemon/Http/Controllers/Statistics/StatisticsHostController.cs delete mode 100644 MoonlightServers.Daemon/Http/Controllers/Sys/SystemDataUsageController.cs delete mode 100644 MoonlightServers.Daemon/Http/Controllers/Sys/SystemInfoController.cs delete mode 100644 MoonlightServers.Daemon/Services/SystemInfoService.cs create mode 100644 MoonlightServers.DaemonShared/Http/Responses/Statistics/StatisticsApplicationResponse.cs rename MoonlightServers.DaemonShared/Http/Responses/{Sys/SystemDataUsageResponse.cs => Statistics/StatisticsDockerResponse.cs} (64%) create mode 100644 MoonlightServers.DaemonShared/Http/Responses/Statistics/StatisticsHostResponse.cs delete mode 100644 MoonlightServers.DaemonShared/Http/Responses/Sys/SystemInfoResponse.cs diff --git a/MoonlightServers.ApiServer/Services/NodeService.cs b/MoonlightServers.ApiServer/Services/NodeService.cs index ab0bbe8..a7abb32 100644 --- a/MoonlightServers.ApiServer/Services/NodeService.cs +++ b/MoonlightServers.ApiServer/Services/NodeService.cs @@ -1,6 +1,7 @@ using MoonCore.Attributes; using MoonCore.Helpers; using MoonlightServers.ApiServer.Database.Entities; +using MoonlightServers.DaemonShared.Http.Responses.Statistics; using MoonlightServers.DaemonShared.Http.Responses.Sys; namespace MoonlightServers.ApiServer.Services; @@ -28,25 +29,32 @@ public class NodeService return new HttpApiClient(httpClient); } - - public async Task GetSystemInfo(Node node) - { - using var apiClient = await CreateApiClient(node); - - return await apiClient.GetJson("api/system/info"); - } public async Task GetSystemStatus(Node node) { using var apiClient = await CreateApiClient(node); - return await apiClient.GetJson("api/system/status"); } - public async Task GetSystemDataUsage(Node node) + #region Statistics + + public async Task GetApplicationStatistics(Node node) { using var apiClient = await CreateApiClient(node); - - return await apiClient.GetJson("api/system/dataUsage"); + return await apiClient.GetJson("api/statistics/application"); } + + public async Task GetHostStatistics(Node node) + { + using var apiClient = await CreateApiClient(node); + return await apiClient.GetJson("api/statistics/host"); + } + + public async Task GetDockerStatistics(Node node) + { + using var apiClient = await CreateApiClient(node); + return await apiClient.GetJson("api/statistics/docker"); + } + + #endregion } \ No newline at end of file diff --git a/MoonlightServers.Daemon/Helpers/HostSystemHelper.cs b/MoonlightServers.Daemon/Helpers/HostSystemHelper.cs new file mode 100644 index 0000000..9196731 --- /dev/null +++ b/MoonlightServers.Daemon/Helpers/HostSystemHelper.cs @@ -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"; + } + + +} \ No newline at end of file diff --git a/MoonlightServers.Daemon/Helpers/OwnProcessHelper.cs b/MoonlightServers.Daemon/Helpers/OwnProcessHelper.cs new file mode 100644 index 0000000..bed4ab8 --- /dev/null +++ b/MoonlightServers.Daemon/Helpers/OwnProcessHelper.cs @@ -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; + } +} \ No newline at end of file diff --git a/MoonlightServers.Daemon/Http/Controllers/Statistics/StatisticsApplicationController.cs b/MoonlightServers.Daemon/Http/Controllers/Statistics/StatisticsApplicationController.cs new file mode 100644 index 0000000..7381488 --- /dev/null +++ b/MoonlightServers.Daemon/Http/Controllers/Statistics/StatisticsApplicationController.cs @@ -0,0 +1,30 @@ +using Microsoft.AspNetCore.Mvc; +using MoonlightServers.Daemon.Helpers; +using MoonlightServers.DaemonShared.Http.Responses.Statistics; + +namespace MoonlightServers.Daemon.Http.Controllers.Statistics; + +// This controller hosts endpoints for the statistics for the daemon application itself + +[ApiController] +[Route("api/statistics/application")] +public class StatisticsApplicationController : Controller +{ + private readonly OwnProcessHelper ProcessHelper; + + public StatisticsApplicationController(OwnProcessHelper processHelper) + { + ProcessHelper = processHelper; + } + + [HttpGet] + public async Task Get() + { + return new StatisticsApplicationResponse() + { + Uptime = ProcessHelper.GetUptime(), + MemoryUsage = ProcessHelper.GetMemoryUsage(), + CpuUsage = ProcessHelper.CpuUsage() + }; + } +} \ No newline at end of file diff --git a/MoonlightServers.Daemon/Http/Controllers/Statistics/StatisticsDockerController.cs b/MoonlightServers.Daemon/Http/Controllers/Statistics/StatisticsDockerController.cs new file mode 100644 index 0000000..ca01872 --- /dev/null +++ b/MoonlightServers.Daemon/Http/Controllers/Statistics/StatisticsDockerController.cs @@ -0,0 +1,36 @@ +using Microsoft.AspNetCore.Mvc; +using MoonlightServers.Daemon.Services; +using MoonlightServers.DaemonShared.Http.Responses.Statistics; + +namespace MoonlightServers.Daemon.Http.Controllers.Statistics; + +// This controller hosts endpoints for the statistics for the docker environment + +[ApiController] +[Route("api/statistics/docker")] +public class StatisticsDockerController : Controller +{ + private readonly DockerInfoService DockerInfoService; + + public StatisticsDockerController(DockerInfoService dockerInfoService) + { + DockerInfoService = dockerInfoService; + } + + [HttpGet] + public async Task Get() + { + var usage = await DockerInfoService.GetDataUsage(); + + return new StatisticsDockerResponse + { + Version = await DockerInfoService.GetDockerVersion(), + ContainersReclaimable = usage.Containers.Reclaimable, + ContainersUsed = usage.Containers.Used, + BuildCacheReclaimable = usage.BuildCache.Reclaimable, + BuildCacheUsed = usage.BuildCache.Used, + ImagesUsed = usage.Images.Used, + ImagesReclaimable = usage.Images.Reclaimable + }; + } +} \ No newline at end of file diff --git a/MoonlightServers.Daemon/Http/Controllers/Statistics/StatisticsHostController.cs b/MoonlightServers.Daemon/Http/Controllers/Statistics/StatisticsHostController.cs new file mode 100644 index 0000000..1a97a0d --- /dev/null +++ b/MoonlightServers.Daemon/Http/Controllers/Statistics/StatisticsHostController.cs @@ -0,0 +1,28 @@ +using Microsoft.AspNetCore.Mvc; +using MoonlightServers.Daemon.Helpers; +using MoonlightServers.DaemonShared.Http.Responses.Statistics; + +namespace MoonlightServers.Daemon.Http.Controllers.Statistics; + +// This controller hosts endpoints for the statistics for host system the daemon runs on + +[ApiController] +[Route("api/statistics/host")] +public class StatisticsHostController : Controller +{ + private readonly HostSystemHelper HostSystemHelper; + + public StatisticsHostController(HostSystemHelper hostSystemHelper) + { + HostSystemHelper = hostSystemHelper; + } + + [HttpGet] + public async Task Get() + { + return new() + { + OperatingSystem = HostSystemHelper.GetOsName() + }; + } +} \ No newline at end of file diff --git a/MoonlightServers.Daemon/Http/Controllers/Sys/SystemDataUsageController.cs b/MoonlightServers.Daemon/Http/Controllers/Sys/SystemDataUsageController.cs deleted file mode 100644 index d8e9efa..0000000 --- a/MoonlightServers.Daemon/Http/Controllers/Sys/SystemDataUsageController.cs +++ /dev/null @@ -1,33 +0,0 @@ -using Microsoft.AspNetCore.Mvc; -using MoonlightServers.Daemon.Services; -using MoonlightServers.DaemonShared.Http.Responses.Sys; - -namespace MoonlightServers.Daemon.Http.Controllers.Sys; - -[ApiController] -[Route("api/system/dataUsage")] -public class SystemDataUsageController : Controller -{ - private readonly DockerInfoService DockerInfoService; - - public SystemDataUsageController(DockerInfoService dockerInfoService) - { - DockerInfoService = dockerInfoService; - } - - [HttpGet] - public async Task Get() - { - var report = await DockerInfoService.GetDataUsage(); - - return new SystemDataUsageResponse() - { - ImagesReclaimable = report.Images.Reclaimable, - ImagesUsed = report.Images.Used, - ContainersReclaimable = report.Containers.Reclaimable, - ContainersUsed = report.Containers.Used, - BuildCacheReclaimable = report.BuildCache.Reclaimable, - BuildCacheUsed = report.BuildCache.Used - }; - } -} \ No newline at end of file diff --git a/MoonlightServers.Daemon/Http/Controllers/Sys/SystemInfoController.cs b/MoonlightServers.Daemon/Http/Controllers/Sys/SystemInfoController.cs deleted file mode 100644 index 69bdb30..0000000 --- a/MoonlightServers.Daemon/Http/Controllers/Sys/SystemInfoController.cs +++ /dev/null @@ -1,33 +0,0 @@ -using Microsoft.AspNetCore.Mvc; -using MoonlightServers.Daemon.Services; -using MoonlightServers.DaemonShared.Http.Responses.Sys; - -namespace MoonlightServers.Daemon.Http.Controllers.Sys; - -[ApiController] -[Route("api/system/info")] -public class SystemInfoController : Controller -{ - private readonly SystemInfoService SystemInfoService; - private readonly DockerInfoService DockerInfoService; - - public SystemInfoController(SystemInfoService systemInfoService, DockerInfoService dockerInfoService) - { - SystemInfoService = systemInfoService; - DockerInfoService = dockerInfoService; - } - - [HttpGet] - public async Task GetInfo() - { - return new SystemInfoResponse() - { - Uptime = await SystemInfoService.GetUptime(), - Version = "2.1.0", - CpuUsage = await SystemInfoService.GetCpuUsage(), - DockerVersion = await DockerInfoService.GetDockerVersion(), - MemoryUsage = await SystemInfoService.GetMemoryUsage(), - OsName = await SystemInfoService.GetOsName() - }; - } -} \ No newline at end of file diff --git a/MoonlightServers.Daemon/Services/SystemInfoService.cs b/MoonlightServers.Daemon/Services/SystemInfoService.cs deleted file mode 100644 index 77587e5..0000000 --- a/MoonlightServers.Daemon/Services/SystemInfoService.cs +++ /dev/null @@ -1,101 +0,0 @@ -using System.Diagnostics; -using System.Runtime.InteropServices; -using Docker.DotNet; -using MoonCore.Attributes; -using MoonlightServers.Daemon.Helpers; - -namespace MoonlightServers.Daemon.Services; - -[Singleton] -public class SystemInfoService -{ - private readonly ILogger Logger; - private readonly IHost Host; - - public SystemInfoService( - ILogger logger, - IHost host - ) - { - Logger = logger; - Host = host; - } - - public Task GetOsName() - { - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - { - // Windows platform detected - var osVersion = Environment.OSVersion.Version; - return Task.FromResult($"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 Task.FromResult("Linux (unknown release)"); - - var release = releaseRaw - .Replace("PRETTY_NAME=", "") - .Replace("\"", ""); - - if (string.IsNullOrEmpty(release)) - return Task.FromResult("Linux (unknown release)"); - - return Task.FromResult(release); - } - - if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) - { - // macOS platform detected - var osVersion = Environment.OSVersion.Version; - return Task.FromResult($"macOS {osVersion.Major}.{osVersion.Minor}.{osVersion.Build}"); - } - - // Unknown platform - return Task.FromResult("N/A"); - } - - public Task GetMemoryUsage() - { - var process = Process.GetCurrentProcess(); - var bytes = process.PrivateMemorySize64; - return Task.FromResult(bytes); - } - - public Task GetUptime() - { - var process = Process.GetCurrentProcess(); - var uptime = DateTime.Now - process.StartTime; - return Task.FromResult(uptime); - } - - public Task GetCpuUsage() - { - 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 Task.FromResult(cpuUsage); - } - - public Task Shutdown() - { - Logger.LogCritical("Restart of daemon has been requested"); - - Task.Run(async () => - { - await Task.Delay(TimeSpan.FromSeconds(1)); - await Host.StopAsync(CancellationToken.None); - }); - - return Task.CompletedTask; - } -} \ No newline at end of file diff --git a/MoonlightServers.DaemonShared/Http/Responses/Statistics/StatisticsApplicationResponse.cs b/MoonlightServers.DaemonShared/Http/Responses/Statistics/StatisticsApplicationResponse.cs new file mode 100644 index 0000000..6ba6f2c --- /dev/null +++ b/MoonlightServers.DaemonShared/Http/Responses/Statistics/StatisticsApplicationResponse.cs @@ -0,0 +1,8 @@ +namespace MoonlightServers.DaemonShared.Http.Responses.Statistics; + +public class StatisticsApplicationResponse +{ + public int CpuUsage { get; set; } + public long MemoryUsage { get; set; } + public TimeSpan Uptime { get; set; } +} \ No newline at end of file diff --git a/MoonlightServers.DaemonShared/Http/Responses/Sys/SystemDataUsageResponse.cs b/MoonlightServers.DaemonShared/Http/Responses/Statistics/StatisticsDockerResponse.cs similarity index 64% rename from MoonlightServers.DaemonShared/Http/Responses/Sys/SystemDataUsageResponse.cs rename to MoonlightServers.DaemonShared/Http/Responses/Statistics/StatisticsDockerResponse.cs index 9b721fb..04667ab 100644 --- a/MoonlightServers.DaemonShared/Http/Responses/Sys/SystemDataUsageResponse.cs +++ b/MoonlightServers.DaemonShared/Http/Responses/Statistics/StatisticsDockerResponse.cs @@ -1,11 +1,15 @@ -namespace MoonlightServers.DaemonShared.Http.Responses.Sys; +namespace MoonlightServers.DaemonShared.Http.Responses.Statistics; -public class SystemDataUsageResponse +public class StatisticsDockerResponse { + public string Version { get; set; } + public long ImagesUsed { get; set; } public long ImagesReclaimable { get; set; } + public long ContainersUsed { get; set; } public long ContainersReclaimable { get; set; } + public long BuildCacheUsed { get; set; } public long BuildCacheReclaimable { get; set; } } \ No newline at end of file diff --git a/MoonlightServers.DaemonShared/Http/Responses/Statistics/StatisticsHostResponse.cs b/MoonlightServers.DaemonShared/Http/Responses/Statistics/StatisticsHostResponse.cs new file mode 100644 index 0000000..c7e7dc5 --- /dev/null +++ b/MoonlightServers.DaemonShared/Http/Responses/Statistics/StatisticsHostResponse.cs @@ -0,0 +1,6 @@ +namespace MoonlightServers.DaemonShared.Http.Responses.Statistics; + +public class StatisticsHostResponse +{ + public string OperatingSystem { get; set; } +} \ No newline at end of file diff --git a/MoonlightServers.DaemonShared/Http/Responses/Sys/SystemInfoResponse.cs b/MoonlightServers.DaemonShared/Http/Responses/Sys/SystemInfoResponse.cs deleted file mode 100644 index b9b1ef5..0000000 --- a/MoonlightServers.DaemonShared/Http/Responses/Sys/SystemInfoResponse.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace MoonlightServers.DaemonShared.Http.Responses.Sys; - -public class SystemInfoResponse -{ - public string DockerVersion { get; set; } - public string Version { get; set; } - public string OsName { get; set; } - public long MemoryUsage { get; set; } - public TimeSpan Uptime { get; set; } - public int CpuUsage { get; set; } -} \ No newline at end of file