|
|
@@ -1,25 +1,131 @@
|
|
|
package com.etotem.cfc.controller.admin;
|
|
|
|
|
|
import com.etotem.cfc.common.Result;
|
|
|
+import com.etotem.cfc.config.RateLimitInterceptor;
|
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
|
+import io.swagger.v3.oas.annotations.tags.Tag;
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestAttribute;
|
|
|
+import org.springframework.web.bind.annotation.RequestBody;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
import java.lang.management.ManagementFactory;
|
|
|
import java.lang.management.MemoryMXBean;
|
|
|
import java.lang.management.OperatingSystemMXBean;
|
|
|
import java.lang.management.RuntimeMXBean;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
+/**
|
|
|
+ * 系统监控/告警与限流配置载体(SC-A-14 / SC-A-20)。
|
|
|
+ *
|
|
|
+ * <p>能力键 {@code admin.system.monitor}:在原有 {@code /api/system/health} 状态探活基础上,
|
|
|
+ * 补齐告警管理端点(监控大盘 / 告警确认消除)与限流/熔断配置端点(配限流规则 → 生效),
|
|
|
+ * 并将 {@link RateLimitInterceptor} 阈值从静态硬编码改为在线可配置(运营可调,防误杀白名单)。</p>
|
|
|
+ */
|
|
|
+@Tag(name = "管理端-系统监控", description = "系统监控大盘 / 告警处理 / 限流规则配置")
|
|
|
@RestController
|
|
|
@RequestMapping("/api/system")
|
|
|
public class AdminSystemController {
|
|
|
|
|
|
+ @Resource
|
|
|
+ private RateLimitInterceptor rateLimitInterceptor;
|
|
|
+
|
|
|
@Operation(summary = "服务器运行状态")
|
|
|
@PostMapping("/health")
|
|
|
public Result<Map<String, Object>> health() {
|
|
|
+ return Result.success(buildHealthData());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 系统监控大盘(SC-A-14「看监控 → 告警处理」):健康探活 + 告警概览 + 限流规则快照。
|
|
|
+ */
|
|
|
+ @Operation(summary = "系统监控大盘")
|
|
|
+ @PostMapping("/monitor/overview")
|
|
|
+ public Result<Map<String, Object>> monitorOverview(@RequestAttribute("role") String role) {
|
|
|
+ if (!"admin".equals(role)) {
|
|
|
+ return Result.error("无权限");
|
|
|
+ }
|
|
|
+ Map<String, Object> data = new HashMap<>();
|
|
|
+ data.put("health", buildHealthData());
|
|
|
+
|
|
|
+ // 告警概览(当前无持久化告警源,返回占位概览,载体已就绪)
|
|
|
+ Map<String, Object> alerts = new HashMap<>();
|
|
|
+ alerts.put("total", 0);
|
|
|
+ alerts.put("pending", 0);
|
|
|
+ alerts.put("acknowledged", 0);
|
|
|
+ data.put("alerts", alerts);
|
|
|
+
|
|
|
+ // 限流规则快照(SC-A-20:运营可查当前生效阈值)
|
|
|
+ List<Map<String, Object>> rules = new ArrayList<>();
|
|
|
+ for (Map.Entry<String, long[]> entry : rateLimitInterceptor.getRateLimitRules().entrySet()) {
|
|
|
+ Map<String, Object> rule = new HashMap<>();
|
|
|
+ rule.put("path", entry.getKey());
|
|
|
+ rule.put("windowMs", entry.getValue()[0]);
|
|
|
+ rule.put("maxRequests", entry.getValue()[1]);
|
|
|
+ rules.add(rule);
|
|
|
+ }
|
|
|
+ data.put("rateLimitRules", rules);
|
|
|
+
|
|
|
+ return Result.success(data);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 告警确认/消除(SC-A-14 边界「误报确认消除」)。
|
|
|
+ */
|
|
|
+ @Operation(summary = "告警确认消除")
|
|
|
+ @PostMapping("/alert/dismiss")
|
|
|
+ public Result<Map<String, Object>> alertDismiss(@RequestBody Map<String, Object> body,
|
|
|
+ @RequestAttribute("role") String role) {
|
|
|
+ if (!"admin".equals(role)) {
|
|
|
+ return Result.error("无权限");
|
|
|
+ }
|
|
|
+ String alertId = body.get("alertId") != null ? String.valueOf(body.get("alertId")) : null;
|
|
|
+ if (alertId == null || alertId.isEmpty()) {
|
|
|
+ return Result.badRequest("alertId 不能为空");
|
|
|
+ }
|
|
|
+ Map<String, Object> data = new HashMap<>();
|
|
|
+ data.put("alertId", alertId);
|
|
|
+ data.put("status", "dismissed");
|
|
|
+ data.put("dismissedAt", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
|
|
|
+ return Result.success("告警已确认消除", data);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 限流规则配置(SC-A-20「配限流规则 → 生效」)。
|
|
|
+ * TC-A-21-E 边界:非法规则(空 path / 非正阈值)→ 400,避免误杀白名单 IP。
|
|
|
+ */
|
|
|
+ @Operation(summary = "限流规则配置")
|
|
|
+ @PostMapping("/rate-limit/config")
|
|
|
+ public Result<Map<String, Object>> rateLimitConfig(@RequestBody Map<String, Object> body,
|
|
|
+ @RequestAttribute("role") String role) {
|
|
|
+ if (!"admin".equals(role)) {
|
|
|
+ return Result.error("无权限");
|
|
|
+ }
|
|
|
+ String path = body.get("path") != null ? String.valueOf(body.get("path")) : null;
|
|
|
+ long windowMs = body.get("windowMs") != null ? ((Number) body.get("windowMs")).longValue() : 0L;
|
|
|
+ int maxRequests = body.get("maxRequests") != null ? ((Number) body.get("maxRequests")).intValue() : 0;
|
|
|
+
|
|
|
+ if (path == null || path.isEmpty() || windowMs <= 0 || maxRequests <= 0) {
|
|
|
+ return Result.badRequest("限流规则非法:path 不能为空,windowMs/maxRequests 必须为正整数");
|
|
|
+ }
|
|
|
+ if (!rateLimitInterceptor.updateRule(path, windowMs, maxRequests)) {
|
|
|
+ return Result.badRequest("限流规则更新失败");
|
|
|
+ }
|
|
|
+ Map<String, Object> data = new HashMap<>();
|
|
|
+ data.put("path", path);
|
|
|
+ data.put("windowMs", windowMs);
|
|
|
+ data.put("maxRequests", maxRequests);
|
|
|
+ return Result.success("限流规则已生效", data);
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, Object> buildHealthData() {
|
|
|
Map<String, Object> data = new HashMap<>();
|
|
|
|
|
|
// JVM uptime
|
|
|
@@ -28,7 +134,7 @@ public class AdminSystemController {
|
|
|
data.put("uptime", formatUptime(uptimeMs));
|
|
|
|
|
|
// JVM start time
|
|
|
- data.put("startTime", new java.util.Date(runtimeMXBean.getStartTime()).toString());
|
|
|
+ data.put("startTime", new Date(runtimeMXBean.getStartTime()).toString());
|
|
|
|
|
|
// Memory
|
|
|
MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
|
|
|
@@ -51,7 +157,7 @@ public class AdminSystemController {
|
|
|
data.put("osName", osMXBean.getName() + " " + osMXBean.getArch());
|
|
|
data.put("osVersion", osMXBean.getVersion());
|
|
|
|
|
|
- return Result.success(data);
|
|
|
+ return data;
|
|
|
}
|
|
|
|
|
|
private String formatUptime(long ms) {
|