diff --git a/warehouse/src/main/java/com/usthe/warehouse/controller/MetricsDataController.java b/warehouse/src/main/java/com/usthe/warehouse/controller/MetricsDataController.java index 809c1a9..b606de5 100644 --- a/warehouse/src/main/java/com/usthe/warehouse/controller/MetricsDataController.java +++ b/warehouse/src/main/java/com/usthe/warehouse/controller/MetricsDataController.java @@ -3,10 +3,13 @@ package com.usthe.warehouse.controller; import com.usthe.common.entity.dto.Field; import com.usthe.common.entity.dto.Message; import com.usthe.common.entity.dto.MetricsData; +import com.usthe.common.entity.dto.MetricsHistoryData; import com.usthe.common.entity.dto.Value; import com.usthe.common.entity.dto.ValueRow; import com.usthe.common.entity.message.CollectRep; +import com.usthe.common.util.CommonConstants; import com.usthe.warehouse.store.RedisDataStorage; +import com.usthe.warehouse.store.TdEngineDataStorage; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; @@ -19,6 +22,7 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.List; +import java.util.Map; import java.util.stream.Collectors; import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; @@ -38,6 +42,9 @@ public class MetricsDataController { @Autowired private RedisDataStorage redisDataStorage; + @Autowired + private TdEngineDataStorage tdEngineDataStorage; + @GetMapping("/monitor/{monitorId}/metrics/{metrics}") @ApiOperation(value = "查询监控指标组的指标数据", notes = "查询监控指标组的指标数据") public ResponseEntity> getMetricsData( @@ -70,21 +77,40 @@ public class MetricsDataController { @GetMapping("/monitor/{monitorId}/metric/{metricFull}") @ApiOperation(value = "查询监控指标组的指定指标的历史数据", notes = "查询监控指标组下的指定指标的历史数据") - public ResponseEntity> getMetricHistoryData( + public ResponseEntity> getMetricHistoryData( @ApiParam(value = "监控ID", example = "343254354") @PathVariable Long monitorId, @ApiParam(value = "监控指标全路径", example = "linux.cpu.usage") @PathVariable() String metricFull, - @ApiParam(value = "查询历史时间段,默认6h-6小时:h-小时, d-天, m-月, y-年", example = "6h") - @RequestParam(required = false) String history + @ApiParam(value = "所属实例,默认空", example = "disk2") + @RequestParam(required = false) String instance, + @ApiParam(value = "查询历史时间段,默认6h-6小时:s-秒、m-分, h-小时, d-天, w-周", example = "6h") + @RequestParam(required = false) String history, + @ApiParam(value = "是否计算聚合数据,需查询时间段大于1周以上,默认不开启,聚合降样时间窗口默认为4小时", example = "false") + @RequestParam(required = false) Boolean interval ) { - String[] names = metricFull.split("."); + String[] names = metricFull.split("\\."); if (names.length != METRIC_FULL_LENGTH) { throw new IllegalArgumentException("metrics full name: " + metricFull + " is illegal."); } String app = names[0]; String metrics = names[1]; String metric = names[2]; - return ResponseEntity.ok().body(null); + if (history == null) { + history = "6h"; + } + Map> instanceValuesMap; + if (interval == null || !interval) { + instanceValuesMap = tdEngineDataStorage + .getHistoryMetricData(monitorId, app, metrics, metric, instance, history); + } else { + instanceValuesMap = tdEngineDataStorage + .getHistoryIntervalMetricData(monitorId, app, metrics, metric, instance, history); + } + MetricsHistoryData historyData = MetricsHistoryData.builder() + .id(monitorId).metric(metrics).values(instanceValuesMap) + .field(Field.builder().name(metric).type(CommonConstants.TYPE_NUMBER).build()) + .build(); + return ResponseEntity.ok().body(new Message<>(historyData)); } }