Files
Chronos/src/Titano.Data/Processing/Jobs/AnalyseJobHandler.cs
T

54 lines
2.8 KiB
C#

using System.Globalization;
using Titano.Core.Analysis.Deep;
using Titano.Core.Localization;
using Titano.Core.Model;
using Titano.Data.Jobs;
namespace Titano.Data.Processing.Jobs;
/// <summary>
/// <see cref="JobKind.LightAnalyse"/> and <see cref="JobKind.DeepAnalyse"/> over the target sequences, one after the
/// other. Progress counts sequences (light) or frames (deep); checkpoint: the number of sequences done. A deep pass
/// interrupted inside a sequence resumes from that sequence's <c>analysis.partial</c>.
/// </summary>
internal sealed class AnalyseJobHandler(TitanoDatabase db, SequenceAnalyser analyser, JobKind kind) : IJobHandler
{
public JobKind Kind => kind;
public async Task RunAsync(JobContext context)
{
var deep = kind == JobKind.DeepAnalyse;
var targets = context.Job.TargetIds;
var first = int.TryParse(context.Checkpoint, NumberStyles.None, CultureInfo.InvariantCulture, out var next) ? next : 0;
// Deep progress is in frames across all targets, so the bar and the ETA cover the whole job.
var frames = new long[targets.Count];
if (deep)
{
for (var i = 0; i < targets.Count; i++)
frames[i] = (await db.Sequences.GetAsync(targets[i], context.Cancellation).ConfigureAwait(false))?.FrameCount ?? 0;
}
var total = deep ? frames.Sum() : targets.Count;
for (var i = first; i < targets.Count; i++)
{
context.Cancellation.ThrowIfCancellationRequested();
var before = deep ? frames[..i].Sum() : i;
var chain = new ProgressChain(context.ReportAsync);
var progress = deep ? chain.For<AnalysisProgress>(p => (before + p.Done, total, null)) : null;
var outcome = await analyser.AnalyseAsync(targets[i], deep, progress, context.Cancellation).ConfigureAwait(false);
await chain.FlushAsync().ConfigureAwait(false);
if (outcome is null) context.Log(Loc.L("Archive.Log.SequenceMissing", targets[i]));
else Report(context, outcome, deep);
await context.CheckpointAsync((i + 1).ToString(CultureInfo.InvariantCulture), deep ? before + frames[i] : i + 1, total).ConfigureAwait(false);
}
}
private static void Report(JobContext context, AnalysisOutcome outcome, bool deep)
{
var title = outcome.Sequence.DisplayTitle;
context.Log(Loc.L(deep ? "Advisor.Log.DeepDone" : "Advisor.Log.LightDone", title, outcome.Frames, outcome.Warnings));
if (outcome.SettingsChanged) context.Log(Loc.L("Advisor.Log.AutoUpdated", title));
if (outcome.SettingsUnreadable) context.Log(Loc.L("Advisor.Log.ProjectUnreadable", title));
if (outcome.Presets.Count > 0) context.Log(Loc.L("Advisor.Log.Presets", string.Join(" · ", outcome.Presets.Select(p => p.DisplayName()))));
}
}