Skip to content

Commit

Permalink
feat(db): better reporting and logging (#493)
Browse files Browse the repository at this point in the history
* feat(db): better reporting and logging

* Fix

* Typo
  • Loading branch information
grtlr authored Aug 1, 2022
1 parent 4dc2aa1 commit 8eaddc6
Show file tree
Hide file tree
Showing 14 changed files with 94 additions and 88 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ path = "bin/inx-chronicle/src/main.rs"
# Required
async-recursion = { version = "1.0", default-features = false }
async-trait = { version = "0.1", default-features = false }
bytesize = { version = "1.1", default-features = false }
clap = { version = "3.2", default-features = false, features = [ "env", "derive", "std" ] }
derive_more = { version = "0.99", default-features = false, features = [ "add", "add_assign", "deref", "deref_mut" ] }
dotenv = { version = "0.15", default-features = false }
Expand Down
5 changes: 4 additions & 1 deletion bin/inx-chronicle/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ impl ChronicleConfig {
/// Applies command line arguments to the config.
pub fn apply_cl_args(&mut self, args: &super::cli::ClArgs) {
if let Some(connect_url) = &args.db_addr {
self.mongodb = MongoDbConfig::new().with_connect_url(connect_url);
self.mongodb = MongoDbConfig {
connect_url: connect_url.clone(),
..Default::default()
};
}
#[cfg(all(feature = "stardust", feature = "inx"))]
{
Expand Down
11 changes: 11 additions & 0 deletions bin/inx-chronicle/src/launcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

use async_trait::async_trait;
use bytesize::ByteSize;
#[cfg(any(feature = "api", feature = "inx"))]
use chronicle::runtime::{ActorError, HandleEvent, Report};
use chronicle::{
Expand Down Expand Up @@ -63,7 +64,17 @@ impl Actor for Launcher {
};
config.apply_cl_args(&cl_args);

log::info!(
"Connecting to database at bind address `{}`.",
config.mongodb.connect_url
);
let db = MongoDb::connect(&config.mongodb).await?;
log::debug!("Available databases: `{:?}`", db.get_databases().await?);
log::info!(
"Connected to database `{}` ({})",
db.name(),
ByteSize::b(db.size().await?)
);

#[cfg(feature = "stardust")]
{
Expand Down
18 changes: 9 additions & 9 deletions src/db/collections/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl BlockDocument {
impl MongoDb {
/// Creates block indexes.
pub async fn create_block_indexes(&self) -> Result<(), Error> {
let collection = self.0.collection::<BlockDocument>(BlockDocument::COLLECTION);
let collection = self.db.collection::<BlockDocument>(BlockDocument::COLLECTION);

collection
.create_index(
Expand Down Expand Up @@ -81,7 +81,7 @@ impl MongoDb {
/// Get a [`Block`] by its [`BlockId`].
pub async fn get_block(&self, block_id: &BlockId) -> Result<Option<Block>, Error> {
let block = self
.0
.db
.collection::<Block>(BlockDocument::COLLECTION)
.aggregate(
vec![
Expand All @@ -108,7 +108,7 @@ impl MongoDb {
}

let raw = self
.0
.db
.collection::<RawResult>(BlockDocument::COLLECTION)
.aggregate(
vec![
Expand All @@ -129,7 +129,7 @@ impl MongoDb {
/// Get the metadata of a [`Block`] by its [`BlockId`].
pub async fn get_block_metadata(&self, block_id: &BlockId) -> Result<Option<BlockMetadata>, Error> {
let block = self
.0
.db
.collection::<BlockMetadata>(BlockDocument::COLLECTION)
.aggregate(
vec![
Expand Down Expand Up @@ -160,7 +160,7 @@ impl MongoDb {
}

Ok(self
.0
.db
.collection::<BlockIdResult>(BlockDocument::COLLECTION)
.aggregate(
vec![
Expand Down Expand Up @@ -195,7 +195,7 @@ impl MongoDb {
let mut doc = bson::to_document(&block_document)?;
doc.insert("_id", block_id.to_hex());

self.0
self.db
.collection::<BlockDocument>(BlockDocument::COLLECTION)
.update_one(
doc! { "metadata.block_id": block_id },
Expand All @@ -210,7 +210,7 @@ impl MongoDb {
/// Finds the [`Block`] that included a transaction by [`TransactionId`].
pub async fn get_block_for_transaction(&self, transaction_id: &TransactionId) -> Result<Option<Block>, Error> {
let block = self
.0
.db
.collection::<Block>(BlockDocument::COLLECTION)
.aggregate(
vec![
Expand All @@ -234,7 +234,7 @@ impl MongoDb {
/// Gets the spending transaction of an [`Output`](crate::types::stardust::block::Output) by [`OutputId`].
pub async fn get_spending_transaction(&self, output_id: &OutputId) -> Result<Option<Block>, Error> {
Ok(self
.0
.db
.collection::<Block>(BlockDocument::COLLECTION)
.aggregate(
vec![
Expand Down Expand Up @@ -274,7 +274,7 @@ mod analytics {
end_milestone: MilestoneIndex,
) -> Result<TransactionAnalyticsResult, Error> {
Ok(self
.0
.db
.collection::<TransactionAnalyticsResult>(BlockDocument::COLLECTION)
.aggregate(
vec![
Expand Down
10 changes: 5 additions & 5 deletions src/db/collections/ledger_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl MongoDb {
/// Creates ledger update indexes.
pub async fn create_ledger_update_indexes(&self) -> Result<(), Error> {
let collection = self
.0
.db
.collection::<LedgerUpdateDocument>(LedgerUpdateDocument::COLLECTION);

collection
Expand Down Expand Up @@ -152,7 +152,7 @@ impl MongoDb {
at,
is_spent: delta.metadata.spent_metadata.is_some(),
};
self.0
self.db
.collection::<LedgerUpdateDocument>(LedgerUpdateDocument::COLLECTION)
.update_one(
doc! { "output_id": &doc.output_id, "is_spent": &doc.is_spent },
Expand Down Expand Up @@ -197,7 +197,7 @@ impl MongoDb {
queries.push(doc! { "$or": cursor_queries });
}

self.0
self.db
.collection::<LedgerUpdateByAddressRecord>(LedgerUpdateDocument::COLLECTION)
.find(
doc! { "$and": queries },
Expand Down Expand Up @@ -226,7 +226,7 @@ impl MongoDb {
queries.push(doc! { "$or": cursor_queries });
}

self.0
self.db
.collection::<LedgerUpdateByMilestoneRecord>(LedgerUpdateDocument::COLLECTION)
.find(
doc! { "$and": queries },
Expand Down Expand Up @@ -263,7 +263,7 @@ mod analytics {
end_timestamp: MilestoneTimestamp,
) -> Result<Option<AddressAnalyticsResult>, Error> {
Ok(self
.0
.db
.collection::<LedgerUpdateDocument>(LedgerUpdateDocument::COLLECTION)
.aggregate(
vec![
Expand Down
26 changes: 13 additions & 13 deletions src/db/collections/milestone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub struct SyncData {
impl MongoDb {
/// Creates ledger update indexes.
pub async fn create_milestone_indexes(&self) -> Result<(), Error> {
let collection = self.0.collection::<MilestoneDocument>(MilestoneDocument::COLLECTION);
let collection = self.db.collection::<MilestoneDocument>(MilestoneDocument::COLLECTION);

collection
.create_index(
Expand Down Expand Up @@ -110,7 +110,7 @@ impl MongoDb {
milestone_id: &MilestoneId,
) -> Result<Option<MilestonePayload>, Error> {
Ok(self
.0
.db
.collection::<MilestonePayload>(MilestoneDocument::COLLECTION)
.aggregate(
vec![
Expand All @@ -129,7 +129,7 @@ impl MongoDb {
/// Gets [`MilestonePayload`] of a milestone by the [`MilestoneIndex`].
pub async fn get_milestone_payload(&self, index: MilestoneIndex) -> Result<Option<MilestonePayload>, Error> {
Ok(self
.0
.db
.collection::<MilestonePayload>(MilestoneDocument::COLLECTION)
.aggregate(
vec![
Expand All @@ -148,7 +148,7 @@ impl MongoDb {
/// Gets the timestamp of a milestone by the [`MilestoneIndex`].
pub async fn get_milestone_timestamp(&self, index: MilestoneIndex) -> Result<Option<MilestoneTimestamp>, Error> {
Ok(self
.0
.db
.collection::<MilestoneIndexTimestamp>(MilestoneDocument::COLLECTION)
.find_one(
doc! { "at.milestone_index": index },
Expand Down Expand Up @@ -184,7 +184,7 @@ impl MongoDb {
let mut doc = bson::to_document(&milestone_document)?;
doc.insert("_id", milestone_document.milestone_id.to_hex());

self.0
self.db
.collection::<MilestoneDocument>(MilestoneDocument::COLLECTION)
.update_one(
doc! { "at.milestone_index": milestone_index },
Expand All @@ -201,7 +201,7 @@ impl MongoDb {
&self,
start_timestamp: MilestoneTimestamp,
) -> Result<Option<MilestoneIndexTimestamp>, Error> {
self.0
self.db
.collection::<MilestoneIndexTimestamp>(MilestoneDocument::COLLECTION)
.find(
doc! {
Expand All @@ -227,7 +227,7 @@ impl MongoDb {
&self,
end_timestamp: MilestoneTimestamp,
) -> Result<Option<MilestoneIndexTimestamp>, Error> {
self.0
self.db
.collection::<MilestoneIndexTimestamp>(MilestoneDocument::COLLECTION)
.find(
doc! {
Expand All @@ -250,7 +250,7 @@ impl MongoDb {

/// Find the latest milestone inserted.
pub async fn get_latest_milestone(&self) -> Result<Option<MilestoneIndexTimestamp>, Error> {
self.0
self.db
.collection::<MilestoneIndexTimestamp>(MilestoneDocument::COLLECTION)
.find(
doc! { "is_synced": true },
Expand All @@ -270,7 +270,7 @@ impl MongoDb {

/// Marks that all [`Block`](crate::types::stardust::block::Block)s of a milestone have been synchronized.
pub async fn set_sync_status_blocks(&self, index: MilestoneIndex) -> Result<(), Error> {
self.0
self.db
.collection::<MilestoneDocument>(MilestoneDocument::COLLECTION)
.update_one(
doc! { "at.milestone_index": index },
Expand All @@ -288,7 +288,7 @@ impl MongoDb {
range: RangeInclusive<MilestoneIndex>,
) -> Result<impl Stream<Item = Result<MilestoneIndex, Error>>, Error> {
Ok(self
.0
.db
.collection::<MilestoneIndexTimestamp>(MilestoneDocument::COLLECTION)
.find(
doc! {
Expand Down Expand Up @@ -347,7 +347,7 @@ impl MongoDb {
/// Retrieves gaps in the milestones collection.
pub async fn get_gaps(&self) -> Result<Vec<RangeInclusive<MilestoneIndex>>, Error> {
let mut synced_ms = self
.0
.db
.collection::<MilestoneIndexTimestamp>(MilestoneDocument::COLLECTION)
.find(
doc! { "is_synced": true },
Expand Down Expand Up @@ -388,7 +388,7 @@ impl MongoDb {
}

Ok(self
.0
.db
.collection::<ReceiptAtIndex>(MilestoneDocument::COLLECTION)
.aggregate(
vec![
Expand Down Expand Up @@ -424,7 +424,7 @@ impl MongoDb {
}

Ok(self
.0
.db
.collection::<ReceiptAtIndex>(MilestoneDocument::COLLECTION)
.aggregate(
vec![
Expand Down
6 changes: 3 additions & 3 deletions src/db/collections/outputs/indexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl MongoDb {
IndexedId::Nft(_) => "output.nft_id",
};
let mut res = self
.0
.db
.collection::<OutputDocument>(OutputDocument::COLLECTION)
.aggregate(
vec![
Expand Down Expand Up @@ -179,7 +179,7 @@ impl MongoDb {
]
} };
let outputs = self
.0
.db
.collection::<OutputResult>(OutputDocument::COLLECTION)
.aggregate(
vec![
Expand All @@ -205,7 +205,7 @@ impl MongoDb {

/// Creates indexer output indexes.
pub async fn create_indexer_output_indexes(&self) -> Result<(), Error> {
let collection = self.0.collection::<OutputDocument>(OutputDocument::COLLECTION);
let collection = self.db.collection::<OutputDocument>(OutputDocument::COLLECTION);

collection
.create_index(
Expand Down
Loading

0 comments on commit 8eaddc6

Please sign in to comment.