| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- using System.Security.Claims;
- using Admin.NET.Plugin.AiDOP.Entity.S8;
- using Admin.NET.Plugin.AiDOP.Service.S8;
- using Microsoft.Extensions.Logging;
- namespace Admin.NET.Plugin.AiDOP.Controllers.S8;
- [ApiController]
- [Route("api/aidop/s8/config/watch-rules")]
- [NonUnify]
- public class AdoS8ConfigWatchRulesController : ControllerBase
- {
- private const string LegacyHeaderUseInstead = "PUT /api/aidop/s8/config/watch-rules/{id}/params";
- private readonly S8WatchRuleService _svc;
- private readonly ILogger<AdoS8ConfigWatchRulesController> _logger;
- public AdoS8ConfigWatchRulesController(
- S8WatchRuleService svc,
- ILogger<AdoS8ConfigWatchRulesController> logger)
- {
- _svc = svc;
- _logger = logger;
- }
- [HttpGet]
- public async Task<IActionResult> ListAsync([FromQuery] long tenantId = 1, [FromQuery] long factoryId = 1) =>
- Ok(await _svc.ListAsync(tenantId, factoryId));
- /// <summary>
- /// Legacy endpoint. Rule configuration UI must use PUT /{id}/params.
- /// 保留兼容历史脚本/集成调用,但响应 header 与日志会标记为 deprecated。
- /// </summary>
- [Obsolete("Legacy full-create endpoint. Use PUT /api/aidop/s8/config/watch-rules/{id}/params for safe params editing.")]
- [HttpPost]
- public async Task<IActionResult> CreateAsync([FromBody] AdoS8WatchRule body)
- {
- MarkLegacyDeprecated("POST /api/aidop/s8/config/watch-rules");
- try { return Ok(await _svc.CreateAsync(body)); }
- catch (S8BizException ex) { return BadRequest(new { message = ex.Message }); }
- }
- /// <summary>
- /// Legacy endpoint. Rule configuration UI must use PUT /{id}/params.
- /// 保留兼容历史脚本/集成调用,但响应 header 与日志会标记为 deprecated。
- /// </summary>
- [Obsolete("Legacy full-update endpoint. Use PUT /api/aidop/s8/config/watch-rules/{id}/params for safe params editing.")]
- [HttpPut("{id:long}")]
- public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS8WatchRule body)
- {
- MarkLegacyDeprecated($"PUT /api/aidop/s8/config/watch-rules/{id}", id);
- try { return Ok(await _svc.UpdateAsync(id, body)); }
- catch (S8BizException ex) { return BadRequest(new { message = ex.Message }); }
- }
- /// <summary>
- /// Legacy endpoint. Rule configuration UI must use PUT /{id}/params.
- /// 保留兼容历史脚本/集成调用,但响应 header 与日志会标记为 deprecated。
- /// </summary>
- [Obsolete("Legacy delete endpoint. Use PUT /api/aidop/s8/config/watch-rules/{id}/params for safe params editing.")]
- [HttpDelete("{id:long}")]
- public async Task<IActionResult> DeleteAsync(long id)
- {
- MarkLegacyDeprecated($"DELETE /api/aidop/s8/config/watch-rules/{id}", id);
- await _svc.DeleteAsync(id);
- return Ok();
- }
- /// <summary>
- /// Legacy endpoint. Rule configuration UI must use PUT /{id}/params.
- /// 保留兼容历史脚本/集成调用,但响应 header 与日志会标记为 deprecated。
- /// </summary>
- [Obsolete("Legacy test endpoint. Use PUT /api/aidop/s8/config/watch-rules/{id}/params for safe params editing.")]
- [HttpPost("{id:long}/test")]
- public async Task<IActionResult> TestAsync(long id)
- {
- MarkLegacyDeprecated($"POST /api/aidop/s8/config/watch-rules/{id}/test", id);
- try { return Ok(await _svc.TestAsync(id)); }
- catch (S8BizException ex) { return BadRequest(new { message = ex.Message }); }
- }
- /// <summary>
- /// R4 安全更新:仅修改 params_json 与 enabled,绝不接受 expression / rule_code / data_source_id /
- /// scene_code / watch_object_type / rule_type / source_object_type 等敏感字段。
- /// 服务端按 rule_type 用对应 evaluator 的 Params.Parse 进行 schema 校验。
- /// </summary>
- [HttpPut("{id:long}/params")]
- public async Task<IActionResult> UpdateParamsAsync(long id, [FromBody] S8WatchRuleParamsPayload body)
- {
- try { return Ok(await _svc.UpdateParamsAsync(id, body)); }
- catch (S8BizException ex) { return BadRequest(new { message = ex.Message }); }
- }
- /// <summary>
- /// S8-SCHED-FRONTEND-1:调度参数安全更新(仅 poll_interval_seconds / trigger_count_required / recover_count_required)。
- /// 不动 params_json / rule_type / expression / data_source_id / scene_code。
- /// </summary>
- [HttpPut("{id:long}/schedule")]
- public async Task<IActionResult> UpdateScheduleAsync(long id, [FromBody] S8WatchRuleSchedulePayload body)
- {
- try { return Ok(await _svc.UpdateScheduleAsync(id, body)); }
- catch (S8BizException ex) { return BadRequest(new { message = ex.Message }); }
- }
- /// <summary>S8-SCHED-FRONTEND-1:立即执行一次,next_run_at 置为 NOW,由下一 tick 拾取。</summary>
- [HttpPost("{id:long}/run-now")]
- public async Task<IActionResult> RunNowAsync(long id)
- {
- try { return Ok(await _svc.RunNowAsync(id)); }
- catch (S8BizException ex) { return BadRequest(new { message = ex.Message }); }
- }
- /// <summary>S8-SCHED-FRONTEND-1:手工暂停,paused_until 置为远未来哨兵 + pause_reason=MANUAL_PAUSED。</summary>
- [HttpPost("{id:long}/pause")]
- public async Task<IActionResult> PauseAsync(long id)
- {
- try { return Ok(await _svc.PauseAsync(id)); }
- catch (S8BizException ex) { return BadRequest(new { message = ex.Message }); }
- }
- /// <summary>S8-SCHED-FRONTEND-1:恢复,清 paused_until / pause_reason / last_error / consecutive_failure_count,next_run_at = NOW。</summary>
- [HttpPost("{id:long}/resume")]
- public async Task<IActionResult> ResumeAsync(long id)
- {
- try { return Ok(await _svc.ResumeAsync(id)); }
- catch (S8BizException ex) { return BadRequest(new { message = ex.Message }); }
- }
- /// <summary>
- /// 旧端点 deprecated 收口:写入响应 header 标记 + 结构化 warning 日志,便于运行期识别 legacy 调用。
- /// 不阻断调用,不改返回结构。
- /// </summary>
- private void MarkLegacyDeprecated(string endpoint, long? ruleId = null)
- {
- // 响应 header — Headers 在响应已开始发送后会抛 InvalidOperationException,此处守卫一下避免污染主调用。
- var headers = Response.Headers;
- if (!headers.ContainsKey("X-AiDOP-Deprecated"))
- headers["X-AiDOP-Deprecated"] = "true";
- if (!headers.ContainsKey("X-AiDOP-Use-Instead"))
- headers["X-AiDOP-Use-Instead"] = LegacyHeaderUseInstead;
- var userId = User?.FindFirst("UserId")?.Value
- ?? User?.FindFirst(ClaimTypes.NameIdentifier)?.Value;
- var tenantId = HttpContext?.Request?.Query["tenantId"].ToString();
- var factoryId = HttpContext?.Request?.Query["factoryId"].ToString();
- _logger.LogWarning(
- "legacy_watch_rule_endpoint endpoint={Endpoint} ruleId={RuleId} userId={UserId} tenantId={TenantId} factoryId={FactoryId} useInstead={UseInstead}",
- endpoint, ruleId, userId, tenantId, factoryId, LegacyHeaderUseInstead);
- }
- }
|