1 | package org.cardanofoundation.explorer.api.service.impl; | |
2 | ||
3 | import static org.cardanofoundation.explorer.api.common.constant.CommonConstant.DATA_IS_NOT_SYNCING; | |
4 | import static org.cardanofoundation.explorer.api.common.constant.CommonConstant.READY_TO_SERVE; | |
5 | import static org.cardanofoundation.explorer.api.common.constant.CommonConstant.SYNCING_BUT_NOT_READY; | |
6 | ||
7 | import java.time.LocalDateTime; | |
8 | import java.time.ZoneOffset; | |
9 | import java.time.temporal.ChronoUnit; | |
10 | import java.util.Objects; | |
11 | import java.util.Optional; | |
12 | ||
13 | import lombok.RequiredArgsConstructor; | |
14 | ||
15 | import org.springframework.beans.factory.annotation.Value; | |
16 | import org.springframework.stereotype.Service; | |
17 | ||
18 | import org.cardanofoundation.explorer.api.model.response.healthcheck.SyncStatus; | |
19 | import org.cardanofoundation.explorer.api.repository.ledgersync.BlockRepository; | |
20 | import org.cardanofoundation.explorer.api.service.HealthCheckService; | |
21 | import org.cardanofoundation.explorer.api.service.cache.AggregatedDataCacheService; | |
22 | import org.cardanofoundation.explorer.common.entity.ledgersync.Block; | |
23 | ||
24 | @Service | |
25 | @RequiredArgsConstructor | |
26 | public class HealthCheckServiceImpl implements HealthCheckService { | |
27 | ||
28 | private final AggregatedDataCacheService aggregatedDataCacheService; | |
29 | private final BlockRepository blockRepository; | |
30 | ||
31 | @Value("${application.healthcheck.block-time-threshold}") | |
32 | private Long blockTimeThresholdInSecond; | |
33 | ||
34 | @Value("${application.healthcheck.inserted-time-threshold}") | |
35 | private Long insertedTimeThresholdInSecond; | |
36 | ||
37 | @Override | |
38 | public SyncStatus getSyncStatus() { | |
39 | SyncStatus syncStatus = new SyncStatus(); | |
40 | ||
41 | LocalDateTime latestBlockTime = aggregatedDataCacheService.getLatestBlockTime(); | |
42 | LocalDateTime latestBlockInsertTime = aggregatedDataCacheService.getLatestBlockInsertTime(); | |
43 | ||
44 |
1
1. getSyncStatus : negated conditional → KILLED |
if (Objects.isNull(latestBlockInsertTime)) { |
45 |
1
1. getSyncStatus : removed call to org/cardanofoundation/explorer/api/model/response/healthcheck/SyncStatus::setMessage → NO_COVERAGE |
syncStatus.setMessage(DATA_IS_NOT_SYNCING); |
46 |
1
1. getSyncStatus : removed call to org/cardanofoundation/explorer/api/model/response/healthcheck/SyncStatus::setIsSyncing → NO_COVERAGE |
syncStatus.setIsSyncing(Boolean.FALSE); |
47 |
1
1. getSyncStatus : replaced return value with null for org/cardanofoundation/explorer/api/service/impl/HealthCheckServiceImpl::getSyncStatus → NO_COVERAGE |
return syncStatus; |
48 | } | |
49 | ||
50 |
1
1. getSyncStatus : negated conditional → KILLED |
if (Objects.isNull(latestBlockTime)) { |
51 | Optional<Block> latestBlock = blockRepository.findLatestBlock(); | |
52 |
1
1. getSyncStatus : negated conditional → KILLED |
if (latestBlock.isEmpty()) { |
53 |
1
1. getSyncStatus : removed call to org/cardanofoundation/explorer/api/model/response/healthcheck/SyncStatus::setMessage → NO_COVERAGE |
syncStatus.setMessage(DATA_IS_NOT_SYNCING); |
54 |
1
1. getSyncStatus : removed call to org/cardanofoundation/explorer/api/model/response/healthcheck/SyncStatus::setIsSyncing → NO_COVERAGE |
syncStatus.setIsSyncing(Boolean.FALSE); |
55 |
1
1. getSyncStatus : replaced return value with null for org/cardanofoundation/explorer/api/service/impl/HealthCheckServiceImpl::getSyncStatus → NO_COVERAGE |
return syncStatus; |
56 | } | |
57 | latestBlockTime = latestBlock.get().getTime().toLocalDateTime(); | |
58 | } | |
59 | boolean isSyncing; | |
60 | String message; | |
61 | ||
62 |
1
1. getSyncStatus : negated conditional → KILLED |
if (isOutOfThreshold(insertedTimeThresholdInSecond, latestBlockInsertTime)) { |
63 | isSyncing = false; | |
64 | message = DATA_IS_NOT_SYNCING; | |
65 | } else { | |
66 | isSyncing = true; | |
67 | message = SYNCING_BUT_NOT_READY; | |
68 | } | |
69 | ||
70 |
1
1. getSyncStatus : negated conditional → KILLED |
if (isSyncing) { |
71 |
1
1. getSyncStatus : negated conditional → KILLED |
if (isOutOfThreshold(blockTimeThresholdInSecond, latestBlockTime)) { |
72 | message = SYNCING_BUT_NOT_READY; | |
73 | } else { | |
74 | message = READY_TO_SERVE; | |
75 | } | |
76 | } | |
77 | ||
78 |
1
1. getSyncStatus : replaced return value with null for org/cardanofoundation/explorer/api/service/impl/HealthCheckServiceImpl::getSyncStatus → KILLED |
return SyncStatus.builder() |
79 | .isSyncing(isSyncing) | |
80 | .message(message) | |
81 | .latestBlockInsertTime(latestBlockInsertTime) | |
82 | .build(); | |
83 | } | |
84 | ||
85 | public boolean isOutOfThreshold(Long threshold, LocalDateTime time) { | |
86 | long value = ChronoUnit.SECONDS.between(time, LocalDateTime.now(ZoneOffset.UTC)); | |
87 |
3
1. isOutOfThreshold : changed conditional boundary → SURVIVED 2. isOutOfThreshold : negated conditional → KILLED 3. isOutOfThreshold : replaced boolean return with true for org/cardanofoundation/explorer/api/service/impl/HealthCheckServiceImpl::isOutOfThreshold → KILLED |
return threshold <= value; |
88 | } | |
89 | } | |
Mutations | ||
44 |
1.1 |
|
45 |
1.1 |
|
46 |
1.1 |
|
47 |
1.1 |
|
50 |
1.1 |
|
52 |
1.1 |
|
53 |
1.1 |
|
54 |
1.1 |
|
55 |
1.1 |
|
62 |
1.1 |
|
70 |
1.1 |
|
71 |
1.1 |
|
78 |
1.1 |
|
87 |
1.1 2.2 3.3 |