|
|
@ -2,7 +2,10 @@ package rition.backend.api.v1.panel; |
|
|
|
|
|
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.springframework.data.redis.core.RedisTemplate; |
|
|
|
import org.springframework.data.redis.core.RedisTemplate; |
|
|
|
import org.springframework.web.bind.annotation.*; |
|
|
|
import org.springframework.web.bind.annotation.PostMapping; |
|
|
|
|
|
|
|
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.request.MetricDataRequest; |
|
|
|
import rition.backend.api.v1.dto.response.MetricDataResponse; |
|
|
|
import rition.backend.api.v1.dto.response.MetricDataResponse; |
|
|
|
import rition.backend.api.v1.dto.response.Response; |
|
|
|
import rition.backend.api.v1.dto.response.Response; |
|
|
@ -12,9 +15,7 @@ import rition.common.data.enums.Constants; |
|
|
|
import rition.service.panel.MetricService; |
|
|
|
import rition.service.panel.MetricService; |
|
|
|
|
|
|
|
|
|
|
|
import java.time.Instant; |
|
|
|
import java.time.Instant; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.*; |
|
|
|
import java.util.HashSet; |
|
|
|
|
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Slf4j |
|
|
|
@Slf4j |
|
|
|
@RestController |
|
|
|
@RestController |
|
|
@ -30,7 +31,10 @@ public class MetricsViewController { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@PostMapping("/list") |
|
|
|
@PostMapping("/list") |
|
|
|
public Response<List<MetricDataResponse>> getMetrics(@RequestBody MetricDataRequest metricDataRequest) { |
|
|
|
public Response<MetricDataResponse> getMetrics(@RequestBody MetricDataRequest metricDataRequest) { |
|
|
|
|
|
|
|
MetricDataResponse metricDataResponse = new MetricDataResponse(); |
|
|
|
|
|
|
|
metricDataResponse.setMetricData(new HashMap<>(metricDataRequest.getMetricItems().size())); |
|
|
|
|
|
|
|
|
|
|
|
// 如果未给定范围参数,则使用最近一小时的数据
|
|
|
|
// 如果未给定范围参数,则使用最近一小时的数据
|
|
|
|
if (metricDataRequest.getStart() == null || metricDataRequest.getEnd() == null) { |
|
|
|
if (metricDataRequest.getStart() == null || metricDataRequest.getEnd() == null) { |
|
|
|
List<Object> recentMetricDataList = |
|
|
|
List<Object> recentMetricDataList = |
|
|
@ -38,19 +42,28 @@ public class MetricsViewController { |
|
|
|
Constants.RedisKeys.RECENT_METRIC_CACHE, 0, Constants.MAX_METRIC_CACHE_NUM |
|
|
|
Constants.RedisKeys.RECENT_METRIC_CACHE, 0, Constants.MAX_METRIC_CACHE_NUM |
|
|
|
); |
|
|
|
); |
|
|
|
if (recentMetricDataList == null) { |
|
|
|
if (recentMetricDataList == null) { |
|
|
|
return Response.success(new ArrayList<>()); |
|
|
|
return Response.success(MetricDataResponse.builder().metricData(new HashMap<>()).build()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (String metricItem : metricDataRequest.getMetricItems()) { |
|
|
|
|
|
|
|
metricDataResponse.getMetricData().put(metricItem, MetricDataResponse.MetricDataResponseItem.builder() |
|
|
|
|
|
|
|
.time(new ArrayList<>(recentMetricDataList.size())) |
|
|
|
|
|
|
|
.value(new ArrayList<>(recentMetricDataList.size())) |
|
|
|
|
|
|
|
.build() |
|
|
|
|
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
List<MetricDataResponse> responseList = new ArrayList<>(recentMetricDataList.size()); |
|
|
|
|
|
|
|
for (Object recentMetricData : recentMetricDataList) { |
|
|
|
for (Object recentMetricData : recentMetricDataList) { |
|
|
|
var data = (MetricDataDto) recentMetricData; |
|
|
|
var metricDataDto = (MetricDataDto) recentMetricData; |
|
|
|
responseList.add(MetricDataResponse.builder() |
|
|
|
Map<String, Double> data = metricDataDto.getData(); |
|
|
|
.metricData(data.getData()) |
|
|
|
for (String metric : data.keySet()) { |
|
|
|
.time(data.getTimestamp()) |
|
|
|
var metricResponseData = metricDataResponse.getMetricData().get(metric); |
|
|
|
.build()); |
|
|
|
metricResponseData.getTime().add(metricDataDto.getTimestamp()); |
|
|
|
|
|
|
|
metricResponseData.getValue().add(data.get(metric)); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return Response.success(responseList); |
|
|
|
return Response.success(metricDataResponse); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var entityResult = metricService.getMetricDataRange( |
|
|
|
var entityResult = metricService.getMetricDataRange( |
|
|
@ -61,16 +74,25 @@ public class MetricsViewController { |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
// 转换数据
|
|
|
|
// 转换数据
|
|
|
|
List<MetricDataResponse> responseList = new ArrayList<>(entityResult.size()); |
|
|
|
|
|
|
|
for (MetricRecordEntity entity : entityResult) { |
|
|
|
for (String metricItem : metricDataRequest.getMetricItems()) { |
|
|
|
responseList.add(MetricDataResponse.builder() |
|
|
|
metricDataResponse.getMetricData().put(metricItem, MetricDataResponse.MetricDataResponseItem.builder() |
|
|
|
.metricData(entity.getMetricData()) |
|
|
|
.time(new ArrayList<>(entityResult.size())) |
|
|
|
.time(entity.getTime().toEpochMilli()) |
|
|
|
.value(new ArrayList<>(entityResult.size())) |
|
|
|
.build() |
|
|
|
.build() |
|
|
|
); |
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return Response.success(responseList); |
|
|
|
for (MetricRecordEntity entity : entityResult) { |
|
|
|
|
|
|
|
var data = entity.getMetricData(); |
|
|
|
|
|
|
|
for (String metric : data.keySet()) { |
|
|
|
|
|
|
|
var metricResponseData = metricDataResponse.getMetricData().get(metric); |
|
|
|
|
|
|
|
metricResponseData.getTime().add(entity.getTime().toEpochMilli()); |
|
|
|
|
|
|
|
metricResponseData.getValue().add(data.get(metric)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return Response.success(metricDataResponse); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|