using Chronos.Core.Model;
using Chronos.Data.Jobs;
namespace Chronos.Data.Tests.Jobs;
///
/// Every progress report a job engine publishes, kept to tell which details a handler sent and in what order (owner review
/// 5, #65). The engine it records has no publish throttle (), so each report a handler makes goes out.
///
internal sealed class DetailRecorder
{
private readonly List _published = [];
/// A started engine's options that publish every report: one worker, fast polls and checkpoints, no progress interval.
public static JobEngineOptions Options(string worker = "detail") => new()
{
Workers = 1,
WorkerId = worker,
PollInterval = TimeSpan.FromMilliseconds(50),
CheckpointInterval = TimeSpan.FromMilliseconds(50),
ProgressInterval = TimeSpan.Zero,
};
/// A new engine with , recorded, not started.
public JobEngine Engine(ChronosDatabase db, IEnumerable handlers, string worker = "detail")
{
var engine = new JobEngine(db, [.. handlers], Options(worker));
engine.Progress += p =>
{
lock (_published) _published.Add(p);
};
return engine;
}
/// The reports of in the order they went out.
public IReadOnlyList Of(Guid jobId)
{
lock (_published) return [.. _published.Where(p => p.JobId == jobId)];
}
/// The details of , without repeats of the one before.
public IReadOnlyList Details(Guid jobId)
{
var details = new List();
foreach (var report in Of(jobId))
{
if (report.Detail is { } detail && (details.Count == 0 || details[^1] != detail)) details.Add(detail);
}
return details;
}
/// The steps of in order, each once in a row.
public IReadOnlyList Steps(Guid jobId)
{
var steps = new List();
foreach (var detail in Details(jobId))
{
if (steps.Count == 0 || steps[^1] != detail.Step) steps.Add(detail.Step);
}
return steps;
}
}