Skip to content

Commit a696ba2

Browse files
committed
feat: add logging
1 parent 0609992 commit a696ba2

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

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

+33-6
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.util.concurrent.CompletableFuture;
3838
import java.util.concurrent.ConcurrentHashMap;
3939
import java.util.concurrent.TimeUnit;
40+
import java.util.concurrent.atomic.AtomicInteger;
4041
import java.util.function.BiConsumer;
4142
import java.util.function.Function;
4243
import java.util.function.Predicate;
@@ -200,6 +201,7 @@ private void createCbHourlyAvg() {
200201
now.setTime(Date.from(LocalDate.now().atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()));
201202
while (timeFrame.end().before(now)) {
202203
Date start = new Date();
204+
final var nonZeroProperties = new AtomicInteger(0);
203205
Query query = new Query();
204206
query.addCriteria(
205207
Criteria.where(DtoUtils.CREATEDAT).gt(timeFrame.begin().getTime()).lt(timeFrame.end().getTime()));
@@ -209,6 +211,7 @@ private void createCbHourlyAvg() {
209211
.onErrorResume(ex -> Mono.empty()).subscribeOn(this.mongoScheduler).collectList()
210212
.map(quotes -> makeCbQuoteHour(quotes, timeFrame.begin(), timeFrame.end()));
211213
collectCb.filter(Predicate.not(Collection::isEmpty))
214+
.map(myColl -> countRelevantProperties(nonZeroProperties, myColl))
212215
.flatMap(myColl -> this.myMongoRepository.insertAll(Mono.just(myColl), CB_HOUR_COL)
213216
.timeout(Duration.ofSeconds(5L))
214217
.doOnError(ex -> LOG.warn("Coinbase prepare hour data failed", ex))
@@ -218,8 +221,8 @@ private void createCbHourlyAvg() {
218221
timeFrame.begin().add(Calendar.DAY_OF_YEAR, 1);
219222
timeFrame.end().add(Calendar.DAY_OF_YEAR, 1);
220223
LOG.info("Prepared Coinbase Hour Data for: " + sdf.format(timeFrame.begin().getTime()) + " Time: "
221-
+ (new Date().getTime() - start.getTime()) + "ms" + " properties: "
222-
+ (cbFunctionCache.size() / 2));
224+
+ (new Date().getTime() - start.getTime()) + "ms" + " 0 < properties: "
225+
+ nonZeroProperties.get());
223226
}
224227
LOG.info(this.serviceUtils.createAvgLogStatement(startAll, "Prepared Coinbase Hourly Data Time:"));
225228
}
@@ -233,6 +236,7 @@ private void createCbDailyAvg() {
233236
now.setTime(Date.from(LocalDate.now().atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()));
234237
while (timeFrame.end().before(now)) {
235238
Date start = new Date();
239+
final var nonZeroProperties = new AtomicInteger(0);
236240
Query query = new Query();
237241
query.addCriteria(
238242
Criteria.where(DtoUtils.CREATEDAT).gt(timeFrame.begin().getTime()).lt(timeFrame.end().getTime()));
@@ -242,6 +246,7 @@ private void createCbDailyAvg() {
242246
.onErrorResume(ex -> Mono.empty()).subscribeOn(this.mongoScheduler).collectList()
243247
.map(quotes -> makeCbQuoteDay(quotes, timeFrame.begin(), timeFrame.end()));
244248
collectCb.filter(Predicate.not(Collection::isEmpty))
249+
.map(myColl -> countRelevantProperties(nonZeroProperties, myColl))
245250
.flatMap(myColl -> this.myMongoRepository.insertAll(Mono.just(myColl), CB_DAY_COL)
246251
.timeout(Duration.ofSeconds(5L))
247252
.doOnError(ex -> LOG.warn("Coinbase prepare day data failed", ex))
@@ -251,12 +256,21 @@ private void createCbDailyAvg() {
251256
timeFrame.begin().add(Calendar.DAY_OF_YEAR, 1);
252257
timeFrame.end().add(Calendar.DAY_OF_YEAR, 1);
253258
LOG.info("Prepared Coinbase Day Data for: " + sdf.format(timeFrame.begin().getTime()) + " Time: "
254-
+ (new Date().getTime() - start.getTime()) + "ms" + " properties: "
255-
+ (cbFunctionCache.size() / 2));
259+
+ (new Date().getTime() - start.getTime()) + "ms" + " 0 < properties: "
260+
+ nonZeroProperties.get());
256261
}
257262
LOG.info(this.serviceUtils.createAvgLogStatement(startAll, "Prepared Coinbase Daily Data Time:"));
258263
}
259264

265+
private Collection<QuoteCb> countRelevantProperties(final AtomicInteger nonZeroProperties,
266+
Collection<QuoteCb> myColl) {
267+
var relevantProperties = myColl.stream().flatMap(myQuote -> Stream.of(this.propertiesNonZero(myQuote)))
268+
.mapToInt(v -> v).max().orElse(0);
269+
nonZeroProperties
270+
.set(nonZeroProperties.get() < relevantProperties ? relevantProperties : nonZeroProperties.get());
271+
return myColl;
272+
}
273+
260274
private Collection<QuoteCb> makeCbQuoteDay(List<QuoteCb> quotes, Calendar begin, Calendar end) {
261275
List<QuoteCb> hourQuotes = new LinkedList<QuoteCb>();
262276
QuoteCb quoteCb = new QuoteCb();
@@ -300,12 +314,25 @@ private QuoteCb avgCbQuotePeriod(QuoteCb q1, QuoteCb q2, long count) {
300314
return result;
301315
}
302316

317+
private Integer propertiesNonZero(QuoteCb quote) {
318+
var result = new AtomicInteger(0);
319+
this.propertyDescriptors.forEach(myPropertyDescriptor -> {
320+
try {
321+
var gsmf = this.createGetMethodFunction(myPropertyDescriptor);
322+
BigDecimal num1 = gsmf.getter.apply(quote);
323+
result.set(num1.compareTo(BigDecimal.ZERO) > 0 ? result.addAndGet(1) : result.get());
324+
} catch (Exception e) {
325+
throw new RuntimeException(e);
326+
}
327+
});
328+
return result.get();
329+
}
330+
303331
private QuoteCb avgCbQuotePeriodMF(QuoteCb q1, QuoteCb q2, long count) {
304332
QuoteCb result = new QuoteCb();
305333
this.propertyDescriptors.forEach(myPropertyDescriptor -> {
306-
GetSetMethodFunctions gsmf;
307334
try {
308-
gsmf = this.createGetMethodFunction(myPropertyDescriptor);
335+
var gsmf = this.createGetMethodFunction(myPropertyDescriptor);
309336
BigDecimal num1 = gsmf.getter.apply(q1);
310337
BigDecimal num2 = gsmf.getter.apply(q2);
311338
BigDecimal resultValue = this.serviceUtils.avgHourValue(num1, num2, count);

0 commit comments

Comments
 (0)