MdpRelationalCapacityController.cs 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using Admin.NET.Plugin.AiDOP.DataPlatform.Core;
  2. using Admin.NET.Plugin.AiDOP.DataPlatform.Core.Connectors;
  3. using Microsoft.Extensions.DependencyInjection;
  4. using static Admin.NET.Plugin.AiDOP.DataPlatform.UnifiedIngress.MdpRelationalSource;
  5. namespace Admin.NET.Plugin.AiDOP.DataPlatform.UnifiedIngress;
  6. public sealed partial class MdpIngestDatasetController
  7. {
  8. [HttpPost("{id:long}/relational-trials/{profileId}/capacity-sample")]
  9. public Task<IActionResult> MeasureRelationalCapacity(long id, string profileId, long? factoryId, CancellationToken token) =>
  10. WithScope(factoryId, async scope =>
  11. {
  12. if (!await Can(TrialPermission)) return Forbid();
  13. var dataset = await Datasets(scope).FirstAsync(d => d.Id == id);
  14. if (dataset is null) return NotFound();
  15. using var deadline = CancellationTokenSource.CreateLinkedTokenSource(token);
  16. deadline.CancelAfter(TimeSpan.FromSeconds(35));
  17. var work = deadline.Token;
  18. try
  19. {
  20. var profile = (await TrialProfiles(scope, id, work)).SingleOrDefault(p => p.Id == profileId);
  21. if (profile is null) return NotFound();
  22. var source = await BoundSource(dataset, profile.SourceId);
  23. if (source is null || !dataset.Enabled || dataset.ActiveSchemaVersion is null || await Lifecycle(id) != "ACTIVE")
  24. return Conflict(new { message = "请先启用该来源已审批的采购接收版本" });
  25. var (runtime, binding, configuration) = await CreateRelationalTrial(db, profile, dataset, source);
  26. var report = await InspectProfile(profile, binding, work);
  27. var blocked = report.Checks.FirstOrDefault(check => check.Severity == "block");
  28. if (blocked is not null) return Conflict(new { message = blocked.Message, code = blocked.Code });
  29. var generation = profile.Preflight?.SourceGeneration is > 0 ? profile.Preflight.SourceGeneration : null;
  30. var sample = await MdpDatasetSandbox.MeasureCapacityAsync(Settings, runtime, binding, configuration,
  31. generation, Math.Clamp(profile.SyncPageSize, 1, 50), report.Advice?.EstimatedRows, work);
  32. var history = profile.SyncEnabled
  33. ? await HttpContext.RequestServices.GetRequiredService<MdpRelationalSyncService>()
  34. .ReadCapacityHistoryAsync(profile, binding, work)
  35. : null;
  36. return Ok(new { sample, history });
  37. }
  38. catch (OperationCanceledException) when (!token.IsCancellationRequested)
  39. {
  40. return StatusCode(504, new { message = "估算试跑超时,请核对来源读取和沙箱响应时间" });
  41. }
  42. catch (MdpIngestHttpException error)
  43. {
  44. return StatusCode(error.StatusCode, new { message = "估算配置未通过接收校验", code = error.Code });
  45. }
  46. catch (ConnectorContractException error)
  47. {
  48. return Conflict(new { message = "来源未通过估算读取检查", code = error.Code.ToString() });
  49. }
  50. catch (IngestContractException error)
  51. {
  52. return Conflict(new { message = "估算样本未通过采购接收校验", code = error.Code.ToString() });
  53. }
  54. catch (Exception error) when (error is System.Data.Common.DbException or SqlSugarException or IOException
  55. or System.Text.Json.JsonException or ArgumentException or InvalidOperationException or UnauthorizedAccessException)
  56. {
  57. return StatusCode(503, new { message = "请核对来源与隔离试跑库,或查看样本对账结果" });
  58. }
  59. });
  60. }