Implements center service

This commit is contained in:
benbierens 2024-09-11 14:00:22 +02:00
parent a7526aaed1
commit f7fa35c7ba
No known key found for this signature in database
GPG Key ID: 877D2C2E09A22F3A
4 changed files with 150 additions and 14 deletions

View File

@ -1,6 +1,9 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup> <PropertyGroup>
<ActiveDebugProfile>Container (Dockerfile)</ActiveDebugProfile> <ActiveDebugProfile>IIS Express</ActiveDebugProfile>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DebuggerFlavor>ProjectDebugger</DebuggerFlavor>
</PropertyGroup> </PropertyGroup>
</Project> </Project>

View File

@ -0,0 +1,76 @@
namespace AutoClientCenter
{
public class CidRepo
{
private readonly Random random = new Random();
private readonly object _lock = new object();
private readonly List<CidEntry> entries = new List<CidEntry>();
public void Add(string cid, long knownSize)
{
lock (_lock)
{
entries.Add(new CidEntry(cid, knownSize));
}
}
public void AddEncoded(string originalCid, string encodedCid)
{
lock (_lock)
{
var entry = entries.SingleOrDefault(e => e.Cid == originalCid);
if (entry == null) return;
entry.Encoded = encodedCid;
}
}
public void Assign(AcDownloadStep downloadStep)
{
lock (_lock)
{
while (true)
{
if (!entries.Any()) return;
var i = random.Next(0, entries.Count);
var entry = entries[i];
if (entry.CreatedUtc < (DateTime.UtcNow + TimeSpan.FromHours(18)))
{
entries.RemoveAt(i);
}
else
{
downloadStep.Cid = entry.Cid;
return;
}
}
}
}
public long? GetSizeKbsForCid(string cid)
{
lock (_lock)
{
var entry = entries.SingleOrDefault(e => e.Cid == cid);
if (entry == null) return null;
return entry.KnownSize;
}
}
}
public class CidEntry
{
public CidEntry(string cid, long knownSize)
{
Cid = cid;
KnownSize = knownSize;
}
public string Cid { get; }
public string Encoded { get; set; } = string.Empty;
public long KnownSize { get; }
public DateTime CreatedUtc { get; } = DateTime.UtcNow;
}
}

View File

@ -2,7 +2,7 @@
{ {
public class AcTasks public class AcTasks
{ {
public TimeSpan StartTaskEvery { get; set; } = TimeSpan.FromHours(6); public int StartTaskEverySeconds { get; set; }
public AcTask[] Tasks { get; set; } = Array.Empty<AcTask>(); public AcTask[] Tasks { get; set; } = Array.Empty<AcTask>();
} }
@ -36,28 +36,25 @@
public int Price { get; set; } public int Price { get; set; }
public int RequiredCollateral { get; set; } public int RequiredCollateral { get; set; }
public string? ResultPurchaseId { get; set; } public string? ResultPurchaseId { get; set; }
public string? ResultCid { get; set; } public string? ResultOriginalCid { get; set; }
public string? ResultEncodedCid { get; set; }
} }
public class AcDownloadStep public class AcDownloadStep
{ {
public string[] Cids { get; set; } = Array.Empty<string>(); public string Cid { get; set; } = string.Empty;
public long[] ResultDownloadTimeSeconds { get; set; } = Array.Empty<long>(); public long ResultDownloadTimeMilliseconds { get; set; }
} }
public class AcStats public class AcStats
{ {
public int NumberOfAutoClients { get; set; } todo send client peerId
public DateTime ServiceStartUtc { get; set; } = DateTime.MinValue; public DateTime ServiceStartUtc { get; set; } = DateTime.MinValue;
public int TotalUploads { get; set; } public int TotalUploads { get; set; }
public int TotalUploadsFailed { get; set; } public int TotalUploadsFailed { get; set; }
public int TotalDownloads { get; set; } public int TotalDownloads { get; set; }
public long[] DownloadTimesSeconds { get; set; } = Array.Empty<long>(); public long[] DownloadTimesMillisecondsPerKb { get; set; } = Array.Empty<long>();
public int TotalDownloadsFailed { get; set; } public int TotalDownloadsFailed { get; set; }
public int TotalContractsStarted { get; set; } public int TotalContractsStarted { get; set; }
public int TotalContractsCompleted { get; set; } public int TotalContractStartsFailed { get; set; }
public int TotalContractsExpired { get; set; }
public int TotalContractsFailed { get; set; }
} }
} }

View File

@ -10,6 +10,7 @@
public class TaskService : ITaskService public class TaskService : ITaskService
{ {
private readonly CidRepo cidRepo = new CidRepo();
private readonly List<long> downloadTimes = new List<long>(); private readonly List<long> downloadTimes = new List<long>();
private readonly AcStats stats = new AcStats private readonly AcStats stats = new AcStats
{ {
@ -18,23 +19,31 @@
private AcTasks tasks = new AcTasks private AcTasks tasks = new AcTasks
{ {
StartTaskEvery = TimeSpan.FromHours(8), StartTaskEverySeconds = Convert.ToInt32(TimeSpan.FromHours(8).TotalSeconds),
Tasks = Array.Empty<AcTask>() Tasks = Array.Empty<AcTask>()
}; };
public AcStats GetStats() public AcStats GetStats()
{ {
stats.DownloadTimesMillisecondsPerKb = downloadTimes.ToArray();
return stats; return stats;
} }
public AcTasks GetTasks() public AcTasks GetTasks()
{ {
foreach (var task in tasks.Tasks)
{
foreach (var step in task.Steps)
{
if (step.DownloadStep != null) cidRepo.Assign(step.DownloadStep);
}
}
return tasks; return tasks;
} }
public void SetConfig(AcTasks newTasks) public void SetConfig(AcTasks newTasks)
{ {
if (newTasks.StartTaskEvery < TimeSpan.FromMinutes(10)) return; if (newTasks.StartTaskEverySeconds < (10 * 60)) return;
foreach (var task in newTasks.Tasks) foreach (var task in newTasks.Tasks)
{ {
if (task.ChanceWeight < 1) return; if (task.ChanceWeight < 1) return;
@ -50,7 +59,58 @@
public void ProcessResults(AcTaskStep[] taskSteps) public void ProcessResults(AcTaskStep[] taskSteps)
{ {
throw new NotImplementedException(); foreach (var step in taskSteps) ProcessResults(step);
}
private void ProcessResults(AcTaskStep step)
{
ProcessResult(step.UploadStep);
ProcessResult(step.StoreStep);
ProcessResult(step.DownloadStep);
}
private void ProcessResult(AcUploadStep? uploadStep)
{
if (uploadStep == null) return;
if (string.IsNullOrWhiteSpace(uploadStep.ResultCid))
{
stats.TotalUploadsFailed++;
}
else
{
stats.TotalUploads++;
cidRepo.Add(uploadStep.ResultCid, uploadStep.SizeInBytes);
}
}
private void ProcessResult(AcStoreStep? storeStep)
{
if (storeStep == null) return;
if (string.IsNullOrWhiteSpace(storeStep.ResultOriginalCid) ||
string.IsNullOrWhiteSpace(storeStep.ResultEncodedCid) ||
string.IsNullOrWhiteSpace(storeStep.ResultPurchaseId))
{
stats.TotalContractStartsFailed++;
}
else
{
stats.TotalContractsStarted++;
cidRepo.AddEncoded(storeStep.ResultOriginalCid, storeStep.ResultEncodedCid);
}
}
private void ProcessResult(AcDownloadStep? downloadStep)
{
if (downloadStep == null) return;
var kbs = cidRepo.GetSizeKbsForCid(downloadStep.Cid);
if (kbs == null) return;
var milliseconds = downloadStep.ResultDownloadTimeMilliseconds;
var millisecondsPerKb = milliseconds / kbs.Value;
downloadTimes.Add(millisecondsPerKb);
} }
} }
} }