Implements center service
This commit is contained in:
parent
a7526aaed1
commit
f7fa35c7ba
|
@ -1,6 +1,9 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<ActiveDebugProfile>Container (Dockerfile)</ActiveDebugProfile>
|
||||
<ActiveDebugProfile>IIS Express</ActiveDebugProfile>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
|
||||
<DebuggerFlavor>ProjectDebugger</DebuggerFlavor>
|
||||
</PropertyGroup>
|
||||
</Project>
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -2,7 +2,7 @@
|
|||
{
|
||||
public class AcTasks
|
||||
{
|
||||
public TimeSpan StartTaskEvery { get; set; } = TimeSpan.FromHours(6);
|
||||
public int StartTaskEverySeconds { get; set; }
|
||||
public AcTask[] Tasks { get; set; } = Array.Empty<AcTask>();
|
||||
}
|
||||
|
||||
|
@ -36,28 +36,25 @@
|
|||
public int Price { get; set; }
|
||||
public int RequiredCollateral { 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 string[] Cids { get; set; } = Array.Empty<string>();
|
||||
public long[] ResultDownloadTimeSeconds { get; set; } = Array.Empty<long>();
|
||||
public string Cid { get; set; } = string.Empty;
|
||||
public long ResultDownloadTimeMilliseconds { get; set; }
|
||||
}
|
||||
|
||||
public class AcStats
|
||||
{
|
||||
public int NumberOfAutoClients { get; set; } todo send client peerId
|
||||
|
||||
public DateTime ServiceStartUtc { get; set; } = DateTime.MinValue;
|
||||
public int TotalUploads { get; set; }
|
||||
public int TotalUploadsFailed { 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 TotalContractsStarted { get; set; }
|
||||
public int TotalContractsCompleted { get; set; }
|
||||
public int TotalContractsExpired { get; set; }
|
||||
public int TotalContractsFailed { get; set; }
|
||||
public int TotalContractStartsFailed { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
|
||||
public class TaskService : ITaskService
|
||||
{
|
||||
private readonly CidRepo cidRepo = new CidRepo();
|
||||
private readonly List<long> downloadTimes = new List<long>();
|
||||
private readonly AcStats stats = new AcStats
|
||||
{
|
||||
|
@ -18,23 +19,31 @@
|
|||
|
||||
private AcTasks tasks = new AcTasks
|
||||
{
|
||||
StartTaskEvery = TimeSpan.FromHours(8),
|
||||
StartTaskEverySeconds = Convert.ToInt32(TimeSpan.FromHours(8).TotalSeconds),
|
||||
Tasks = Array.Empty<AcTask>()
|
||||
};
|
||||
|
||||
public AcStats GetStats()
|
||||
{
|
||||
stats.DownloadTimesMillisecondsPerKb = downloadTimes.ToArray();
|
||||
return stats;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public void SetConfig(AcTasks newTasks)
|
||||
{
|
||||
if (newTasks.StartTaskEvery < TimeSpan.FromMinutes(10)) return;
|
||||
if (newTasks.StartTaskEverySeconds < (10 * 60)) return;
|
||||
foreach (var task in newTasks.Tasks)
|
||||
{
|
||||
if (task.ChanceWeight < 1) return;
|
||||
|
@ -50,7 +59,58 @@
|
|||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue