AdoS8ConfigWatchRulesController.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using System.Security.Claims;
  2. using Admin.NET.Plugin.AiDOP.Entity.S8;
  3. using Admin.NET.Plugin.AiDOP.Service.S8;
  4. using Microsoft.Extensions.Logging;
  5. namespace Admin.NET.Plugin.AiDOP.Controllers.S8;
  6. [ApiController]
  7. [Route("api/aidop/s8/config/watch-rules")]
  8. [NonUnify]
  9. public class AdoS8ConfigWatchRulesController : ControllerBase
  10. {
  11. private const string LegacyHeaderUseInstead = "PUT /api/aidop/s8/config/watch-rules/{id}/params";
  12. private readonly S8WatchRuleService _svc;
  13. private readonly ILogger<AdoS8ConfigWatchRulesController> _logger;
  14. public AdoS8ConfigWatchRulesController(
  15. S8WatchRuleService svc,
  16. ILogger<AdoS8ConfigWatchRulesController> logger)
  17. {
  18. _svc = svc;
  19. _logger = logger;
  20. }
  21. [HttpGet]
  22. public async Task<IActionResult> ListAsync([FromQuery] long tenantId = 1, [FromQuery] long factoryId = 1) =>
  23. Ok(await _svc.ListAsync(tenantId, factoryId));
  24. /// <summary>
  25. /// Legacy endpoint. Rule configuration UI must use PUT /{id}/params.
  26. /// 保留兼容历史脚本/集成调用,但响应 header 与日志会标记为 deprecated。
  27. /// </summary>
  28. [Obsolete("Legacy full-create endpoint. Use PUT /api/aidop/s8/config/watch-rules/{id}/params for safe params editing.")]
  29. [HttpPost]
  30. public async Task<IActionResult> CreateAsync([FromBody] AdoS8WatchRule body)
  31. {
  32. MarkLegacyDeprecated("POST /api/aidop/s8/config/watch-rules");
  33. try { return Ok(await _svc.CreateAsync(body)); }
  34. catch (S8BizException ex) { return BadRequest(new { message = ex.Message }); }
  35. }
  36. /// <summary>
  37. /// Legacy endpoint. Rule configuration UI must use PUT /{id}/params.
  38. /// 保留兼容历史脚本/集成调用,但响应 header 与日志会标记为 deprecated。
  39. /// </summary>
  40. [Obsolete("Legacy full-update endpoint. Use PUT /api/aidop/s8/config/watch-rules/{id}/params for safe params editing.")]
  41. [HttpPut("{id:long}")]
  42. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS8WatchRule body)
  43. {
  44. MarkLegacyDeprecated($"PUT /api/aidop/s8/config/watch-rules/{id}", id);
  45. try { return Ok(await _svc.UpdateAsync(id, body)); }
  46. catch (S8BizException ex) { return BadRequest(new { message = ex.Message }); }
  47. }
  48. /// <summary>
  49. /// Legacy endpoint. Rule configuration UI must use PUT /{id}/params.
  50. /// 保留兼容历史脚本/集成调用,但响应 header 与日志会标记为 deprecated。
  51. /// </summary>
  52. [Obsolete("Legacy delete endpoint. Use PUT /api/aidop/s8/config/watch-rules/{id}/params for safe params editing.")]
  53. [HttpDelete("{id:long}")]
  54. public async Task<IActionResult> DeleteAsync(long id)
  55. {
  56. MarkLegacyDeprecated($"DELETE /api/aidop/s8/config/watch-rules/{id}", id);
  57. await _svc.DeleteAsync(id);
  58. return Ok();
  59. }
  60. /// <summary>
  61. /// Legacy endpoint. Rule configuration UI must use PUT /{id}/params.
  62. /// 保留兼容历史脚本/集成调用,但响应 header 与日志会标记为 deprecated。
  63. /// </summary>
  64. [Obsolete("Legacy test endpoint. Use PUT /api/aidop/s8/config/watch-rules/{id}/params for safe params editing.")]
  65. [HttpPost("{id:long}/test")]
  66. public async Task<IActionResult> TestAsync(long id)
  67. {
  68. MarkLegacyDeprecated($"POST /api/aidop/s8/config/watch-rules/{id}/test", id);
  69. try { return Ok(await _svc.TestAsync(id)); }
  70. catch (S8BizException ex) { return BadRequest(new { message = ex.Message }); }
  71. }
  72. /// <summary>
  73. /// R4 安全更新:仅修改 params_json 与 enabled,绝不接受 expression / rule_code / data_source_id /
  74. /// scene_code / watch_object_type / rule_type / source_object_type 等敏感字段。
  75. /// 服务端按 rule_type 用对应 evaluator 的 Params.Parse 进行 schema 校验。
  76. /// </summary>
  77. [HttpPut("{id:long}/params")]
  78. public async Task<IActionResult> UpdateParamsAsync(long id, [FromBody] S8WatchRuleParamsPayload body)
  79. {
  80. try { return Ok(await _svc.UpdateParamsAsync(id, body)); }
  81. catch (S8BizException ex) { return BadRequest(new { message = ex.Message }); }
  82. }
  83. /// <summary>
  84. /// S8-SCHED-FRONTEND-1:调度参数安全更新(仅 poll_interval_seconds / trigger_count_required / recover_count_required)。
  85. /// 不动 params_json / rule_type / expression / data_source_id / scene_code。
  86. /// </summary>
  87. [HttpPut("{id:long}/schedule")]
  88. public async Task<IActionResult> UpdateScheduleAsync(long id, [FromBody] S8WatchRuleSchedulePayload body)
  89. {
  90. try { return Ok(await _svc.UpdateScheduleAsync(id, body)); }
  91. catch (S8BizException ex) { return BadRequest(new { message = ex.Message }); }
  92. }
  93. /// <summary>S8-SCHED-FRONTEND-1:立即执行一次,next_run_at 置为 NOW,由下一 tick 拾取。</summary>
  94. [HttpPost("{id:long}/run-now")]
  95. public async Task<IActionResult> RunNowAsync(long id)
  96. {
  97. try { return Ok(await _svc.RunNowAsync(id)); }
  98. catch (S8BizException ex) { return BadRequest(new { message = ex.Message }); }
  99. }
  100. /// <summary>S8-SCHED-FRONTEND-1:手工暂停,paused_until 置为远未来哨兵 + pause_reason=MANUAL_PAUSED。</summary>
  101. [HttpPost("{id:long}/pause")]
  102. public async Task<IActionResult> PauseAsync(long id)
  103. {
  104. try { return Ok(await _svc.PauseAsync(id)); }
  105. catch (S8BizException ex) { return BadRequest(new { message = ex.Message }); }
  106. }
  107. /// <summary>S8-SCHED-FRONTEND-1:恢复,清 paused_until / pause_reason / last_error / consecutive_failure_count,next_run_at = NOW。</summary>
  108. [HttpPost("{id:long}/resume")]
  109. public async Task<IActionResult> ResumeAsync(long id)
  110. {
  111. try { return Ok(await _svc.ResumeAsync(id)); }
  112. catch (S8BizException ex) { return BadRequest(new { message = ex.Message }); }
  113. }
  114. /// <summary>
  115. /// 旧端点 deprecated 收口:写入响应 header 标记 + 结构化 warning 日志,便于运行期识别 legacy 调用。
  116. /// 不阻断调用,不改返回结构。
  117. /// </summary>
  118. private void MarkLegacyDeprecated(string endpoint, long? ruleId = null)
  119. {
  120. // 响应 header — Headers 在响应已开始发送后会抛 InvalidOperationException,此处守卫一下避免污染主调用。
  121. var headers = Response.Headers;
  122. if (!headers.ContainsKey("X-AiDOP-Deprecated"))
  123. headers["X-AiDOP-Deprecated"] = "true";
  124. if (!headers.ContainsKey("X-AiDOP-Use-Instead"))
  125. headers["X-AiDOP-Use-Instead"] = LegacyHeaderUseInstead;
  126. var userId = User?.FindFirst("UserId")?.Value
  127. ?? User?.FindFirst(ClaimTypes.NameIdentifier)?.Value;
  128. var tenantId = HttpContext?.Request?.Query["tenantId"].ToString();
  129. var factoryId = HttpContext?.Request?.Query["factoryId"].ToString();
  130. _logger.LogWarning(
  131. "legacy_watch_rule_endpoint endpoint={Endpoint} ruleId={RuleId} userId={UserId} tenantId={TenantId} factoryId={FactoryId} useInstead={UseInstead}",
  132. endpoint, ruleId, userId, tenantId, factoryId, LegacyHeaderUseInstead);
  133. }
  134. }