using Microsoft.AspNetCore.Mvc;
using RoslynBridge.WebApi.Models;
using RoslynBridge.WebApi.Services;
namespace RoslynBridge.WebApi.Controllers;
///
/// Controller for accessing query history
///
[ApiController]
[Route("api/[controller]")]
[Produces("application/json")]
public class HistoryController : ControllerBase
{
private readonly IHistoryService _historyService;
private readonly ILogger _logger;
public HistoryController(IHistoryService historyService, ILogger logger)
{
_historyService = historyService;
_logger = logger;
}
///
/// Get all history entries
///
/// List of all history entries
/// Returns the list of history entries
[HttpGet]
[ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)]
public ActionResult> GetAll()
{
var entries = _historyService.GetAll();
return Ok(entries);
}
///
/// Get a specific history entry by ID
///
/// The history entry ID
/// The history entry
/// Returns the history entry
/// Entry not found
[HttpGet("{id}")]
[ProducesResponseType(typeof(QueryHistoryEntry), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public ActionResult GetById(string id)
{
var entry = _historyService.GetById(id);
if (entry == null)
{
return NotFound(new { message = $"History entry {id} not found" });
}
return Ok(entry);
}
///
/// Get recent history entries
///
/// Number of entries to return (default: 50, max: 500)
/// List of recent history entries
/// Returns the list of recent entries
[HttpGet("recent")]
[ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)]
public ActionResult> GetRecent([FromQuery] int count = 50)
{
if (count > 500) count = 500;
if (count < 1) count = 1;
var entries = _historyService.GetRecent(count);
return Ok(entries);
}
///
/// Get history statistics
///
/// Statistics about history entries
[HttpGet("stats")]
[ProducesResponseType(typeof(object), StatusCodes.Status200OK)]
public ActionResult GetStats()
{
var entries = _historyService.GetAll();
var stats = new
{
totalEntries = _historyService.GetCount(),
successfulRequests = entries.Count(e => e.Success),
failedRequests = entries.Count(e => !e.Success),
averageDurationMs = entries.Any() ? entries.Average(e => e.DurationMs) : 0,
oldestEntry = entries.Any() ? entries.Last().Timestamp : (DateTime?)null,
newestEntry = entries.Any() ? entries.First().Timestamp : (DateTime?)null,
topPaths = entries
.GroupBy(e => e.Path)
.OrderByDescending(g => g.Count())
.Take(10)
.Select(g => new { path = g.Key, count = g.Count() })
};
return Ok(stats);
}
///
/// Clear all history entries
///
/// Confirmation message
/// History cleared successfully
[HttpDelete]
[ProducesResponseType(typeof(object), StatusCodes.Status200OK)]
public ActionResult Clear()
{
var count = _historyService.GetCount();
_historyService.Clear();
_logger.LogInformation("History cleared: {Count} entries removed", count);
return Ok(new { message = $"History cleared: {count} entries removed" });
}
}