Skip to content

Commit

Permalink
Fix missing time on first block
Browse files Browse the repository at this point in the history
The first block is always missing time at the moment, as the query can only
compare the time to the previous row.  Since we'll always be missing the
previous row on the first entry, we end up missing the `time` for the first
entry as well.  The fix is simply to pull one more row than we want, and to
remove the first entry when the time comes.  As a result the `Vec` has
been replaced with a `VecDeque`.

Additionally the hard-coded values have been replaced with constants with
comments.
  • Loading branch information
Ayiga committed Nov 26, 2024
1 parent 2b2f0fb commit 29b91a4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 19 deletions.
51 changes: 36 additions & 15 deletions src/data_source/storage/sql/queries/explorer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use futures::stream::{self, StreamExt, TryStreamExt};
use hotshot_types::traits::node_implementation::NodeType;
use itertools::Itertools;
use sqlx::{types::Json, FromRow, Row};
use std::num::NonZeroUsize;
use std::{collections::VecDeque, num::NonZeroUsize};
use tagged_base64::{Tagged, TaggedBase64};

impl From<sqlx::Error> for GetExplorerSummaryError {
Expand Down Expand Up @@ -251,6 +251,18 @@ lazy_static::lazy_static! {
};
}

/// [EXPLORER_SUMMARY_HISTOGRAM_NUM_ENTRIES] is the number of entries we want
/// to return in our histogram summary.
const EXPLORER_SUMMARY_HISTOGRAM_NUM_ENTRIES: usize = 50;

/// [EXPLORER_SUMMARY_NUM_BLOCKS] is the number of blocks we want to return in
/// our explorer summary.
const EXPLORER_SUMMARY_NUM_BLOCKS: usize = 10;

/// [EXPLORER_SUMMARY_NUM_TRANSACTIONS] is the number of transactions we want
/// to return in our explorer summary.
const EXPLORER_SUMMARY_NUM_TRANSACTIONS: usize = 10;

#[async_trait]
impl<Mode, Types> ExplorerStorage<Types> for Transaction<Mode>
where
Expand Down Expand Up @@ -471,13 +483,14 @@ where
JOIN payload AS p ON
p.height = h.height
WHERE
h.height IN (SELECT height FROM header ORDER BY height DESC LIMIT 50)
h.height IN (SELECT height FROM header ORDER BY height DESC LIMIT $1)
ORDER BY h.height
",
)
.bind((EXPLORER_SUMMARY_HISTOGRAM_NUM_ENTRIES + 1) as i64)
.fetch(self.as_mut());

let histograms: Result<ExplorerHistograms, sqlx::Error> = histogram_query_result
let mut histograms: ExplorerHistograms = histogram_query_result
.map(|row_stream| {
row_stream.map(|row| {
let height: i64 = row.try_get("height")?;
Expand All @@ -491,24 +504,32 @@ where
})
.try_fold(
ExplorerHistograms {
block_time: Vec::with_capacity(50),
block_size: Vec::with_capacity(50),
block_transactions: Vec::with_capacity(50),
block_heights: Vec::with_capacity(50),
block_time: VecDeque::with_capacity(EXPLORER_SUMMARY_HISTOGRAM_NUM_ENTRIES),
block_size: VecDeque::with_capacity(EXPLORER_SUMMARY_HISTOGRAM_NUM_ENTRIES),
block_transactions: VecDeque::with_capacity(EXPLORER_SUMMARY_HISTOGRAM_NUM_ENTRIES),
block_heights: VecDeque::with_capacity(EXPLORER_SUMMARY_HISTOGRAM_NUM_ENTRIES),
},
|mut histograms: ExplorerHistograms,
row: sqlx::Result<(i64, i64, Option<i64>, Option<i32>, i32)>| async {
let (height, _timestamp, time, size, num_transactions) = row?;
histograms.block_time.push(time.map(|i| i as u64));
histograms.block_size.push(size.map(|i| i as u64));
histograms.block_transactions.push(num_transactions as u64);
histograms.block_heights.push(height as u64);

histograms.block_time.push_back(time.map(|i| i as u64));
histograms.block_size.push_back(size.map(|i| i as u64));
histograms.block_transactions.push_back(num_transactions as u64);
histograms.block_heights.push_back(height as u64);
Ok(histograms)
},
)
.await;
.await?;

while histograms.block_time.len() > EXPLORER_SUMMARY_HISTOGRAM_NUM_ENTRIES {
histograms.block_time.pop_front();
histograms.block_size.pop_front();
histograms.block_transactions.pop_front();
histograms.block_heights.pop_front();
}

histograms?
histograms
};

let genesis_overview = {
Expand All @@ -528,15 +549,15 @@ where
let latest_blocks: Vec<BlockSummary<Types>> = self
.get_block_summaries(GetBlockSummariesRequest(BlockRange {
target: BlockIdentifier::Latest,
num_blocks: NonZeroUsize::new(10).unwrap(),
num_blocks: NonZeroUsize::new(EXPLORER_SUMMARY_NUM_BLOCKS).unwrap(),
}))
.await?;

let latest_transactions: Vec<TransactionSummary<Types>> = self
.get_transaction_summaries(GetTransactionSummariesRequest {
range: TransactionRange {
target: TransactionIdentifier::Latest,
num_transactions: NonZeroUsize::new(10).unwrap(),
num_transactions: NonZeroUsize::new(EXPLORER_SUMMARY_NUM_TRANSACTIONS).unwrap(),
},
filter: TransactionSummaryFilter::None,
})
Expand Down
9 changes: 5 additions & 4 deletions src/explorer/query_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use crate::{node::BlockHash, types::HeightIndexed};
use hotshot_types::traits::node_implementation::NodeType;
use serde::{Deserialize, Serialize};
use std::{
collections::VecDeque,
fmt::{Debug, Display},
num::{NonZeroUsize, TryFromIntError},
};
Expand Down Expand Up @@ -469,10 +470,10 @@ pub struct GenesisOverview {
/// `block_transactions` for those `block_heights`.
#[derive(Debug, Serialize, Deserialize)]
pub struct ExplorerHistograms {
pub block_time: Vec<Option<u64>>,
pub block_size: Vec<Option<u64>>,
pub block_transactions: Vec<u64>,
pub block_heights: Vec<u64>,
pub block_time: VecDeque<Option<u64>>,
pub block_size: VecDeque<Option<u64>>,
pub block_transactions: VecDeque<u64>,
pub block_heights: VecDeque<u64>,
}

/// [ExplorerSummary] is a struct that represents an at-a-glance snapshot of
Expand Down

0 comments on commit 29b91a4

Please sign in to comment.