Skip to content

Commit 7f2aafa

Browse files
committed
feat: improve efficiency
1 parent 1bfa967 commit 7f2aafa

File tree

1 file changed

+11
-39
lines changed

1 file changed

+11
-39
lines changed

backend/src/main/java/ch/xxx/trader/usecase/services/CoinbaseService.java

Lines changed: 11 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import java.util.Optional;
3939
import java.util.concurrent.CompletableFuture;
4040
import java.util.concurrent.ConcurrentHashMap;
41-
import java.util.concurrent.ForkJoinPool;
4241
import java.util.concurrent.TimeUnit;
4342
import java.util.concurrent.atomic.AtomicInteger;
4443
import java.util.function.BiConsumer;
@@ -84,7 +83,7 @@ private record GetSetMethodFunctions(Function<QuoteCb, BigDecimal> getter, BiCon
8483
private boolean cpuConstraint;
8584
private final List<String> nonValueFieldNames = List.of("_id", "createdAt", "class");
8685
private final List<PropertyDescriptor> propertyDescriptors;
87-
private final Scheduler mongoScheduler = Schedulers.newBoundedElastic(10, 10, "mongoImport", 10);
86+
private final Scheduler mongoScheduler = Schedulers.newBoundedElastic(6, 10, "mongoImport", 10);
8887
@Value("${single.instance.deployment:false}")
8988
private boolean singleInstanceDeployment;
9089

@@ -176,17 +175,17 @@ private String createHourDayAvg() {
176175
LocalDateTime start = LocalDateTime.now();
177176
LOG.info("CpuConstraint property: " + this.cpuConstraint);
178177
if (this.cpuConstraint) {
179-
this.createCbHourlyAvg();
180-
this.createCbDailyAvg();
178+
this.createCbIntervalAvg(false);
179+
this.createCbIntervalAvg(true);
181180
LOG.info(this.serviceUtils.createAvgLogStatement(start, "Prepared Coinbase Data Time:"));
182181
} else {
183182
// This can only be used on machines without cpu constraints.
184183
CompletableFuture<String> future7 = CompletableFuture.supplyAsync(() -> {
185-
this.createCbHourlyAvg();
184+
this.createCbIntervalAvg(false);
186185
return "createCbHourlyAvg() Done.";
187186
}, CompletableFuture.delayedExecutor(10, TimeUnit.SECONDS));
188187
CompletableFuture<String> future8 = CompletableFuture.supplyAsync(() -> {
189-
this.createCbDailyAvg();
188+
this.createCbIntervalAvg(true);
190189
return "createCbDailyAvg() Done.";
191190
}, CompletableFuture.delayedExecutor(10, TimeUnit.SECONDS));
192191
String combined = Stream.of(future7, future8).map(CompletableFuture::join).collect(Collectors.joining(" "));
@@ -195,25 +194,6 @@ private String createHourDayAvg() {
195194
return "done.";
196195
}
197196

198-
private void createCbHourlyAvg() {
199-
LOG.info("createCbHourlyAvg()");
200-
LocalDateTime startAll = LocalDateTime.now();
201-
MyTimeFrame timeFrame = this.serviceUtils.createTimeFrame(CB_HOUR_COL, QuoteCb.class, true);
202-
Calendar now = Calendar.getInstance();
203-
now.setTime(Date.from(LocalDate.now().atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()));
204-
final var timeFrames = this.createTimeFrames(timeFrame, now);
205-
if (this.cpuConstraint) {
206-
timeFrames.stream().forEachOrdered(timeFrame1 -> this.processTimeFrame(timeFrame1, false));
207-
} else {
208-
try (ForkJoinPool customThreadPool = new ForkJoinPool(2)) {
209-
customThreadPool.submit(() -> timeFrames.parallelStream()
210-
.forEachOrdered(timeFrame1 -> this.processTimeFrame(timeFrame1, false)));
211-
customThreadPool.shutdown();
212-
}
213-
}
214-
LOG.info(this.serviceUtils.createAvgLogStatement(startAll, "Prepared Coinbase Hourly Data Time:"));
215-
}
216-
217197
private void processTimeFrame(MyTimeFrame timeFrame1, boolean isDay) {
218198
Date start = new Date();
219199
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
@@ -238,23 +218,15 @@ private void processTimeFrame(MyTimeFrame timeFrame1, boolean isDay) {
238218
+ (new Date().getTime() - start.getTime()) + "ms" + " 0 < properties: " + nonZeroProperties.get());
239219
}
240220

241-
private void createCbDailyAvg() {
242-
LOG.info("createCbDailyAvg()");
221+
private void createCbIntervalAvg(boolean isDay) {
222+
LOG.info(isDay ? "createCbDailyAvg()" : "createCbHourlyAvg()");
243223
LocalDateTime startAll = LocalDateTime.now();
244-
final MyTimeFrame timeFrame = this.serviceUtils.createTimeFrame(CB_DAY_COL, QuoteCb.class, false);
224+
final MyTimeFrame timeFrame = this.serviceUtils.createTimeFrame(isDay ? CB_DAY_COL : CB_HOUR_COL, QuoteCb.class, false);
245225
final Calendar now = Calendar.getInstance();
246226
now.setTime(Date.from(LocalDate.now().atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()));
247-
final var timeFrames = this.createTimeFrames(timeFrame, now);
248-
if (this.cpuConstraint) {
249-
timeFrames.stream().forEachOrdered(timeFrame1 -> this.processTimeFrame(timeFrame1, true));
250-
} else {
251-
try (ForkJoinPool customThreadPool = new ForkJoinPool(2)) {
252-
customThreadPool.submit(() -> timeFrames.parallelStream()
253-
.forEachOrdered(timeFrame1 -> this.processTimeFrame(timeFrame1, true)));
254-
customThreadPool.shutdown();
255-
}
256-
}
257-
LOG.info(this.serviceUtils.createAvgLogStatement(startAll, "Prepared Coinbase Daily Data Time:"));
227+
this.createTimeFrames(timeFrame, now).stream().forEachOrdered(timeFrame1 -> this.processTimeFrame(timeFrame1, isDay));
228+
var logStmt = String.format("Prepared Coinbase %s Data Time:", isDay ? "Daily" : "Hourly");
229+
LOG.info(this.serviceUtils.createAvgLogStatement(startAll, logStmt));
258230
}
259231

260232
private List<MyTimeFrame> createTimeFrames(final MyTimeFrame timeFrame, final Calendar now) {

0 commit comments

Comments
 (0)