166 lines
4.4 KiB
C#
Raw Normal View History

2025-04-03 09:15:39 +02:00
using Newtonsoft.Json.Linq;
2024-03-26 11:39:59 +01:00
using System.Numerics;
2024-03-26 14:07:06 +01:00
using Utils;
2024-03-26 08:58:16 +01:00
2025-01-16 11:31:50 +01:00
namespace CodexClient
2024-03-26 08:58:16 +01:00
{
public class Mapper
{
public DebugInfo Map(CodexOpenApi.DebugInfo debugInfo)
{
return new DebugInfo
{
Id = debugInfo.Id,
Spr = debugInfo.Spr,
Addrs = debugInfo.Addrs.ToArray(),
2025-01-08 11:52:48 +01:00
AnnounceAddresses = debugInfo.AnnounceAddresses.ToArray(),
2026-02-11 10:18:06 +01:00
Version = Map(debugInfo.Storage),
2024-10-29 14:02:00 +01:00
Table = Map(debugInfo.Table)
2024-03-26 08:58:16 +01:00
};
}
2024-03-26 10:03:52 +01:00
2024-03-26 14:07:06 +01:00
public LocalDatasetList Map(CodexOpenApi.DataList dataList)
2024-03-26 11:39:59 +01:00
{
2024-03-26 14:07:06 +01:00
return new LocalDatasetList
{
Content = dataList.Content.Select(Map).ToArray()
};
}
public LocalDataset Map(CodexOpenApi.DataItem dataItem)
{
return new LocalDataset
{
Cid = new ContentId(dataItem.Cid),
Manifest = MapManifest(dataItem.Manifest)
};
2024-03-26 11:39:59 +01:00
}
2025-01-16 11:31:50 +01:00
public CodexSpace Map(CodexOpenApi.Space space)
{
return new CodexSpace
{
QuotaMaxBytes = space.QuotaMaxBytes,
QuotaReservedBytes = space.QuotaReservedBytes,
QuotaUsedBytes = space.QuotaUsedBytes,
TotalBlocks = space.TotalBlocks
};
}
2026-02-11 10:18:06 +01:00
private DebugInfoVersion Map(CodexOpenApi.StorageVersion obj)
2024-03-26 10:03:52 +01:00
{
return new DebugInfoVersion
{
2024-10-29 14:02:00 +01:00
Version = obj.Version,
2026-02-11 10:18:06 +01:00
Revision = obj.Revision
2024-03-26 10:03:52 +01:00
};
}
2025-01-16 11:31:50 +01:00
private DebugInfoTable Map(CodexOpenApi.PeersTable obj)
2024-03-26 10:03:52 +01:00
{
return new DebugInfoTable
{
2024-10-29 14:02:00 +01:00
LocalNode = Map(obj.LocalNode),
Nodes = Map(obj.Nodes)
2024-03-26 10:03:52 +01:00
};
}
2025-01-16 11:31:50 +01:00
private DebugInfoTableNode Map(CodexOpenApi.Node? token)
2024-03-26 10:03:52 +01:00
{
2024-10-29 14:02:00 +01:00
if (token == null) return new DebugInfoTableNode();
2024-03-26 10:03:52 +01:00
return new DebugInfoTableNode
{
2024-10-29 14:02:00 +01:00
Address = token.Address,
NodeId = token.NodeId,
PeerId = token.PeerId,
Record = token.Record,
Seen = token.Seen
2024-03-26 10:03:52 +01:00
};
}
2025-01-16 11:31:50 +01:00
private DebugInfoTableNode[] Map(ICollection<CodexOpenApi.Node> nodes)
{
if (nodes == null || nodes.Count == 0)
{
return new DebugInfoTableNode[0];
}
2024-10-29 14:02:00 +01:00
return nodes.Select(Map).ToArray();
}
2024-03-26 14:07:06 +01:00
private Manifest MapManifest(CodexOpenApi.ManifestItem manifest)
{
return new Manifest
{
BlockSize = new ByteSize(Convert.ToInt64(manifest.BlockSize)),
2025-06-09 10:14:30 +02:00
DatasetSize = new ByteSize(Convert.ToInt64(manifest.DatasetSize)),
2026-02-11 10:18:06 +01:00
RootHash = manifest.TreeCid
2024-03-26 14:07:06 +01:00
};
}
2024-03-26 10:03:52 +01:00
private JArray JArray(IDictionary<string, object> map, string name)
{
return (JArray)map[name];
}
private JObject JObject(IDictionary<string, object> map, string name)
{
return (JObject)map[name];
}
private string StringOrEmpty(JObject obj, string name)
{
if (obj.TryGetValue(name, out var token))
{
var str = (string?)token;
if (!string.IsNullOrEmpty(str)) return str;
}
return string.Empty;
}
private bool Bool(JObject obj, string name)
{
if (obj.TryGetValue(name, out var token))
{
return (bool)token;
}
return false;
}
2024-03-26 11:39:59 +01:00
private string ToDecInt(double d)
{
var i = new BigInteger(d);
return i.ToString("D");
}
private string ToDecInt(TestToken t)
{
return t.TstWei.ToString("D");
2024-03-26 11:39:59 +01:00
}
2024-09-23 10:52:12 +02:00
2025-04-03 09:15:39 +02:00
private TestToken ToTestToken(string s)
{
return new TestToken(ToBigInt(s));
}
private long ToLong(double value)
{
return Convert.ToInt64(value);
}
private BigInteger ToBigInt(string tokens)
2024-09-23 10:52:12 +02:00
{
return BigInteger.Parse(tokens);
}
2025-04-03 09:15:39 +02:00
private TimeSpan ToTimespan(long duration)
2024-09-23 10:52:12 +02:00
{
2025-04-03 09:15:39 +02:00
return TimeSpan.FromSeconds(duration);
2024-09-23 10:52:12 +02:00
}
2025-04-03 09:15:39 +02:00
private ByteSize ToByteSize(long size)
2024-09-23 10:52:12 +02:00
{
2025-04-03 09:15:39 +02:00
return new ByteSize(size);
2024-09-23 10:52:12 +02:00
}
2024-03-26 08:58:16 +01:00
}
}