refactor: 优化年线情况计算
This commit is contained in:
@@ -32,11 +32,7 @@ public class AssessmentService {
|
||||
|
||||
public Set<Result> assess(Set<Stock> stocks, int year) {
|
||||
if (ObjectUtil.isNotEmpty(stocks)) {
|
||||
var industries = stocks
|
||||
.stream()
|
||||
.map(Stock::getIndustry)
|
||||
.collect(Collectors.toSet());
|
||||
var topChange = industryService.topChange(year, industries, stocks);
|
||||
var topChange = industryService.topChange(year, stocks);
|
||||
var dailyMap = dailyRepository.findAll(
|
||||
QDaily.daily.tradeDate.year().eq(year)
|
||||
.and(QDaily.daily.stock.in(stocks))
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.lanyuanxiaoyao.leopard.core.service;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.lanyuanxiaoyao.leopard.core.entity.Daily;
|
||||
import com.lanyuanxiaoyao.leopard.core.entity.QDaily;
|
||||
import com.lanyuanxiaoyao.leopard.core.entity.Stock;
|
||||
@@ -31,29 +32,24 @@ public class IndustryService {
|
||||
}
|
||||
|
||||
public Map<IndustryYearlyKey, Double> topChange(int year) {
|
||||
return topChange(year, null, null);
|
||||
return topChange(year, null);
|
||||
}
|
||||
|
||||
public Map<IndustryYearlyKey, Double> topChange(int year, Set<String> includeIndustries) {
|
||||
return topChange(year, includeIndustries, null);
|
||||
}
|
||||
|
||||
public Map<IndustryYearlyKey, Double> topChange(int year, Set<String> includeIndustries, Set<Stock> includeStocks) {
|
||||
return topChange(year, year, includeIndustries, includeStocks);
|
||||
public Map<IndustryYearlyKey, Double> topChange(int year, Set<Stock> includeStocks) {
|
||||
return topChange(year, year, includeStocks);
|
||||
}
|
||||
|
||||
public Map<IndustryYearlyKey, Double> topChange(int startYear, int endYear) {
|
||||
return topChange(startYear, endYear, null, null);
|
||||
return topChange(startYear, endYear, null);
|
||||
}
|
||||
|
||||
public Map<IndustryYearlyKey, Double> topChange(int startYear, int endYear, Set<String> includeIndustries) {
|
||||
return topChange(startYear, endYear, includeIndustries, null);
|
||||
}
|
||||
|
||||
public Map<IndustryYearlyKey, Double> topChange(int startYear, int endYear, Set<String> includeIndustries, Set<Stock> includeStocks) {
|
||||
public Map<IndustryYearlyKey, Double> topChange(int startYear, int endYear, Set<Stock> includeStocks) {
|
||||
var includeIndustries = ObjectUtil.isNull(includeStocks)
|
||||
? null
|
||||
: includeStocks.stream().map(Stock::getIndustry).collect(Collectors.toSet());
|
||||
return stockRepository.findDistinctIndustries()
|
||||
.parallelStream()
|
||||
.filter(industry -> includeIndustries == null || includeIndustries.contains(industry))
|
||||
.filter(o -> ObjectUtil.isNull(includeIndustries) || includeIndustries.contains(o))
|
||||
.flatMap(industry -> {
|
||||
var keys = new ArrayList<IndustryYearlyKey>();
|
||||
for (int year = startYear; year <= endYear; year++) {
|
||||
@@ -62,7 +58,6 @@ public class IndustryService {
|
||||
return keys.stream();
|
||||
})
|
||||
.map(key -> {
|
||||
log.info("计算行业 {} {} 年度涨跌幅", key.industry(), key.year());
|
||||
var maxChange = dailyRepository
|
||||
.findAll(
|
||||
QDaily.daily.stock.industry.eq(key.industry())
|
||||
@@ -90,4 +85,14 @@ public class IndustryService {
|
||||
|
||||
public record IndustryYearlyKey(String industry, int year) {
|
||||
}
|
||||
|
||||
public record IndustryYearlyData(
|
||||
String industry,
|
||||
int year,
|
||||
double maxChange,
|
||||
double minChange,
|
||||
double avgChange,
|
||||
double medianChange
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,6 @@ import com.lanyuanxiaoyao.leopard.core.task.UpdateFinanceIndicatorTask;
|
||||
import com.lanyuanxiaoyao.leopard.core.task.UpdateStockTask;
|
||||
import com.lanyuanxiaoyao.leopard.core.task.UpdateYearlyTask;
|
||||
import com.lanyuanxiaoyao.service.template.service.SimpleServiceSupport;
|
||||
import jakarta.transaction.Transactional;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
@@ -20,9 +19,7 @@ import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
import lombok.Getter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
@@ -33,7 +30,6 @@ import org.springframework.stereotype.Service;
|
||||
@Service
|
||||
public class TaskService extends SimpleServiceSupport<Task> {
|
||||
private final ExecutorService executor = Executors.newFixedThreadPool(50);
|
||||
private final TaskRepository taskRepository;
|
||||
private final ApplicationContext context;
|
||||
|
||||
@Getter
|
||||
@@ -49,17 +45,9 @@ public class TaskService extends SimpleServiceSupport<Task> {
|
||||
|
||||
public TaskService(TaskRepository repository, ApplicationContext context) {
|
||||
super(repository);
|
||||
this.taskRepository = repository;
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@Transactional(rollbackOn = Throwable.class)
|
||||
@EventListener(ApplicationReadyEvent.class)
|
||||
public void onApplicationReady() {
|
||||
log.warn("更新所有未完成的任务状态为失败");
|
||||
taskRepository.updateAllRunningTaskToFailure();
|
||||
}
|
||||
|
||||
public TaskTemplate getTemplate(String templateId) {
|
||||
return templateMap.get(templateId);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.lanyuanxiaoyao.leopard.server;
|
||||
|
||||
import com.lanyuanxiaoyao.leopard.core.repository.TaskRepository;
|
||||
import jakarta.transaction.Transactional;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
@@ -15,11 +17,20 @@ import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
|
||||
@SpringBootApplication(scanBasePackages = "com.lanyuanxiaoyao.leopard")
|
||||
@EnableJpaAuditing
|
||||
public class LeopardServerApplication implements ApplicationRunner {
|
||||
private final TaskRepository taskRepository;
|
||||
|
||||
public LeopardServerApplication(TaskRepository taskRepository) {
|
||||
this.taskRepository = taskRepository;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(LeopardServerApplication.class, args);
|
||||
}
|
||||
|
||||
@Transactional(rollbackOn = Throwable.class)
|
||||
@Override
|
||||
public void run(ApplicationArguments args) {
|
||||
log.warn("更新所有未完成的任务状态为失败");
|
||||
taskRepository.updateAllRunningTaskToFailure();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,14 +24,6 @@
|
||||
<groupId>com.yomahub</groupId>
|
||||
<artifactId>liteflow-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.github.ralfkonrad.quantlib_for_maven</groupId>
|
||||
<artifactId>quantlib</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.ta4j</groupId>
|
||||
<artifactId>ta4j-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.commonmark</groupId>
|
||||
<artifactId>commonmark</artifactId>
|
||||
@@ -53,6 +45,11 @@
|
||||
<artifactId>postgresql</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
Reference in New Issue
Block a user