Skip to content

Commit

Permalink
Merge pull request #1176 from cardano-foundation/feat/MET-2039-drep-o…
Browse files Browse the repository at this point in the history
…verview-filter-and-sort

feat: obtain range values and fix date filtering in DRep filter
  • Loading branch information
Sotatek-DucPhung committed May 6, 2024
2 parents 78af6f3 + 1b54614 commit 62e7847
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.cardanofoundation.explorer.api.model.response.drep.DRepDetailsResponse;
import org.cardanofoundation.explorer.api.model.response.drep.DRepFilterResponse;
import org.cardanofoundation.explorer.api.model.response.drep.DRepOverviewResponse;
import org.cardanofoundation.explorer.api.model.response.drep.DRepRangeValuesResponse;
import org.cardanofoundation.explorer.api.model.response.drep.VotingProcedureChartResponse;
import org.cardanofoundation.explorer.api.service.DRepService;
import org.cardanofoundation.explorer.common.entity.enumeration.GovActionType;
Expand Down Expand Up @@ -126,4 +127,13 @@ public ResponseEntity<BaseFilterResponse<DRepFilterResponse>> getDRepsByFilter(
return ResponseEntity.ok(
dRepService.getDRepsByFilter(dRepFilterRequest, pagination.toPageable()));
}

@GetMapping("/range-values-for-filter")
@LogMessage
@Operation(
summary = "Get range value to filter on DRep overview page",
tags = {"dRep"})
public ResponseEntity<DRepRangeValuesResponse> getDRepRangeValues() {
return ResponseEntity.ok(dRepService.getDRepRangeValues());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import org.cardanofoundation.explorer.api.model.response.drep.DRepDelegatorsResponse;
import org.cardanofoundation.explorer.api.model.response.drep.DRepDetailsResponse;
import org.cardanofoundation.explorer.api.model.response.drep.DRepFilterResponse;
import org.cardanofoundation.explorer.api.model.response.drep.DRepRangeValuesResponse;
import org.cardanofoundation.explorer.api.projection.DRepDelegatorProjection;
import org.cardanofoundation.explorer.api.projection.DRepRangeProjection;
import org.cardanofoundation.explorer.common.entity.explorer.DRepInfo;

@Mapper(componentModel = "spring")
Expand All @@ -26,6 +28,8 @@ DRepDelegatorsResponse fromDRepDelegatorProjection(
@Mapping(target = "updatedAt", expression = "java(fromLong(dRepInfo.getUpdatedAt()))")
DRepFilterResponse fromDRepInfo(DRepInfo dRepInfo);

DRepRangeValuesResponse fromDRepRangeProjection(DRepRangeProjection dRepRangeProjection);

default Date fromLong(Long value) {
return value == null ? null : new Date(value * 1000);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package org.cardanofoundation.explorer.api.model.request.drep;

import java.math.BigInteger;
import java.util.Date;
import java.time.LocalDateTime;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.SuperBuilder;

import org.springframework.format.annotation.DateTimeFormat;

import org.cardanofoundation.explorer.common.entity.enumeration.DRepStatus;
import org.cardanofoundation.explorer.common.validation.date.DatePattern;
import org.cardanofoundation.explorer.common.validation.date.param.DateValid;

@Getter
@Setter
Expand All @@ -31,9 +31,9 @@ public class DRepFilterRequest {

private DRepStatus drepStatus;

@DateValid(pattern = DatePattern.YYYY_MM_DD)
private Date fromDate;
@DateTimeFormat(pattern = "yyyy/MM/dd HH:mm:ss")
private LocalDateTime fromDate;

@DateValid(pattern = DatePattern.YYYY_MM_DD)
private Date toDate;
@DateTimeFormat(pattern = "yyyy/MM/dd HH:mm:ss")
private LocalDateTime toDate;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.cardanofoundation.explorer.api.model.response.drep;

import java.math.BigInteger;

import lombok.Data;
import lombok.experimental.SuperBuilder;

@Data
@SuperBuilder
public class DRepRangeValuesResponse {
Double minVotingPower;
Double maxVotingPower;

BigInteger maxActiveVoteStake;

BigInteger minActiveVoteStake;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.cardanofoundation.explorer.api.projection;

import java.math.BigInteger;

public interface DRepRangeProjection {
BigInteger getMinActiveVoteStake();

BigInteger getMaxActiveVoteStake();

Double getMinVotingPower();

Double getMaxVotingPower();
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.springframework.data.repository.query.Param;

import org.cardanofoundation.explorer.api.model.response.drep.projection.DRepStatusCountProjection;
import org.cardanofoundation.explorer.api.projection.DRepRangeProjection;
import org.cardanofoundation.explorer.common.entity.enumeration.DRepStatus;
import org.cardanofoundation.explorer.common.entity.explorer.DRepInfo;

Expand Down Expand Up @@ -48,4 +49,12 @@ Page<DRepInfo> getDRepInfoByFilterRequest(
@Param("fromDate") Long fromDate,
@Param("toDate") Long toDate,
Pageable pageable);

@Query(
value =
"""
select max(dri.votingPower) as maxVotingPower , max(dri.activeVoteStake) as maxActiveVoteStake,
min(dri.votingPower) as minVotingPower , min(dri.activeVoteStake) as minActiveVoteStake from DRepInfo dri
""")
DRepRangeProjection getDRepRangeValues();
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.cardanofoundation.explorer.api.model.response.drep.DRepDetailsResponse;
import org.cardanofoundation.explorer.api.model.response.drep.DRepFilterResponse;
import org.cardanofoundation.explorer.api.model.response.drep.DRepOverviewResponse;
import org.cardanofoundation.explorer.api.model.response.drep.DRepRangeValuesResponse;
import org.cardanofoundation.explorer.api.model.response.drep.VotingProcedureChartResponse;
import org.cardanofoundation.explorer.common.entity.enumeration.GovActionType;

Expand All @@ -28,4 +29,6 @@ BaseFilterResponse<DRepDelegatorsResponse> getDRepDelegators(

BaseFilterResponse<DRepFilterResponse> getDRepsByFilter(
DRepFilterRequest dRepFilterRequest, Pageable pageable);

DRepRangeValuesResponse getDRepRangeValues();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.math.BigInteger;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Comparator;
Expand Down Expand Up @@ -35,10 +34,12 @@
import org.cardanofoundation.explorer.api.model.response.drep.DRepDetailsResponse;
import org.cardanofoundation.explorer.api.model.response.drep.DRepFilterResponse;
import org.cardanofoundation.explorer.api.model.response.drep.DRepOverviewResponse;
import org.cardanofoundation.explorer.api.model.response.drep.DRepRangeValuesResponse;
import org.cardanofoundation.explorer.api.model.response.drep.VotingProcedureChartResponse;
import org.cardanofoundation.explorer.api.model.response.drep.projection.DRepCertificateProjection;
import org.cardanofoundation.explorer.api.model.response.drep.projection.DRepStatusCountProjection;
import org.cardanofoundation.explorer.api.projection.DRepDelegatorProjection;
import org.cardanofoundation.explorer.api.projection.DRepRangeProjection;
import org.cardanofoundation.explorer.api.projection.VotingProcedureProjection;
import org.cardanofoundation.explorer.api.repository.explorer.DrepInfoRepository;
import org.cardanofoundation.explorer.api.repository.ledgersync.DRepRegistrationRepository;
Expand Down Expand Up @@ -229,18 +230,13 @@ public BaseFilterResponse<DRepFilterResponse> getDRepsByFilter(

long fromDate = Timestamp.valueOf(MIN_TIME).getTime() / 1000;
fromDate = fromDate < 0 ? 0 : fromDate;
long toDate =
Timestamp.from(
LocalDateTime.ofInstant(Instant.now(), ZoneOffset.UTC)
.toInstant(ZoneOffset.UTC))
.getTime()
/ 1000;
long toDate = LocalDateTime.now().toEpochSecond(ZoneOffset.UTC);

if (Objects.nonNull(dRepFilterRequest.getFromDate())) {
fromDate = Timestamp.from(dRepFilterRequest.getFromDate().toInstant()).getTime() / 1000;
fromDate = dRepFilterRequest.getFromDate().toEpochSecond(ZoneOffset.UTC);
}
if (Objects.nonNull(dRepFilterRequest.getToDate())) {
long to = Timestamp.from(dRepFilterRequest.getToDate().toInstant()).getTime() / 1000;
long to = dRepFilterRequest.getToDate().toEpochSecond(ZoneOffset.UTC);
toDate = Math.min(to, toDate);
}

Expand Down Expand Up @@ -278,6 +274,12 @@ public BaseFilterResponse<DRepFilterResponse> getDRepsByFilter(
return new BaseFilterResponse<>(dRepInfoPage);
}

@Override
public DRepRangeValuesResponse getDRepRangeValues() {
DRepRangeProjection projection = drepInfoRepository.getDRepRangeValues();
return dRepMapper.fromDRepRangeProjection(projection);
}

@Override
public BaseFilterResponse<DRepDelegatorsResponse> getDRepDelegators(
String drepHashOrDrepId, Pageable pageable) {
Expand Down

0 comments on commit 62e7847

Please sign in to comment.