zcbot/windows-node/Zcbot.WindowsNode/Application/DiagnosticService.cs

73 lines
2.9 KiB
C#

using System.Text;
namespace Zcbot.WindowsNode;
internal interface IDiagnosticService
{
string Build(NodeConfig config);
}
internal sealed class DiagnosticService : IDiagnosticService
{
private readonly NodePaths paths;
private readonly AdapterCatalog adapters;
internal DiagnosticService(NodePaths paths)
{
this.paths = paths;
adapters = AdapterCatalog.CreateDefault(new JobRepository(paths.JobsDirectory), paths);
}
public string Build(NodeConfig config)
{
var builder = new StringBuilder()
.AppendLine("zcbot Windows Node diagnostics")
.AppendLine($"Node: {config.NodeName}")
.AppendLine($"Node ID: {config.NodeId}")
.AppendLine($"Server: {config.ServerUrl}")
.AppendLine($"Node version: {typeof(DiagnosticService).Assembly.GetName().Version}")
.AppendLine($"OS: {Environment.OSVersion.VersionString}")
.AppendLine($"Data root: {paths.RootDirectory}")
.AppendLine($"ANSYS gate: {AnsysGateState()}");
foreach (var adapter in adapters.All.OrderBy(
item => item.Capability,
StringComparer.Ordinal))
{
try
{
var runtime = adapter.DetectRuntime();
builder.AppendLine()
.AppendLine($"Capability: {adapter.Capability}")
.AppendLine($"Adapter: {adapter.AdapterVersion}")
.AppendLine($"Software: {runtime.Software} {runtime.SoftwareVersion ?? "unknown"}")
.AppendLine($"Health: {runtime.Health}")
.AppendLine($"Detail: {runtime.Detail}")
.AppendLine($"Runtime: {adapter.RuntimePath}")
.AppendLine($"Contract: {adapter.ContractPath}")
.AppendLine($"Contract SHA-256: {adapter.ContractSha256}")
.AppendLine(
$"Workspace protocol: {(adapter.WorkspaceStateFilename is null ? "disabled" : "v1")}")
.AppendLine(
$"Workspace state: {adapter.WorkspaceStateFilename ?? "none"}");
}
catch (Exception exception) when (
exception is IOException
or InvalidOperationException
or UnauthorizedAccessException)
{
builder.AppendLine()
.AppendLine($"Capability: {adapter.Capability}")
.AppendLine($"Adapter: {adapter.AdapterVersion}")
.AppendLine($"Diagnostic error: {exception.Message}");
}
}
return builder.ToString();
}
private static string AnsysGateState() => Environment.GetEnvironmentVariable(
"ZCBOT_ANSYS_242_VALIDATED",
EnvironmentVariableTarget.Machine) == "1"
? "enabled"
: "disabled";
}