Refactor ElkSharp routing sources into partial modules
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text.Json;
|
||||
using System.Threading;
|
||||
|
||||
namespace StellaOps.ElkSharp;
|
||||
@@ -38,6 +39,7 @@ internal sealed class ElkLayoutRunDiagnostics
|
||||
public List<ElkHighwayDiagnostics> DetectedHighways { get; } = [];
|
||||
public List<string> ProgressLog { get; } = [];
|
||||
public string? ProgressLogPath { get; set; }
|
||||
public string? SnapshotPath { get; set; }
|
||||
}
|
||||
|
||||
internal sealed class ElkHighwayDiagnostics
|
||||
@@ -58,11 +60,14 @@ internal sealed class ElkIterativeStrategyDiagnostics
|
||||
public int Attempts { get; set; }
|
||||
public double TotalDurationMs { get; set; }
|
||||
public EdgeRoutingScore? BestScore { get; set; }
|
||||
public required string Outcome { get; init; }
|
||||
public required string Outcome { get; set; }
|
||||
public double BendPenalty { get; init; }
|
||||
public double DiagonalPenalty { get; init; }
|
||||
public double SoftObstacleWeight { get; init; }
|
||||
|
||||
[System.Text.Json.Serialization.JsonIgnore]
|
||||
internal bool RegisteredLive { get; set; }
|
||||
|
||||
[System.Text.Json.Serialization.JsonIgnore]
|
||||
public ElkRoutedEdge[]? BestEdges { get; set; }
|
||||
|
||||
@@ -93,6 +98,8 @@ internal sealed class ElkIterativeRouteDiagnostics
|
||||
public int SoftObstacleSegments { get; init; }
|
||||
public IReadOnlyCollection<string> RepairedEdgeIds { get; init; } = [];
|
||||
public IReadOnlyCollection<string> RepairReasons { get; init; } = [];
|
||||
public string? BuilderMode { get; init; }
|
||||
public int BuilderParallelism { get; init; }
|
||||
}
|
||||
|
||||
internal sealed class ElkIterativePhaseDiagnostics
|
||||
@@ -135,6 +142,7 @@ internal sealed class ElkDiagnosticSectionPath
|
||||
internal static class ElkLayoutDiagnostics
|
||||
{
|
||||
private static readonly AsyncLocal<ElkLayoutRunDiagnostics?> CurrentDiagnostics = new();
|
||||
private static readonly JsonSerializerOptions SnapshotJsonOptions = new() { WriteIndented = true };
|
||||
|
||||
internal static ElkLayoutRunDiagnostics? Current => CurrentDiagnostics.Value;
|
||||
|
||||
@@ -175,6 +183,8 @@ internal static class ElkLayoutDiagnostics
|
||||
{
|
||||
File.AppendAllText(diagnostics.ProgressLogPath, line + Environment.NewLine);
|
||||
}
|
||||
|
||||
WriteSnapshotLocked(diagnostics);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -189,6 +199,7 @@ internal static class ElkLayoutDiagnostics
|
||||
lock (diagnostics.SyncRoot)
|
||||
{
|
||||
diagnostics.DetectedHighways.Add(diagnostic);
|
||||
WriteSnapshotLocked(diagnostics);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -203,6 +214,49 @@ internal static class ElkLayoutDiagnostics
|
||||
lock (diagnostics.SyncRoot)
|
||||
{
|
||||
diagnostics.Attempts.Add(attempt);
|
||||
WriteSnapshotLocked(diagnostics);
|
||||
}
|
||||
}
|
||||
|
||||
internal static void FlushSnapshot()
|
||||
{
|
||||
var diagnostics = CurrentDiagnostics.Value;
|
||||
if (diagnostics is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
lock (diagnostics.SyncRoot)
|
||||
{
|
||||
WriteSnapshotLocked(diagnostics);
|
||||
}
|
||||
}
|
||||
|
||||
internal static void FlushSnapshot(ElkLayoutRunDiagnostics diagnostics)
|
||||
{
|
||||
lock (diagnostics.SyncRoot)
|
||||
{
|
||||
WriteSnapshotLocked(diagnostics);
|
||||
}
|
||||
}
|
||||
|
||||
private static void WriteSnapshotLocked(ElkLayoutRunDiagnostics diagnostics)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(diagnostics.SnapshotPath))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var snapshotPath = diagnostics.SnapshotPath!;
|
||||
var snapshotDir = Path.GetDirectoryName(snapshotPath);
|
||||
if (!string.IsNullOrWhiteSpace(snapshotDir))
|
||||
{
|
||||
Directory.CreateDirectory(snapshotDir);
|
||||
}
|
||||
|
||||
var tempPath = snapshotPath + ".tmp";
|
||||
File.WriteAllText(tempPath, JsonSerializer.Serialize(diagnostics, SnapshotJsonOptions));
|
||||
File.Copy(tempPath, snapshotPath, overwrite: true);
|
||||
File.Delete(tempPath);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user