소스 검색

优化数据结构 添加增量接口

JiahengHe 1 년 전
부모
커밋
dd4fe2b773

+ 11 - 24
price_collection/src/main/java/com/liangjiang/price_collection/controller/ApiController.java

@@ -2,6 +2,7 @@ package com.liangjiang.price_collection.controller;
 
 import com.liangjiang.price_collection.dto.ExchangeCoinPriceArrDto;
 import com.liangjiang.price_collection.dto.PriceInfoDto;
+import com.liangjiang.price_collection.dto.PriceListDto;
 import com.liangjiang.price_collection.service.ITableService;
 import com.liangjiang.price_collection.service.impl.QueryService;
 import lombok.RequiredArgsConstructor;
@@ -37,38 +38,24 @@ public class ApiController {
     }
 
     @GetMapping("/getPrices")
-    public List<ExchangeCoinPriceArrDto> getPriceRest(@RequestParam("exchange") String exchange, @RequestParam("coin")String coin){
+    public PriceListDto getPriceRest(@RequestParam("exchange") String exchange, @RequestParam("coin")String coin){
         Integer[] times = QueryService.getDaysAgoTime(System.currentTimeMillis(), 3L);
         try {
             return queryService.performQueries(exchange.split(","), coin, times[1], times[0]);
         } catch (Exception ex){
             log.error("查询出错", ex);
-            return new LinkedList<>();
+            return PriceListDto.builder().time(new LinkedList<>()).values(new LinkedList<>()).build();
         }
     }
 
-    @GetMapping("/getPricesRest")
-    public List<ExchangeCoinPriceArrDto> getPrices(@RequestParam("exchange") String exchange, @RequestParam("coin")String coin){
-        System.out.println("exchange = " + exchange + ", coin = " + coin);
-        List<ExchangeCoinPriceArrDto> result = new LinkedList<>();
-        ExchangeCoinPriceArrDto dto1 = ExchangeCoinPriceArrDto.builder().exchange("BINANCE_SPOT").symbol("BTCUSDT").ask(new LinkedList<>()).bid(new LinkedList<>()).time(new LinkedList<>()).build();
-        ExchangeCoinPriceArrDto dto2 = ExchangeCoinPriceArrDto.builder().exchange("GATE_SPOT").symbol("BTCUSDT").ask(new LinkedList<>()).bid(new LinkedList<>()).time(new LinkedList<>()).build();
-        int time = 1565613908;
-        for (int i = 1; i < 5; i ++){
-            int x = i * 2 + 1;
-            int y = i * 2 - 2;
-            dto1.getAsk().add(Integer.toString(x));
-            dto1.getBid().add(Integer.toString(y));
-            dto1.getTime().add(time);
-            int x1 = i * 2;
-            int y1 = i * 2 - 1;
-            dto2.getAsk().add(Integer.toString(x1));
-            dto2.getBid().add(Integer.toString(y1));
-            dto2.getTime().add(time);
-            time ++;
+    @GetMapping("/getAddPrices")
+    public PriceListDto getAddPrices(@RequestParam("exchange") String exchange, @RequestParam("coin")String coin,@RequestParam("startTime") Integer startTime){
+        Integer endTime = Math.toIntExact(System.currentTimeMillis()/1000);
+        try {
+            return queryService.performQueries(exchange.split(","), coin, startTime, endTime);
+        } catch (Exception ex){
+            log.error("查询出错", ex);
+            return PriceListDto.builder().time(new LinkedList<>()).values(new LinkedList<>()).build();
         }
-        result.add(dto1);
-        result.add(dto2);
-        return result;
     }
 }

+ 0 - 1
price_collection/src/main/java/com/liangjiang/price_collection/dto/ExchangeCoinPriceArrDto.java

@@ -10,7 +10,6 @@ import java.util.List;
 public class ExchangeCoinPriceArrDto {
     private String exchange;
     private String symbol;
-    private List<Integer> time;
     private List<String> ask;
     private List<String> bid;
 }

+ 15 - 0
price_collection/src/main/java/com/liangjiang/price_collection/dto/PriceListDto.java

@@ -0,0 +1,15 @@
+package com.liangjiang.price_collection.dto;
+
+import lombok.Builder;
+import lombok.Data;
+
+import java.util.List;
+
+@Data
+@Builder
+public class PriceListDto {
+    // 时间
+    private List<Integer> time;
+    // 交易所价格数据
+    private List<ExchangeCoinPriceArrDto> values;
+}

+ 6 - 2
price_collection/src/main/java/com/liangjiang/price_collection/service/impl/QueryService.java

@@ -4,6 +4,7 @@ import cn.hutool.core.collection.CollectionUtil;
 import com.liangjiang.price_collection.dto.ExchangeCoinPriceArrDto;
 import com.liangjiang.price_collection.dto.ExchangeCoinPriceDto;
 import com.liangjiang.price_collection.dto.PriceDto;
+import com.liangjiang.price_collection.dto.PriceListDto;
 import com.liangjiang.price_collection.service.ITableService;
 import com.liangjiang.price_collection.utils.DataAssembleTask;
 import com.liangjiang.price_collection.utils.TableQueryTask;
@@ -26,9 +27,10 @@ public class QueryService {
         this.tableService = tableService;
     }
 
-    public List<ExchangeCoinPriceArrDto> performQueries(String[] exchanges, String coin, Integer startTime, Integer endTime) throws Exception {
+    public PriceListDto performQueries(String[] exchanges, String coin, Integer startTime, Integer endTime) throws Exception {
         List<Future<ExchangeCoinPriceDto>> futures = new ArrayList<>();
         Set<ExchangeCoinPriceDto> results = new HashSet<>();
+        PriceListDto priceListDto = PriceListDto.builder().build();
         // 分发查询
         for (String exchange : exchanges) {
             TableQueryTask queryTask = new TableQueryTask(tableService, coin, exchange, startTime, endTime);
@@ -57,7 +59,9 @@ public class QueryService {
             ExchangeCoinPriceArrDto dto = future.get();
             data.add(dto);
         }
-        return data;
+        priceListDto.setTime(times);
+        priceListDto.setValues(data);
+        return priceListDto;
     }
 
     // 用于优雅关闭服务

+ 0 - 2
price_collection/src/main/java/com/liangjiang/price_collection/utils/DataAssembleTask.java

@@ -23,7 +23,6 @@ public class DataAssembleTask  implements Callable<ExchangeCoinPriceArrDto> {
         ExchangeCoinPriceArrDto exchangeCoinPrice = ExchangeCoinPriceArrDto.builder()
                 .exchange(dto.getExchange())
                 .symbol(coin)
-                .time(new LinkedList<>())
                 .ask(new LinkedList<>()).bid(new LinkedList<>()).build();
         for(Integer time:times){
             TreeMap<Integer, PriceDto> priceDtoTreeMap = dto.getPrices();
@@ -44,7 +43,6 @@ public class DataAssembleTask  implements Callable<ExchangeCoinPriceArrDto> {
             }
             exchangeCoinPrice.getAsk().add(ask);
             exchangeCoinPrice.getBid().add(bid);
-            exchangeCoinPrice.getTime().add(time);
         }
         return exchangeCoinPrice;
     }