using Admin.NET.Plugin.AiDOP.DataPlatform.Core; using Admin.NET.Plugin.AiDOP.DataPlatform.Core.Connectors; using static Admin.NET.Plugin.AiDOP.DataPlatform.UnifiedIngress.MdpRelationalSource; namespace Admin.NET.Plugin.AiDOP.DataPlatform.UnifiedIngress; public sealed partial class MdpIngestDatasetController { [HttpPost("{id:long}/relational-trials/{profileId}/capacity-sample")] public Task MeasureRelationalCapacity(long id, string profileId, long? factoryId, CancellationToken token) => WithScope(factoryId, async scope => { if (!await Can(TrialPermission)) return Forbid(); var dataset = await Datasets(scope).FirstAsync(d => d.Id == id); if (dataset is null) return NotFound(); using var deadline = CancellationTokenSource.CreateLinkedTokenSource(token); deadline.CancelAfter(TimeSpan.FromSeconds(35)); var work = deadline.Token; try { var profile = (await TrialProfiles(scope, id, work)).SingleOrDefault(p => p.Id == profileId); if (profile is null) return NotFound(); var source = await BoundSource(dataset, profile.SourceId); if (source is null || !dataset.Enabled || dataset.ActiveSchemaVersion is null || await Lifecycle(id) != "ACTIVE") return Conflict(new { message = "请先启用该来源已审批的采购接收版本" }); var (runtime, binding, configuration) = await CreateRelationalTrial(db, profile, dataset, source); var report = await InspectProfile(profile, binding, work); var blocked = report.Checks.FirstOrDefault(check => check.Severity == "block"); if (blocked is not null) return Conflict(new { message = blocked.Message, code = blocked.Code }); var sample = await MdpDatasetSandbox.MeasureCapacityAsync(Settings, runtime, binding, configuration, Math.Clamp(profile.SyncPageSize, 1, 50), report.Advice?.EstimatedRows, work); return Ok(new { sample, history = (object?)null }); } catch (OperationCanceledException) when (!token.IsCancellationRequested) { return StatusCode(504, new { message = "估算试跑超时,请核对来源读取和沙箱响应时间" }); } catch (MdpIngestHttpException error) { return StatusCode(error.StatusCode, new { message = "估算配置未通过接收校验", code = error.Code }); } catch (ConnectorContractException error) { return Conflict(new { message = "来源未通过估算读取检查", code = error.Code.ToString() }); } catch (IngestContractException error) { return Conflict(new { message = "估算样本未通过采购接收校验", code = error.Code.ToString() }); } catch (Exception error) when (error is System.Data.Common.DbException or SqlSugarException or IOException or System.Text.Json.JsonException or ArgumentException or InvalidOperationException or UnauthorizedAccessException) { return StatusCode(503, new { message = "请核对来源与隔离试跑库,或查看样本对账结果" }); } }); }