MdpIngestDatasetController.Trace.cs 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System.Data.Common;
  2. using System.Globalization;
  3. using System.Text.Json;
  4. namespace Admin.NET.Plugin.AiDOP.DataPlatform.UnifiedIngress;
  5. public sealed partial class MdpIngestDatasetController
  6. {
  7. [HttpGet("{id:long}/raw-retention")]
  8. public Task<IActionResult> ReadRawRetention(long id, long? factoryId, [FromServices] MdpIngestRawRetentionService retention) =>
  9. LifecycleQuery(id, factoryId, TrialPermission, async scope => new
  10. {
  11. days = await retention.ReadDaysAsync(scope.TenantId, scope.FactoryId, id),
  12. canChange = await Can(PublishPermission)
  13. });
  14. [HttpPut("{id:long}/raw-retention"), RequestSizeLimit(4096)]
  15. public Task<IActionResult> SetRawRetention(long id, long? factoryId, RawRetentionInput input,
  16. [FromServices] MdpIngestRawRetentionService retention) =>
  17. LifecycleQuery(id, factoryId, PublishPermission, async scope =>
  18. {
  19. await retention.SetDaysAsync(scope.TenantId, scope.FactoryId, id, input.Days, user.UserId);
  20. return new { input.Days };
  21. });
  22. [HttpGet("{id:long}/batches/{batchId:guid}/trace")]
  23. public Task<IActionResult> ReadBatchTrace(long id, Guid batchId, long? factoryId,
  24. [FromServices] MdpIngestBatchTraceService trace, CancellationToken token) =>
  25. LifecycleQuery(id, factoryId, TrialPermission,
  26. async scope => await trace.ReadAsync(await BatchScope(scope, id, batchId), await Can(PublishPermission), token));
  27. [HttpPost("{id:long}/batches/{batchId:guid}/replay-preview")]
  28. public Task<IActionResult> PreviewBatchReplay(long id, Guid batchId, long? factoryId,
  29. [FromServices] MdpIngestReplayService replay, CancellationToken token) =>
  30. LifecycleQuery(id, factoryId, TrialPermission,
  31. async scope => await replay.PreviewAsync(await BatchScope(scope, id, batchId), token));
  32. [HttpPost("{id:long}/batches/{batchId:guid}/restore"), RequestSizeLimit(4096)]
  33. public Task<IActionResult> RestoreBatch(long id, Guid batchId, long? factoryId, IngestReplayApproval input,
  34. [FromServices] MdpIngestReplayService replay, CancellationToken token) =>
  35. LifecycleQuery(id, factoryId, PublishPermission,
  36. async scope => await replay.RestoreAsync(await BatchScope(scope, id, batchId), input, user.UserId, token));
  37. private Task<IActionResult> LifecycleQuery(long id, long? factoryId, string permission, Func<DatasetScope, Task<object>> action) =>
  38. WithScope(factoryId, async scope =>
  39. {
  40. if (!await Can(permission)) return Forbid();
  41. if (!await Datasets(scope).AnyAsync(row => row.Id == id)) return NotFound();
  42. Response.Headers.CacheControl = "no-store";
  43. try
  44. {
  45. var value = await action(scope);
  46. return Content(JsonSerializer.Serialize(value, MdpIngestBatchTraceService.Wire), "application/json; charset=utf-8");
  47. }
  48. catch (MdpIngestHttpException error)
  49. {
  50. return StatusCode(error.StatusCode, new { error.Code });
  51. }
  52. catch (Exception error) when (error is DbException or SqlSugarException)
  53. {
  54. return StatusCode(503, new { code = "RAW_LIFECYCLE_UNAVAILABLE" });
  55. }
  56. });
  57. private async Task<BatchAccess> BatchScope(DatasetScope scope, long datasetId, Guid batchId)
  58. {
  59. var factories = await AuthorizedFactories(scope.TenantId);
  60. return new(batchId.ToString("D"), scope.TenantId, scope.FactoryId, datasetId,
  61. string.Join(',', factories.Select(row => row.Id.ToString(CultureInfo.InvariantCulture))));
  62. }
  63. }
  64. public sealed record RawRetentionInput(int Days);