1 | package org.cardanofoundation.explorer.api.service.impl; | |
2 | ||
3 | import java.sql.Timestamp; | |
4 | import java.time.Duration; | |
5 | import java.time.Instant; | |
6 | import java.time.LocalDateTime; | |
7 | ||
8 | import lombok.RequiredArgsConstructor; | |
9 | import lombok.extern.log4j.Log4j2; | |
10 | ||
11 | import org.springframework.data.domain.Page; | |
12 | import org.springframework.data.domain.Pageable; | |
13 | import org.springframework.stereotype.Service; | |
14 | ||
15 | import org.cardanofoundation.explorer.api.common.constant.CommonConstant; | |
16 | import org.cardanofoundation.explorer.api.interceptor.auth.UserPrincipal; | |
17 | import org.cardanofoundation.explorer.api.model.request.stake.report.ReportHistoryFilterRequest; | |
18 | import org.cardanofoundation.explorer.api.model.response.BaseFilterResponse; | |
19 | import org.cardanofoundation.explorer.api.model.response.ReportLimitResponse; | |
20 | import org.cardanofoundation.explorer.api.model.response.stake.report.ReportHistoryResponse; | |
21 | import org.cardanofoundation.explorer.api.repository.explorer.ReportHistoryRepository; | |
22 | import org.cardanofoundation.explorer.api.service.ReportHistoryService; | |
23 | import org.cardanofoundation.explorer.api.service.RoleService; | |
24 | import org.cardanofoundation.explorer.api.util.DataUtil; | |
25 | import org.cardanofoundation.explorer.common.entity.enumeration.ReportStatus; | |
26 | ||
27 | @Service | |
28 | @RequiredArgsConstructor | |
29 | @Log4j2 | |
30 | public class ReportHistoryServiceImpl implements ReportHistoryService { | |
31 | ||
32 | public static final String MIN_TIME = "1970-01-01 00:00:00"; | |
33 | ||
34 | private final ReportHistoryRepository reportHistoryRepository; | |
35 | ||
36 | private final RoleService roleService; | |
37 | ||
38 | /** | |
39 | * Get report history | |
40 | * | |
41 | * @param filterRequest | |
42 | * @param username | |
43 | * @param pageable pageable | |
44 | * @return BaseFilterResponse<StakeKeyReportHistoryResponse> | |
45 | */ | |
46 | @Override | |
47 | public BaseFilterResponse<ReportHistoryResponse> getReportHistory( | |
48 | ReportHistoryFilterRequest filterRequest, String username, Pageable pageable) { | |
49 | String reportName = DataUtil.makeLikeQuery(filterRequest.getReportName()); | |
50 | Timestamp fromDate = Timestamp.valueOf(MIN_TIME); | |
51 | Timestamp toDate = Timestamp.from(Instant.now()); | |
52 |
1
1. getReportHistory : negated conditional → SURVIVED |
if (!DataUtil.isNullOrEmpty(filterRequest.getFromDate())) { |
53 | fromDate = Timestamp.from(filterRequest.getFromDate().toInstant()); | |
54 | } | |
55 |
1
1. getReportHistory : negated conditional → SURVIVED |
if (!DataUtil.isNullOrEmpty(filterRequest.getToDate())) { |
56 | toDate = Timestamp.from(filterRequest.getToDate().toInstant()); | |
57 | } | |
58 | ||
59 | LocalDateTime timeAt7DayAgo = LocalDateTime.now().minus(Duration.ofDays(7)); | |
60 | ||
61 | Page<ReportHistoryResponse> reportHistoryProjections = | |
62 | reportHistoryRepository | |
63 | .getRecordHistoryByFilter(reportName, fromDate, toDate, username, pageable) | |
64 | .map( | |
65 | reportHistoryProjection -> { | |
66 | ReportHistoryResponse response = | |
67 | ReportHistoryResponse.builder() | |
68 | .stakeKeyReportId(reportHistoryProjection.getStakeKeyReportId()) | |
69 | .poolReportId(reportHistoryProjection.getPoolReportId()) | |
70 | .reportName(reportHistoryProjection.getReportName()) | |
71 | .status(reportHistoryProjection.getStatus()) | |
72 | .type(reportHistoryProjection.getType()) | |
73 | .createdAt(reportHistoryProjection.getCreatedAt()) | |
74 | .build(); | |
75 |
1
1. lambda$getReportHistory$0 : negated conditional → KILLED |
if (response.getCreatedAt().isBefore(timeAt7DayAgo)) { |
76 |
1
1. lambda$getReportHistory$0 : removed call to org/cardanofoundation/explorer/api/model/response/stake/report/ReportHistoryResponse::setStatus → NO_COVERAGE |
response.setStatus(ReportStatus.EXPIRED); |
77 | } | |
78 |
1
1. lambda$getReportHistory$0 : replaced return value with null for org/cardanofoundation/explorer/api/service/impl/ReportHistoryServiceImpl::lambda$getReportHistory$0 → KILLED |
return response; |
79 | }); | |
80 | ||
81 |
1
1. getReportHistory : replaced return value with null for org/cardanofoundation/explorer/api/service/impl/ReportHistoryServiceImpl::getReportHistory → KILLED |
return new BaseFilterResponse<>(reportHistoryProjections); |
82 | } | |
83 | ||
84 | @Override | |
85 | public Boolean isLimitReached(String username, int limit) { | |
86 |
2
1. isLimitReached : replaced Boolean return with True for org/cardanofoundation/explorer/api/service/impl/ReportHistoryServiceImpl::isLimitReached → NO_COVERAGE 2. isLimitReached : negated conditional → KILLED |
if (limit == CommonConstant.UNLIMITED_REPORT) return false; |
87 | Instant currentTime = Instant.now(); | |
88 | Integer reportCount = | |
89 | reportHistoryRepository.countByUsernameAndCreatedAtBetween( | |
90 | username, | |
91 | Timestamp.from(currentTime.minus(Duration.ofDays(1))), | |
92 | Timestamp.from(currentTime)); | |
93 |
3
1. isLimitReached : changed conditional boundary → KILLED 2. isLimitReached : replaced Boolean return with True for org/cardanofoundation/explorer/api/service/impl/ReportHistoryServiceImpl::isLimitReached → KILLED 3. isLimitReached : negated conditional → KILLED |
return reportCount >= limit; |
94 | } | |
95 | ||
96 | @Override | |
97 | public ReportLimitResponse getReportLimit(UserPrincipal userPrincipal) { | |
98 | int limit = roleService.getReportLimit(userPrincipal.getRoleDescription()); | |
99 |
1
1. getReportLimit : replaced return value with null for org/cardanofoundation/explorer/api/service/impl/ReportHistoryServiceImpl::getReportLimit → KILLED |
return ReportLimitResponse.builder() |
100 | .limitPer24hours(limit) | |
101 | .isLimitReached(isLimitReached(userPrincipal.getUsername(), limit)) | |
102 | .build(); | |
103 | } | |
104 | } | |
Mutations | ||
52 |
1.1 |
|
55 |
1.1 |
|
75 |
1.1 |
|
76 |
1.1 |
|
78 |
1.1 |
|
81 |
1.1 |
|
86 |
1.1 2.2 |
|
93 |
1.1 2.2 3.3 |
|
99 |
1.1 |