MdpDatasetReleaseController.Governance.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using Admin.NET.Plugin.AiDOP.DataPlatform.Core;
  2. using System.Text.Json;
  3. namespace Admin.NET.Plugin.AiDOP.DataPlatform.UnifiedIngress;
  4. public sealed record MdpGovernanceTrial(string Config, JsonElement Sample);
  5. public sealed partial class MdpIngestDatasetController
  6. {
  7. [HttpGet("{id:long}/governance/templates")]
  8. public Task<IActionResult> GovernanceTemplateList(long id, [FromQuery] long? factoryId) => WithScope(factoryId, async scope =>
  9. await Datasets(scope).AnyAsync(x => x.Id == id) ? Ok(GovernanceTemplates.All) : NotFound());
  10. [HttpPost("{id:long}/governance/templates/{key}/trial")]
  11. [RequestSizeLimit(192 * 1024)]
  12. public Task<IActionResult> GovernanceTemplateTrial(long id, string key, [FromQuery] long? factoryId, MdpGovernanceTrial input) =>
  13. WithScope(factoryId, async scope =>
  14. {
  15. if (!await Can(TrialPermission)) return Forbid();
  16. if (!await Datasets(scope).AnyAsync(x => x.Id == id)) return NotFound();
  17. var template = GovernanceTemplates.All.FirstOrDefault(x => x.Key == key);
  18. if (template == null) return NotFound();
  19. try
  20. {
  21. var package = GovernancePackage.Parse(input.Config, template.Fields);
  22. var converted = new Dictionary<string, JsonElement>(StringComparer.Ordinal);
  23. var issue = package.Apply(input.Sample, converted);
  24. return Ok(new { digest = package.Digest, output = issue == null ? converted : null, issue,
  25. sourceSchema = package.SourceSchema(template.SourceSchema.Stamp.Version),
  26. standardSchema = package.StandardSchema(template.StandardObject.Stamp.Version) });
  27. }
  28. catch (Exception error) when (error is JsonException or ArgumentException or DatasetChangeException)
  29. {
  30. return BadRequest(new { message = error is JsonException ? "配置包 JSON 不符合合同" : error.Message });
  31. }
  32. });
  33. [HttpPost("{id:long}/governance/templates/{key}/suggestions")]
  34. [RequestSizeLimit(24 * 1024)]
  35. public Task<IActionResult> GovernanceSuggestions(long id, string key, [FromQuery] long? factoryId, MdpGovernanceSuggestInput input) =>
  36. WithScope(factoryId, async scope =>
  37. {
  38. if (!await Can(TrialPermission)) return Forbid();
  39. if (!await Datasets(scope).AnyAsync(x => x.Id == id)) return NotFound();
  40. var template = GovernanceTemplates.All.FirstOrDefault(x => x.Key == key);
  41. if (template == null) return NotFound();
  42. if (!MdpGovernanceSuggestions.ValidFields(input.Fields))
  43. return BadRequest(new { message = "来源字段清单有重复或无效的字段名或类型" });
  44. if (!Uri.TryCreate(Settings["MDP_GOVERNANCE_AI_URL"], UriKind.Absolute, out var endpoint) ||
  45. endpoint.Scheme != Uri.UriSchemeHttps || !string.IsNullOrEmpty(endpoint.UserInfo) ||
  46. Settings["MDP_GOVERNANCE_AI_KEY"] is not { Length: > 0 } apiKey ||
  47. Settings["MDP_GOVERNANCE_AI_MODEL"] is not { Length: > 0 } model)
  48. return StatusCode(503, new { message = "此环境暂未配置映射建议服务,可手工编辑规则" });
  49. // The address and credentials belong to this environment. No payload samples or master records are sent.
  50. using var handler = new HttpClientHandler { AllowAutoRedirect = false };
  51. using var client = new HttpClient(handler);
  52. using var deadline = CancellationTokenSource.CreateLinkedTokenSource(HttpContext.RequestAborted);
  53. deadline.CancelAfter(TimeSpan.FromSeconds(15));
  54. try
  55. {
  56. return Ok(await MdpGovernanceSuggestions.Recommend(client, endpoint, apiKey, model, input.Fields, template.Fields, deadline.Token));
  57. }
  58. catch (Exception error) when (!HttpContext.RequestAborted.IsCancellationRequested &&
  59. error is HttpRequestException or JsonException or InvalidOperationException or KeyNotFoundException or OperationCanceledException)
  60. {
  61. return StatusCode(502, new { message = "建议服务暂不可用或返回结构无效,可手工编辑规则" });
  62. }
  63. });
  64. }