Selaa lähdekoodia

[manager] 监控类型指标组指标层级接口

tomsun28 4 vuotta sitten
vanhempi
commit
f1f2a04b8d

+ 4 - 0
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)

+ 13 - 4
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<Message<Object>> queryAppsHierarchy() {
-
-        return null;
+    public ResponseEntity<Message<List<Hierarchy>>> 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<Hierarchy> appHierarchies = appService.getAllAppHierarchy(lang);
+        return ResponseEntity.ok(new Message<>(appHierarchies));
     }
 
 }

+ 35 - 0
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<Hierarchy> children;
+}

+ 9 - 0
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<String, String> getI18nResources(String lang);
+
+    /**
+     * 查询所有监控的类型-指标组-指标层级
+     * @param lang 语言
+     * @return 层级信息
+     */
+    List<Hierarchy> getAllAppHierarchy(String lang);
+
 }

+ 43 - 0
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;
@@ -77,6 +80,46 @@ public class AppServiceImpl implements AppService, CommandLineRunner {
     }
 
     @Override
+    public List<Hierarchy> getAllAppHierarchy(String lang) {
+        List<Hierarchy> hierarchies = new LinkedList<>();
+        for (Job job : appDefines.values()) {
+            Hierarchy hierarchyApp = new Hierarchy();
+            hierarchyApp.setValue(job.getApp());
+            Map<String, String> nameMap = job.getName();
+            if (nameMap != null) {
+                String i18nName = nameMap.get(lang);
+                if (i18nName == null) {
+                    i18nName = nameMap.values().stream().findFirst().get();
+                }
+                hierarchyApp.setLabel(i18nName);
+            }
+            List<Hierarchy> 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<Hierarchy> 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
         Yaml yaml = new Yaml();