84 lines
3.0 KiB
C#
84 lines
3.0 KiB
C#
|
|
using Dapper;
|
||
|
|
using Inventario.API.Data;
|
||
|
|
using Microsoft.AspNetCore.Mvc;
|
||
|
|
|
||
|
|
namespace Inventario.API.Controllers
|
||
|
|
{
|
||
|
|
[ApiController]
|
||
|
|
[Route("api/[controller]")]
|
||
|
|
public class DashboardController : ControllerBase
|
||
|
|
{
|
||
|
|
private readonly DapperContext _context;
|
||
|
|
|
||
|
|
public DashboardController(DapperContext context)
|
||
|
|
{
|
||
|
|
_context = context;
|
||
|
|
}
|
||
|
|
|
||
|
|
public class StatItemDto
|
||
|
|
{
|
||
|
|
// Cambiamos el tipo de Label a string para que Dapper no tenga problemas
|
||
|
|
// al leer el ram_installed (que es un int). Lo formatearemos en el frontend.
|
||
|
|
public string Label { get; set; } = "";
|
||
|
|
public int Count { get; set; }
|
||
|
|
}
|
||
|
|
|
||
|
|
public class DashboardStatsDto
|
||
|
|
{
|
||
|
|
public IEnumerable<StatItemDto> OsStats { get; set; } = new List<StatItemDto>();
|
||
|
|
public IEnumerable<StatItemDto> SectorStats { get; set; } = new List<StatItemDto>();
|
||
|
|
public IEnumerable<StatItemDto> CpuStats { get; set; } = new List<StatItemDto>();
|
||
|
|
public IEnumerable<StatItemDto> RamStats { get; set; } = new List<StatItemDto>(); // <-- 1. Añadir propiedad para RAM
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpGet("stats")]
|
||
|
|
public async Task<IActionResult> GetDashboardStats()
|
||
|
|
{
|
||
|
|
var osQuery = @"
|
||
|
|
SELECT Os AS Label, COUNT(Id) AS Count
|
||
|
|
FROM dbo.equipos
|
||
|
|
WHERE Os IS NOT NULL AND Os != ''
|
||
|
|
GROUP BY Os
|
||
|
|
ORDER BY Count DESC;";
|
||
|
|
|
||
|
|
var sectorQuery = @"
|
||
|
|
SELECT s.Nombre AS Label, COUNT(e.Id) AS Count
|
||
|
|
FROM dbo.equipos e
|
||
|
|
JOIN dbo.sectores s ON e.sector_id = s.id
|
||
|
|
GROUP BY s.Nombre
|
||
|
|
ORDER BY Count DESC;";
|
||
|
|
|
||
|
|
var cpuQuery = @"
|
||
|
|
SELECT Cpu AS Label, COUNT(Id) AS Count
|
||
|
|
FROM dbo.equipos
|
||
|
|
WHERE Cpu IS NOT NULL AND Cpu != ''
|
||
|
|
GROUP BY Cpu
|
||
|
|
ORDER BY Count DESC;";
|
||
|
|
|
||
|
|
var ramQuery = @"
|
||
|
|
SELECT CAST(ram_installed AS VARCHAR) AS Label, COUNT(Id) AS Count
|
||
|
|
FROM dbo.equipos
|
||
|
|
WHERE ram_installed > 0
|
||
|
|
GROUP BY ram_installed
|
||
|
|
ORDER BY ram_installed ASC;";
|
||
|
|
|
||
|
|
using (var connection = _context.CreateConnection())
|
||
|
|
{
|
||
|
|
var osStats = await connection.QueryAsync<StatItemDto>(osQuery);
|
||
|
|
var sectorStats = await connection.QueryAsync<StatItemDto>(sectorQuery);
|
||
|
|
var cpuStats = await connection.QueryAsync<StatItemDto>(cpuQuery);
|
||
|
|
var ramStats = await connection.QueryAsync<StatItemDto>(ramQuery);
|
||
|
|
|
||
|
|
var result = new DashboardStatsDto
|
||
|
|
{
|
||
|
|
OsStats = osStats,
|
||
|
|
SectorStats = sectorStats,
|
||
|
|
CpuStats = cpuStats,
|
||
|
|
RamStats = ramStats
|
||
|
|
};
|
||
|
|
|
||
|
|
return Ok(result);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|