Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(analytics): add nft and native token activity endpoints #560

Merged
merged 5 commits into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add native tokens activity stats
  • Loading branch information
Alexandcoats committed Aug 16, 2022
commit 96635086d241198d938ae762e06a023109f6fd76
14 changes: 14 additions & 0 deletions bin/inx-chronicle/src/api/stardust/analytics/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub fn routes() -> Router {
"/activity",
Router::new()
.route("/addresses", get(address_analytics))
.route("/native-tokens", get(foundry_output_analytics))
.route("/nfts", get(nft_output_analytics))
.nest(
"/blocks",
Expand Down Expand Up @@ -149,3 +150,16 @@ async fn nft_output_analytics(
burned_count: res.burned_count.to_string(),
})
}

async fn foundry_output_analytics(
database: Extension<MongoDb>,
MilestoneRange { start_index, end_index }: MilestoneRange,
) -> ApiResult<OutputDiffAnalyticsResponse> {
let res = database.get_foundry_output_analytics(start_index, end_index).await?;

Ok(OutputDiffAnalyticsResponse {
created_count: res.created_count.to_string(),
transferred_count: res.transferred_count.to_string(),
burned_count: res.burned_count.to_string(),
})
}
56 changes: 56 additions & 0 deletions src/db/collections/outputs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,62 @@ impl MongoDb {
.transpose()?
.unwrap_or_default())
}

/// Gathers foundry output analytics.
pub async fn get_foundry_output_analytics(
&self,
start_index: Option<MilestoneIndex>,
end_index: Option<MilestoneIndex>,
) -> Result<OutputDiffAnalyticsResult, Error> {
Ok(self
.db
.collection::<OutputDiffAnalyticsResult>(OutputDocument::COLLECTION)
.aggregate(
vec![
doc! { "$match": {
"output.kind": "foundry",
"metadata.booked.milestone_index": { "$not": { "$gt": start_index } },
DaughterOfMars marked this conversation as resolved.
Show resolved Hide resolved
} },
doc! { "$facet": {
"start_state": [
{ "$match": {
"$or": [
{ "metadata.spent_metadata.spent": null },
{ "metadata.spent_metadata.spent.milestone_index": { "$not": { "$lte": start_index } } },
],
} },
DaughterOfMars marked this conversation as resolved.
Show resolved Hide resolved
{ "$project": {
"foundry_id": "$output.foundry_id"
} },
],
"end_state": [
{ "$match": {
"metadata.booked.milestone_index": { "$not": { "$lte": start_index } },
DaughterOfMars marked this conversation as resolved.
Show resolved Hide resolved
"$or": [
{ "metadata.spent_metadata.spent": null },
{ "metadata.spent_metadata.spent.milestone_index": { "$not": { "$lte": end_index } } },
],
} },
{ "$project": {
"foundry_id": "$output.foundry_id"
} },
],
} },
doc! { "$project": {
"created_count": { "$size": { "$setDifference": [ "$end_state", "$start_state" ] } },
"transferred_count": { "$size": { "$setIntersection": [ "$start_state", "$end_state" ] } },
"burned_count": { "$size": { "$setDifference": [ "$start_state", "$end_state" ] } },
} },
],
None,
)
.await?
.try_next()
.await?
.map(bson::from_document)
.transpose()?
.unwrap_or_default())
}
}

#[derive(Clone, Debug, Serialize, Deserialize)]
Expand Down