2026-07-20 10:30:01 +00:00
|
|
|
using Heimdall.Application.DTOs;
|
2026-07-22 19:54:55 +00:00
|
|
|
using Heimdall.Application.Stats.Queries.GetStats;
|
|
|
|
|
using Heimdall.Application.Stats.Queries.GetAgentStats;
|
2026-07-20 10:30:01 +00:00
|
|
|
using Heimdall.Infrastructure.Security;
|
2026-07-22 19:54:55 +00:00
|
|
|
using MediatR;
|
2026-07-20 10:30:01 +00:00
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.AspNetCore.RateLimiting;
|
|
|
|
|
|
|
|
|
|
namespace Heimdall.Api.Controllers;
|
|
|
|
|
|
|
|
|
|
[ApiController]
|
|
|
|
|
[Route("api/stats")]
|
|
|
|
|
[EnableRateLimiting("Api")]
|
|
|
|
|
public class StatsController : ControllerBase
|
|
|
|
|
{
|
2026-07-22 19:54:55 +00:00
|
|
|
private readonly IMediator _mediator;
|
2026-07-20 10:30:01 +00:00
|
|
|
|
2026-07-22 19:54:55 +00:00
|
|
|
public StatsController(IMediator mediator) => _mediator = mediator;
|
2026-07-20 10:30:01 +00:00
|
|
|
|
|
|
|
|
/// <summary>Get system-wide statistics.</summary>
|
2026-08-11 08:32:41 +02:00
|
|
|
[Authorize(AuthenticationSchemes = "JwtBearer,Cookies", Policy = AppPermissions.SiteRead)]
|
2026-07-20 10:30:01 +00:00
|
|
|
[HttpGet]
|
|
|
|
|
[ProducesResponseType(typeof(StatsResponse), StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status400BadRequest)]
|
|
|
|
|
public async Task<IActionResult> GetStats(CancellationToken ct)
|
|
|
|
|
{
|
2026-07-22 19:54:55 +00:00
|
|
|
var result = await _mediator.Send(new GetStatsQuery(), ct);
|
2026-07-20 10:30:01 +00:00
|
|
|
if (!result.IsSuccess)
|
|
|
|
|
return StatusCode(result.StatusCode ?? 400, new { error = result.Error });
|
|
|
|
|
return Ok(new { data = result.Data, timestamp = DateTime.UtcNow });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Get agent-specific KPIs for the dashboard.</summary>
|
2026-08-11 08:32:41 +02:00
|
|
|
[Authorize(AuthenticationSchemes = "JwtBearer,Cookies", Policy = AppPermissions.SiteRead)]
|
2026-07-20 10:30:01 +00:00
|
|
|
[HttpGet("agent-stats")]
|
|
|
|
|
[ProducesResponseType(typeof(List<AgentStatItem>), StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status400BadRequest)]
|
|
|
|
|
public async Task<IActionResult> GetAgentStats(CancellationToken ct)
|
|
|
|
|
{
|
2026-07-22 19:54:55 +00:00
|
|
|
var result = await _mediator.Send(new GetAgentStatsQuery(), ct);
|
2026-07-20 10:30:01 +00:00
|
|
|
if (!result.IsSuccess)
|
|
|
|
|
return StatusCode(result.StatusCode ?? 400, new { error = result.Error });
|
|
|
|
|
return Ok(result.Data);
|
|
|
|
|
}
|
|
|
|
|
}
|