From f1f2a04b8d2553ba9ced2682862481c15214548d Mon Sep 17 00:00:00 2001 From: tomsun28 Date: Sun, 12 Dec 2021 18:31:25 +0800 Subject: [PATCH] =?UTF-8?q?[manager]=20=E7=9B=91=E6=8E=A7=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E6=8C=87=E6=A0=87=E7=BB=84=E6=8C=87=E6=A0=87=E5=B1=82?= =?UTF-8?q?=E7=BA=A7=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../usthe/alert/pojo/entity/AlertDefine.java | 4 ++ .../manager/controller/AppController.java | 17 ++++++-- .../com/usthe/manager/pojo/dto/Hierarchy.java | 35 +++++++++++++++ .../com/usthe/manager/service/AppService.java | 9 ++++ .../manager/service/impl/AppServiceImpl.java | 43 +++++++++++++++++++ 5 files changed, 104 insertions(+), 4 deletions(-) create mode 100644 manager/src/main/java/com/usthe/manager/pojo/dto/Hierarchy.java diff --git a/alerter/src/main/java/com/usthe/alert/pojo/entity/AlertDefine.java b/alerter/src/main/java/com/usthe/alert/pojo/entity/AlertDefine.java index c62d396..a441b87 100644 --- a/alerter/src/main/java/com/usthe/alert/pojo/entity/AlertDefine.java +++ b/alerter/src/main/java/com/usthe/alert/pojo/entity/AlertDefine.java @@ -16,6 +16,7 @@ import javax.persistence.Id; import javax.persistence.Table; import javax.validation.constraints.Max; import javax.validation.constraints.Min; +import javax.validation.constraints.NotNull; import java.time.LocalDateTime; @@ -43,14 +44,17 @@ public class AlertDefine { @ApiModelProperty(value = "配置告警的监控类型", example = "linux", accessMode = READ_WRITE, position = 1) @Length(max = 100) + @NotNull private String app; @ApiModelProperty(value = "配置告警的指标集合", example = "cpu", accessMode = READ_WRITE, position = 2) @Length(max = 100) + @NotNull private String metric; @ApiModelProperty(value = "配置告警的指标", example = "usage", accessMode = READ_WRITE, position = 3) @Length(max = 100) + @NotNull private String field; @ApiModelProperty(value = "是否是默认预置告警", example = "false", accessMode = READ_WRITE, position = 4) diff --git a/manager/src/main/java/com/usthe/manager/controller/AppController.java b/manager/src/main/java/com/usthe/manager/controller/AppController.java index 2090540..95b5c04 100644 --- a/manager/src/main/java/com/usthe/manager/controller/AppController.java +++ b/manager/src/main/java/com/usthe/manager/controller/AppController.java @@ -1,6 +1,7 @@ package com.usthe.manager.controller; import com.usthe.common.entity.dto.Message; +import com.usthe.manager.pojo.dto.Hierarchy; import com.usthe.manager.pojo.entity.ParamDefine; import com.usthe.manager.service.AppService; import io.swagger.annotations.Api; @@ -11,6 +12,7 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.List; @@ -38,11 +40,18 @@ public class AppController { return ResponseEntity.ok(new Message<>(paramDefines)); } - @GetMapping + @GetMapping(path = "/hierarchy") @ApiOperation(value = "查询全部层级的监控类型", notes = "查询所有监控类型,以层级结构输出") - public ResponseEntity> queryAppsHierarchy() { - - return null; + public ResponseEntity>> queryAppsHierarchy( + @ApiParam(value = "语言类型", example = "zh-CN", defaultValue = "zh-CN") + @RequestParam(name = "lang", required = false) String lang) { + if (lang == null || "".equals(lang)) { + lang = "zh-CN"; + } + lang = lang.equalsIgnoreCase("zh-cn")? "zh-CN" : lang; + lang = lang.equalsIgnoreCase("en-us")? "en-US" : lang; + List appHierarchies = appService.getAllAppHierarchy(lang); + return ResponseEntity.ok(new Message<>(appHierarchies)); } } diff --git a/manager/src/main/java/com/usthe/manager/pojo/dto/Hierarchy.java b/manager/src/main/java/com/usthe/manager/pojo/dto/Hierarchy.java new file mode 100644 index 0000000..f240066 --- /dev/null +++ b/manager/src/main/java/com/usthe/manager/pojo/dto/Hierarchy.java @@ -0,0 +1,35 @@ +package com.usthe.manager.pojo.dto; + +import io.swagger.annotations.ApiModelProperty; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.List; + +import static io.swagger.annotations.ApiModelProperty.AccessMode.READ_WRITE; + +/** + * 层级关系结构 + * eg: 监控类型指标组指标信息层级关系 + * @author tom + * @date 2021/12/12 16:23 + */ +@AllArgsConstructor +@NoArgsConstructor +@Data +public class Hierarchy { + + @ApiModelProperty(value = "属性值", example = "linux", accessMode = READ_WRITE, position = 0) + String value; + + @ApiModelProperty(value = "属性国际化标签", example = "Linux系统", accessMode = READ_WRITE, position = 1) + String label; + + @ApiModelProperty(value = "是否是叶子节点", example = "true", accessMode = READ_WRITE, position = 2) + Boolean isLeaf = false; + + @ApiModelProperty(value = "下一关联层级", accessMode = READ_WRITE, position = 3) + private List children; +} diff --git a/manager/src/main/java/com/usthe/manager/service/AppService.java b/manager/src/main/java/com/usthe/manager/service/AppService.java index 0741257..59f993a 100644 --- a/manager/src/main/java/com/usthe/manager/service/AppService.java +++ b/manager/src/main/java/com/usthe/manager/service/AppService.java @@ -1,6 +1,7 @@ package com.usthe.manager.service; import com.usthe.common.entity.job.Job; +import com.usthe.manager.pojo.dto.Hierarchy; import com.usthe.manager.pojo.entity.ParamDefine; import java.util.List; @@ -34,4 +35,12 @@ public interface AppService { * @return I18N资源 */ Map getI18nResources(String lang); + + /** + * 查询所有监控的类型-指标组-指标层级 + * @param lang 语言 + * @return 层级信息 + */ + List getAllAppHierarchy(String lang); + } diff --git a/manager/src/main/java/com/usthe/manager/service/impl/AppServiceImpl.java b/manager/src/main/java/com/usthe/manager/service/impl/AppServiceImpl.java index 619dd51..fcd95f8 100644 --- a/manager/src/main/java/com/usthe/manager/service/impl/AppServiceImpl.java +++ b/manager/src/main/java/com/usthe/manager/service/impl/AppServiceImpl.java @@ -1,7 +1,9 @@ package com.usthe.manager.service.impl; import com.usthe.common.entity.job.Job; +import com.usthe.common.entity.job.Metrics; import com.usthe.manager.dao.ParamDefineDao; +import com.usthe.manager.pojo.dto.Hierarchy; import com.usthe.manager.pojo.dto.ParamDefineDto; import com.usthe.manager.pojo.entity.ParamDefine; import com.usthe.manager.service.AppService; @@ -19,6 +21,7 @@ import java.io.IOException; import java.net.URL; import java.util.Collections; import java.util.HashMap; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Objects; @@ -76,6 +79,46 @@ public class AppServiceImpl implements AppService, CommandLineRunner { return i18nMap; } + @Override + public List getAllAppHierarchy(String lang) { + List hierarchies = new LinkedList<>(); + for (Job job : appDefines.values()) { + Hierarchy hierarchyApp = new Hierarchy(); + hierarchyApp.setValue(job.getApp()); + Map nameMap = job.getName(); + if (nameMap != null) { + String i18nName = nameMap.get(lang); + if (i18nName == null) { + i18nName = nameMap.values().stream().findFirst().get(); + } + hierarchyApp.setLabel(i18nName); + } + List hierarchyMetricList = new LinkedList<>(); + if (job.getMetrics() != null) { + for (Metrics metrics : job.getMetrics()) { + Hierarchy hierarchyMetric = new Hierarchy(); + hierarchyMetric.setValue(metrics.getName()); + hierarchyMetric.setLabel(metrics.getName()); + List hierarchyFieldList = new LinkedList<>(); + if (metrics.getFields() != null) { + for (Metrics.Field field : metrics.getFields()) { + Hierarchy hierarchyField = new Hierarchy(); + hierarchyField.setValue(field.getField()); + hierarchyField.setLabel(field.getField()); + hierarchyField.setIsLeaf(true); + hierarchyFieldList.add(hierarchyField); + } + hierarchyMetric.setChildren(hierarchyFieldList); + } + hierarchyMetricList.add(hierarchyMetric); + } + } + hierarchyApp.setChildren(hierarchyMetricList); + hierarchies.add(hierarchyApp); + } + return hierarchies; + } + @Override public void run(String... args) throws Exception { // 读取app定义配置加载到内存中 define/app/*.yml