瀏覽代碼

修改时间段统计

JiahengHe 1 年之前
父節點
當前提交
00c74e9b95

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

@@ -6,11 +6,11 @@ import com.liangjiang.price_collection.service.ITableService;
 import com.liangjiang.price_collection.service.impl.QueryService;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
-import org.springframework.jms.core.JmsMessagingTemplate;
 import org.springframework.jms.core.JmsTemplate;
 import org.springframework.web.bind.annotation.*;
 
 import javax.jms.Queue;
+import java.util.HashSet;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Set;
@@ -56,7 +56,7 @@ public class ApiController {
 //            return queryService.performQueries(exchange.split(","), coin, start, end);
         } catch (Exception ex){
             log.error("查询出错", ex);
-            return PriceListDto.builder().time(new LinkedList<>()).values(new LinkedList<>()).build();
+            return PriceListDto.builder().time(new HashSet<>()).values(new LinkedList<>()).build();
         }
     }
 
@@ -67,7 +67,7 @@ public class ApiController {
             return queryService.performQueries(exchange.split(","), coin, startTime, endTime);
         } catch (Exception ex){
             log.error("查询出错", ex);
-            return PriceListDto.builder().time(new LinkedList<>()).values(new LinkedList<>()).build();
+            return PriceListDto.builder().time(new HashSet<>()).values(new LinkedList<>()).build();
         }
     }
 }

+ 2 - 1
price_collection/src/main/java/com/liangjiang/price_collection/dto/PriceListDto.java

@@ -4,12 +4,13 @@ import lombok.Builder;
 import lombok.Data;
 
 import java.util.List;
+import java.util.Set;
 
 @Data
 @Builder
 public class PriceListDto {
     // 时间
-    private List<Integer> time;
+    private Set<Integer> time;
     // 交易所价格数据
     private List<ExchangeCoinPriceArrDto> values;
 }

+ 9 - 5
price_collection/src/main/java/com/liangjiang/price_collection/service/impl/QueryService.java

@@ -11,10 +11,12 @@ import com.liangjiang.price_collection.utils.TableQueryTask;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import javax.print.DocFlavor;
 import java.util.*;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
+import java.util.stream.Collectors;
 
 @Service
 public class QueryService {
@@ -44,13 +46,15 @@ public class QueryService {
             }
         }
         if(CollectionUtil.isEmpty(results)){
-            return PriceListDto.builder().time(new LinkedList<>()).values(new LinkedList<>()).build();
+            return PriceListDto.builder().time(new HashSet<>()).values(new LinkedList<>()).build();
         }
         List<Future<ExchangeCoinPriceArrDto>> resultFutures = new LinkedList<>();
         List<ExchangeCoinPriceArrDto> data = new LinkedList<>();
-        // 获取时间段的所有秒级
-        List<Integer> times = generateArithmeticSequence(startTime, endTime);
-
+        // 获取已有时间的合集
+        Set<Integer> times = new HashSet<>();
+        results.forEach(dto -> {
+            times.addAll(dto.getPrices().keySet());
+        });
         for (ExchangeCoinPriceDto dto : results){
             DataAssembleTask task = new DataAssembleTask(dto, coin, times);
             resultFutures.add(executorService.submit(task));
@@ -62,7 +66,7 @@ public class QueryService {
             data.add(dto);
         }
         priceListDto.setTime(times);
-        priceListDto.setValues(data);
+        priceListDto.setValues(data.stream().sorted(Comparator.comparing(ExchangeCoinPriceArrDto::getExchange)).collect(Collectors.toList()));
         return priceListDto;
     }
 

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

@@ -5,10 +5,9 @@ import com.liangjiang.price_collection.dto.ExchangeCoinPriceDto;
 import com.liangjiang.price_collection.dto.PriceDto;
 import lombok.AllArgsConstructor;
 import lombok.Data;
-import lombok.extern.slf4j.Slf4j;
 
 import java.util.LinkedList;
-import java.util.List;
+import java.util.Set;
 import java.util.TreeMap;
 import java.util.concurrent.Callable;
 
@@ -17,7 +16,7 @@ import java.util.concurrent.Callable;
 public class DataAssembleTask  implements Callable<ExchangeCoinPriceArrDto> {
     private ExchangeCoinPriceDto dto;
     private String coin;
-    private List<Integer> times;
+    private Set<Integer> times;
 
     public ExchangeCoinPriceArrDto call() {
         ExchangeCoinPriceArrDto exchangeCoinPrice = ExchangeCoinPriceArrDto.builder()