Files
Chronos/src/Chronos.App/Jobs/JobText.cs
T
Alby96andClaude Opus 5.5 b95bc997d5 feat(jobs): show what mass passes do now on the desktop, the server section and the admin panel
A running job now says what it does now: "Verifying photo 812 of 2,400 · Harbour sunset · sequence 3 of 41". A paused or
failed job says where it stopped. The line appears in the Jobs table under the target, in the job bar (at most 60
characters, the item dropped first, the bar text also its tooltip), in the Archive's recent maintenance (refreshed at
most twice a second), in the editor's server-job line and in the sync tooltip. Byte jobs now count sizes in the bar.

While connected, the Server section lists the server's running and paused jobs, live through the jobs API and the
jobProgress events. Pause, Resume and Cancel are offered only to an account that manages the archive, which is what
the server requires to act on someone else's job; for any other account the list is read-only. The list hides while
nothing runs, and the capture gains a server-jobs scene.

The admin panel's Jobs page and dashboard show the same line under the target and update it with every progress event.
The steps, parts and sub-steps are in English and Italian, and an unknown step shows its counts only.

Refs #65

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 13:42:47 +01:00

225 lines
12 KiB
C#
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Chronos.App.Infrastructure;
using Chronos.Core.Localization;
using Chronos.Core.Model;
namespace Chronos.App.Jobs;
/// <summary>Localized texts of jobs: kind, speed, time left, place in the queue, state (Jobs screen and job bar).</summary>
public static class JobText
{
/// <summary>"Render", "Deep analysis", …</summary>
public static string Kind(JobKind kind) => Loc.L("Jobs.Kind." + kind);
/// <summary>True for jobs whose items are frames, so their speed reads in fps.</summary>
public static bool CountsFrames(JobKind kind) =>
kind is JobKind.Render or JobKind.Proxies or JobKind.Thumbs or JobKind.DeepAnalyse or JobKind.WorkspaceRender;
/// <summary>True for jobs whose progress counts bytes (hash verification, card import).</summary>
public static bool CountsBytes(JobKind kind) => kind is JobKind.Verify or JobKind.Import;
/// <summary>"11.3 fps" for frame jobs, "57 MB/s" for byte jobs, "41/s" otherwise; empty when unknown.</summary>
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,
};
/// <summary>"1,240 / 3,600"; empty while the total is unknown.</summary>
public static string Counter(long done, long total) =>
total > 0 ? Loc.L("Jobs.Counter", Formatting.Count(done), Formatting.Count(total)) : string.Empty;
/// <summary>"1,240 / 3,600", or "52 GB / 1.1 TB" for a job that counts bytes; empty while the total is unknown.</summary>
public static string Counter(JobKind kind, long done, long total) => total <= 0 ? string.Empty
: CountsBytes(kind) ? Loc.L("Jobs.Counter", Formatting.Bytes(done), Formatting.Bytes(total))
: Counter(done, total);
/// <summary>The longest status line the job bar shows (<see cref="Bar"/>); the item goes first, the full line is in the Jobs table.</summary>
public const int BarStatusLength = 60;
/// <summary>
/// What a job does now (owner review 5, #65), from its <paramref name="detail"/>: the step ("Verifying photo 812 of 2,400"),
/// the item ("Harbour sunset", a folder kept left to right) and the part ("sequence 3 of 41"), each left out when empty;
/// a clean-up or maintenance sub-step names itself in place of the step. A step this build does not know (a newer server)
/// shows its counts only. Empty for no detail.
/// </summary>
public static string Status(JobDetail? detail) => Status(detail, int.MaxValue);
/// <summary>
/// <see cref="Status(JobDetail?)"/> in at most <paramref name="maxLength"/> characters: a longer line drops the item
/// first, then is cut with an ellipsis.
/// </summary>
public static string Status(JobDetail? detail, int maxLength)
{
if (detail is null) return string.Empty;
var (step, consumed) = StepPhrase(detail);
var item = consumed ? null : ItemText(detail.Item);
var part = PartPhrase(detail);
var full = JoinParts(step, item, part);
if (full.Length <= maxLength) return full;
if (item is not null && JoinParts(step, null, part) is { Length: > 0 } shorter) full = shorter;
return Cut(full, maxLength);
}
/// <summary>
/// The status line of a Jobs row: what a running job does now; "Stopped at …" with where a paused or failed job stopped;
/// empty for queued and finished jobs, and for a job whose handler reports no detail.
/// </summary>
public static string RowStatus(JobEntry entry)
{
ArgumentNullException.ThrowIfNull(entry);
return entry.State switch
{
JobState.Running => Status(entry.Detail),
JobState.Paused or JobState.Failed when Status(entry.Job.Detail) is { Length: > 0 } status => Loc.L("Jobs.Detail.StoppedAt", status),
_ => string.Empty,
};
}
/// <summary>The tooltip of a row's status line: the line whole, then the speed and the time left while the job runs.</summary>
public static string RowStatusToolTip(JobEntry entry)
{
ArgumentNullException.ThrowIfNull(entry);
var parts = new List<string>(3);
if (RowStatus(entry) is { Length: > 0 } status) parts.Add(status);
if (entry.State == JobState.Running)
{
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);
}
/// <summary>
/// The step phrase, counted when the item is ("Verifying photo 812 of 2,400"), and whether it took the item's place (the
/// named sub-step of a clean-up or maintenance).
/// </summary>
private static (string? Phrase, bool ConsumedItem) StepPhrase(JobDetail detail)
{
var counted = detail.ItemTotal > 0;
var done = Formatting.Count(Math.Clamp(detail.ItemDone, 0, detail.ItemTotal));
var total = Formatting.Count(detail.ItemTotal);
var counts = counted ? Loc.L("Jobs.Detail.Count", done, total) : null;
// A clean-up step ("job-history") and a maintenance step ("packs") name themselves.
var named = detail.Step switch
{
JobStep.Cleanup => Known("Jobs.Schedule.Step." + Pascal(detail.Item)),
JobStep.Maintenance => Known("Jobs.Maintenance.Step." + Pascal(detail.Item)),
_ => null,
};
if (named is not null) return (JoinParts(Loc.L(named), counts, null), true);
if (detail.Step == JobStep.None) return (counts, false);
var key = "Jobs.Step." + detail.Step;
// The item of a clean-up or maintenance step is a key, not data: one this build does not know is never shown.
if (detail.Step is JobStep.Cleanup or JobStep.Maintenance) return (JoinParts(Known(key) is { } step ? Loc.L(step) : null, counts, null), true);
if (counted && Known(key + ".Count") is { } countKey) return (Loc.L(countKey, done, total), false);
if (Known(key) is null) return (counts, false);
return (JoinParts(Loc.L(key), counts, null), false);
}
/// <summary>"sequence 3 of 41" when the job has more than one part; null otherwise.</summary>
private static string? PartPhrase(JobDetail detail)
{
if (detail.Parts <= 1) return null;
var part = Formatting.Count(Math.Clamp(detail.Part, 0, detail.Parts));
var parts = Formatting.Count(detail.Parts);
return detail.PartUnit != JobPartUnit.None && Known("Jobs.Part." + detail.PartUnit) is { } key
? Loc.L(key, part, parts)
: Loc.L("Jobs.Detail.Count", part, parts);
}
/// <summary>The item as data: a folder or a path kept left to right inside right-to-left text (DECISIONS row 218).</summary>
private static string? ItemText(string? item)
{
if (string.IsNullOrWhiteSpace(item)) return null;
return item.Contains('/') || item.Contains('\\') ? Formatting.LeftToRight(item) : item;
}
/// <summary>"job-history" → "JobHistory"; null for a key that is not one (data from a newer peer).</summary>
private static string? Pascal(string? key)
{
if (string.IsNullOrEmpty(key) || key.Length > 40 || !key.All(c => char.IsAsciiLetterOrDigit(c) || c is '-' or '_')) return null;
return string.Concat(key.Split('-', '_', StringSplitOptions.RemoveEmptyEntries).Select(p => char.ToUpperInvariant(p[0]) + p[1..]));
}
/// <summary>The key when the catalog has it (the step, unit or sub-step of a newer peer may have none); null otherwise.</summary>
private static string? Known(string? key) => key is not null && !key.EndsWith('.') && Loc.Get(key, Loc.Culture) is not null ? key : null;
private static string JoinParts(string? a, string? b, string? c)
{
var pieces = new List<string>(3);
if (!string.IsNullOrEmpty(a)) pieces.Add(a);
if (!string.IsNullOrEmpty(b)) pieces.Add(b);
if (!string.IsNullOrEmpty(c)) pieces.Add(c);
return string.Join(Loc.Separator, pieces);
}
/// <summary>Cuts <paramref name="text"/> to <paramref name="max"/> characters with an ellipsis, closing a left-to-right island it cut into.</summary>
private static string Cut(string text, int max)
{
if (text.Length <= max) return text;
var length = Math.Max(1, max - 1);
if (char.IsHighSurrogate(text[length - 1])) length--;
var cut = text[..length] + "…";
return cut.Count(c => c == '‪') > cut.Count(c => c == '‬') ? cut + "‬" : cut;
}
/// <summary>
/// The "Left" column: time left while running, "after n" while queued, the finish time of the history.
/// </summary>
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,
};
/// <summary>State word, with the run time for done jobs ("done · 2:41").</summary>
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;
}
/// <summary>Where the job runs: "this PC" for this machine's workers, the node name otherwise.</summary>
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);
}
/// <summary>The job was claimed by one of this machine's numbered workers ("node#0", "node#1", …).</summary>
public static bool IsLocal(string? nodeId, string localWorkerId) =>
!string.IsNullOrEmpty(nodeId) && NodeName(nodeId).Equals(localWorkerId, StringComparison.OrdinalIgnoreCase);
/// <summary>"Kind: target" ("Render: Harbour sunset → 4K HEVC"), what a row is called for screen readers.</summary>
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];
}
/// <summary>
/// Job bar line (DESIGN §4): "Render · Harbour sunset · 2,232 / 3,600 · 11.3 fps · 2 min left"; with a detail, what the
/// job does now in place of the counter ("Scan · Archive 2024 · Indexing photo 1,240 of 3,600 · folder 12 of 340 · 41/s ·
/// 3 min left", at most <see cref="BarStatusLength"/> characters of it); a job that counts bytes shows sizes.
/// </summary>
public static string Bar(JobEntry entry)
{
ArgumentNullException.ThrowIfNull(entry);
var parts = new List<string>(5) { Kind(entry.Job.Kind) };
if (!string.IsNullOrWhiteSpace(entry.Job.TargetLabel)) parts.Add(entry.Job.TargetLabel!);
if (Status(entry.Detail, BarStatusLength) is { Length: > 0 } status) parts.Add(status);
else if (Counter(entry.Job.Kind, 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);
}
}