Skip to content

Commit a7526aa

Browse files
committed
setup center api
1 parent e7d9e83 commit a7526aa

14 files changed

+346
-2
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net7.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<UserSecretsId>80f55fc5-9f8f-4bb4-89bc-314ed7379c5f</UserSecretsId>
8+
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.20.1" />
13+
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
14+
</ItemGroup>
15+
16+
</Project>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<ActiveDebugProfile>Container (Dockerfile)</ActiveDebugProfile>
5+
</PropertyGroup>
6+
</Project>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@AutoClientCenter_HostAddress = http://localhost:5185
2+
3+
GET {{AutoClientCenter_HostAddress}}/weatherforecast/
4+
Accept: application/json
5+
6+
###
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
3+
namespace AutoClientCenter.Controllers
4+
{
5+
[ApiController]
6+
[Route("[controller]")]
7+
public class ConfigController : ControllerBase
8+
{
9+
private readonly ITaskService taskService;
10+
11+
public ConfigController(ITaskService taskService)
12+
{
13+
this.taskService = taskService;
14+
}
15+
16+
[HttpGet("Stats")]
17+
public AcStats Get()
18+
{
19+
return taskService.GetStats();
20+
}
21+
22+
[HttpPost("Set")]
23+
public void Post([FromBody] AcTasks tasks)
24+
{
25+
taskService.SetConfig(tasks);
26+
}
27+
}
28+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
3+
namespace AutoClientCenter.Controllers
4+
{
5+
[ApiController]
6+
[Route("[controller]")]
7+
public class TasksController : ControllerBase
8+
{
9+
private readonly ITaskService taskService;
10+
private static readonly object processLock = new object();
11+
12+
public TasksController(ITaskService taskService)
13+
{
14+
this.taskService = taskService;
15+
}
16+
17+
[HttpGet]
18+
public AcTasks Get()
19+
{
20+
return taskService.GetTasks();
21+
}
22+
23+
[HttpPost("Results")]
24+
public void Post([FromBody] AcTaskStep[] taskSteps)
25+
{
26+
Task.Run(() =>
27+
{
28+
lock (processLock)
29+
{
30+
taskService.ProcessResults(taskSteps);
31+
}
32+
});
33+
}
34+
}
35+
}

AutoClientCenter/Dockerfile

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.
2+
3+
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
4+
USER app
5+
WORKDIR /app
6+
EXPOSE 8080
7+
EXPOSE 8081
8+
9+
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
10+
ARG BUILD_CONFIGURATION=Release
11+
WORKDIR /src
12+
COPY ["AutoClientCenter/AutoClientCenter.csproj", "AutoClientCenter/"]
13+
RUN dotnet restore "./AutoClientCenter/AutoClientCenter.csproj"
14+
COPY . .
15+
WORKDIR "/src/AutoClientCenter"
16+
RUN dotnet build "./AutoClientCenter.csproj" -c $BUILD_CONFIGURATION -o /app/build
17+
18+
FROM build AS publish
19+
ARG BUILD_CONFIGURATION=Release
20+
RUN dotnet publish "./AutoClientCenter.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
21+
22+
FROM base AS final
23+
WORKDIR /app
24+
COPY --from=publish /app/publish .
25+
ENTRYPOINT ["dotnet", "AutoClientCenter.dll"]

AutoClientCenter/Model.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
namespace AutoClientCenter
2+
{
3+
public class AcTasks
4+
{
5+
public TimeSpan StartTaskEvery { get; set; } = TimeSpan.FromHours(6);
6+
public AcTask[] Tasks { get; set; } = Array.Empty<AcTask>();
7+
}
8+
9+
public class AcTask
10+
{
11+
public int ChanceWeight { get; set; }
12+
public AcTaskStep[] Steps { get; set; } = Array.Empty<AcTaskStep>();
13+
}
14+
15+
public class AcTaskStep
16+
{
17+
public string Id { get; set; } = string.Empty;
18+
public AcUploadStep? UploadStep { get; set; }
19+
public AcStoreStep? StoreStep { get; set; }
20+
public AcDownloadStep? DownloadStep { get; set; }
21+
public string? ResultErrorMsg { get; set; }
22+
}
23+
24+
public class AcUploadStep
25+
{
26+
public long SizeInBytes { get; set; }
27+
public string? ResultCid { get; set; }
28+
}
29+
30+
public class AcStoreStep
31+
{
32+
public int ContractDurationMinutes { get; set; }
33+
public int ContractExpiryMinutes { get; set; }
34+
public int NumHosts { get; set; }
35+
public int HostTolerance { get; set; }
36+
public int Price { get; set; }
37+
public int RequiredCollateral { get; set; }
38+
public string? ResultPurchaseId { get; set; }
39+
public string? ResultCid { get; set; }
40+
}
41+
42+
public class AcDownloadStep
43+
{
44+
public string[] Cids { get; set; } = Array.Empty<string>();
45+
public long[] ResultDownloadTimeSeconds { get; set; } = Array.Empty<long>();
46+
}
47+
48+
public class AcStats
49+
{
50+
public int NumberOfAutoClients { get; set; } todo send client peerId
51+
52+
public DateTime ServiceStartUtc { get; set; } = DateTime.MinValue;
53+
public int TotalUploads { get; set; }
54+
public int TotalUploadsFailed { get; set; }
55+
public int TotalDownloads { get; set; }
56+
public long[] DownloadTimesSeconds { get; set; } = Array.Empty<long>();
57+
public int TotalDownloadsFailed { get; set; }
58+
public int TotalContractsStarted { get; set; }
59+
public int TotalContractsCompleted { get; set; }
60+
public int TotalContractsExpired { get; set; }
61+
public int TotalContractsFailed { get; set; }
62+
}
63+
}

AutoClientCenter/Program.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
namespace AutoClientCenter
3+
{
4+
public class Program
5+
{
6+
public static void Main(string[] args)
7+
{
8+
var builder = WebApplication.CreateBuilder(args);
9+
10+
builder.Services.AddSingleton<ITaskService>(new TaskService());
11+
builder.Services.AddControllers();
12+
builder.Services.AddEndpointsApiExplorer();
13+
builder.Services.AddSwaggerGen();
14+
15+
var app = builder.Build();
16+
17+
if (app.Environment.IsDevelopment())
18+
{
19+
app.UseSwagger();
20+
app.UseSwaggerUI();
21+
}
22+
23+
app.UseHttpsRedirection();
24+
25+
app.UseAuthorization();
26+
27+
28+
app.MapControllers();
29+
30+
app.Run();
31+
}
32+
}
33+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"profiles": {
3+
"http": {
4+
"commandName": "Project",
5+
"launchBrowser": true,
6+
"launchUrl": "swagger",
7+
"environmentVariables": {
8+
"ASPNETCORE_ENVIRONMENT": "Development"
9+
},
10+
"dotnetRunMessages": true,
11+
"applicationUrl": "http://localhost:5185"
12+
},
13+
"https": {
14+
"commandName": "Project",
15+
"launchBrowser": true,
16+
"launchUrl": "swagger",
17+
"environmentVariables": {
18+
"ASPNETCORE_ENVIRONMENT": "Development"
19+
},
20+
"dotnetRunMessages": true,
21+
"applicationUrl": "https://localhost:7077;http://localhost:5185"
22+
},
23+
"IIS Express": {
24+
"commandName": "IISExpress",
25+
"launchBrowser": true,
26+
"launchUrl": "swagger",
27+
"environmentVariables": {
28+
"ASPNETCORE_ENVIRONMENT": "Development"
29+
}
30+
},
31+
"Container (Dockerfile)": {
32+
"commandName": "Docker",
33+
"launchBrowser": true,
34+
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/swagger",
35+
"environmentVariables": {
36+
"ASPNETCORE_HTTPS_PORTS": "8081",
37+
"ASPNETCORE_HTTP_PORTS": "8080"
38+
},
39+
"publishAllPorts": true,
40+
"useSSL": true
41+
}
42+
},
43+
"$schema": "http://json.schemastore.org/launchsettings.json",
44+
"iisSettings": {
45+
"windowsAuthentication": false,
46+
"anonymousAuthentication": true,
47+
"iisExpress": {
48+
"applicationUrl": "http://localhost:50475",
49+
"sslPort": 44395
50+
}
51+
}
52+
}

AutoClientCenter/TaskService.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
namespace AutoClientCenter
2+
{
3+
public interface ITaskService
4+
{
5+
AcStats GetStats();
6+
AcTasks GetTasks();
7+
void ProcessResults(AcTaskStep[] taskSteps);
8+
void SetConfig(AcTasks tasks);
9+
}
10+
11+
public class TaskService : ITaskService
12+
{
13+
private readonly List<long> downloadTimes = new List<long>();
14+
private readonly AcStats stats = new AcStats
15+
{
16+
ServiceStartUtc = DateTime.UtcNow,
17+
};
18+
19+
private AcTasks tasks = new AcTasks
20+
{
21+
StartTaskEvery = TimeSpan.FromHours(8),
22+
Tasks = Array.Empty<AcTask>()
23+
};
24+
25+
public AcStats GetStats()
26+
{
27+
return stats;
28+
}
29+
30+
public AcTasks GetTasks()
31+
{
32+
return tasks;
33+
}
34+
35+
public void SetConfig(AcTasks newTasks)
36+
{
37+
if (newTasks.StartTaskEvery < TimeSpan.FromMinutes(10)) return;
38+
foreach (var task in newTasks.Tasks)
39+
{
40+
if (task.ChanceWeight < 1) return;
41+
foreach (var step in task.Steps)
42+
{
43+
if (string.IsNullOrWhiteSpace(step.Id)) return;
44+
if (step.UploadStep == null && step.StoreStep == null && step.DownloadStep == null) return;
45+
}
46+
}
47+
48+
tasks = newTasks;
49+
}
50+
51+
public void ProcessResults(AcTaskStep[] taskSteps)
52+
{
53+
throw new NotImplementedException();
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)