[monitor] 提供指标实时数据查询API,初始化监控详情页代码

This commit is contained in:
tomsun28
2021-12-05 18:23:45 +08:00
parent 2b8b1430b6
commit f4b20ab13a
15 changed files with 352 additions and 7 deletions

View File

@@ -0,0 +1,82 @@
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.Value;
import com.usthe.common.entity.dto.ValueRow;
import com.usthe.common.entity.message.CollectRep;
import com.usthe.warehouse.store.RedisDataStorage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.stream.Collectors;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
/**
* 指标数据查询接口
* @author tom
* @date 2021/12/5 15:52
*/
@RestController
@RequestMapping(produces = {APPLICATION_JSON_VALUE})
@Api(tags = "监控指标数据API")
public class MetricsDataController {
@Autowired
private RedisDataStorage redisDataStorage;
@GetMapping("/monitors/{monitorId}/metrics/{metric}")
@ApiOperation(value = "查询监控指标组的指标数据", notes = "查询监控指标组的指标数据")
public ResponseEntity<Message<MetricsData>> getMetricsData(
@ApiParam(value = "监控ID", example = "343254354")
@PathVariable Long monitorId,
@ApiParam(value = "监控指标组", example = "cpu")
@PathVariable String metric) {
CollectRep.MetricsData redisData = redisDataStorage.getCurrentMetricsData(monitorId, metric);
if (redisData == null) {
return ResponseEntity.ok().body(new Message<>("query metrics data is empty"));
}
{
MetricsData.MetricsDataBuilder dataBuilder = MetricsData.builder();
dataBuilder.id(redisData.getId()).app(redisData.getApp()).metric(redisData.getMetrics())
.time(redisData.getTime());
List<Field> fields = redisData.getFieldsList().stream().map(redisField ->
Field.builder().name(redisField.getName())
.type(Integer.valueOf(redisField.getType()).byteValue()).build())
.collect(Collectors.toList());
dataBuilder.fields(fields);
List<ValueRow> valueRows = redisData.getValuesList().stream().map(redisValueRow ->
ValueRow.builder().instance(redisValueRow.getInstance())
.values(redisValueRow.getColumnsList().stream().map(Value::new).collect(Collectors.toList()))
.build()).collect(Collectors.toList());
dataBuilder.valueRows(valueRows);
return ResponseEntity.ok().body(new Message<>(dataBuilder.build()));
}
}
@GetMapping("/monitors/{monitorId}/metrics/{metric}/fields/{field}")
@ApiOperation(value = "查询监控指标组的指定指标的历史数据", notes = "查询监控指标组下的指定指标的历史数据")
public ResponseEntity<Message<Void>> getMetricHistoryData(
@ApiParam(value = "监控ID", example = "343254354")
@PathVariable Long monitorId,
@ApiParam(value = "监控指标组", example = "cpu")
@PathVariable String metric,
@ApiParam(value = "监控指标组指标", example = "343254354")
@PathVariable String field,
@ApiParam(value = "查询历史时间段,默认6h-6小时:h-小时, d-天, m-月, y-年", example = "6h")
@RequestParam(required = false) String history
) {
return ResponseEntity.ok().body(null);
}
}

View File

@@ -8,11 +8,13 @@ import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisURI;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.async.RedisAsyncCommands;
import io.lettuce.core.api.sync.RedisCommands;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.NonNull;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
@@ -43,6 +45,11 @@ public class RedisDataStorage implements DisposableBean {
startStorageData();
}
public CollectRep.MetricsData getCurrentMetricsData(@NonNull Long monitorId, @NonNull String metric) {
RedisCommands<String, CollectRep.MetricsData> commands = connection.sync();
return commands.hget(String.valueOf(monitorId), metric);
}
private void startStorageData() {
Runnable runnable = () -> {
Thread.currentThread().setName("warehouse-redis-data-storage");

View File

@@ -4,4 +4,5 @@ com.usthe.warehouse.MetricsDataQueue,\
com.usthe.warehouse.WarehouseWorkerPool,\
com.usthe.warehouse.entrance.KafkaDataConsume,\
com.usthe.warehouse.store.InfluxdbDataStorage,\
com.usthe.warehouse.store.RedisDataStorage
com.usthe.warehouse.store.RedisDataStorage,\
com.usthe.warehouse.controller.MetricsDataController