ref: 调整前端面板监测数据格式,优化传输大小

dev-lensfrex
lensfrex 7 months ago
parent 41221a4bca
commit c82c6d5799
Signed by: lensfrex
GPG Key ID: B1E395B3C6CA0356
  1. 18
      rition-center/api/src/main/java/rition/backend/api/v1/dto/response/MetricDataResponse.java
  2. 60
      rition-center/api/src/main/java/rition/backend/api/v1/panel/MetricsViewController.java
  3. 9
      rition-panel/src/pages/MetricPage.vue

@ -1,12 +1,11 @@
package rition.backend.api.v1.dto.response;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
import java.util.Map;
@Data
@ -17,11 +16,14 @@ public class MetricDataResponse {
/**
* 监控指标值使用json格式存储
*/
@TableField(typeHandler = JacksonTypeHandler.class)
private Map<String, Double> metricData;
private Map<String, MetricDataResponseItem> metricData;
/**
* create_time
*/
private Long time;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class MetricDataResponseItem {
private List<Long> time;
private List<Double> value;
}
}

@ -2,7 +2,10 @@ package rition.backend.api.v1.panel;
import lombok.extern.slf4j.Slf4j;
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.response.MetricDataResponse;
import rition.backend.api.v1.dto.response.Response;
@ -12,9 +15,7 @@ import rition.common.data.enums.Constants;
import rition.service.panel.MetricService;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.*;
@Slf4j
@RestController
@ -30,7 +31,10 @@ public class MetricsViewController {
}
@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) {
List<Object> recentMetricDataList =
@ -38,19 +42,28 @@ public class MetricsViewController {
Constants.RedisKeys.RECENT_METRIC_CACHE, 0, Constants.MAX_METRIC_CACHE_NUM
);
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) {
var data = (MetricDataDto) recentMetricData;
responseList.add(MetricDataResponse.builder()
.metricData(data.getData())
.time(data.getTimestamp())
.build());
var metricDataDto = (MetricDataDto) recentMetricData;
Map<String, Double> data = metricDataDto.getData();
for (String metric : data.keySet()) {
var metricResponseData = metricDataResponse.getMetricData().get(metric);
metricResponseData.getTime().add(metricDataDto.getTimestamp());
metricResponseData.getValue().add(data.get(metric));
}
}
return Response.success(responseList);
return Response.success(metricDataResponse);
}
var entityResult = metricService.getMetricDataRange(
@ -61,16 +74,25 @@ public class MetricsViewController {
);
// 转换数据
List<MetricDataResponse> responseList = new ArrayList<>(entityResult.size());
for (MetricRecordEntity entity : entityResult) {
responseList.add(MetricDataResponse.builder()
.metricData(entity.getMetricData())
.time(entity.getTime().toEpochMilli())
for (String metricItem : metricDataRequest.getMetricItems()) {
metricDataResponse.getMetricData().put(metricItem, MetricDataResponse.MetricDataResponseItem.builder()
.time(new ArrayList<>(entityResult.size()))
.value(new ArrayList<>(entityResult.size()))
.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);
}

@ -143,10 +143,11 @@ function getMetricData() {
})
}
for (const dataItem of response.data) {
for (const metricItem in dataItem.metricData) {
const value = dataItem.metricData[metricItem]
metricOptionsMap.get(metricItem).series[0].data.push([dataItem.time / 1000, value])
const data = response.data.metricData
for (const metric in data) {
const metricDataItem = data[metric]
for (let i = 0; i < metricDataItem.time.length; i++) {
metricOptionsMap.get(metric).series[0].data.push([metricDataItem.time[i], metricDataItem.value[i]])
}
}

Loading…
Cancel
Save