@@ -2,32 +2,46 @@ use crate::api::{ChainId, RestError};
22use crate :: history:: RequestStatus ;
33use axum:: extract:: { Query , State } ;
44use axum:: Json ;
5+ use chrono:: { DateTime , Utc } ;
56use ethers:: types:: { Address , TxHash } ;
67use std:: str:: FromStr ;
7- use utoipa:: { IntoParams , ToSchema } ;
8+ use utoipa:: IntoParams ;
89
910#[ derive( Debug , serde:: Serialize , serde:: Deserialize , IntoParams ) ]
1011#[ into_params( parameter_in=Query ) ]
1112pub struct ExplorerQueryParams {
12- pub min_timestamp : Option < u64 > ,
13- pub max_timestamp : Option < u64 > ,
13+ /// Only return logs that are newer or equal to this timestamp.
14+ #[ param( value_type = Option <String >, example = "2023-10-01T00:00:00Z" ) ]
15+ pub min_timestamp : Option < DateTime < Utc > > ,
16+ /// Only return logs that are older or equal to this timestamp.
17+ #[ param( value_type = Option <String >, example = "2023-10-01T00:00:00Z" ) ]
18+ pub max_timestamp : Option < DateTime < Utc > > ,
19+ /// The query string to search for. This can be a transaction hash, sender address, or sequence number.
1420 pub query : Option < String > ,
1521 #[ param( value_type = Option <String >) ]
22+ /// The chain ID to filter the results by.
1623 pub chain_id : Option < ChainId > ,
1724}
1825
26+ const LOG_RETURN_LIMIT : u64 = 1000 ;
27+
1928#[ utoipa:: path(
2029 get,
21- path = "/v1/explorer " ,
30+ path = "/v1/logs " ,
2231 responses(
23- ( status = 200 , description = "Random value successfully retrieved " , body = Vec <RequestJournal >)
32+ ( status = 200 , description = "Entropy request logs " , body = Vec <RequestStatus >)
2433 ) ,
2534 params( ExplorerQueryParams )
2635) ]
2736pub async fn explorer (
2837 State ( state) : State < crate :: api:: ApiState > ,
2938 Query ( query_params) : Query < ExplorerQueryParams > ,
3039) -> anyhow:: Result < Json < Vec < RequestStatus > > , RestError > {
40+ if let Some ( chain_id) = & query_params. chain_id {
41+ if !state. chains . read ( ) . await . contains_key ( chain_id) {
42+ return Err ( RestError :: InvalidChainId ) ;
43+ }
44+ }
3145 if let Some ( query) = query_params. query {
3246 if let Ok ( tx_hash) = TxHash :: from_str ( & query) {
3347 return Ok ( Json ( state. history . get_requests_by_tx_hash ( tx_hash) . await ) ) ;
@@ -48,7 +62,17 @@ pub async fn explorer(
4862 . await ,
4963 ) ) ;
5064 }
65+ return Err ( RestError :: InvalidQueryString ) ;
5166 }
52- //TODO: handle more types of queries
53- Ok ( Json ( vec ! [ ] ) )
67+ Ok ( Json (
68+ state
69+ . history
70+ . get_requests_by_time (
71+ query_params. chain_id ,
72+ LOG_RETURN_LIMIT ,
73+ query_params. min_timestamp ,
74+ query_params. max_timestamp ,
75+ )
76+ . await ,
77+ ) )
5478}
0 commit comments