using Cleanuparr.Infrastructure.Stats;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace Cleanuparr.Api.Controllers;
///
/// Aggregated statistics endpoint for dashboard integrations
///
[ApiController]
[Route("api/[controller]")]
[Authorize]
public class StatsController : ControllerBase
{
private readonly ILogger _logger;
private readonly IStatsService _statsService;
public StatsController(
ILogger logger,
IStatsService statsService)
{
_logger = logger;
_statsService = statsService;
}
///
/// Gets aggregated application statistics for the specified timeframe
///
/// Timeframe in hours (default 24, range 1-720)
/// Number of recent events to include (0 = none, max 100)
/// Number of recent strikes to include (0 = none, max 100)
[HttpGet]
public async Task GetStats(
[FromQuery] int hours = 24,
[FromQuery] int includeEvents = 0,
[FromQuery] int includeStrikes = 0)
{
try
{
hours = Math.Clamp(hours, 1, 720);
includeEvents = Math.Clamp(includeEvents, 0, 100);
includeStrikes = Math.Clamp(includeStrikes, 0, 100);
var stats = await _statsService.GetStatsAsync(hours, includeEvents, includeStrikes);
return Ok(stats);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error retrieving stats");
return StatusCode(500, new { Error = "An error occurred while retrieving stats" });
}
}
}