setup center api

This commit is contained in:
Ben 2024-09-11 12:08:06 +02:00
parent e7d9e833f1
commit a7526aaed1
No known key found for this signature in database
GPG Key ID: 0F16E812E736C24B
14 changed files with 346 additions and 2 deletions

View File

@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>80f55fc5-9f8f-4bb4-89bc-314ed7379c5f</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.20.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ActiveDebugProfile>Container (Dockerfile)</ActiveDebugProfile>
</PropertyGroup>
</Project>

View File

@ -0,0 +1,6 @@
@AutoClientCenter_HostAddress = http://localhost:5185
GET {{AutoClientCenter_HostAddress}}/weatherforecast/
Accept: application/json
###

View File

@ -0,0 +1,28 @@
using Microsoft.AspNetCore.Mvc;
namespace AutoClientCenter.Controllers
{
[ApiController]
[Route("[controller]")]
public class ConfigController : ControllerBase
{
private readonly ITaskService taskService;
public ConfigController(ITaskService taskService)
{
this.taskService = taskService;
}
[HttpGet("Stats")]
public AcStats Get()
{
return taskService.GetStats();
}
[HttpPost("Set")]
public void Post([FromBody] AcTasks tasks)
{
taskService.SetConfig(tasks);
}
}
}

View File

@ -0,0 +1,35 @@
using Microsoft.AspNetCore.Mvc;
namespace AutoClientCenter.Controllers
{
[ApiController]
[Route("[controller]")]
public class TasksController : ControllerBase
{
private readonly ITaskService taskService;
private static readonly object processLock = new object();
public TasksController(ITaskService taskService)
{
this.taskService = taskService;
}
[HttpGet]
public AcTasks Get()
{
return taskService.GetTasks();
}
[HttpPost("Results")]
public void Post([FromBody] AcTaskStep[] taskSteps)
{
Task.Run(() =>
{
lock (processLock)
{
taskService.ProcessResults(taskSteps);
}
});
}
}
}

View File

@ -0,0 +1,25 @@
#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.
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
USER app
WORKDIR /app
EXPOSE 8080
EXPOSE 8081
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["AutoClientCenter/AutoClientCenter.csproj", "AutoClientCenter/"]
RUN dotnet restore "./AutoClientCenter/AutoClientCenter.csproj"
COPY . .
WORKDIR "/src/AutoClientCenter"
RUN dotnet build "./AutoClientCenter.csproj" -c $BUILD_CONFIGURATION -o /app/build
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./AutoClientCenter.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "AutoClientCenter.dll"]

63
AutoClientCenter/Model.cs Normal file
View File

@ -0,0 +1,63 @@
namespace AutoClientCenter
{
public class AcTasks
{
public TimeSpan StartTaskEvery { get; set; } = TimeSpan.FromHours(6);
public AcTask[] Tasks { get; set; } = Array.Empty<AcTask>();
}
public class AcTask
{
public int ChanceWeight { get; set; }
public AcTaskStep[] Steps { get; set; } = Array.Empty<AcTaskStep>();
}
public class AcTaskStep
{
public string Id { get; set; } = string.Empty;
public AcUploadStep? UploadStep { get; set; }
public AcStoreStep? StoreStep { get; set; }
public AcDownloadStep? DownloadStep { get; set; }
public string? ResultErrorMsg { get; set; }
}
public class AcUploadStep
{
public long SizeInBytes { get; set; }
public string? ResultCid { get; set; }
}
public class AcStoreStep
{
public int ContractDurationMinutes { get; set; }
public int ContractExpiryMinutes { get; set; }
public int NumHosts { get; set; }
public int HostTolerance { get; set; }
public int Price { get; set; }
public int RequiredCollateral { get; set; }
public string? ResultPurchaseId { get; set; }
public string? ResultCid { get; set; }
}
public class AcDownloadStep
{
public string[] Cids { get; set; } = Array.Empty<string>();
public long[] ResultDownloadTimeSeconds { get; set; } = Array.Empty<long>();
}
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 int TotalDownloadsFailed { get; set; }
public int TotalContractsStarted { get; set; }
public int TotalContractsCompleted { get; set; }
public int TotalContractsExpired { get; set; }
public int TotalContractsFailed { get; set; }
}
}

View File

@ -0,0 +1,33 @@
namespace AutoClientCenter
{
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSingleton<ITaskService>(new TaskService());
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
}
}
}

View File

@ -0,0 +1,52 @@
{
"profiles": {
"http": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"dotnetRunMessages": true,
"applicationUrl": "http://localhost:5185"
},
"https": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"dotnetRunMessages": true,
"applicationUrl": "https://localhost:7077;http://localhost:5185"
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Container (Dockerfile)": {
"commandName": "Docker",
"launchBrowser": true,
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/swagger",
"environmentVariables": {
"ASPNETCORE_HTTPS_PORTS": "8081",
"ASPNETCORE_HTTP_PORTS": "8080"
},
"publishAllPorts": true,
"useSSL": true
}
},
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:50475",
"sslPort": 44395
}
}
}

View File

@ -0,0 +1,56 @@
namespace AutoClientCenter
{
public interface ITaskService
{
AcStats GetStats();
AcTasks GetTasks();
void ProcessResults(AcTaskStep[] taskSteps);
void SetConfig(AcTasks tasks);
}
public class TaskService : ITaskService
{
private readonly List<long> downloadTimes = new List<long>();
private readonly AcStats stats = new AcStats
{
ServiceStartUtc = DateTime.UtcNow,
};
private AcTasks tasks = new AcTasks
{
StartTaskEvery = TimeSpan.FromHours(8),
Tasks = Array.Empty<AcTask>()
};
public AcStats GetStats()
{
return stats;
}
public AcTasks GetTasks()
{
return tasks;
}
public void SetConfig(AcTasks newTasks)
{
if (newTasks.StartTaskEvery < TimeSpan.FromMinutes(10)) return;
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)
{
throw new NotImplementedException();
}
}
}

View File

@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View File

@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}

View File

@ -7,7 +7,7 @@ namespace CodexPlugin
{
public class CodexContainerRecipe : ContainerRecipeFactory
{
private const string DefaultDockerImage = "codexstorage/nim-codex:sha-64b82de-dist-tests";
private const string DefaultDockerImage = "codexstorage/nim-codex:sha-1e2ad95-dist-tests";
public const string ApiPortTag = "codex_api_port";
public const string ListenPortTag = "codex_listen_port";

View File

@ -74,7 +74,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OverwatchTranscript", "Fram
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TranscriptAnalysis", "Tools\TranscriptAnalysis\TranscriptAnalysis.csproj", "{C0EEBD32-23CB-45EC-A863-79FB948508C8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MarketInsights", "Tools\MarketInsights\MarketInsights.csproj", "{004614DF-1C65-45E3-882D-59AE44282573}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MarketInsights", "Tools\MarketInsights\MarketInsights.csproj", "{004614DF-1C65-45E3-882D-59AE44282573}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutoClientCenter", "AutoClientCenter\AutoClientCenter.csproj", "{023B6C60-406B-47A1-9ED8-12688759D2CC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -202,6 +204,10 @@ Global
{004614DF-1C65-45E3-882D-59AE44282573}.Debug|Any CPU.Build.0 = Debug|Any CPU
{004614DF-1C65-45E3-882D-59AE44282573}.Release|Any CPU.ActiveCfg = Release|Any CPU
{004614DF-1C65-45E3-882D-59AE44282573}.Release|Any CPU.Build.0 = Release|Any CPU
{023B6C60-406B-47A1-9ED8-12688759D2CC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{023B6C60-406B-47A1-9ED8-12688759D2CC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{023B6C60-406B-47A1-9ED8-12688759D2CC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{023B6C60-406B-47A1-9ED8-12688759D2CC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -237,6 +243,7 @@ Global
{870DDFBE-D7ED-4196-9681-13CA947BDEA6} = {81AE04BC-CBFA-4E6F-B039-8208E9AFAAE7}
{C0EEBD32-23CB-45EC-A863-79FB948508C8} = {7591C5B3-D86E-4AE4-8ED2-B272D17FE7E3}
{004614DF-1C65-45E3-882D-59AE44282573} = {7591C5B3-D86E-4AE4-8ED2-B272D17FE7E3}
{023B6C60-406B-47A1-9ED8-12688759D2CC} = {7591C5B3-D86E-4AE4-8ED2-B272D17FE7E3}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {237BF0AA-9EC4-4659-AD9A-65DEB974250C}