Procházet zdrojové kódy

[warehouse]指标历史聚合数据接口实现

tomsun28 před 4 roky
rodič
revize
ef7f71952b

+ 31 - 5
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<Message<MetricsData>> getMetricsData(
@@ -70,21 +77,40 @@ public class MetricsDataController {
 
     @GetMapping("/monitor/{monitorId}/metric/{metricFull}")
     @ApiOperation(value = "查询监控指标组的指定指标的历史数据", notes = "查询监控指标组下的指定指标的历史数据")
-    public ResponseEntity<Message<Void>> getMetricHistoryData(
+    public ResponseEntity<Message<MetricsHistoryData>> 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<String, List<Value>> 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));
     }
 }