Files
gitcommiteditor/Models/ImproverState.cs

52 lines
1.9 KiB
C#

namespace MarketAlly.GitCommitEditor.Models;
public sealed class ImproverState
{
private const int DefaultMaxHistorySize = 1000;
private const int DefaultMaxHistoryAgeDays = 90;
public List<ManagedRepo> Repos { get; set; } = [];
public List<RewriteOperation> History { get; set; } = [];
public Dictionary<string, string> LastAnalyzedCommits { get; set; } = [];
public DateTimeOffset LastUpdated { get; set; } = DateTimeOffset.UtcNow;
/// <summary>
/// Prunes history to keep only recent entries within size and age limits.
/// </summary>
/// <param name="maxSize">Maximum number of history entries to retain.</param>
/// <param name="maxAgeDays">Maximum age in days for history entries.</param>
/// <returns>Number of entries removed.</returns>
public int PruneHistory(int maxSize = DefaultMaxHistorySize, int maxAgeDays = DefaultMaxHistoryAgeDays)
{
var initialCount = History.Count;
var cutoffDate = DateTimeOffset.UtcNow.AddDays(-maxAgeDays);
// Remove entries older than max age
History.RemoveAll(h => h.CreatedAt < cutoffDate);
// If still over size limit, keep only the most recent entries
if (History.Count > maxSize)
{
var sorted = History.OrderByDescending(h => h.CreatedAt).ToList();
History.Clear();
History.AddRange(sorted.Take(maxSize));
}
return initialCount - History.Count;
}
/// <summary>
/// Removes history entries for repositories that are no longer registered.
/// </summary>
/// <returns>Number of orphaned entries removed.</returns>
public int RemoveOrphanedHistory()
{
var repoIds = Repos.Select(r => r.Id).ToHashSet();
var initialCount = History.Count;
History.RemoveAll(h => !repoIds.Contains(h.RepoId));
return initialCount - History.Count;
}
}