using Chronos.App.Infrastructure;
using Chronos.Core.Localization;
using Chronos.Core.Model;
namespace Chronos.App.Jobs;
/// Localized texts of jobs: kind, speed, time left, place in the queue, state (Jobs screen and job bar).
public static class JobText
{
/// "Render", "Deep analysis", …
public static string Kind(JobKind kind) => Loc.L("Jobs.Kind." + kind);
/// True for jobs whose items are frames, so their speed reads in fps.
public static bool CountsFrames(JobKind kind) =>
kind is JobKind.Render or JobKind.Proxies or JobKind.Thumbs or JobKind.DeepAnalyse or JobKind.WorkspaceRender;
/// True for jobs whose progress counts bytes (hash verification, card import).
public static bool CountsBytes(JobKind kind) => kind is JobKind.Verify or JobKind.Import;
/// "11.3 fps" for frame jobs, "57 MB/s" for byte jobs, "41/s" otherwise; empty when unknown.
public static string Speed(JobKind kind, double? perSecond) => perSecond switch
{
> 0 and var rate when CountsBytes(kind) => Loc.L("Jobs.Speed.Bytes", Formatting.Bytes((long)rate)),
> 0 and var rate => Loc.L(CountsFrames(kind) ? "Jobs.Speed.Fps" : "Jobs.Speed.PerSecond", Formatting.Rate(rate)),
_ => string.Empty,
};
/// "1,240 / 3,600"; empty while the total is unknown.
public static string Counter(long done, long total) =>
total > 0 ? Loc.L("Jobs.Counter", Formatting.Count(done), Formatting.Count(total)) : string.Empty;
///
/// The "Left" column: time left while running, "after n" while queued, the finish time of the history.
///
public static string Left(JobEntry entry, int queuePosition) => entry.State switch
{
JobState.Running => entry.Eta is { } eta ? Formatting.Duration(eta) : string.Empty,
JobState.Queued => queuePosition > 1 ? Loc.L("Jobs.Left.After", queuePosition - 1) : Loc.L("Jobs.Left.Next"),
_ when entry.Job.Finished is { } finished => Formatting.TimeOfDay(finished),
_ => string.Empty,
};
/// State word, with the run time for done jobs ("done · 2:41").
public static string State(JobEntry entry)
{
var word = Loc.L("Jobs.State." + entry.State);
return entry.State == JobState.Done && entry.Job is { Started: { } started, Finished: { } finished }
? Loc.L("Jobs.State.WithDetail", word, Formatting.Elapsed(finished - started))
: word;
}
/// Where the job runs: "this PC" for this machine's workers, the node name otherwise.
public static string Where(string? nodeId, string localWorkerId)
{
if (string.IsNullOrEmpty(nodeId)) return string.Empty;
return IsLocal(nodeId, localWorkerId) ? Loc.L("Jobs.Where.ThisPc") : NodeName(nodeId);
}
/// The job was claimed by one of this machine's numbered workers ("node#0", "node#1", …).
public static bool IsLocal(string? nodeId, string localWorkerId) =>
!string.IsNullOrEmpty(nodeId) && NodeName(nodeId).Equals(localWorkerId, StringComparison.OrdinalIgnoreCase);
/// "Kind: target" ("Render: Harbour sunset → 4K HEVC"), what a row is called for screen readers.
public static string Name(Job job) =>
string.IsNullOrWhiteSpace(job.TargetLabel) ? Kind(job.Kind) : Loc.L("Jobs.Row.Name", Kind(job.Kind), job.TargetLabel);
private static string NodeName(string nodeId)
{
var hash = nodeId.IndexOf('#', StringComparison.Ordinal);
return hash < 0 ? nodeId : nodeId[..hash];
}
///
/// Job bar line (DESIGN §4): "Render · Harbour sunset · 2,232 / 3,600 · 11.3 fps · 2 min left".
///
public static string Bar(JobEntry entry)
{
var parts = new List(5) { Kind(entry.Job.Kind) };
if (!string.IsNullOrWhiteSpace(entry.Job.TargetLabel)) parts.Add(entry.Job.TargetLabel!);
if (Counter(entry.Done, entry.Total) is { Length: > 0 } counter) parts.Add(counter);
if (Speed(entry.Job.Kind, entry.ItemsPerSecond) is { Length: > 0 } speed) parts.Add(speed);
if (entry.Eta is { } eta) parts.Add(Loc.L("Jobs.Bar.Left", Formatting.Duration(eta)));
return string.Join(Loc.Separator, parts);
}
}