using System.Data.Common; using System.Globalization; using System.Text.Json; namespace Admin.NET.Plugin.AiDOP.DataPlatform.UnifiedIngress; public sealed partial class MdpIngestDatasetController { [HttpGet("{id:long}/raw-retention")] public Task ReadRawRetention(long id, long? factoryId, [FromServices] MdpIngestRawRetentionService retention) => LifecycleQuery(id, factoryId, TrialPermission, async scope => new { days = await retention.ReadDaysAsync(scope.TenantId, scope.FactoryId, id), canChange = await Can(PublishPermission) }); [HttpPut("{id:long}/raw-retention"), RequestSizeLimit(4096)] public Task SetRawRetention(long id, long? factoryId, RawRetentionInput input, [FromServices] MdpIngestRawRetentionService retention) => LifecycleQuery(id, factoryId, PublishPermission, async scope => { await retention.SetDaysAsync(scope.TenantId, scope.FactoryId, id, input.Days, user.UserId); return new { input.Days }; }); [HttpGet("{id:long}/batches/{batchId:guid}/trace")] public Task ReadBatchTrace(long id, Guid batchId, long? factoryId, [FromServices] MdpIngestBatchTraceService trace, CancellationToken token) => LifecycleQuery(id, factoryId, TrialPermission, async scope => await trace.ReadAsync(await BatchScope(scope, id, batchId), await Can(PublishPermission), token)); [HttpPost("{id:long}/batches/{batchId:guid}/replay-preview")] public Task PreviewBatchReplay(long id, Guid batchId, long? factoryId, [FromServices] MdpIngestReplayService replay, CancellationToken token) => LifecycleQuery(id, factoryId, TrialPermission, async scope => await replay.PreviewAsync(await BatchScope(scope, id, batchId), token)); [HttpPost("{id:long}/batches/{batchId:guid}/restore"), RequestSizeLimit(4096)] public Task RestoreBatch(long id, Guid batchId, long? factoryId, IngestReplayApproval input, [FromServices] MdpIngestReplayService replay, CancellationToken token) => LifecycleQuery(id, factoryId, PublishPermission, async scope => await replay.RestoreAsync(await BatchScope(scope, id, batchId), input, user.UserId, token)); private Task LifecycleQuery(long id, long? factoryId, string permission, Func> action) => WithScope(factoryId, async scope => { if (!await Can(permission)) return Forbid(); if (!await Datasets(scope).AnyAsync(row => row.Id == id)) return NotFound(); Response.Headers.CacheControl = "no-store"; try { var value = await action(scope); return Content(JsonSerializer.Serialize(value, MdpIngestBatchTraceService.Wire), "application/json; charset=utf-8"); } catch (MdpIngestHttpException error) { return StatusCode(error.StatusCode, new { error.Code }); } catch (Exception error) when (error is DbException or SqlSugarException) { return StatusCode(503, new { code = "RAW_LIFECYCLE_UNAVAILABLE" }); } }); private async Task BatchScope(DatasetScope scope, long datasetId, Guid batchId) { var factories = await AuthorizedFactories(scope.TenantId); return new(batchId.ToString("D"), scope.TenantId, scope.FactoryId, datasetId, string.Join(',', factories.Select(row => row.Id.ToString(CultureInfo.InvariantCulture)))); } } public sealed record RawRetentionInput(int Days);