AE86 3 роки тому
батько
коміт
dfdef1a974

+ 0 - 7
dbsyncer-biz/src/main/java/org/dbsyncer/biz/MonitorService.java

@@ -17,13 +17,6 @@ import java.util.Map;
  */
 public interface MonitorService {
 
-    /**
-     * 获取线程信息
-     *
-     * @return
-     */
-    Map getThreadInfo();
-
     /**
      * 获取驱动元信息列表
      *

+ 7 - 9
dbsyncer-biz/src/main/java/org/dbsyncer/biz/impl/MonitorServiceImpl.java

@@ -49,11 +49,6 @@ public class MonitorServiceImpl implements MonitorService {
     @Autowired
     private Manager manager;
 
-    @Override
-    public Map getThreadInfo() {
-        return monitor.getThreadInfo();
-    }
-
     @Override
     public List<MetaVo> getMetaAll() {
         List<MetaVo> list = manager.getMetaAll()
@@ -149,10 +144,13 @@ public class MonitorServiceImpl implements MonitorService {
 
     @Override
     public List<MetricResponseVo> queryMetric(List<MetricResponse> metrics) {
-        if (CollectionUtils.isEmpty(metrics)) {
-            return Collections.EMPTY_LIST;
-        }
-        return metrics.stream().map(metric -> {
+        // 线程池状态
+        List<MetricResponse> metricList = monitor.getThreadPoolInfo();
+        // 系统指标
+        metricList.addAll(metrics);
+
+        // TODO 转换显示
+        return metricList.stream().map(metric -> {
             MetricResponseVo vo = new MetricResponseVo();
             BeanUtils.copyProperties(metric, vo);
             return vo;

+ 1 - 1
dbsyncer-biz/src/main/java/org/dbsyncer/biz/vo/MetricResponseVo.java

@@ -4,4 +4,4 @@ import org.dbsyncer.monitor.model.MetricResponse;
 
 public class MetricResponseVo extends MetricResponse {
 
-}
+}

+ 2 - 2
dbsyncer-monitor/src/main/java/org/dbsyncer/monitor/Monitor.java

@@ -1,9 +1,9 @@
 package org.dbsyncer.monitor;
 
 import org.dbsyncer.monitor.enums.MetricEnum;
+import org.dbsyncer.monitor.model.MetricResponse;
 
 import java.util.List;
-import java.util.Map;
 
 /**
  * @author AE86
@@ -16,5 +16,5 @@ public interface Monitor {
 
     List<MetricEnum> getMetricEnumAll();
 
-    Map getThreadInfo();
+    List<MetricResponse> getThreadPoolInfo();
 }

+ 27 - 0
dbsyncer-monitor/src/main/java/org/dbsyncer/monitor/MonitorException.java

@@ -0,0 +1,27 @@
+package org.dbsyncer.monitor;
+
+/**
+ * @author AE86
+ * @version 1.0.0
+ * @date 2021/7/23 22:39
+ */
+public class MonitorException extends RuntimeException {
+
+	private static final long serialVersionUID = 1L;
+
+	public MonitorException(String message) {
+        super(message);
+    }
+
+    public MonitorException(String message, Throwable cause) {
+        super(message, cause);
+    }
+
+    public MonitorException(Throwable cause) {
+        super(cause);
+    }
+
+    protected MonitorException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
+        super(message, cause, enableSuppression, writableStackTrace);
+    }
+}

+ 18 - 16
dbsyncer-monitor/src/main/java/org/dbsyncer/monitor/MonitorFactory.java

@@ -2,6 +2,10 @@ package org.dbsyncer.monitor;
 
 import org.dbsyncer.manager.Manager;
 import org.dbsyncer.monitor.enums.MetricEnum;
+import org.dbsyncer.monitor.enums.StatisticEnum;
+import org.dbsyncer.monitor.enums.ThreadPoolMetricEnum;
+import org.dbsyncer.monitor.model.MetricResponse;
+import org.dbsyncer.monitor.model.Sample;
 import org.dbsyncer.parser.model.Connector;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -10,10 +14,7 @@ import org.springframework.cache.annotation.Cacheable;
 import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
 import org.springframework.stereotype.Component;
 
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 import java.util.concurrent.Executor;
 import java.util.concurrent.ThreadPoolExecutor;
 
@@ -46,19 +47,20 @@ public class MonitorFactory implements Monitor {
     }
 
     @Override
-    public Map getThreadInfo() {
-        Map map = new HashMap();
-        if (taskExecutor instanceof ThreadPoolTaskExecutor) {
-            ThreadPoolTaskExecutor threadTask = (ThreadPoolTaskExecutor) taskExecutor;
-            ThreadPoolExecutor threadPoolExecutor = threadTask.getThreadPoolExecutor();
+    public List<MetricResponse> getThreadPoolInfo() {
+        ThreadPoolTaskExecutor threadTask = (ThreadPoolTaskExecutor) taskExecutor;
+        ThreadPoolExecutor pool = threadTask.getThreadPoolExecutor();
 
-            map.put("已提交", threadPoolExecutor.getTaskCount());
-            map.put("已完成", threadPoolExecutor.getCompletedTaskCount());
-            map.put("处理中", threadPoolExecutor.getActiveCount());
-            map.put("排队中", threadPoolExecutor.getQueue().size());
-            map.put("队列长度", threadPoolExecutor.getQueue().remainingCapacity());
-        }
-        return map;
+        List<MetricResponse> list = new ArrayList<>();
+        list.add(createMetricResponse(ThreadPoolMetricEnum.QUEUE_UP, pool.getQueue().size()));
+        list.add(createMetricResponse(ThreadPoolMetricEnum.TASK, pool.getTaskCount()));
+        list.add(createMetricResponse(ThreadPoolMetricEnum.ACTIVE, pool.getActiveCount()));
+        list.add(createMetricResponse(ThreadPoolMetricEnum.COMPLETED, pool.getCompletedTaskCount()));
+        list.add(createMetricResponse(ThreadPoolMetricEnum.REMAINING_CAPACITY, pool.getQueue().remainingCapacity()));
+        return list;
     }
 
+    private MetricResponse createMetricResponse(ThreadPoolMetricEnum metricEnum, Object value) {
+        return new MetricResponse(metricEnum.getCode(), metricEnum.getGroup(), metricEnum.getMetricName(), Arrays.asList(new Sample(StatisticEnum.COUNT.getTagValueRepresentation(), value)));
+    }
 }

+ 21 - 8
dbsyncer-monitor/src/main/java/org/dbsyncer/monitor/enums/MetricEnum.java

@@ -1,5 +1,9 @@
 package org.dbsyncer.monitor.enums;
 
+import org.apache.commons.lang.StringUtils;
+import org.dbsyncer.connector.ConnectorException;
+import org.dbsyncer.monitor.MonitorException;
+
 /**
  * 系统指标
  *
@@ -17,7 +21,7 @@ public enum MetricEnum {
     /**
      * 线程峰值
      */
-    THREADS_PEAK("jvm.threads.peak", "线程", "峰值"),
+    THREADS_PEAK("jvm.threads.peak", "线程", "峰值"),
 
     /**
      * 内存已用
@@ -46,22 +50,31 @@ public enum MetricEnum {
 
     private String code;
     private String group;
-    private String name;
+    private String metricName;
     private String baseUnit;
 
-    MetricEnum(String code, String group, String name) {
+    MetricEnum(String code, String group, String metricName) {
         this.code = code;
         this.group = group;
-        this.name = name;
+        this.metricName = metricName;
     }
 
-    MetricEnum(String code, String group, String name, String baseUnit) {
+    MetricEnum(String code, String group, String metricName, String baseUnit) {
         this.code = code;
         this.group = group;
-        this.name = name;
+        this.metricName = metricName;
         this.baseUnit = baseUnit;
     }
 
+    public static MetricEnum getMetric(String code) throws ConnectorException {
+        for (MetricEnum e : MetricEnum.values()) {
+            if (StringUtils.equals(code, e.getCode())) {
+                return e;
+            }
+        }
+        throw new MonitorException(String.format("Metric code \"%s\" does not exist.", code));
+    }
+
     public String getCode() {
         return code;
     }
@@ -70,8 +83,8 @@ public enum MetricEnum {
         return group;
     }
 
-    public String getName() {
-        return name;
+    public String getMetricName() {
+        return metricName;
     }
 
     public String getBaseUnit() {

+ 54 - 0
dbsyncer-monitor/src/main/java/org/dbsyncer/monitor/enums/ThreadPoolMetricEnum.java

@@ -0,0 +1,54 @@
+package org.dbsyncer.monitor.enums;
+
+/**
+ * 线程池指标
+ *
+ * @author AE86
+ * @version 1.0.0
+ * @date 2021/07/23 0:19
+ */
+public enum ThreadPoolMetricEnum {
+
+    /**
+     * 排队中
+     */
+    QUEUE_UP("thread.pool.queue.up", "线程池", "排队中"),
+    /**
+     * 已提交
+     */
+    TASK("thread.pool.task", "线程池", "已提交"),
+    /**
+     * 处理中
+     */
+    ACTIVE("thread.pool.active", "线程池", "处理中"),
+    /**
+     * 已完成
+     */
+    COMPLETED("thread.pool.completed", "线程池", "已完成"),
+    /**
+     * 队列长度
+     */
+    REMAINING_CAPACITY("thread.pool.remaining.capacity", "线程池", "队列长度");
+
+    private String code;
+    private String group;
+    private String metricName;
+
+    ThreadPoolMetricEnum(String code, String group, String metricName) {
+        this.code = code;
+        this.group = group;
+        this.metricName = metricName;
+    }
+
+    public String getCode() {
+        return code;
+    }
+
+    public String getGroup() {
+        return group;
+    }
+
+    public String getMetricName() {
+        return metricName;
+    }
+}

+ 35 - 5
dbsyncer-monitor/src/main/java/org/dbsyncer/monitor/model/MetricResponse.java

@@ -4,16 +4,46 @@ import java.util.List;
 
 public class MetricResponse {
 
-    private String name;
+    private String code;
+
+    private String group;
+
+    private String metricName;
 
     private List<Sample> measurements;
 
-    public String getName() {
-        return name;
+    public MetricResponse() {
+    }
+
+    public MetricResponse(String code, String group, String metricName, List<Sample> measurements) {
+        this.code = code;
+        this.group = group;
+        this.metricName = metricName;
+        this.measurements = measurements;
+    }
+
+    public String getCode() {
+        return code;
+    }
+
+    public void setCode(String code) {
+        this.code = code;
+    }
+
+    public String getGroup() {
+        return group;
+    }
+
+    public void setGroup(String group) {
+        this.group = group;
+    }
+
+    public String getMetricName() {
+        return metricName;
     }
 
-    public void setName(String name) {
-        this.name = name;
+    public void setMetricName(String metricName) {
+        this.metricName = metricName;
     }
 
     public List<Sample> getMeasurements() {

+ 4 - 4
dbsyncer-monitor/src/main/java/org/dbsyncer/monitor/model/Sample.java

@@ -4,9 +4,9 @@ public final class Sample {
 
     private String statistic;
 
-    private Double value;
+    private Object value;
 
-    public Sample(String statistic, Double value) {
+    public Sample(String statistic, Object value) {
         this.statistic = statistic;
         this.value = value;
     }
@@ -19,11 +19,11 @@ public final class Sample {
         this.statistic = statistic;
     }
 
-    public Double getValue() {
+    public Object getValue() {
         return value;
     }
 
-    public void setValue(Double value) {
+    public void setValue(Object value) {
         this.value = value;
     }
 }

+ 5 - 2
dbsyncer-web/src/main/java/org/dbsyncer/web/controller/monitor/MonitorController.java

@@ -45,7 +45,10 @@ public class MonitorController extends BaseController {
             metricEnumList.forEach(m -> {
                 MetricsEndpoint.MetricResponse metric = metricsEndpoint.metric(m.getCode(), null);
                 MetricResponse metricResponse = new MetricResponse();
-                metricResponse.setName(metric.getName());
+                MetricEnum metricEnum = MetricEnum.getMetric(metric.getName());
+                metricResponse.setCode(metricEnum.getCode());
+                metricResponse.setGroup(metricEnum.getGroup());
+                metricResponse.setMetricName(metricEnum.getMetricName());
                 if (!CollectionUtils.isEmpty(metric.getMeasurements())) {
                     List<Sample> measurements = new ArrayList<>();
                     metric.getMeasurements().forEach(s -> measurements.add(new Sample(s.getStatistic().getTagValueRepresentation(), s.getValue())));
@@ -63,7 +66,7 @@ public class MonitorController extends BaseController {
     @RequestMapping("")
     public String index(HttpServletRequest request, ModelMap model) {
         Map<String, String> params = getParams(request);
-        model.put("threadInfo", monitorService.getThreadInfo());
+        model.put("metrics", get().getResultValue());
         model.put("metaId", monitorService.getDefaultMetaId(params));
         model.put("meta", monitorService.getMetaAll());
         model.put("storageDataStatus", monitorService.getStorageDataStatusEnumAll());

+ 6 - 6
dbsyncer-web/src/main/resources/public/monitor/monitor.html

@@ -93,11 +93,11 @@
                 </div>
             </div>
 
-            <!-- 线程任务 -->
+            <!-- 系统指标 -->
             <div class="col-md-12">
                 <div class="col-md-6">
                     <table class="table table-hover">
-                        <caption>线程任务</caption>
+                        <caption>系统指标</caption>
                         <thead>
                         <tr>
                             <th style="width:3%;"></th>
@@ -105,10 +105,10 @@
                             <th style="width:27%;">指标</th>
                         </tr>
                         </thead>
-                        <tr th:each="item,userStat:${threadInfo}">
-                            <td th:text="${userStat.index}+1"></td>
-                            <td th:text="${userStat.current.key}"></td><!-- key-->
-                            <td th:text="${userStat.current.value}"></td><!-- value-->
+                        <tr th:each="m,s : ${metrics}">
+                            <td th:text="${s.index}+1"></td>
+                            <td th:text="${m?.group}"></td><!-- key-->
+                            <td th:text="${m?.metricName}"></td><!-- value-->
                         </tr>
                     </table>
                 </div>