11use std:: collections:: { BTreeMap , VecDeque } ;
22use chrono:: DateTime ;
33use ethers:: types:: TxHash ;
4+ use serde:: { Deserialize , Serialize } ;
5+ use utoipa:: ToSchema ;
46use {
57 crate :: {
68 chain:: reader:: { BlockNumber , BlockStatus , EntropyReader } ,
@@ -47,7 +49,7 @@ pub struct ApiMetrics {
4749
4850
4951
50- #[ derive( Clone ) ]
52+ #[ derive( Clone , Debug , Serialize ) ]
5153enum JournalLog {
5254 Observed {
5355 tx_hash : TxHash
@@ -74,13 +76,13 @@ impl JournalLog {
7476 }
7577}
7678
77- #[ derive( Clone ) ]
79+ #[ derive( Clone , Debug , Serialize ) ]
7880struct TimedJournalLog {
7981 pub timestamp : DateTime < chrono:: Utc > ,
8082 pub log : JournalLog ,
8183}
8284
83- #[ derive( Clone ) ]
85+ #[ derive( Clone , Debug , Serialize , ToSchema ) ]
8486struct RequestJournal {
8587 pub chain_id : ChainId ,
8688 pub sequence : u64 ,
@@ -90,7 +92,7 @@ struct RequestJournal {
9092type RequestKey = ( ChainId , u64 ) ;
9193
9294#[ derive( Default ) ]
93- struct History {
95+ pub struct History {
9496 pub by_hash : HashMap < TxHash , Vec < RequestKey > > ,
9597 pub by_chain_and_time : BTreeMap < ( ChainId , DateTime < chrono:: Utc > ) , RequestKey > ,
9698 pub by_time : BTreeMap < DateTime < chrono:: Utc > , RequestKey > ,
@@ -133,16 +135,16 @@ impl History {
133135 }
134136 }
135137
136- pub fn get_request_logs ( & self , request_key : & RequestKey ) -> Option < & RequestJournal > {
137- self . by_request_key . get ( request_key)
138+ pub fn get_request_logs ( & self , request_key : & RequestKey ) -> Option < RequestJournal > {
139+ self . by_request_key . get ( request_key) . cloned ( )
138140 }
139141
140- pub fn get_request_logs_by_tx_hash ( & self , tx_hash : TxHash ) -> Option < Vec < & RequestJournal > > {
142+ pub fn get_request_logs_by_tx_hash ( & self , tx_hash : TxHash ) -> Vec < RequestJournal > {
141143 self . by_hash . get ( & tx_hash) . map ( |request_keys| {
142144 request_keys. iter ( )
143- . map ( |request_key| self . by_request_key . get ( request_key) . unwrap ( ) )
145+ . map ( |request_key| self . by_request_key . get ( request_key) . unwrap ( ) . clone ( ) )
144146 . collect ( )
145- } )
147+ } ) . unwrap_or_default ( )
146148 }
147149
148150 pub fn get_latest_requests ( & self , chain_id : Option < & ChainId > , limit : u64 ,
@@ -190,6 +192,7 @@ impl ApiState {
190192 pub async fn new (
191193 chains : HashMap < ChainId , BlockchainState > ,
192194 metrics_registry : Arc < RwLock < Registry > > ,
195+ history : Arc < RwLock < History > >
193196 ) -> ApiState {
194197 let metrics = ApiMetrics {
195198 http_requests : Family :: default ( ) ,
@@ -205,7 +208,7 @@ impl ApiState {
205208 ApiState {
206209 chains : Arc :: new ( chains) ,
207210 metrics : Arc :: new ( metrics) ,
208- history : Arc :: new ( RwLock :: new ( History :: new ( ) ) ) ,
211+ history,
209212 metrics_registry,
210213 }
211214 }
0 commit comments