merge: integration branch claude/keen-knuth-uhxv8w (remote editor after review) into the Library workstream
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
81 files changed
+3827
-263
No files matched your search
@@ -0,0 +1,75 @@
|
||||
using System.Text;
|
||||
using Titano.Core.Model;
|
||||
using Titano.Protocol;
|
||||
|
||||
namespace Titano.Data.Jobs;
|
||||
|
||||
/// <summary>
|
||||
/// Wire names of job kinds, states and priorities in API v1: kebab-case (<c>deep-analyse</c>) and lower-case
|
||||
/// (<c>running</c>). The server writes them; clients read the server's jobs back with <see cref="FromDto"/>.
|
||||
/// </summary>
|
||||
public static class JobWire
|
||||
{
|
||||
public static string Kind(JobKind kind)
|
||||
{
|
||||
var name = kind.ToString();
|
||||
var sb = new StringBuilder(name.Length + 4);
|
||||
for (var i = 0; i < name.Length; i++)
|
||||
{
|
||||
if (i > 0 && char.IsUpper(name[i])) sb.Append('-');
|
||||
sb.Append(char.ToLowerInvariant(name[i]));
|
||||
}
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
public static bool TryParseKind(string? text, out JobKind kind) => TryParse(text, Kind, out kind);
|
||||
|
||||
public static string State(JobState state) => state.ToString().ToLowerInvariant();
|
||||
|
||||
public static bool TryParseState(string? text, out JobState state) => TryParse(text, State, out state);
|
||||
|
||||
public static string Priority(JobPriority priority) => priority.ToString().ToLowerInvariant();
|
||||
|
||||
public static bool TryParsePriority(string? text, out JobPriority priority) => TryParse(text, Priority, out priority);
|
||||
|
||||
/// <summary>A server job as the client's job model; null for a kind or state this build does not know.</summary>
|
||||
public static Job? FromDto(JobDto dto)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(dto);
|
||||
if (!TryParseKind(dto.Kind, out var kind) || !TryParseState(dto.State, out var state)) return null;
|
||||
return new Job
|
||||
{
|
||||
Id = dto.Id,
|
||||
Kind = kind,
|
||||
TargetIds = dto.TargetIds,
|
||||
TargetLabel = dto.TargetLabel,
|
||||
State = state,
|
||||
Priority = TryParsePriority(dto.Priority, out var priority) ? priority : JobPriority.Normal,
|
||||
ProgressDone = dto.ProgressDone,
|
||||
ProgressTotal = dto.ProgressTotal,
|
||||
ItemsPerSecond = dto.ItemsPerSecond,
|
||||
Eta = dto.EtaSeconds is { } eta ? TimeSpan.FromSeconds(eta) : null,
|
||||
LogTail = dto.LogTail,
|
||||
NodeId = dto.NodeId,
|
||||
Attempts = dto.Attempts,
|
||||
Error = dto.Error,
|
||||
Created = dto.Created,
|
||||
Started = dto.Started,
|
||||
Finished = dto.Finished,
|
||||
};
|
||||
}
|
||||
|
||||
private static bool TryParse<T>(string? text, Func<T, string> name, out T value) where T : struct, Enum
|
||||
{
|
||||
foreach (var candidate in Enum.GetValues<T>())
|
||||
{
|
||||
if (string.Equals(name(candidate), text, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
value = candidate;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
value = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using Titano.Core.Model;
|
||||
using Titano.Core.Project;
|
||||
using Titano.Data.Repositories;
|
||||
|
||||
namespace Titano.Data.Processing;
|
||||
|
||||
/// <summary>
|
||||
/// The index's hero frame (<see cref="Sequence.HeroFrameId"/>, the frame the Library shows) follows the project's hero
|
||||
/// (<see cref="ProjectSettings.HeroFrame"/>, a frame index; frame indices are positions).
|
||||
/// </summary>
|
||||
public static class HeroFrame
|
||||
{
|
||||
/// <summary>
|
||||
/// The <see cref="SequenceEdit.HeroFrameId"/> of a project whose hero went from <paramref name="before"/> to
|
||||
/// <paramref name="after"/>: null when it did not change, else the frame at the new index, or <see cref="Guid.Empty"/>
|
||||
/// (the first frame shows) when there is none.
|
||||
/// </summary>
|
||||
public static async Task<Guid?> EditAsync(TitanoDatabase db, Guid sequenceId, int? before, int? after, CancellationToken cancellationToken)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(db);
|
||||
if (before == after) return null;
|
||||
if (after is not >= 0) return Guid.Empty;
|
||||
var frames = await db.Frames.ListBySequenceAsync(sequenceId, after.Value - 1, 1, cancellationToken).ConfigureAwait(false);
|
||||
return frames is [{ } frame] && frame.Index == after ? frame.Id : Guid.Empty;
|
||||
}
|
||||
}
|
||||
@@ -110,7 +110,7 @@ public static class ProcessingJobs
|
||||
|
||||
/// <summary>The parameters of a render job and the export they select.</summary>
|
||||
/// <exception cref="InvalidDataException">The job has no valid render parameters.</exception>
|
||||
internal static (RenderJobParameters Parameters, ExportSettings Export) ReadRender(string? parametersJson)
|
||||
public static (RenderJobParameters Parameters, ExportSettings Export) ReadRender(string? parametersJson)
|
||||
{
|
||||
var parameters = ReadParameters(parametersJson);
|
||||
return (parameters, ReadExport(parameters.PresetJson));
|
||||
|
||||
@@ -48,11 +48,14 @@ internal sealed class ProjectStore(TitanoDatabase db, SequenceWriter writer)
|
||||
var row = await db.Sequences.GetAsync(sequence.Id, cancellationToken).ConfigureAwait(false) ?? sequence;
|
||||
var current = Read(row, folder);
|
||||
var before = current.ToJson();
|
||||
var after = edit(current).ToJson();
|
||||
var edited = edit(current);
|
||||
var after = edited.ToJson();
|
||||
if (after == before) return false;
|
||||
AtomicFile.WriteAllText(folder.ProjectPath, after);
|
||||
var result = await db.Sequences.EditAsync(sequence.Id, new SequenceEdit { SettingsJson = after }, row.SettingsVersion, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
// The Library shows the project's hero frame (advice fills it, a sync takes another PC's).
|
||||
var hero = await HeroFrame.EditAsync(db, sequence.Id, current.HeroFrame, edited.HeroFrame, cancellationToken).ConfigureAwait(false);
|
||||
var result = await db.Sequences.EditAsync(sequence.Id, new SequenceEdit { SettingsJson = after, HeroFrameId = hero }, row.SettingsVersion,
|
||||
cancellationToken).ConfigureAwait(false);
|
||||
// A conflict means the API changed the settings meanwhile: apply the edit again on top of them.
|
||||
if (result.Outcome != SequenceEditOutcome.Conflict || attempt == Attempts) return true;
|
||||
}
|
||||
|
||||
@@ -74,6 +74,12 @@ public sealed record SequenceEdit
|
||||
public int? Rating { get; init; }
|
||||
public string? Notes { get; init; }
|
||||
public string? SettingsJson { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// The frame the Library shows for the sequence (the project's hero frame); <see cref="Guid.Empty"/> clears it, so
|
||||
/// the first frame shows. A frame of another sequence is ignored.
|
||||
/// </summary>
|
||||
public Guid? HeroFrameId { get; init; }
|
||||
}
|
||||
|
||||
public enum SequenceEditOutcome
|
||||
@@ -233,12 +239,17 @@ public sealed class SequenceRepository
|
||||
if (current is null) return new SequenceEditResult(SequenceEditOutcome.NotFound, null);
|
||||
if (expectedSettingsVersion is { } expected && expected != current.SettingsVersion)
|
||||
return new SequenceEditResult(SequenceEditOutcome.Conflict, current);
|
||||
var hero = current.HeroFrameId;
|
||||
if (edit.HeroFrameId == Guid.Empty) hero = null;
|
||||
else if (edit.HeroFrameId is { } frame && await session.ScalarAsync<long>("SELECT count(*) FROM frames WHERE id = $1 AND sequence_id = $2",
|
||||
[frame, id], cancellationToken).ConfigureAwait(false) > 0) hero = frame;
|
||||
var updated = current with
|
||||
{
|
||||
Title = edit.Title is null ? current.Title : NullIfEmpty(edit.Title),
|
||||
Notes = edit.Notes is null ? current.Notes : NullIfEmpty(edit.Notes),
|
||||
Rating = edit.Rating ?? current.Rating,
|
||||
SettingsJson = edit.SettingsJson ?? current.SettingsJson,
|
||||
HeroFrameId = hero,
|
||||
SettingsVersion = current.SettingsVersion + 1,
|
||||
UpdatedAt = DateTimeOffset.UtcNow,
|
||||
};
|
||||
|
||||
@@ -20,8 +20,10 @@ namespace Titano.Data.Sync;
|
||||
/// </summary>
|
||||
public sealed class RemoteSequence
|
||||
{
|
||||
internal RemoteSequence(string server, Sequence sequence, IReadOnlyList<Frame> frames, IReadOnlyList<string?> files)
|
||||
/// <param name="files">Absolute path of each frame's proxy in frame order; null (and the frame flagged missing) when it is not cached.</param>
|
||||
public RemoteSequence(string server, Sequence sequence, IReadOnlyList<Frame> frames, IReadOnlyList<string?> files)
|
||||
{
|
||||
if (files.Count != frames.Count) throw new ArgumentException("One file entry per frame is required.", nameof(files));
|
||||
Server = server;
|
||||
Sequence = sequence;
|
||||
Frames = frames;
|
||||
|
||||
@@ -270,6 +270,10 @@ public sealed class SyncService
|
||||
/// <summary>A server sequence from the proxy cache, for the editor; null when its proxies were never fetched or were cleared.</summary>
|
||||
public RemoteSequence? OpenRemote(string server, Guid remoteId) => Proxies.Load(server, remoteId);
|
||||
|
||||
/// <summary>The link of a local sequence to a sequence of <paramref name="server"/>; null when the server does not have it.</summary>
|
||||
public Task<SyncLink?> LinkOfAsync(string server, Guid localId, CancellationToken cancellationToken = default) =>
|
||||
_links.FindByLocalAsync(server, localId, cancellationToken);
|
||||
|
||||
// ---------------- Engine ----------------
|
||||
|
||||
/// <summary>Server ids of the sequences an upload or download job is working on.</summary>
|
||||
@@ -649,7 +653,25 @@ public sealed class SyncService
|
||||
return path;
|
||||
}
|
||||
|
||||
internal static string SavedCopyName(DateTime localTime) => string.Create(CultureInfo.InvariantCulture, $"project.this-pc-{localTime:yyyyMMdd-HHmmss}.json");
|
||||
/// <summary>The file name of a "Save both" copy kept at <paramref name="localTime"/>.</summary>
|
||||
public static string SavedCopyName(DateTime localTime) =>
|
||||
string.Create(CultureInfo.InvariantCulture, $"{SavedCopyPrefix}{localTime.ToString(SavedCopyTimeFormat, CultureInfo.InvariantCulture)}.json");
|
||||
|
||||
/// <summary>File name pattern of "Save both" copies in a sequence's <c>.titano</c> folder.</summary>
|
||||
public const string SavedCopyPattern = SavedCopyPrefix + "*.json";
|
||||
|
||||
private const string SavedCopyPrefix = "project.this-pc-";
|
||||
private const string SavedCopyTimeFormat = "yyyyMMdd-HHmmss";
|
||||
|
||||
/// <summary>When a "Save both" copy was kept (local time, from its name); null for a name of another shape.</summary>
|
||||
public static DateTime? SavedCopyTime(string fileName)
|
||||
{
|
||||
var name = Path.GetFileNameWithoutExtension(fileName);
|
||||
return name.StartsWith(SavedCopyPrefix, StringComparison.Ordinal)
|
||||
&& DateTime.TryParseExact(name[SavedCopyPrefix.Length..], SavedCopyTimeFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out var time)
|
||||
? time
|
||||
: null;
|
||||
}
|
||||
|
||||
private async Task ForgetAsync(SyncLink link, CancellationToken cancellationToken)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user