diff --git a/crates/iota-indexer/src/indexer_reader.rs b/crates/iota-indexer/src/indexer_reader.rs index 94fa737b6d6..3915588184f 100644 --- a/crates/iota-indexer/src/indexer_reader.rs +++ b/crates/iota-indexer/src/indexer_reader.rs @@ -63,7 +63,7 @@ use crate::{ }, schema::{ address_metrics, checkpoints, display, epochs, events, move_call_metrics, objects, - objects_snapshot, transactions, + objects_snapshot, packages, transactions, }, store::{diesel_macro::*, package_resolver::IndexerStorePackageResolver}, types::{IndexerResult, OwnerType}, @@ -1509,10 +1509,17 @@ impl IndexerReader { } pub fn get_latest_network_metrics(&self) -> IndexerResult { - let metrics = run_query!(&self.pool, |conn| { + let mut metrics = run_query!(&self.pool, |conn| { diesel::sql_query("SELECT * FROM network_metrics;") .get_result::(conn) })?; + if metrics.total_packages == -1 { + // this implies that the estimate is not available in the db + // so we fallback to the more expensive count query + metrics.total_packages = run_query!(&self.pool, |conn| { + packages::dsl::packages.count().get_result::(conn) + })?; + } Ok(metrics.into()) }