Skip to content

Commit

Permalink
Merge pull request #1074 from cardano-foundation/feature/MET-1963-gov…
Browse files Browse the repository at this point in the history
…-action-filter

feat: update gov action filter
  • Loading branch information
Sotatek-DucPhung committed Mar 27, 2024
2 parents af5404a + 547f694 commit 23beb95
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 50 deletions.
Expand Up @@ -13,9 +13,10 @@
@Getter
public enum GovActionStatus {
ANY("ANY"),
OPEN("OPEN"),
OPEN_BALLOT("OPEN_BALLOT"),
RATIFIED("RATIFIED"),
ENACTED("ENACTED");
ENACTED("ENACTED"),
EXPIRED("EXPIRED");

String value;

Expand Down
@@ -1,8 +1,10 @@
package org.cardanofoundation.explorer.api.controller;

import jakarta.validation.Valid;

import lombok.RequiredArgsConstructor;

import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -24,6 +26,9 @@
import org.cardanofoundation.explorer.api.model.response.governanceAction.GovernanceActionResponse;
import org.cardanofoundation.explorer.api.model.response.governanceAction.VotingChartResponse;
import org.cardanofoundation.explorer.api.service.GovernanceActionService;
import org.cardanofoundation.explorer.common.validation.pagination.Pagination;
import org.cardanofoundation.explorer.common.validation.pagination.PaginationDefault;
import org.cardanofoundation.explorer.common.validation.pagination.PaginationValid;

@RestController
@RequestMapping("/api/v1/gov-actions")
Expand All @@ -42,11 +47,18 @@ public class GovernanceActionController {
public ResponseEntity<BaseFilterResponse<GovernanceActionResponse>> getGovActionByFilter(
@PathVariable @Parameter(description = "The DRep hash or pool hash")
String dRepHashOrPoolHash,
GovernanceActionFilter governanceActionFilter,
@ParameterObject Pageable pageable) {
@ParameterObject GovernanceActionFilter governanceActionFilter,
@ParameterObject
@PaginationValid
@PaginationDefault(
size = 20,
sort = {"vp.blockTime"},
direction = Sort.Direction.DESC)
@Valid
Pagination pagination) {
return ResponseEntity.ok(
governanceActionService.getGovernanceActions(
dRepHashOrPoolHash, governanceActionFilter, pageable));
dRepHashOrPoolHash, governanceActionFilter, pagination.toPageable()));
}

@GetMapping("{dRepHashOrPoolHash}/voting-procedure-detail")
Expand Down
Expand Up @@ -12,6 +12,8 @@

@Mapper(componentModel = "spring")
public interface GovernanceActionMapper {

@Mapping(source = "status", target = "status")
GovernanceActionResponse fromGovernanceActionProjection(
GovernanceActionProjection governanceActionProjection);

Expand Down
Expand Up @@ -2,6 +2,8 @@

import java.util.Date;

import jakarta.validation.constraints.NotNull;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -26,13 +28,13 @@ public class GovernanceActionFilter {

String anchorText;

GovActionType actionType;
@NotNull GovActionType actionType;

GovActionStatus actionStatus;
@NotNull GovActionStatus actionStatus;

VoteType voteType;
@NotNull VoteType voteType;

VoterType voterType;
@NotNull VoterType voterType;

@DateValid(pattern = DatePattern.YYYY_MM_DD)
private Date fromDate;
Expand Down
@@ -1,5 +1,8 @@
package org.cardanofoundation.explorer.api.projection;

import java.math.BigInteger;

import org.cardanofoundation.explorer.common.entity.enumeration.GovActionStatus;
import org.cardanofoundation.explorer.common.entity.ledgersync.enumeration.GovActionType;
import org.cardanofoundation.explorer.common.entity.ledgersync.enumeration.Vote;

Expand All @@ -13,4 +16,10 @@ public interface GovernanceActionProjection {
Integer getSlot();

Vote getVote();

Boolean getRepeatVote();

GovActionStatus getStatus();

BigInteger getVotingPower();
}
@@ -1,6 +1,5 @@
package org.cardanofoundation.explorer.api.repository.ledgersync;

import java.sql.Timestamp;
import java.util.Optional;

import org.springframework.data.domain.Page;
Expand All @@ -12,6 +11,7 @@

import org.cardanofoundation.explorer.api.projection.GovActionDetailsProjection;
import org.cardanofoundation.explorer.api.projection.GovernanceActionProjection;
import org.cardanofoundation.explorer.common.entity.enumeration.GovActionStatus;
import org.cardanofoundation.explorer.common.entity.ledgersync.GovActionProposal;
import org.cardanofoundation.explorer.common.entity.ledgersync.compositeKey.GovActionProposalId;
import org.cardanofoundation.explorer.common.entity.ledgersync.enumeration.GovActionType;
Expand All @@ -22,27 +22,31 @@ public interface GovernanceActionRepository
extends JpaRepository<GovActionProposal, GovActionProposalId> {
@Query(
value =
"select gap.txHash as txHash, gap.index as index, vp.vote as vote, vp.slot as slot, gap.type as type from GovActionProposal gap"
+ " left join VotingProcedure vp on ("
"select gap.txHash as txHash, gap.index as index, vp.vote as vote, vp.slot as slot, "
+ "gap.type as type, vp.repeatVote as repeatVote, "
+ "gapInfo.status as status, gapInfo.votingPower as votingPower "
+ "from GovActionProposal gap "
+ " join GovActionProposalInfo gapInfo on (gap.txHash = gapInfo.txHash and gap.index = gapInfo.index)"
+ " left join LatestVotingProcedure vp on ("
+ " vp.govActionTxHash = gap.txHash"
+ " and vp.govActionIndex = gap.index"
+ " and vp.voterHash = :voterHash"
+ " and not exists (select 1 from VotingProcedure vp2"
+ " where vp2.govActionTxHash = gap.txHash"
+ " and vp2.govActionIndex = gap.index"
+ " and vp2.voterHash = :voterHash"
+ " and vp2.blockTime > vp.blockTime))"
+ " where (:type is null or gap.type = :type)"
+ " and (:vote is null or (vp.vote = :vote and vp.slot >= :slotDRep))"
+ " and (:from is null or vp.blockTime >= :from)"
+ " and (:to is null or vp.blockTime <= :to)")
+ " and vp.voterHash = :voterHash)"
+ " where (:vote is null or (:vote != 'NONE' and :vote = vp.vote) or (:vote = 'NONE' and vp.vote is null))"
+ " and (:isRepeatVote is null or (vp.repeatVote = :isRepeatVote))"
+ " and (:gapStatus is null or (gapInfo.status = :gapStatus))"
+ " and (:type is null or (gap.type = :type))"
+ " and (gap.slot >= :slot)"
+ " and (gap.blockTime >= :from)"
+ " and (gap.blockTime <= :to)")
Page<GovernanceActionProjection> getAllByFilter(
@Param("isRepeatVote") Boolean isRepeatVote,
@Param("gapStatus") GovActionStatus gapStatus,
@Param("vote") Vote vote,
@Param("voterHash") String dRepHash,
@Param("type") GovActionType type,
@Param("from") Timestamp from,
@Param("to") Timestamp to,
@Param("slotDRep") Long slotDRep,
@Param("from") Long from,
@Param("to") Long to,
@Param("slot") Long slot,
Pageable pageable);

@Query(
Expand Down
Expand Up @@ -2,7 +2,9 @@

import java.math.BigInteger;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
Expand Down Expand Up @@ -59,24 +61,18 @@ public class GovernanceActionServiceImpl implements GovernanceActionService {
@Value("${application.epoch.days}")
public long epochDays;

private final DRepRegistrationRepository dRepRegistrationRepository;
public static final String MIN_TIME = "1970-01-01 00:00:00";

private final DRepRegistrationRepository dRepRegistrationRepository;
private final GovernanceActionRepository governanceActionRepository;

private final PoolHashRepository poolHashRepository;

private final GovernanceActionMapper governanceActionMapper;

private final VotingProcedureMapper votingProcedureMapper;

private final VotingProcedureRepository votingProcedureRepository;

private final EpochParamRepository epochParamRepository;

private final LatestVotingProcedureRepository latestVotingProcedureRepository;

private final EpochMapper epochMapper;

private final EpochRepository epochRepository;

@Override
Expand Down Expand Up @@ -104,29 +100,43 @@ public BaseFilterResponse<GovernanceActionResponse> getGovernanceActions(
: org.cardanofoundation.explorer.common.entity.ledgersync.enumeration.GovActionType
.valueOf(governanceActionFilter.getActionType().name());

Timestamp from =
Objects.isNull(governanceActionFilter.getFromDate())
? null
: new Timestamp(governanceActionFilter.getFromDate().getTime());
long fromDate = Timestamp.valueOf(MIN_TIME).getTime() / 1000;
long toDate =
Timestamp.from(
LocalDateTime.ofInstant(Instant.now(), ZoneOffset.UTC)
.toInstant(ZoneOffset.UTC))
.getTime()
/ 1000;

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

org.cardanofoundation.explorer.common.entity.enumeration.GovActionStatus govActionStatus =
governanceActionFilter.getActionStatus().equals(GovActionStatus.ANY)
? null
: new Timestamp(governanceActionFilter.getToDate().getTime());
: org.cardanofoundation.explorer.common.entity.enumeration.GovActionStatus.valueOf(
governanceActionFilter.getActionStatus().name());

Page<GovernanceActionProjection> governanceActionProjections =
governanceActionRepository.getAllByFilter(
vote, dRepHashOrPoolHash, govActionType, from, to, slot, pageable);
governanceActionFilter.getIsRepeatVote(),
govActionStatus,
vote,
dRepHashOrPoolHash,
govActionType,
fromDate,
toDate,
slot,
pageable);

List<GovernanceActionResponse> governanceActionResponses =
governanceActionProjections.stream()
.map(
governanceActionProjection -> {
GovernanceActionResponse governanceActionResponse =
governanceActionMapper.fromGovernanceActionProjection(
governanceActionProjection);
governanceActionResponse.setStatus(GovActionStatus.OPEN);
return governanceActionResponse;
})
.map(governanceActionMapper::fromGovernanceActionProjection)
.collect(Collectors.toList());

if (governanceActionResponses.isEmpty()) {
Expand Down

0 comments on commit 23beb95

Please sign in to comment.