using Infrastructure.Events; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; namespace Executable.Controllers; [ApiController] [Route("api/[controller]")] public class EventsController : ControllerBase { private readonly EventDbContext _context; public EventsController(EventDbContext context) { _context = context; } /// /// Gets recent events /// [HttpGet] public async Task>> GetEvents( [FromQuery] int count = 100, [FromQuery] string? severity = null, [FromQuery] string? eventType = null, [FromQuery] string? source = null) { var query = _context.Events.AsQueryable(); // Apply filters if (!string.IsNullOrWhiteSpace(severity)) query = query.Where(e => e.Severity == severity); if (!string.IsNullOrWhiteSpace(eventType)) query = query.Where(e => e.EventType == eventType); if (!string.IsNullOrWhiteSpace(source)) query = query.Where(e => e.Source.Contains(source)); // Order and limit var events = await query .OrderByDescending(e => e.Timestamp) .Take(Math.Min(count, 1000)) // Cap at 1000 .ToListAsync(); return Ok(events); } /// /// Gets a specific event by ID /// [HttpGet("{id}")] public async Task> GetEvent(string id) { var eventEntity = await _context.Events.FindAsync(id); if (eventEntity == null) return NotFound(); return Ok(eventEntity); } /// /// Gets events by correlation ID /// [HttpGet("correlation/{correlationId}")] public async Task>> GetEventsByCorrelation(string correlationId) { var events = await _context.Events .Where(e => e.CorrelationId == correlationId) .OrderBy(e => e.Timestamp) .ToListAsync(); return Ok(events); } /// /// Gets event statistics /// [HttpGet("stats")] public async Task> GetEventStats() { var stats = new { TotalEvents = await _context.Events.CountAsync(), EventsBySeverity = await _context.Events .GroupBy(e => e.Severity) .Select(g => new { Severity = g.Key, Count = g.Count() }) .ToListAsync(), EventsByType = await _context.Events .GroupBy(e => e.EventType) .Select(g => new { EventType = g.Key, Count = g.Count() }) .OrderByDescending(x => x.Count) .Take(10) .ToListAsync(), RecentEventsCount = await _context.Events .Where(e => e.Timestamp > DateTime.UtcNow.AddHours(-24)) .CountAsync() }; return Ok(stats); } /// /// Manually triggers cleanup of old events /// [HttpPost("cleanup")] public async Task> CleanupOldEvents([FromQuery] int retentionDays = 30) { var removedCount = await _context.CleanupOldEventsAsync(retentionDays); return Ok(new { RemovedCount = removedCount, RetentionDays = retentionDays }); } /// /// Gets unique event sources /// [HttpGet("sources")] public async Task>> GetEventSources() { var sources = await _context.Events .Select(e => e.Source) .Distinct() .OrderBy(s => s) .ToListAsync(); return Ok(sources); } /// /// Gets unique event types /// [HttpGet("types")] public async Task>> GetEventTypes() { var types = await _context.Events .Select(e => e.EventType) .Distinct() .OrderBy(t => t) .ToListAsync(); return Ok(types); } }