From eee3a9523d4c0dc6292df4fae4231793740aa41f Mon Sep 17 00:00:00 2001 From: lensfrex Date: Wed, 8 May 2024 21:56:52 +0800 Subject: [PATCH] =?UTF-8?q?impl:=20=E6=B7=BB=E5=8A=A0=E5=88=9D=E6=AD=A5?= =?UTF-8?q?=E7=9A=84=E7=9B=91=E6=B5=8B=E6=95=B0=E6=8D=AE=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=20ref:=20=E7=A7=BB=E5=8A=A8=E9=83=A8?= =?UTF-8?q?=E5=88=86=E6=95=B0=E6=8D=AE=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rition-center/api/pom.xml | 5 ++ .../collector/DataCollectingController.java | 2 +- .../api/v1/panel/MetricsViewController.java | 34 ++++++++- .../service/MetricDataCollectingService.java | 4 +- .../api/src/main/resources/application.yml | 2 +- .../data/dao/mapper/MetricRecordMapper.java | 10 +++ .../data/dto/{ => service}/MetricDataDto.java | 2 +- .../rition/common/data/entity/RuleEntity.java | 2 +- .../resources/mapper/MetricRecordMapper.xml | 35 +++++++++ .../collector/MetricCollectingService.java | 2 +- .../service/monitor/MonitorService.java | 2 +- .../main/resources/application-monitor.yml | 2 +- .../rition/service/panel/MetricService.java | 73 +++++++++++++++++++ .../src/main/resources/application-panel.yml | 12 +++ 14 files changed, 175 insertions(+), 12 deletions(-) rename rition-center/common/src/main/java/rition/common/data/dto/{ => service}/MetricDataDto.java (88%) create mode 100644 rition-center/service/panel/src/main/java/rition/service/panel/MetricService.java create mode 100644 rition-center/service/panel/src/main/resources/application-panel.yml diff --git a/rition-center/api/pom.xml b/rition-center/api/pom.xml index 1fb24f8..d581c1a 100644 --- a/rition-center/api/pom.xml +++ b/rition-center/api/pom.xml @@ -43,6 +43,11 @@ notify ${revision} + + net.rition + panel + ${revision} + org.springframework.boot diff --git a/rition-center/api/src/main/java/rition/backend/api/v1/collector/DataCollectingController.java b/rition-center/api/src/main/java/rition/backend/api/v1/collector/DataCollectingController.java index 8610894..e6f1a47 100644 --- a/rition-center/api/src/main/java/rition/backend/api/v1/collector/DataCollectingController.java +++ b/rition-center/api/src/main/java/rition/backend/api/v1/collector/DataCollectingController.java @@ -9,7 +9,7 @@ import rition.backend.annotation.paramter.RequestId; import rition.backend.api.v1.dto.request.MetricDataUploadRequest; import rition.backend.api.v1.dto.response.Response; import rition.backend.service.MetricDataCollectingService; -import rition.common.data.dto.MetricDataDto; +import rition.common.data.dto.service.MetricDataDto; import java.util.HashMap; import java.util.List; diff --git a/rition-center/api/src/main/java/rition/backend/api/v1/panel/MetricsViewController.java b/rition-center/api/src/main/java/rition/backend/api/v1/panel/MetricsViewController.java index 1138f54..60c5fb5 100644 --- a/rition-center/api/src/main/java/rition/backend/api/v1/panel/MetricsViewController.java +++ b/rition-center/api/src/main/java/rition/backend/api/v1/panel/MetricsViewController.java @@ -5,14 +5,44 @@ import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import rition.backend.api.v1.dto.request.MetricDataRequest; +import rition.backend.api.v1.dto.response.MetricDataResponse; import rition.backend.api.v1.dto.response.Response; +import rition.common.data.entity.MetricRecordEntity; +import rition.service.panel.MetricService; + +import java.time.Instant; +import java.util.ArrayList; +import java.util.List; @RestController @RequestMapping("/panel/metrics") public class MetricsViewController { + private final MetricService metricService; + + public MetricsViewController(MetricService metricService) { + this.metricService = metricService; + } + @GetMapping("/list") - public Response getMetrics(@RequestBody MetricDataRequest metricDataRequest) { - return Response.success(); + public Response> getMetrics(@RequestBody MetricDataRequest metricDataRequest) { + var entityResult = metricService.getMetricDataRange( + metricDataRequest.getInstanceId(), + metricDataRequest.getMetricItems(), + Instant.ofEpochMilli(metricDataRequest.getStart()), + Instant.ofEpochMilli(metricDataRequest.getEnd()) + ); + + // 转换数据 + List responseList = new ArrayList<>(entityResult.size()); + for (MetricRecordEntity entity : entityResult) { + responseList.add(MetricDataResponse.builder() + .metricData(entity.getMetricData()) + .time(entity.getTime()) + .build() + ); + } + + return Response.success(responseList); } } diff --git a/rition-center/api/src/main/java/rition/backend/service/MetricDataCollectingService.java b/rition-center/api/src/main/java/rition/backend/service/MetricDataCollectingService.java index 80f142e..f350cec 100644 --- a/rition-center/api/src/main/java/rition/backend/service/MetricDataCollectingService.java +++ b/rition-center/api/src/main/java/rition/backend/service/MetricDataCollectingService.java @@ -4,12 +4,10 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; -import rition.common.data.dto.MetricDataDto; import rition.common.data.dto.log.RequestProcessTraceRecord; +import rition.common.data.dto.service.MetricDataDto; import rition.service.collector.MetricCollectingService; -import java.time.Duration; -import java.time.temporal.ChronoUnit; import java.util.Map; /** diff --git a/rition-center/api/src/main/resources/application.yml b/rition-center/api/src/main/resources/application.yml index 0c15219..c5b4a68 100644 --- a/rition-center/api/src/main/resources/application.yml +++ b/rition-center/api/src/main/resources/application.yml @@ -5,7 +5,7 @@ server: spring: profiles: - include: collector, monitor + include: collector, monitor, panel data: redis: host: ${REDIS_HOST:127.0.0.1} diff --git a/rition-center/common/src/main/java/rition/common/data/dao/mapper/MetricRecordMapper.java b/rition-center/common/src/main/java/rition/common/data/dao/mapper/MetricRecordMapper.java index 1c8de16..3534439 100644 --- a/rition-center/common/src/main/java/rition/common/data/dao/mapper/MetricRecordMapper.java +++ b/rition-center/common/src/main/java/rition/common/data/dao/mapper/MetricRecordMapper.java @@ -2,11 +2,21 @@ package rition.common.data.dao.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; +import org.springframework.stereotype.Component; import rition.common.data.entity.MetricRecordEntity; +import java.time.Instant; import java.util.List; @Mapper +@Component public interface MetricRecordMapper extends BaseMapper { int insertBatchSomeColumn(List entityList); + + List getMetricDataGroupByHour( + @Param("instanceId") String instanceId, + @Param("metricItems") List metricItems, + @Param("startTime") Instant startTime, @Param("endTime") Instant endTime + ); } diff --git a/rition-center/common/src/main/java/rition/common/data/dto/MetricDataDto.java b/rition-center/common/src/main/java/rition/common/data/dto/service/MetricDataDto.java similarity index 88% rename from rition-center/common/src/main/java/rition/common/data/dto/MetricDataDto.java rename to rition-center/common/src/main/java/rition/common/data/dto/service/MetricDataDto.java index ad25c9b..da59fb4 100644 --- a/rition-center/common/src/main/java/rition/common/data/dto/MetricDataDto.java +++ b/rition-center/common/src/main/java/rition/common/data/dto/service/MetricDataDto.java @@ -1,5 +1,5 @@ -package rition.common.data.dto; +package rition.common.data.dto.service; import lombok.AllArgsConstructor; import lombok.Builder; diff --git a/rition-center/common/src/main/java/rition/common/data/entity/RuleEntity.java b/rition-center/common/src/main/java/rition/common/data/entity/RuleEntity.java index 295646f..a789de7 100644 --- a/rition-center/common/src/main/java/rition/common/data/entity/RuleEntity.java +++ b/rition-center/common/src/main/java/rition/common/data/entity/RuleEntity.java @@ -40,7 +40,7 @@ public class RuleEntity { /** * 阈值 */ - private String threshold; + private Double threshold; /** * 触发方法,实时计算或定时计算 diff --git a/rition-center/common/src/main/resources/mapper/MetricRecordMapper.xml b/rition-center/common/src/main/resources/mapper/MetricRecordMapper.xml index fe2a73f..02badd8 100644 --- a/rition-center/common/src/main/resources/mapper/MetricRecordMapper.xml +++ b/rition-center/common/src/main/resources/mapper/MetricRecordMapper.xml @@ -1,5 +1,40 @@ + + + + + \ No newline at end of file diff --git a/rition-center/service/collector/src/main/java/rition/service/collector/MetricCollectingService.java b/rition-center/service/collector/src/main/java/rition/service/collector/MetricCollectingService.java index c01c94f..92eaebe 100644 --- a/rition-center/service/collector/src/main/java/rition/service/collector/MetricCollectingService.java +++ b/rition-center/service/collector/src/main/java/rition/service/collector/MetricCollectingService.java @@ -4,7 +4,7 @@ import com.github.yitter.idgen.YitIdHelper; import lombok.extern.slf4j.Slf4j; import org.springframework.kafka.core.KafkaTemplate; import org.springframework.stereotype.Service; -import rition.common.data.dto.MetricDataDto; +import rition.common.data.dto.service.MetricDataDto; import rition.common.data.entity.MetricRecordEntity; import rition.service.collector.configure.CollectorServiceKafkaConfigure; import rition.common.data.dao.mapper.MetricRecordMapper; diff --git a/rition-center/service/monitor/src/main/java/rition/service/monitor/MonitorService.java b/rition-center/service/monitor/src/main/java/rition/service/monitor/MonitorService.java index 7870c70..cbf5cd1 100644 --- a/rition-center/service/monitor/src/main/java/rition/service/monitor/MonitorService.java +++ b/rition-center/service/monitor/src/main/java/rition/service/monitor/MonitorService.java @@ -3,7 +3,7 @@ package rition.service.monitor; import org.apache.kafka.clients.consumer.ConsumerRecord; import org.springframework.kafka.annotation.KafkaListener; import org.springframework.stereotype.Service; -import rition.common.data.dto.MetricDataDto; +import rition.common.data.dto.service.MetricDataDto; @Service public class MonitorService { diff --git a/rition-center/service/monitor/src/main/resources/application-monitor.yml b/rition-center/service/monitor/src/main/resources/application-monitor.yml index 76ca54d..e24f762 100644 --- a/rition-center/service/monitor/src/main/resources/application-monitor.yml +++ b/rition-center/service/monitor/src/main/resources/application-monitor.yml @@ -13,7 +13,7 @@ spring: spring: json: trusted: - packages: "rition.common.data.dto" + packages: "rition.common.data.*" producer: retries: 4 compression-type: zstd diff --git a/rition-center/service/panel/src/main/java/rition/service/panel/MetricService.java b/rition-center/service/panel/src/main/java/rition/service/panel/MetricService.java new file mode 100644 index 0000000..c20b5d2 --- /dev/null +++ b/rition-center/service/panel/src/main/java/rition/service/panel/MetricService.java @@ -0,0 +1,73 @@ +package rition.service.panel; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +import rition.common.data.dao.mapper.MetricRecordMapper; +import rition.common.data.entity.MetricRecordEntity; + +import java.time.Instant; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; + +@Slf4j +@Service +public class MetricService { + + private final MetricRecordMapper metricRecordMapper; + + public MetricService(MetricRecordMapper metricRecordMapper) { + this.metricRecordMapper = metricRecordMapper; + } + + public List getMetricDataRange(String instanceId, + List metricItems, + Instant start, Instant end) { + var filteredMetricItems = this.filterMetricItems(metricItems); + if (filteredMetricItems.isEmpty()) { + return new ArrayList<>(); + } + + return metricRecordMapper.getMetricDataGroupByHour(instanceId, metricItems, start, end); + } + + private static final HashSet allowedMetricItems = new HashSet<>(22); + + static { + allowedMetricItems.add("up"); + allowedMetricItems.add("node_load5"); + allowedMetricItems.add("node_sockstat_TCP_tw"); + allowedMetricItems.add("node_cpu_seconds_total"); + allowedMetricItems.add("node_memory_Cached_bytes"); + allowedMetricItems.add("node_memory_Buffers_bytes"); + allowedMetricItems.add("node_memory_MemFree_bytes"); + allowedMetricItems.add("node_disk_read_bytes_total"); + allowedMetricItems.add("node_filesystem_free_bytes"); + allowedMetricItems.add("node_filesystem_size_bytes"); + allowedMetricItems.add("node_memory_MemTotal_bytes"); + allowedMetricItems.add("node_netstat_Tcp_CurrEstab"); + allowedMetricItems.add("node_filesystem_avail_bytes"); + allowedMetricItems.add("node_disk_written_bytes_total"); + allowedMetricItems.add("node_disk_reads_completed_total"); + allowedMetricItems.add("node_network_receive_drop_total"); + allowedMetricItems.add("node_disk_writes_completed_total"); + allowedMetricItems.add("node_network_receive_bytes_total"); + allowedMetricItems.add("node_network_transmit_drop_total"); + allowedMetricItems.add("node_network_transmit_bytes_total"); + allowedMetricItems.add("node_network_receive_packets_total"); + allowedMetricItems.add("node_network_transmit_packets_total"); + } + + public List filterMetricItems(List metricItems) { + List filteredMetricItems = new ArrayList<>(metricItems.size()); + for (String metricItem : metricItems) { + if (allowedMetricItems.contains(metricItem)) { + filteredMetricItems.add(metricItem); + } else { + log.warn("[MetricService]: 含有非允许的指标值:{}", metricItem); + } + } + + return filteredMetricItems; + } +} diff --git a/rition-center/service/panel/src/main/resources/application-panel.yml b/rition-center/service/panel/src/main/resources/application-panel.yml new file mode 100644 index 0000000..4baa07c --- /dev/null +++ b/rition-center/service/panel/src/main/resources/application-panel.yml @@ -0,0 +1,12 @@ +spring: + datasource: + driver-class-name: com.mysql.cj.jdbc.Driver + url: jdbc:mysql://127.0.0.1:3306/rition?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true + username: root + password: Test2333! +mybatis: + configuration: + log-impl: org.apache.ibatis.logging.stdout.StdOutImpl +mybatis-plus: + configuration: + log-impl: org.apache.ibatis.logging.stdout.StdOutImpl