83 lines
3.2 KiB
C#
83 lines
3.2 KiB
C#
// File: Controllers/ProcessController.cs
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
using WebmrAPI.Models;
|
|
using WebmrAPI.Services;
|
|
|
|
namespace WebmrAPI.Controllers
|
|
{
|
|
[ApiController]
|
|
[Route("api/v1/[controller]")]
|
|
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
|
|
public class ProcessController : ControllerBase
|
|
{
|
|
private readonly ProcessMonitor _monitor;
|
|
private readonly ILogger<ProcessController> _logger;
|
|
|
|
public ProcessController(ProcessMonitor monitor, ILogger<ProcessController> logger)
|
|
{
|
|
_monitor = monitor;
|
|
_logger = logger;
|
|
}
|
|
private string GetFormattedJson<T>(T data, bool pretty)
|
|
{
|
|
var options = new JsonSerializerOptions
|
|
{
|
|
WriteIndented = pretty,
|
|
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower,
|
|
Converters = { new JsonStringEnumConverter() }
|
|
};
|
|
return JsonSerializer.Serialize(data, options);
|
|
}
|
|
|
|
[HttpGet]
|
|
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(IEnumerable<ProcessBaseInfo>))]
|
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
|
public IActionResult GetProcesses([FromQuery] bool pretty = false)
|
|
{
|
|
try
|
|
{
|
|
var processes = _monitor.GetBufferedProcesses();
|
|
return Content(GetFormattedJson(processes, pretty), "application/json");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "An error occurred when getting the list of processes");
|
|
return StatusCode(StatusCodes.Status500InternalServerError, "An internal server error occurred when receiving processes.");
|
|
}
|
|
}
|
|
|
|
[HttpGet("{pid}")]
|
|
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(ProcessInfo))]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
|
public IActionResult GetProcessById(int pid, [FromQuery] bool pretty = false)
|
|
{
|
|
try
|
|
{
|
|
var data = _monitor.GetProcessDetails(pid);
|
|
if (data == null)
|
|
{
|
|
return NotFound($"The process with the PID {pid} was not found or its details could not be obtained.");
|
|
}
|
|
return Content(GetFormattedJson(data, pretty), "application/json");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, $"An error occurred while receiving process details for PID {pid}.");
|
|
return StatusCode(StatusCodes.Status500InternalServerError, $"An internal server error occurred while receiving process details for PID {pid}.");
|
|
}
|
|
}
|
|
|
|
// TODO: Добавить эндпоинты для:
|
|
// - /api/Process/{pid}/memory_regions
|
|
// - /api/Process/{pid}/modules
|
|
// - /api/Process/{pid}/threads
|
|
// - Поиск по имени
|
|
// - Пагинация
|
|
// - Нагрузка на CPU
|
|
}
|
|
}
|