Добавил метод для получения нагрузки на процессор

This commit is contained in:
2026-06-28 21:34:58 +03:00
parent b53c690457
commit c4ead0d8de
12 changed files with 198 additions and 3 deletions
@@ -0,0 +1,7 @@
namespace MonitorAgent.API.Contracts;
public class CpuUsageResponse(decimal total, IEnumerable<decimal> cores)
{
public decimal Total { get; set; } = total;
public List<decimal> Cores { get; set; } = new([.. cores]);
}
@@ -1,11 +1,15 @@
using Microsoft.AspNetCore.Mvc;
using MonitorAgent.API.Contracts;
using MonitorAgent.Application.Services;
using MonitorAgent.Core.Abstraction;
namespace MonitorAgent.API.Controllers;
[ApiController]
[Route("[controller]")]
public class MonitorController(IMonitorService monitorService) : ControllerBase
public class MonitorController(
IMonitorService monitorService,
CpuMonitorBackgroundService cpuMonitorService)
: ControllerBase
{
[HttpGet("uptime")]
public async Task<ActionResult<decimal>> GetUptimeAsync(CancellationToken cancellationToken = default)
@@ -30,9 +34,18 @@ public class MonitorController(IMonitorService monitorService) : ControllerBase
}
[HttpGet("loadavg")]
public async Task<ActionResult<LoadAverageResponse>> GetActionResultAsync(CancellationToken cancellationToken = default)
public async Task<ActionResult<LoadAverageResponse>> GetLoadAverageAsync(CancellationToken cancellationToken = default)
{
var response = new LoadAverageResponse(await monitorService.GetLoadAverageAsync(cancellationToken));
return Ok(response);
}
[HttpGet("cpuUsage")]
public async Task<ActionResult<CpuUsageResponse>> GetCpuUsageAsync(CancellationToken cancellationToken = default)
{
var usage = await cpuMonitorService.GetCpuUsageAsync(cancellationToken);
var response = new CpuUsageResponse(usage.TotalUsagePercent, usage.CoresUsagePercent);
return Ok(response);
}
}
@@ -1,5 +1,6 @@
using MonitorAgent.Core.Abstraction;
using MonitorAgent.Application.Services;
using MonitorAgent.Application.Settings;
var builder = WebApplication.CreateBuilder(args);
@@ -12,7 +13,12 @@ builder.Services.AddOpenApi();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.Configure<CpuMonitorSettings>(builder.Configuration.GetSection("CpuMonitorSettings"));
builder.Services.AddScoped<IMonitorService, MonitorService>();
builder.Services.AddSingleton<RawCpuMonitorService>();
builder.Services.AddSingleton<CpuMonitorBackgroundService>();
builder.Services.AddHostedService(provider => provider.GetRequiredService<CpuMonitorBackgroundService>());
var app = builder.Build();
@@ -5,5 +5,8 @@
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"CpuMonitorSettings": {
"DelayTime": 2000
}
}