| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- using Admin.NET.Plugin.AiDOP.DataPlatform.Core;
- using System.Text.Json;
- namespace Admin.NET.Plugin.AiDOP.DataPlatform.UnifiedIngress;
- public sealed record MdpGovernanceTrial(string Config, JsonElement Sample);
- public sealed partial class MdpIngestDatasetController
- {
- [HttpGet("{id:long}/governance/templates")]
- public Task<IActionResult> GovernanceTemplateList(long id, [FromQuery] long? factoryId) => WithScope(factoryId, async scope =>
- await Datasets(scope).AnyAsync(x => x.Id == id) ? Ok(GovernanceTemplates.All) : NotFound());
- [HttpPost("{id:long}/governance/templates/{key}/trial")]
- [RequestSizeLimit(192 * 1024)]
- public Task<IActionResult> GovernanceTemplateTrial(long id, string key, [FromQuery] long? factoryId, MdpGovernanceTrial input) =>
- WithScope(factoryId, async scope =>
- {
- if (!await Can(TrialPermission)) return Forbid();
- if (!await Datasets(scope).AnyAsync(x => x.Id == id)) return NotFound();
- var template = GovernanceTemplates.All.FirstOrDefault(x => x.Key == key);
- if (template == null) return NotFound();
- try
- {
- var package = GovernancePackage.Parse(input.Config, template.Fields);
- var converted = new Dictionary<string, JsonElement>(StringComparer.Ordinal);
- var issue = package.Apply(input.Sample, converted);
- return Ok(new { digest = package.Digest, output = issue == null ? converted : null, issue,
- sourceSchema = package.SourceSchema(template.SourceSchema.Stamp.Version),
- standardSchema = package.StandardSchema(template.StandardObject.Stamp.Version) });
- }
- catch (Exception error) when (error is JsonException or ArgumentException or DatasetChangeException)
- {
- return BadRequest(new { message = error is JsonException ? "配置包 JSON 不符合合同" : error.Message });
- }
- });
- [HttpPost("{id:long}/governance/templates/{key}/suggestions")]
- [RequestSizeLimit(24 * 1024)]
- public Task<IActionResult> GovernanceSuggestions(long id, string key, [FromQuery] long? factoryId, MdpGovernanceSuggestInput input) =>
- WithScope(factoryId, async scope =>
- {
- if (!await Can(TrialPermission)) return Forbid();
- if (!await Datasets(scope).AnyAsync(x => x.Id == id)) return NotFound();
- var template = GovernanceTemplates.All.FirstOrDefault(x => x.Key == key);
- if (template == null) return NotFound();
- if (!MdpGovernanceSuggestions.ValidFields(input.Fields))
- return BadRequest(new { message = "来源字段清单有重复或无效的字段名或类型" });
- if (!Uri.TryCreate(Settings["MDP_GOVERNANCE_AI_URL"], UriKind.Absolute, out var endpoint) ||
- endpoint.Scheme != Uri.UriSchemeHttps || !string.IsNullOrEmpty(endpoint.UserInfo) ||
- Settings["MDP_GOVERNANCE_AI_KEY"] is not { Length: > 0 } apiKey ||
- Settings["MDP_GOVERNANCE_AI_MODEL"] is not { Length: > 0 } model)
- return StatusCode(503, new { message = "此环境暂未配置映射建议服务,可手工编辑规则" });
- // The address and credentials belong to this environment. No payload samples or master records are sent.
- using var handler = new HttpClientHandler { AllowAutoRedirect = false };
- using var client = new HttpClient(handler);
- using var deadline = CancellationTokenSource.CreateLinkedTokenSource(HttpContext.RequestAborted);
- deadline.CancelAfter(TimeSpan.FromSeconds(15));
- try
- {
- return Ok(await MdpGovernanceSuggestions.Recommend(client, endpoint, apiKey, model, input.Fields, template.Fields, deadline.Token));
- }
- catch (Exception error) when (!HttpContext.RequestAborted.IsCancellationRequested &&
- error is HttpRequestException or JsonException or InvalidOperationException or KeyNotFoundException or OperationCanceledException)
- {
- return StatusCode(502, new { message = "建议服务暂不可用或返回结构无效,可手工编辑规则" });
- }
- });
- }
|