Something went wrong. Try again.
fork of hydrant
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192use super::Hydrant;use miette::{IntoDiagnostic, Result};use std::collections::BTreeMap;
/// database statistics returned by [`Hydrant::stats`].#[derive(serde::Serialize)]pub struct StatsResponse { /// record counts per logical category (repos, records, events, error kinds, etc.) pub counts: BTreeMap<&'static str, u64>, /// on-disk size in bytes per keyspace pub sizes: BTreeMap<&'static str, u64>,}
impl Hydrant { /// return database counts and on-disk sizes for all keyspaces. /// /// counts include: `repos`, `pending`, `resync`, `records`, `history`, `events`, /// `error_ratelimited`, `error_transport`, `error_generic`. /// /// sizes are in bytes, reported per keyspace. pub async fn stats(&self) -> Result<StatsResponse> { let state = self.state.clone();
#[allow(unused_mut)] let mut count_keys = vec![ "repos", "error_ratelimited", "error_transport", "error_generic", ];
#[cfg(feature = "indexer")] { count_keys.push("pending"); count_keys.push("records"); count_keys.push("history"); count_keys.push("resync"); }
#[cfg_attr( not(any(feature = "indexer_stream", feature = "relay")), allow(unused_mut) )] let mut counts: BTreeMap<&'static str, u64> = futures::future::join_all(count_keys.into_iter().map(|name| { let state = state.clone(); async move { (name, state.db.get_count(name).await) } })) .await .into_iter() .collect();
#[cfg(feature = "indexer_stream")] counts.insert("events", state.db.stream.approximate_event_count() as u64);
// only meaningful when bounded history can legitimately trim bodies #[cfg(feature = "indexer_stream")] if state.history_ttl.is_some() { counts.insert( "history_trim_misses", state .history_trim_misses .load(std::sync::atomic::Ordering::Relaxed), ); }
#[cfg(feature = "relay")] counts.insert( "relay_events", state.db.relay.events.approximate_len() as u64, ); #[cfg(feature = "jetstream")] counts.insert( "jetstream_events", state.db.jetstream.events.approximate_len() as u64, );
let sizes = state .db .run(move |db| { Ok(db .all_keyspaces() .into_iter() .map(|(name, ks)| (name, ks.disk_space())) .collect::<BTreeMap<_, _>>()) }) .await?;
Ok(StatsResponse { counts, sizes }) }}