Skip to content

Commit

Permalink
fix(db)!: fix status and milestone queries (#478)
Browse files Browse the repository at this point in the history
Fix status and milestone queries
  • Loading branch information
DaughterOfMars committed Jul 27, 2022
1 parent 71f3e6d commit 44aece3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
45 changes: 28 additions & 17 deletions src/db/collections/milestone.rs
Expand Up @@ -109,7 +109,7 @@ impl MongoDb {
&self,
milestone_id: &MilestoneId,
) -> Result<Option<MilestonePayload>, Error> {
let payload = self
Ok(self
.0
.collection::<MilestonePayload>(MilestoneDocument::COLLECTION)
.aggregate(
Expand All @@ -123,14 +123,12 @@ impl MongoDb {
.try_next()
.await?
.map(bson::from_document)
.transpose()?;

Ok(payload)
.transpose()?)
}

/// Gets [`MilestonePayload`] of a milestone by the [`MilestoneIndex`].
pub async fn get_milestone_payload(&self, index: MilestoneIndex) -> Result<Option<MilestonePayload>, Error> {
let payload = self
Ok(self
.0
.collection::<MilestonePayload>(MilestoneDocument::COLLECTION)
.aggregate(
Expand All @@ -144,24 +142,25 @@ impl MongoDb {
.try_next()
.await?
.map(bson::from_document)
.transpose()?;

Ok(payload)
.transpose()?)
}

/// Gets the timestamp of a milestone by the [`MilestoneIndex`].
pub async fn get_milestone_timestamp(&self, index: MilestoneIndex) -> Result<Option<MilestoneTimestamp>, Error> {
let timestamp = self
Ok(self
.0
.collection::<MilestoneIndexTimestamp>(MilestoneDocument::COLLECTION)
.find_one(
doc! { "at.milestone_index": index },
FindOneOptions::builder().projection(doc! { "at": 1 }).build(),
FindOneOptions::builder()
.projection(doc! {
"milestone_index": "$at.milestone_index",
"milestone_timestamp": "$at.milestone_timestamp",
})
.build(),
)
.await?
.map(|ts| ts.milestone_timestamp);

Ok(timestamp)
.map(|ts| ts.milestone_timestamp))
}

/// Inserts the information of a milestone into the database.
Expand Down Expand Up @@ -212,7 +211,10 @@ impl MongoDb {
FindOptions::builder()
.sort(doc! { "at.milestone_index": 1 })
.limit(1)
.projection(doc! { "at": 1 })
.projection(doc! {
"milestone_index": "$at.milestone_index",
"milestone_timestamp": "$at.milestone_timestamp",
})
.build(),
)
.await?
Expand All @@ -235,7 +237,10 @@ impl MongoDb {
FindOptions::builder()
.sort(doc! { "at.milestone_index": -1 })
.limit(1)
.projection(doc! { "at": 1 })
.projection(doc! {
"milestone_index": "$at.milestone_index",
"milestone_timestamp": "$at.milestone_timestamp",
})
.build(),
)
.await?
Expand All @@ -252,7 +257,10 @@ impl MongoDb {
FindOptions::builder()
.sort(doc! { "at.milestone_index": -1 })
.limit(1)
.projection(doc! { "at": 1 })
.projection(doc! {
"milestone_index": "$at.milestone_index",
"milestone_timestamp": "$at.milestone_timestamp",
})
.build(),
)
.await?
Expand Down Expand Up @@ -289,7 +297,10 @@ impl MongoDb {
},
FindOptions::builder()
.sort(doc! { "at.milestone_index": 1 })
.projection(doc! { "at": 1 })
.projection(doc! {
"milestone_index": "$at.milestone_index",
"milestone_timestamp": "$at.milestone_timestamp",
})
.build(),
)
.await?
Expand Down
2 changes: 1 addition & 1 deletion src/db/collections/status.rs
Expand Up @@ -41,7 +41,7 @@ impl MongoDb {
.collection::<StatusDocument>(StatusDocument::COLLECTION)
.update_one(
doc! {},
doc! { "$set": { "protocol.parameters": to_document(&protocol_info)? } },
doc! { "$set": { "protocol": to_document(&protocol_info)? } },
UpdateOptions::builder().upsert(true).build(),
)
.await?;
Expand Down

0 comments on commit 44aece3

Please sign in to comment.