Skip to content

Commit

Permalink
Merge pull request #2705 from input-output-hk/explorer-paginate-votes…
Browse files Browse the repository at this point in the history
…tatuses

Explorer paginate votestatuses
  • Loading branch information
Mikhail Zabaluev committed Nov 24, 2020
2 parents 72dab25 + fc7f9e0 commit a0cdc02
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 31 deletions.
56 changes: 54 additions & 2 deletions jormungandr/src/explorer/graphql/connections.rs
@@ -1,6 +1,8 @@
use super::error::ErrorKind;
use super::scalars::{BlockCount, IndexCursor, PoolCount, TransactionCount, VotePlanCount};
use super::{Block, Context, Pool, Transaction, VotePlanStatus};
use super::scalars::{
BlockCount, IndexCursor, PoolCount, TransactionCount, VotePlanCount, VoteStatusCount,
};
use super::{Block, Context, Pool, Transaction, VotePlanStatus, VoteStatus};
use crate::blockcfg::HeaderHash;
use crate::explorer::indexing::ExplorerTransaction;
use juniper::FieldResult;
Expand Down Expand Up @@ -82,6 +84,19 @@ impl VotePlanEdge {
}
}

#[juniper::object(
Context = Context
)]
impl VoteStatusEdge {
pub fn node(&self) -> &VoteStatus {
&self.node
}

pub fn cursor(&self) -> &IndexCursor {
&self.cursor
}
}

#[juniper::object(
Context = Context,
name = "BlockConnection"
Expand Down Expand Up @@ -158,6 +173,25 @@ impl VotePlanConnection {
}
}

#[juniper::object(
Context = Context,
name = "VoteStatusConnection"
)]
impl VoteStatusConnection {
pub fn page_info(&self) -> &PageInfo {
&self.page_info
}

pub fn edges(&self) -> &Vec<VoteStatusEdge> {
&self.edges
}

/// A count of the total number of objects in this connection, ignoring pagination.
pub fn total_count(&self) -> &VoteStatusCount {
&self.total_count
}
}

pub struct PageInfo {
pub has_next_page: bool,
pub has_previous_page: bool,
Expand Down Expand Up @@ -191,6 +225,11 @@ pub struct VotePlanEdge {
pub cursor: IndexCursor,
}

pub struct VoteStatusEdge {
node: VoteStatus,
pub cursor: IndexCursor,
}

pub trait Edge {
type Node;
fn new(node: Self::Node, cursor: IndexCursor) -> Self;
Expand Down Expand Up @@ -295,6 +334,7 @@ pub type BlockConnection = Connection<BlockEdge, BlockCount>;
pub type TransactionConnection = Connection<TransactionEdge, TransactionCount>;
pub type PoolConnection = Connection<PoolEdge, PoolCount>;
pub type VotePlanConnection = Connection<VotePlanEdge, VotePlanCount>;
pub type VoteStatusConnection = Connection<VoteStatusEdge, VoteStatusCount>;

#[allow(clippy::large_enum_variant)]
#[derive(Clone)]
Expand Down Expand Up @@ -356,6 +396,18 @@ impl Edge for VotePlanEdge {
}
}

impl Edge for VoteStatusEdge {
type Node = VoteStatus;

fn new(node: Self::Node, cursor: IndexCursor) -> Self {
VoteStatusEdge { node, cursor }
}

fn cursor(&self) -> &IndexCursor {
&self.cursor
}
}

fn compute_range_boundaries(
total_elements: InclusivePaginationInterval<u64>,
pagination_arguments: ValidatedPaginationArguments<u64>,
Expand Down
45 changes: 43 additions & 2 deletions jormungandr/src/explorer/graphql/mod.rs
Expand Up @@ -6,6 +6,7 @@ mod scalars;
use self::connections::{
BlockConnection, InclusivePaginationInterval, PaginationArguments, PaginationInterval,
PoolConnection, TransactionConnection, TransactionNodeFetchInfo, VotePlanConnection,
VoteStatusConnection,
};
use self::error::ErrorKind;
use super::indexing::{
Expand Down Expand Up @@ -1165,8 +1166,48 @@ impl VoteProposalStatus {
self.tally.as_ref()
}

pub fn votes(&self) -> &[VoteStatus] {
&self.votes
pub fn votes(
&self,
first: Option<i32>,
last: Option<i32>,
before: Option<IndexCursor>,
after: Option<IndexCursor>,
context: &Context,
) -> FieldResult<VoteStatusConnection> {
let boundaries = if !self.votes.is_empty() {
PaginationInterval::Inclusive(InclusivePaginationInterval {
lower_bound: 0u32,
upper_bound: self
.votes
.len()
.checked_sub(1)
.unwrap()
.try_into()
.expect("tried to paginate more than 2^32 elements"),
})
} else {
PaginationInterval::Empty
};

let pagination_arguments = PaginationArguments {
first,
last,
before: before.map(u32::try_from).transpose()?,
after: after.map(u32::try_from).transpose()?,
}
.validate()?;

VoteStatusConnection::new(boundaries, pagination_arguments, |range| match range {
PaginationInterval::Empty => vec![],
PaginationInterval::Inclusive(range) => {
let from = range.lower_bound;
let to = range.upper_bound;

(from..=to)
.map(|i: u32| (self.votes[i as usize].clone(), i))
.collect::<Vec<(VoteStatus, u32)>>()
}
})
}
}

Expand Down
9 changes: 9 additions & 0 deletions jormungandr/src/explorer/graphql/scalars.rs
Expand Up @@ -58,6 +58,9 @@ pub struct Weight(pub String);
#[derive(juniper::GraphQLScalarValue)]
pub struct VotePlanCount(pub String);

#[derive(juniper::GraphQLScalarValue)]
pub struct VoteStatusCount(pub String);

/// Vote option range
///
/// provide a range of available choices for a given proposal. Usual value would
Expand Down Expand Up @@ -266,3 +269,9 @@ impl From<u32> for VotePlanCount {
VotePlanCount(format!("{}", number))
}
}

impl From<u64> for VoteStatusCount {
fn from(number: u64) -> VoteStatusCount {
VoteStatusCount(format!("{}", number))
}
}
17 changes: 17 additions & 0 deletions testing/jormungandr-integration-tests/src/jormungandr/vit/mod.rs
Expand Up @@ -454,4 +454,21 @@ pub fn jcli_e2e_flow() {
.votes_cast,
3
);

assert_eq!(
jormungandr
.explorer()
.vote_plans(1)
.unwrap()
.data
.unwrap()
.all_vote_plans
.edges[0]
.node
.proposals[0]
.votes
.edges
.len(),
3
);
}
Expand Up @@ -104,6 +104,7 @@ type PageInfo {

enum PayloadType {
PUBLIC
PRIVATE
}

type Pool {
Expand Down Expand Up @@ -371,14 +372,31 @@ type VoteProposalStatus {
proposalId: ExternalProposalId!
options: VoteOptionRange!
tally: TallyStatus
votes: [VoteStatus!]!
votes(first: Int, last: Int, before: IndexCursor, after: IndexCursor): VoteStatusConnection!
}

type VoteStatus {
address: Address!
payload: VotePayloadStatus!
}

type VoteStatusConnection {
pageInfo: PageInfo!
edges: [VoteStatusEdge!]!

"""
A count of the total number of objects in this connection, ignoring pagination.
"""
totalCount: VoteStatusCount!
}

scalar VoteStatusCount

type VoteStatusEdge {
node: VoteStatus!
cursor: IndexCursor!
}

type VoteTally {
votePlan: VotePlanId!
}
Expand Down
@@ -1,29 +1,51 @@
query AllVotePlans($first: Int!){
allVotePlans(first: $first) {
edges{
node {
id,
voteStart{
epoch{
id
}
slot
}
voteEnd{
epoch{
id
}
slot
}
committeeEnd {
epoch{
id
}
slot
}
payloadType,
query AllVotePlans($first: Int!) {
allVotePlans(first: $first) {
edges {
node {
id
voteStart {
epoch {
id
}
slot
}
voteEnd {
epoch {
id
}
slot
}
committeeEnd {
epoch {
id
}
slot
}
payloadType
proposals {
# proposalId
options {
start
end
}
votes {
edges {
node {
address {
id
}
payload {
__typename
... on VotePayloadPublicStatus {
choice
}
}
}
}
}
}
totalCount
}
}
}
totalCount
}
}

0 comments on commit a0cdc02

Please sign in to comment.