cs-codex-dist-tests/Tools/AutoClientCenter/TaskService.cs

117 lines
3.5 KiB
C#
Raw Normal View History

2024-09-11 10:08:06 +00:00
namespace AutoClientCenter
{
public interface ITaskService
{
AcStats GetStats();
AcTasks GetTasks();
void ProcessResults(AcTaskStep[] taskSteps);
void SetConfig(AcTasks tasks);
}
public class TaskService : ITaskService
{
2024-09-11 12:00:22 +00:00
private readonly CidRepo cidRepo = new CidRepo();
2024-09-11 10:08:06 +00:00
private readonly List<long> downloadTimes = new List<long>();
private readonly AcStats stats = new AcStats
{
ServiceStartUtc = DateTime.UtcNow,
};
private AcTasks tasks = new AcTasks
{
2024-09-11 12:00:22 +00:00
StartTaskEverySeconds = Convert.ToInt32(TimeSpan.FromHours(8).TotalSeconds),
2024-09-11 10:08:06 +00:00
Tasks = Array.Empty<AcTask>()
};
public AcStats GetStats()
{
2024-09-11 12:00:22 +00:00
stats.DownloadTimesMillisecondsPerKb = downloadTimes.ToArray();
2024-09-11 10:08:06 +00:00
return stats;
}
public AcTasks GetTasks()
{
2024-09-11 12:00:22 +00:00
foreach (var task in tasks.Tasks)
{
foreach (var step in task.Steps)
{
if (step.DownloadStep != null) cidRepo.Assign(step.DownloadStep);
}
}
2024-09-11 10:08:06 +00:00
return tasks;
}
public void SetConfig(AcTasks newTasks)
{
2024-09-11 12:00:22 +00:00
if (newTasks.StartTaskEverySeconds < (10 * 60)) return;
2024-09-11 10:08:06 +00:00
foreach (var task in newTasks.Tasks)
{
if (task.ChanceWeight < 1) return;
foreach (var step in task.Steps)
{
if (string.IsNullOrWhiteSpace(step.Id)) return;
if (step.UploadStep == null && step.StoreStep == null && step.DownloadStep == null) return;
}
}
tasks = newTasks;
}
public void ProcessResults(AcTaskStep[] taskSteps)
{
2024-09-11 12:00:22 +00:00
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);
2024-09-11 10:08:06 +00:00
}
}
}