Skip to content

Commit d015291

Browse files
committed
limit and offset
1 parent d8fe643 commit d015291

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

apps/fortuna/src/api/explorer.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ pub struct ExplorerQueryParams {
2727
#[param(value_type = Option<String>)]
2828
/// The chain ID to filter the results by.
2929
pub chain_id: Option<ChainId>,
30+
/// The maximum number of logs to return. Max value is 1000.
31+
#[param(default = 1000)]
32+
pub limit: Option<u64>,
33+
/// The offset to start returning logs from.
34+
#[param(default = 0)]
35+
pub offset: Option<u64>,
3036
}
3137

3238
const LOG_RETURN_LIMIT: u64 = 1000;
@@ -50,6 +56,11 @@ pub async fn explorer(
5056
return Err(RestError::InvalidChainId);
5157
}
5258
}
59+
if let Some(limit) = query_params.limit {
60+
if limit > LOG_RETURN_LIMIT || limit == 0 {
61+
return Err(RestError::InvalidQueryString);
62+
}
63+
}
5364
if let Some(query) = query_params.query {
5465
if let Ok(tx_hash) = TxHash::from_str(&query) {
5566
return Ok(Json(
@@ -85,7 +96,8 @@ pub async fn explorer(
8596
.history
8697
.get_requests_by_time(
8798
query_params.chain_id,
88-
LOG_RETURN_LIMIT,
99+
query_params.limit.unwrap_or(LOG_RETURN_LIMIT),
100+
query_params.offset.unwrap_or(0),
89101
query_params.min_timestamp,
90102
query_params.max_timestamp,
91103
)

apps/fortuna/src/history.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ impl History {
414414
&self,
415415
chain_id: Option<ChainId>,
416416
limit: u64,
417+
offset: u64,
417418
min_timestamp: Option<DateTime<chrono::Utc>>,
418419
max_timestamp: Option<DateTime<chrono::Utc>>,
419420
) -> Result<Vec<RequestStatus>> {
@@ -430,20 +431,23 @@ impl History {
430431
.unwrap(),
431432
);
432433
let limit = limit as i64;
434+
let offset = offset as i64;
433435
let rows = match chain_id {
434436
Some(chain_id) => {
435437
let chain_id = chain_id.to_string();
436-
sqlx::query_as!(RequestRow, "SELECT * FROM request WHERE chain_id = ? AND created_at >= ? AND created_at <= ? ORDER BY created_at DESC LIMIT ?",
438+
sqlx::query_as!(RequestRow, "SELECT * FROM request WHERE chain_id = ? AND created_at >= ? AND created_at <= ? ORDER BY created_at DESC LIMIT ? OFFSET ?",
437439
chain_id,
438440
min_timestamp,
439441
max_timestamp,
440-
limit).fetch_all(&self.pool).await
442+
limit,
443+
offset).fetch_all(&self.pool).await
441444
}
442445
None => {
443-
sqlx::query_as!(RequestRow, "SELECT * FROM request WHERE created_at >= ? AND created_at <= ? ORDER BY created_at DESC LIMIT ?",
446+
sqlx::query_as!(RequestRow, "SELECT * FROM request WHERE created_at >= ? AND created_at <= ? ORDER BY created_at DESC LIMIT ? OFFSET ?",
444447
min_timestamp,
445448
max_timestamp,
446-
limit).fetch_all(&self.pool).await
449+
limit,
450+
offset).fetch_all(&self.pool).await
447451
}
448452
}.map_err(|e| {
449453
tracing::error!("Failed to fetch request by time: {}", e);
@@ -645,6 +649,7 @@ mod test {
645649
.get_requests_by_time(
646650
chain_id.clone(),
647651
10,
652+
0,
648653
Some(status.created_at),
649654
Some(status.created_at),
650655
)
@@ -657,6 +662,7 @@ mod test {
657662
.get_requests_by_time(
658663
chain_id.clone(),
659664
10,
665+
0,
660666
Some(status.created_at + Duration::seconds(1)),
661667
None,
662668
)
@@ -669,6 +675,7 @@ mod test {
669675
.get_requests_by_time(
670676
chain_id.clone(),
671677
10,
678+
0,
672679
None,
673680
Some(status.created_at - Duration::seconds(1)),
674681
)
@@ -678,7 +685,7 @@ mod test {
678685

679686
// no min or max
680687
let logs = history
681-
.get_requests_by_time(chain_id.clone(), 10, None, None)
688+
.get_requests_by_time(chain_id.clone(), 10, 0, None, None)
682689
.await
683690
.unwrap();
684691
assert_eq!(logs, vec![status.clone()]);

0 commit comments

Comments
 (0)