1 | package org.cardanofoundation.explorer.api.service.impl; | |
2 | ||
3 | import java.nio.charset.StandardCharsets; | |
4 | import java.util.*; | |
5 | ||
6 | import lombok.RequiredArgsConstructor; | |
7 | ||
8 | import org.springframework.beans.factory.annotation.Value; | |
9 | import org.springframework.context.annotation.Profile; | |
10 | import org.springframework.stereotype.Service; | |
11 | import org.springframework.web.reactive.function.client.WebClient; | |
12 | ||
13 | import reactor.core.publisher.Mono; | |
14 | ||
15 | import org.cardanofoundation.explorer.api.exception.BusinessCode; | |
16 | import org.cardanofoundation.explorer.api.repository.ledgersync.AdaPotsRepository; | |
17 | import org.cardanofoundation.explorer.api.repository.ledgersync.EpochStakeCheckpointRepository; | |
18 | import org.cardanofoundation.explorer.api.repository.ledgersync.PoolHistoryCheckpointRepository; | |
19 | import org.cardanofoundation.explorer.api.repository.ledgersync.RewardCheckpointRepository; | |
20 | import org.cardanofoundation.explorer.api.service.FetchRewardDataService; | |
21 | import org.cardanofoundation.explorer.common.entity.enumeration.EraType; | |
22 | import org.cardanofoundation.explorer.common.entity.ledgersync.Epoch; | |
23 | import org.cardanofoundation.explorer.common.exception.BusinessException; | |
24 | ||
25 | @Profile("koios") | |
26 | @Service | |
27 | @RequiredArgsConstructor | |
28 | public class FetchRewardDataFromKoiosServiceImpl implements FetchRewardDataService { | |
29 | ||
30 | @Value("${application.api.check-reward.base-url}") | |
31 | private String apiCheckRewardUrl; | |
32 | ||
33 | @Value("${application.api.check-pool-history.base-url}") | |
34 | private String apiCheckPoolHistoryUrl; | |
35 | ||
36 | @Value("${application.api.check-epoch-stake.base-url}") | |
37 | private String apiCheckEpochStakeUrl; | |
38 | ||
39 | @Value("${application.api.check-ada-pots.base-url}") | |
40 | private String apiCheckAdaPotsUrl; | |
41 | ||
42 | @Value("${application.api.check-epoch.base-url}") | |
43 | private String apiCheckEpochUrl; | |
44 | ||
45 | private final RewardCheckpointRepository rewardCheckpointRepository; | |
46 | ||
47 | private final PoolHistoryCheckpointRepository poolHistoryCheckpointRepository; | |
48 | ||
49 | private final EpochStakeCheckpointRepository epochStakeCheckpointRepository; | |
50 | ||
51 | private final AdaPotsRepository adaPotsRepository; | |
52 | ||
53 | private final WebClient webClient; | |
54 | ||
55 | @Override | |
56 | public boolean checkRewardAvailable(String stakeKey) { | |
57 |
2
1. checkRewardAvailable : replaced boolean return with false for org/cardanofoundation/explorer/api/service/impl/FetchRewardDataFromKoiosServiceImpl::checkRewardAvailable → SURVIVED 2. checkRewardAvailable : replaced boolean return with true for org/cardanofoundation/explorer/api/service/impl/FetchRewardDataFromKoiosServiceImpl::checkRewardAvailable → KILLED |
return rewardCheckpointRepository.checkRewardByStakeAddressAndEpoch(stakeKey); |
58 | } | |
59 | ||
60 | @Override | |
61 | public Boolean fetchReward(String stakeKey) { | |
62 |
2
1. fetchReward : replaced Boolean return with True for org/cardanofoundation/explorer/api/service/impl/FetchRewardDataFromKoiosServiceImpl::fetchReward → SURVIVED 2. fetchReward : replaced Boolean return with False for org/cardanofoundation/explorer/api/service/impl/FetchRewardDataFromKoiosServiceImpl::fetchReward → KILLED |
return postWebClient(apiCheckRewardUrl, Boolean.class, Collections.singleton(stakeKey)).block(); |
63 | } | |
64 | ||
65 | @Override | |
66 | public Boolean checkRewardAvailable(List<String> stakeAddressList) { | |
67 | Integer countCheckPoint = | |
68 | rewardCheckpointRepository.checkRewardByRewardAccountsAndEpoch(stakeAddressList); | |
69 | Integer sizeCheck = stakeAddressList.size(); | |
70 |
2
1. checkRewardAvailable : replaced Boolean return with True for org/cardanofoundation/explorer/api/service/impl/FetchRewardDataFromKoiosServiceImpl::checkRewardAvailable → SURVIVED 2. checkRewardAvailable : replaced Boolean return with False for org/cardanofoundation/explorer/api/service/impl/FetchRewardDataFromKoiosServiceImpl::checkRewardAvailable → KILLED |
return Objects.equals(countCheckPoint, sizeCheck); |
71 | } | |
72 | ||
73 | @Override | |
74 | public Boolean fetchReward(List<String> stakeAddressList) { | |
75 |
2
1. fetchReward : replaced Boolean return with True for org/cardanofoundation/explorer/api/service/impl/FetchRewardDataFromKoiosServiceImpl::fetchReward → SURVIVED 2. fetchReward : replaced Boolean return with False for org/cardanofoundation/explorer/api/service/impl/FetchRewardDataFromKoiosServiceImpl::fetchReward → KILLED |
return postWebClient(apiCheckRewardUrl, Boolean.class, stakeAddressList).block(); |
76 | } | |
77 | ||
78 | @Override | |
79 | public Boolean checkPoolHistoryForPool(Set<String> poolIds) { | |
80 | Integer countCheckPoint = | |
81 | poolHistoryCheckpointRepository.checkRewardByPoolViewAndEpoch(poolIds); | |
82 | Integer sizeCheck = poolIds.size(); | |
83 |
2
1. checkPoolHistoryForPool : replaced Boolean return with True for org/cardanofoundation/explorer/api/service/impl/FetchRewardDataFromKoiosServiceImpl::checkPoolHistoryForPool → SURVIVED 2. checkPoolHistoryForPool : replaced Boolean return with False for org/cardanofoundation/explorer/api/service/impl/FetchRewardDataFromKoiosServiceImpl::checkPoolHistoryForPool → KILLED |
return Objects.equals(countCheckPoint, sizeCheck); |
84 | } | |
85 | ||
86 | @Override | |
87 | public Boolean fetchPoolHistoryForPool(Set<String> poolIds) { | |
88 |
2
1. fetchPoolHistoryForPool : replaced Boolean return with True for org/cardanofoundation/explorer/api/service/impl/FetchRewardDataFromKoiosServiceImpl::fetchPoolHistoryForPool → SURVIVED 2. fetchPoolHistoryForPool : replaced Boolean return with False for org/cardanofoundation/explorer/api/service/impl/FetchRewardDataFromKoiosServiceImpl::fetchPoolHistoryForPool → KILLED |
return postWebClient(apiCheckPoolHistoryUrl, Boolean.class, poolIds).block(); |
89 | } | |
90 | ||
91 | @Override | |
92 | public Boolean checkEpochStakeForPool(List<String> rewardAccounts) { | |
93 | Integer countCheckPoint = | |
94 | epochStakeCheckpointRepository.checkEpochStakeByAccountsAndEpoch(rewardAccounts); | |
95 | Integer sizeCheck = rewardAccounts.size(); | |
96 |
2
1. checkEpochStakeForPool : replaced Boolean return with True for org/cardanofoundation/explorer/api/service/impl/FetchRewardDataFromKoiosServiceImpl::checkEpochStakeForPool → SURVIVED 2. checkEpochStakeForPool : replaced Boolean return with False for org/cardanofoundation/explorer/api/service/impl/FetchRewardDataFromKoiosServiceImpl::checkEpochStakeForPool → KILLED |
return Objects.equals(countCheckPoint, sizeCheck); |
97 | } | |
98 | ||
99 | @Override | |
100 | public Boolean fetchEpochStakeForPool(List<String> rewardAccounts) { | |
101 |
2
1. fetchEpochStakeForPool : replaced Boolean return with True for org/cardanofoundation/explorer/api/service/impl/FetchRewardDataFromKoiosServiceImpl::fetchEpochStakeForPool → SURVIVED 2. fetchEpochStakeForPool : replaced Boolean return with False for org/cardanofoundation/explorer/api/service/impl/FetchRewardDataFromKoiosServiceImpl::fetchEpochStakeForPool → KILLED |
return postWebClient(apiCheckEpochStakeUrl, Boolean.class, rewardAccounts).block(); |
102 | } | |
103 | ||
104 | @Override | |
105 | public Boolean checkRewardForPool(List<String> rewardAccounts) { | |
106 | Integer countCheckPoint = | |
107 | rewardCheckpointRepository.checkRewardByRewardAccountsAndEpoch(rewardAccounts); | |
108 | Integer sizeCheck = rewardAccounts.size(); | |
109 |
2
1. checkRewardForPool : replaced Boolean return with True for org/cardanofoundation/explorer/api/service/impl/FetchRewardDataFromKoiosServiceImpl::checkRewardForPool → SURVIVED 2. checkRewardForPool : replaced Boolean return with False for org/cardanofoundation/explorer/api/service/impl/FetchRewardDataFromKoiosServiceImpl::checkRewardForPool → KILLED |
return Objects.equals(countCheckPoint, sizeCheck); |
110 | } | |
111 | ||
112 | @Override | |
113 | public Boolean fetchRewardForPool(List<String> rewardAccounts) { | |
114 |
2
1. fetchRewardForPool : replaced Boolean return with True for org/cardanofoundation/explorer/api/service/impl/FetchRewardDataFromKoiosServiceImpl::fetchRewardForPool → SURVIVED 2. fetchRewardForPool : replaced Boolean return with False for org/cardanofoundation/explorer/api/service/impl/FetchRewardDataFromKoiosServiceImpl::fetchRewardForPool → KILLED |
return postWebClient(apiCheckRewardUrl, Boolean.class, rewardAccounts).block(); |
115 | } | |
116 | ||
117 | @Override | |
118 | public Boolean checkAdaPots(Integer epochNo) { | |
119 |
2
1. checkAdaPots : replaced Boolean return with True for org/cardanofoundation/explorer/api/service/impl/FetchRewardDataFromKoiosServiceImpl::checkAdaPots → SURVIVED 2. checkAdaPots : replaced Boolean return with False for org/cardanofoundation/explorer/api/service/impl/FetchRewardDataFromKoiosServiceImpl::checkAdaPots → KILLED |
return adaPotsRepository.existsByEpochNo(epochNo); |
120 | } | |
121 | ||
122 | @Override | |
123 | public Boolean fetchAdaPots(List<Integer> epochNo) { | |
124 |
2
1. fetchAdaPots : replaced Boolean return with True for org/cardanofoundation/explorer/api/service/impl/FetchRewardDataFromKoiosServiceImpl::fetchAdaPots → SURVIVED 2. fetchAdaPots : replaced Boolean return with False for org/cardanofoundation/explorer/api/service/impl/FetchRewardDataFromKoiosServiceImpl::fetchAdaPots → KILLED |
return postWebClient(apiCheckAdaPotsUrl, Boolean.class, epochNo).block(); |
125 | } | |
126 | ||
127 | @Override | |
128 | public Boolean checkEpochRewardDistributed(Epoch epoch) { | |
129 |
2
1. checkEpochRewardDistributed : negated conditional → NO_COVERAGE 2. checkEpochRewardDistributed : replaced Boolean return with True for org/cardanofoundation/explorer/api/service/impl/FetchRewardDataFromKoiosServiceImpl::checkEpochRewardDistributed → NO_COVERAGE |
return Objects.nonNull(epoch.getRewardsDistributed()) |
130 |
1
1. checkEpochRewardDistributed : negated conditional → NO_COVERAGE |
|| epoch.getEra().equals(EraType.BYRON) |
131 |
1
1. checkEpochRewardDistributed : negated conditional → NO_COVERAGE |
|| epoch.getEra().equals(EraType.BYRON_EBB); |
132 | } | |
133 | ||
134 | @Override | |
135 | public List<Epoch> fetchEpochRewardDistributed(List<Integer> epochNoList) { | |
136 | try { | |
137 | Epoch[] epoch = postWebClient(apiCheckEpochUrl, Epoch[].class, epochNoList).block(); | |
138 |
1
1. fetchEpochRewardDistributed : negated conditional → KILLED |
if (Objects.nonNull(epoch)) { |
139 |
1
1. fetchEpochRewardDistributed : replaced return value with Collections.emptyList for org/cardanofoundation/explorer/api/service/impl/FetchRewardDataFromKoiosServiceImpl::fetchEpochRewardDistributed → KILLED |
return Arrays.asList(epoch); |
140 | } | |
141 | return Collections.emptyList(); | |
142 | } catch (Exception e) { | |
143 |
1
1. fetchEpochRewardDistributed : replaced return value with Collections.emptyList for org/cardanofoundation/explorer/api/service/impl/FetchRewardDataFromKoiosServiceImpl::fetchEpochRewardDistributed → NO_COVERAGE |
return null; |
144 | } | |
145 | } | |
146 | ||
147 | @Override | |
148 | public Boolean useKoios() { | |
149 |
1
1. useKoios : replaced Boolean return with False for org/cardanofoundation/explorer/api/service/impl/FetchRewardDataFromKoiosServiceImpl::useKoios → KILLED |
return true; |
150 | } | |
151 | ||
152 | public <T> Mono<T> postWebClient(String url, Class<T> clazz, Object body) { | |
153 |
1
1. postWebClient : replaced return value with null for org/cardanofoundation/explorer/api/service/impl/FetchRewardDataFromKoiosServiceImpl::postWebClient → KILLED |
return webClient |
154 | .post() | |
155 | .uri(url) | |
156 | .bodyValue(body) | |
157 | .acceptCharset(StandardCharsets.UTF_8) | |
158 | .retrieve() | |
159 | .onStatus( | |
160 |
3
1. lambda$postWebClient$0 : negated conditional → KILLED 2. lambda$postWebClient$0 : replaced boolean return with true for org/cardanofoundation/explorer/api/service/impl/FetchRewardDataFromKoiosServiceImpl::lambda$postWebClient$0 → KILLED 3. lambda$postWebClient$0 : negated conditional → KILLED |
status -> status.is4xxClientError() || status.is5xxServerError(), |
161 |
1
1. lambda$postWebClient$1 : replaced return value with null for org/cardanofoundation/explorer/api/service/impl/FetchRewardDataFromKoiosServiceImpl::lambda$postWebClient$1 → NO_COVERAGE |
clientResponse -> Mono.error(new BusinessException(BusinessCode.FETCH_REWARD_ERROR))) |
162 | .bodyToMono(clazz); | |
163 | } | |
164 | } | |
Mutations | ||
57 |
1.1 2.2 |
|
62 |
1.1 2.2 |
|
70 |
1.1 2.2 |
|
75 |
1.1 2.2 |
|
83 |
1.1 2.2 |
|
88 |
1.1 2.2 |
|
96 |
1.1 2.2 |
|
101 |
1.1 2.2 |
|
109 |
1.1 2.2 |
|
114 |
1.1 2.2 |
|
119 |
1.1 2.2 |
|
124 |
1.1 2.2 |
|
129 |
1.1 2.2 |
|
130 |
1.1 |
|
131 |
1.1 |
|
138 |
1.1 |
|
139 |
1.1 |
|
143 |
1.1 |
|
149 |
1.1 |
|
153 |
1.1 |
|
160 |
1.1 2.2 3.3 |
|
161 |
1.1 |