parent
5070304883
commit
eee3a9523d
@ -1,5 +1,5 @@ |
||||
|
||||
package rition.common.data.dto; |
||||
package rition.common.data.dto.service; |
||||
|
||||
import lombok.AllArgsConstructor; |
||||
import lombok.Builder; |
@ -1,5 +1,40 @@ |
||||
<?xml version="1.0" encoding="UTF-8" ?> |
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > |
||||
<mapper namespace="rition.common.data.dao.mapper.MetricRecordMapper"> |
||||
<resultMap id="metricDataEntity" type="rition.common.data.entity.MetricRecordEntity"> |
||||
<result property="metricData" column="metric_data" typeHandler="com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler"/> |
||||
</resultMap> |
||||
|
||||
<select id="getMetricDataGroupByHour" resultMap="metricDataEntity" resultType="rition.common.data.entity.MetricRecordEntity"> |
||||
SELECT JSON_OBJECT( |
||||
<foreach collection="metricItems" item="metricItem" separator=","> |
||||
"${metricItem}", ${metricItem} |
||||
</foreach> |
||||
) AS metric_data, |
||||
t AS time |
||||
FROM ( |
||||
SELECT |
||||
<foreach collection="metricItems" item="metricItem"> |
||||
avg(JSON_EXTRACT(metric_data, "$.${metricItem}")) as ${metricItem}, |
||||
</foreach> |
||||
DATE_FORMAT(time,'%Y-%m-%d %H:00:00') as t |
||||
FROM record WHERE `time` BETWEEN #{startTime} and #{endTime} and `instance_id` = #{instanceId} |
||||
GROUP BY t ORDER BY t |
||||
) AS tab; |
||||
|
||||
<!-- |
||||
SELECT |
||||
JSON_OBJECT('node_sockstat_TCP_tw', node_sockstat_TCP_tw), |
||||
t AS time |
||||
FROM ( |
||||
SELECT |
||||
avg(JSON_EXTRACT(metric_data, "$.node_sockstat_TCP_tw")) node_sockstat_TCP_tw, |
||||
DATE_FORMAT(time,'%Y-%m-%d %H:00:00') as t |
||||
FROM record |
||||
WHERE `time` BETWEEN '2024-04-17 14:00:00' and '2024-04-17 15:00:00' |
||||
GROUP BY t ORDER BY t |
||||
) AS tab |
||||
--> |
||||
</select> |
||||
|
||||
</mapper> |
@ -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<MetricRecordEntity> getMetricDataRange(String instanceId, |
||||
List<String> 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<String> 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<String> filterMetricItems(List<String> metricItems) { |
||||
List<String> filteredMetricItems = new ArrayList<>(metricItems.size()); |
||||
for (String metricItem : metricItems) { |
||||
if (allowedMetricItems.contains(metricItem)) { |
||||
filteredMetricItems.add(metricItem); |
||||
} else { |
||||
log.warn("[MetricService]: 含有非允许的指标值:{}", metricItem); |
||||
} |
||||
} |
||||
|
||||
return filteredMetricItems; |
||||
} |
||||
} |
@ -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 |
Loading…
Reference in new issue