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; /// "1,240 / 3,600", or "52 GB / 1.1 TB" for a job that counts bytes; empty while the total is unknown. 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); /// The longest status line the job bar shows (); the item goes first, the full line is in the Jobs table. public const int BarStatusLength = 60; /// /// What a job does now (owner review 5, #65), from its : 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. /// public static string Status(JobDetail? detail) => Status(detail, int.MaxValue); /// /// in at most characters: a longer line drops the item /// first, then is cut with an ellipsis. /// 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); } /// /// 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. /// 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, }; } /// The tooltip of a row's status line: the line whole, then the speed and the time left while the job runs. public static string RowStatusToolTip(JobEntry entry) { ArgumentNullException.ThrowIfNull(entry); var parts = new List(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); } /// /// 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). /// 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); } /// "sequence 3 of 41" when the job has more than one part; null otherwise. 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); } /// The item as data: a folder or a path kept left to right inside right-to-left text (DECISIONS row 218). private static string? ItemText(string? item) { if (string.IsNullOrWhiteSpace(item)) return null; return item.Contains('/') || item.Contains('\\') ? Formatting.LeftToRight(item) : item; } /// "job-history" → "JobHistory"; null for a key that is not one (data from a newer peer). 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..])); } /// The key when the catalog has it (the step, unit or sub-step of a newer peer may have none); null otherwise. 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(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); } /// Cuts to characters with an ellipsis, closing a left-to-right island it cut into. 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; } /// /// 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"; 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 characters of it); a job that counts bytes shows sizes. /// public static string Bar(JobEntry entry) { ArgumentNullException.ThrowIfNull(entry); var parts = new List(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); } }