| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- using System.Text.Json;
- using Admin.NET.Plugin.AiDOP.DataPlatform.Core;
- using Microsoft.AspNetCore.Http;
- using Microsoft.Extensions.DependencyInjection;
- namespace Admin.NET.Plugin.AiDOP.DataPlatform.UnifiedIngress;
- public sealed partial class MdpIngestDatasetController
- {
- [HttpGet("{id:long}/api-pull")]
- public Task<IActionResult> ReadApiPull(long id, long? factoryId) =>
- ApiPullAction(id, factoryId, TrialPermission, async (service, scope) =>
- Ok(new
- {
- profiles = await service.ListProfilesAsync(scope.TenantId, scope.FactoryId, id),
- runs = await service.ListRunsAsync(scope.TenantId, scope.FactoryId, id),
- canApprove = await Can(PublishPermission)
- }));
- [HttpPost("{id:long}/api-pull/import"), RequestSizeLimit(524288)]
- public Task<IActionResult> ImportApiPull(long id, long? factoryId, MdpApiPullImportInput input) =>
- ApiPullAction(id, factoryId, TrialPermission, (_, _) =>
- Task.FromResult<IActionResult>(Ok(MdpApiPullImport.Read(input.Kind, input.Text, input.Selection))));
- [HttpPost("{id:long}/api-pull/profiles"), RequestSizeLimit(65536)]
- public Task<IActionResult> CreateApiPull(long id, long? factoryId, MdpApiPullCreateInput input, CancellationToken token) =>
- ApiPullAction(id, factoryId, TrialPermission, async (service, scope) =>
- Ok(new { id = await service.CreateAsync(scope.TenantId, scope.FactoryId, id, input.SourceId, user.UserId, input.Plan, token) }));
- [HttpPost("{id:long}/api-pull/profiles/{profile:long}/preflight")]
- public Task<IActionResult> PreflightApiPull(long id, long profile, long? factoryId, CancellationToken token) =>
- ApiPullAction(id, factoryId, TrialPermission, async (service, scope) =>
- Ok(await service.PreflightAsync(scope.TenantId, scope.FactoryId, id, profile, user.UserId, token)));
- [HttpPost("{id:long}/api-pull/profiles/{profile:long}/activate")]
- public Task<IActionResult> ActivateApiPull(long id, long profile, long? factoryId, CancellationToken token) =>
- ApiPullAction(id, factoryId, PublishPermission, async (service, scope) =>
- {
- await service.ActivateAsync(scope.TenantId, scope.FactoryId, id, profile, user.UserId, token);
- return NoContent();
- });
- [HttpPost("{id:long}/api-pull/profiles/{profile:long}/pause")]
- public Task<IActionResult> PauseApiPull(long id, long profile, long? factoryId) =>
- ApiPullAction(id, factoryId, PublishPermission, async (service, scope) =>
- {
- await service.PauseAsync(scope.TenantId, scope.FactoryId, id, profile);
- return NoContent();
- });
- [HttpPost("{id:long}/api-pull/profiles/{profile:long}/runs")]
- public Task<IActionResult> StartApiPull(long id, long profile, long? factoryId, CancellationToken token) =>
- ApiPullAction(id, factoryId, PublishPermission, async (service, scope) =>
- Ok(new { runId = await service.StartAsync(scope.TenantId, scope.FactoryId, id, profile, token) }));
- [HttpPost("{id:long}/api-pull/runs/{run:guid}/advance")]
- public Task<IActionResult> AdvanceApiPull(long id, Guid run, long? factoryId, CancellationToken token) =>
- ApiPullAction(id, factoryId, PublishPermission, async (service, scope) =>
- Ok(new { phase = await service.AdvanceAsync(scope.TenantId, scope.FactoryId, id, run.ToString("D"), token) }));
- [HttpGet("{id:long}/api-pull/runs/{run:guid}/pages/{ordinal:int}/history")]
- public Task<IActionResult> ReadApiPullRecoveryHistory(long id, Guid run, int ordinal, long? factoryId) =>
- ApiPullAction(id, factoryId, TrialPermission, async (service, scope) =>
- Ok(await service.ReadRecoveryHistoryAsync(scope.TenantId, scope.FactoryId, id, run.ToString("D"), ordinal)));
- [HttpPost("{id:long}/api-pull/runs/{run:guid}/pages/{ordinal:int}/attempts/{attempt:int}/raw")]
- public Task<IActionResult> ExportApiPullRaw(long id, Guid run, int ordinal, int attempt, long? factoryId, CancellationToken token) =>
- ApiPullAction(id, factoryId, PublishPermission, async (service, scope) =>
- {
- var capture = await service.ExportCaptureAsync(scope.TenantId, scope.FactoryId, id, run.ToString("D"),
- ordinal, attempt, user.UserId, token);
- Response.Headers.CacheControl = "no-store";
- Response.Headers["X-Content-Type-Options"] = "nosniff";
- return File(capture.Payload, "application/octet-stream", $"{capture.BatchId}.raw");
- });
- [HttpPost("{id:long}/api-pull/runs/{run:guid}/recovery/preview"), RequestSizeLimit(4096)]
- public Task<IActionResult> PreviewApiPullRecovery(long id, Guid run, long? factoryId, MdpApiPullRecoveryPreviewInput input,
- CancellationToken token) =>
- ApiPullAction(id, factoryId, TrialPermission, async (service, scope) =>
- Ok(await service.PreviewRecoveryAsync(scope.TenantId, scope.FactoryId, id, run.ToString("D"), input.ResponsePath, token)));
- [HttpPost("{id:long}/api-pull/runs/{run:guid}/recovery"), RequestSizeLimit(4096)]
- public Task<IActionResult> RecoverApiPull(long id, Guid run, long? factoryId, MdpApiPullRecoveryInput input, CancellationToken token) =>
- ApiPullAction(id, factoryId, PublishPermission, async (service, scope) =>
- {
- await service.RecoverAsync(scope.TenantId, scope.FactoryId, id, run.ToString("D"), user.UserId, input, token);
- return NoContent();
- });
- [HttpPost("{id:long}/api-pull/runs/{run:guid}/cancel")]
- public Task<IActionResult> CancelApiPull(long id, Guid run, long? factoryId) =>
- ApiPullAction(id, factoryId, PublishPermission, async (service, scope) =>
- {
- await service.CancelAsync(scope.TenantId, scope.FactoryId, id, run.ToString("D"));
- return NoContent();
- });
- private Task<IActionResult> ApiPullAction(long id, long? factoryId, string permission,
- Func<MdpApiPullService, DatasetScope, Task<IActionResult>> action)
- => WithScope(factoryId, async scope =>
- {
- if (!await Can(permission)) return Forbid();
- if (!await Datasets(scope).AnyAsync(row => row.Id == id)) return NotFound();
- try { return await action(HttpContext.RequestServices.GetRequiredService<MdpApiPullService>(), scope); }
- catch (MdpApiPullException error)
- {
- var status = error.Code switch
- {
- "PULL_PROFILE_NOT_FOUND" or "PULL_CAPTURE_NOT_FOUND" => 404,
- "RAW_PAYLOAD_EXPIRED" => 410,
- "PULL_TARGET_FORBIDDEN" or "PULL_ADDRESS_FORBIDDEN" or "PULL_SOURCE_NOT_AUTHORIZED" or "PULL_CREDENTIAL_FORBIDDEN" => 403,
- "PULL_LOCAL_RATE_LIMITED" or "PULL_SOURCE_RATE_LIMITED" => 429,
- "PULL_TIMEOUT" => 504,
- "PULL_TEMPORARY" => 503,
- _ when error.Code.StartsWith("PULL_IMPORT", StringComparison.Ordinal) || error.Code is "PULL_CONFIG_INVALID" or "PULL_RECOVERY_INVALID" => 400,
- _ => 409
- };
- if (error.Retry is { } retry) Response.Headers.RetryAfter = Math.Clamp((int)retry.TotalSeconds, 1, 3600).ToString();
- return StatusCode(status, new { code = error.Code, traceId = HttpContext.TraceIdentifier });
- }
- catch (MdpIngestHttpException error) { return StatusCode(error.StatusCode, new { code = error.Code }); }
- catch (IngestContractException error) { return UnprocessableEntity(new { code = error.Code.ToString() }); }
- catch (Exception error) when (error is JsonException or ArgumentException)
- {
- return BadRequest(new { code = "PULL_CONFIG_INVALID" });
- }
- catch (Exception error) when (error is System.Data.Common.DbException or SqlSugarException)
- {
- return StatusCode(503, new { code = "PULL_STORE_UNAVAILABLE", traceId = HttpContext.TraceIdentifier });
- }
- });
- }
- public sealed record MdpApiPullImportInput(string Kind, string Text, string? Selection);
- public sealed record MdpApiPullCreateInput(long SourceId, MdpApiPullPlan Plan);
- public sealed record MdpApiPullRecoveryPreviewInput(string ResponsePath);
|