1
0

refactor: 优化年线情况计算

This commit is contained in:
2025-09-26 17:25:29 +08:00
parent 0dd421ca43
commit d4fec4c426
5 changed files with 37 additions and 40 deletions

View File

@@ -32,11 +32,7 @@ public class AssessmentService {
public Set<Result> assess(Set<Stock> stocks, int year) { public Set<Result> assess(Set<Stock> stocks, int year) {
if (ObjectUtil.isNotEmpty(stocks)) { if (ObjectUtil.isNotEmpty(stocks)) {
var industries = stocks var topChange = industryService.topChange(year, stocks);
.stream()
.map(Stock::getIndustry)
.collect(Collectors.toSet());
var topChange = industryService.topChange(year, industries, stocks);
var dailyMap = dailyRepository.findAll( var dailyMap = dailyRepository.findAll(
QDaily.daily.tradeDate.year().eq(year) QDaily.daily.tradeDate.year().eq(year)
.and(QDaily.daily.stock.in(stocks)) .and(QDaily.daily.stock.in(stocks))

View File

@@ -1,5 +1,6 @@
package com.lanyuanxiaoyao.leopard.core.service; 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.Daily;
import com.lanyuanxiaoyao.leopard.core.entity.QDaily; import com.lanyuanxiaoyao.leopard.core.entity.QDaily;
import com.lanyuanxiaoyao.leopard.core.entity.Stock; import com.lanyuanxiaoyao.leopard.core.entity.Stock;
@@ -31,29 +32,24 @@ public class IndustryService {
} }
public Map<IndustryYearlyKey, Double> topChange(int year) { 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) { public Map<IndustryYearlyKey, Double> topChange(int year, Set<Stock> includeStocks) {
return topChange(year, includeIndustries, null); return topChange(year, year, includeStocks);
}
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 startYear, int endYear) { 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) { public Map<IndustryYearlyKey, Double> topChange(int startYear, int endYear, Set<Stock> includeStocks) {
return topChange(startYear, endYear, includeIndustries, null); var includeIndustries = ObjectUtil.isNull(includeStocks)
} ? null
: includeStocks.stream().map(Stock::getIndustry).collect(Collectors.toSet());
public Map<IndustryYearlyKey, Double> topChange(int startYear, int endYear, Set<String> includeIndustries, Set<Stock> includeStocks) {
return stockRepository.findDistinctIndustries() return stockRepository.findDistinctIndustries()
.parallelStream() .parallelStream()
.filter(industry -> includeIndustries == null || includeIndustries.contains(industry)) .filter(o -> ObjectUtil.isNull(includeIndustries) || includeIndustries.contains(o))
.flatMap(industry -> { .flatMap(industry -> {
var keys = new ArrayList<IndustryYearlyKey>(); var keys = new ArrayList<IndustryYearlyKey>();
for (int year = startYear; year <= endYear; year++) { for (int year = startYear; year <= endYear; year++) {
@@ -62,7 +58,6 @@ public class IndustryService {
return keys.stream(); return keys.stream();
}) })
.map(key -> { .map(key -> {
log.info("计算行业 {} {} 年度涨跌幅", key.industry(), key.year());
var maxChange = dailyRepository var maxChange = dailyRepository
.findAll( .findAll(
QDaily.daily.stock.industry.eq(key.industry()) QDaily.daily.stock.industry.eq(key.industry())
@@ -90,4 +85,14 @@ public class IndustryService {
public record IndustryYearlyKey(String industry, int year) { public record IndustryYearlyKey(String industry, int year) {
} }
public record IndustryYearlyData(
String industry,
int year,
double maxChange,
double minChange,
double avgChange,
double medianChange
) {
}
} }

View File

@@ -11,7 +11,6 @@ import com.lanyuanxiaoyao.leopard.core.task.UpdateFinanceIndicatorTask;
import com.lanyuanxiaoyao.leopard.core.task.UpdateStockTask; import com.lanyuanxiaoyao.leopard.core.task.UpdateStockTask;
import com.lanyuanxiaoyao.leopard.core.task.UpdateYearlyTask; import com.lanyuanxiaoyao.leopard.core.task.UpdateYearlyTask;
import com.lanyuanxiaoyao.service.template.service.SimpleServiceSupport; import com.lanyuanxiaoyao.service.template.service.SimpleServiceSupport;
import jakarta.transaction.Transactional;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
@@ -20,9 +19,7 @@ import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
import lombok.Getter; import lombok.Getter;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
/** /**
@@ -33,7 +30,6 @@ import org.springframework.stereotype.Service;
@Service @Service
public class TaskService extends SimpleServiceSupport<Task> { public class TaskService extends SimpleServiceSupport<Task> {
private final ExecutorService executor = Executors.newFixedThreadPool(50); private final ExecutorService executor = Executors.newFixedThreadPool(50);
private final TaskRepository taskRepository;
private final ApplicationContext context; private final ApplicationContext context;
@Getter @Getter
@@ -49,17 +45,9 @@ public class TaskService extends SimpleServiceSupport<Task> {
public TaskService(TaskRepository repository, ApplicationContext context) { public TaskService(TaskRepository repository, ApplicationContext context) {
super(repository); super(repository);
this.taskRepository = repository;
this.context = context; this.context = context;
} }
@Transactional(rollbackOn = Throwable.class)
@EventListener(ApplicationReadyEvent.class)
public void onApplicationReady() {
log.warn("更新所有未完成的任务状态为失败");
taskRepository.updateAllRunningTaskToFailure();
}
public TaskTemplate getTemplate(String templateId) { public TaskTemplate getTemplate(String templateId) {
return templateMap.get(templateId); return templateMap.get(templateId);
} }

View File

@@ -1,5 +1,7 @@
package com.lanyuanxiaoyao.leopard.server; package com.lanyuanxiaoyao.leopard.server;
import com.lanyuanxiaoyao.leopard.core.repository.TaskRepository;
import jakarta.transaction.Transactional;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner; import org.springframework.boot.ApplicationRunner;
@@ -15,11 +17,20 @@ import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
@SpringBootApplication(scanBasePackages = "com.lanyuanxiaoyao.leopard") @SpringBootApplication(scanBasePackages = "com.lanyuanxiaoyao.leopard")
@EnableJpaAuditing @EnableJpaAuditing
public class LeopardServerApplication implements ApplicationRunner { public class LeopardServerApplication implements ApplicationRunner {
private final TaskRepository taskRepository;
public LeopardServerApplication(TaskRepository taskRepository) {
this.taskRepository = taskRepository;
}
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(LeopardServerApplication.class, args); SpringApplication.run(LeopardServerApplication.class, args);
} }
@Transactional(rollbackOn = Throwable.class)
@Override @Override
public void run(ApplicationArguments args) { public void run(ApplicationArguments args) {
log.warn("更新所有未完成的任务状态为失败");
taskRepository.updateAllRunningTaskToFailure();
} }
} }

View File

@@ -24,14 +24,6 @@
<groupId>com.yomahub</groupId> <groupId>com.yomahub</groupId>
<artifactId>liteflow-spring-boot-starter</artifactId> <artifactId>liteflow-spring-boot-starter</artifactId>
</dependency> </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> <dependency>
<groupId>org.commonmark</groupId> <groupId>org.commonmark</groupId>
<artifactId>commonmark</artifactId> <artifactId>commonmark</artifactId>
@@ -53,6 +45,11 @@
<artifactId>postgresql</artifactId> <artifactId>postgresql</artifactId>
<scope>runtime</scope> <scope>runtime</scope>
</dependency> </dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>