using System.Collections.Concurrent;
using RoslynBridge.WebApi.Models;
namespace RoslynBridge.WebApi.Services;
///
/// In-memory implementation of history service
///
public class HistoryService : IHistoryService
{
private readonly ConcurrentQueue _entries = new();
private readonly ILogger _logger;
private readonly int _maxEntries;
public HistoryService(ILogger logger, IConfiguration configuration)
{
_logger = logger;
_maxEntries = configuration.GetValue("History:MaxEntries", 1000);
}
public void Add(QueryHistoryEntry entry)
{
_entries.Enqueue(entry);
// Trim old entries if we exceed max
while (_entries.Count > _maxEntries)
{
_entries.TryDequeue(out _);
}
_logger.LogDebug("Added history entry: {Id} - {Path}", entry.Id, entry.Path);
}
public IEnumerable GetAll()
{
return _entries.Reverse();
}
public QueryHistoryEntry? GetById(string id)
{
return _entries.FirstOrDefault(e => e.Id == id);
}
public IEnumerable GetRecent(int count = 50)
{
return _entries.Reverse().Take(count);
}
public void Clear()
{
_entries.Clear();
_logger.LogInformation("History cleared");
}
public int GetCount()
{
return _entries.Count;
}
}