Skip to content

Commit

Permalink
fix(analytics): flip claimed to unclaimed analytics (#871)
Browse files Browse the repository at this point in the history
* Flip claimed to unclaimed analytics

* Fix test case

* Fix dashboard aliases

Co-authored-by: Jochen Görtler <jochen.goertler@iota.org>
  • Loading branch information
Alexandcoats and grtlr authored Nov 9, 2022
1 parent 4fcbbe3 commit bf6bca7
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 47 deletions.
18 changes: 9 additions & 9 deletions docker/assets/grafana/dashboards/analytics_dashboard.json
Original file line number Diff line number Diff line change
Expand Up @@ -1959,7 +1959,7 @@
"options": {
"mode": "exclude",
"names": [
"stardust_claimed_tokens.sum"
"stardust_unclaimed_tokens.sum"
],
"prefix": "All except:",
"readOnly": true
Expand Down Expand Up @@ -1999,7 +1999,7 @@
},
"targets": [
{
"alias": "Claimed Value",
"alias": "Unclaimed Value",
"datasource": {
"type": "influxdb",
"uid": "P951FEA4DE68E13C5"
Expand All @@ -2018,7 +2018,7 @@
"type": "fill"
}
],
"measurement": "stardust_claiming_rewards",
"measurement": "stardust_unclaimed_rewards",
"orderByTime": "ASC",
"policy": "default",
"refId": "A",
Expand All @@ -2027,7 +2027,7 @@
[
{
"params": [
"claimed_value"
"unclaimed_value"
],
"type": "field"
},
Expand All @@ -2040,7 +2040,7 @@
"tags": []
},
{
"alias": "Claimed Count",
"alias": "Unclaimed Count",
"datasource": {
"type": "influxdb",
"uid": "P951FEA4DE68E13C5"
Expand All @@ -2060,7 +2060,7 @@
}
],
"hide": false,
"measurement": "stardust_claiming_rewards",
"measurement": "stardust_unclaimed_rewards",
"orderByTime": "ASC",
"policy": "default",
"refId": "B",
Expand All @@ -2069,7 +2069,7 @@
[
{
"params": [
"claimed_count"
"unclaimed_count"
],
"type": "field"
},
Expand All @@ -2082,7 +2082,7 @@
"tags": []
}
],
"title": "Claimed Tokens",
"title": "Unclaimed Tokens",
"type": "timeseries"
},
{
Expand Down Expand Up @@ -2677,4 +2677,4 @@
"uid": "w6B8aUI4z",
"version": 8,
"weekStart": ""
}
}
14 changes: 7 additions & 7 deletions src/db/collections/analytics/influx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl InfluxDb {
self.insert_analytics(milestone_timestamp, milestone_index, analytics.native_tokens),
self.insert_analytics(milestone_timestamp, milestone_index, analytics.nfts),
self.insert_analytics(milestone_timestamp, milestone_index, analytics.storage_deposits),
self.insert_analytics(milestone_timestamp, milestone_index, analytics.claimed_tokens),
self.insert_analytics(milestone_timestamp, milestone_index, analytics.unclaimed_tokens),
self.insert_analytics(milestone_timestamp, milestone_index, analytics.payload_activity),
self.insert_analytics(milestone_timestamp, milestone_index, analytics.unlock_conditions),
self.insert_analytics(milestone_timestamp, milestone_index, analytics.transaction_activity),
Expand Down Expand Up @@ -168,21 +168,21 @@ impl InfluxDbMeasurement for AnalyticsSchema<LedgerSizeAnalytics> {
const NAME: &'static str = "stardust_ledger_size";
}

impl InfluxDbWriteable for AnalyticsSchema<ClaimedTokensAnalytics> {
impl InfluxDbWriteable for AnalyticsSchema<UnclaimedTokensAnalytics> {
fn into_query<I: Into<String>>(self, name: I) -> influxdb::WriteQuery {
Timestamp::from(self.milestone_timestamp)
.into_query(name)
.add_tag("milestone_index", self.milestone_index)
.add_field("claimed_count", self.data.claimed_count)
.add_field("unclaimed_count", self.data.unclaimed_count)
.add_field(
"claimed_value",
self.data.claimed_value.to_string().parse::<u64>().unwrap(),
"unclaimed_value",
self.data.unclaimed_value.to_string().parse::<u64>().unwrap(),
)
}
}

impl InfluxDbMeasurement for AnalyticsSchema<ClaimedTokensAnalytics> {
const NAME: &'static str = "stardust_claiming_rewards";
impl InfluxDbMeasurement for AnalyticsSchema<UnclaimedTokensAnalytics> {
const NAME: &'static str = "stardust_unclaimed_rewards";
}

impl InfluxDbWriteable for AnalyticsSchema<PayloadActivityAnalytics> {
Expand Down
14 changes: 7 additions & 7 deletions src/db/collections/analytics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub struct Analytics {
pub native_tokens: FoundryActivityAnalytics,
pub nfts: NftActivityAnalytics,
pub storage_deposits: LedgerSizeAnalytics,
pub claimed_tokens: ClaimedTokensAnalytics,
pub unclaimed_tokens: UnclaimedTokensAnalytics,
pub payload_activity: PayloadActivityAnalytics,
pub transaction_activity: TransactionActivityAnalytics,
pub unlock_conditions: UnlockConditionAnalytics,
Expand All @@ -50,7 +50,7 @@ impl MongoDb {
native_tokens,
nfts,
storage_deposits,
claimed_tokens,
unclaimed_tokens,
unlock_conditions,
address_activity,
base_token,
Expand All @@ -64,7 +64,7 @@ impl MongoDb {
output_collection.get_foundry_output_analytics(milestone_index),
output_collection.get_nft_output_analytics(milestone_index),
output_collection.get_ledger_size_analytics(milestone_index),
output_collection.get_claimed_token_analytics(milestone_index),
output_collection.get_unclaimed_token_analytics(milestone_index),
output_collection.get_unlock_condition_analytics(milestone_index),
output_collection.get_address_activity_analytics(milestone_index),
output_collection.get_base_token_activity_analytics(milestone_index),
Expand All @@ -84,7 +84,7 @@ impl MongoDb {
native_tokens,
nfts,
storage_deposits,
claimed_tokens,
unclaimed_tokens,
payload_activity,
transaction_activity,
unlock_conditions,
Expand Down Expand Up @@ -156,9 +156,9 @@ impl LedgerSizeAnalytics {

#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[allow(missing_docs)]
pub struct ClaimedTokensAnalytics {
pub claimed_count: u64,
pub claimed_value: d128,
pub struct UnclaimedTokensAnalytics {
pub unclaimed_count: u64,
pub unclaimed_value: d128,
}

#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
Expand Down
18 changes: 9 additions & 9 deletions src/db/collections/outputs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,8 @@ mod analytics {
db::{
collections::analytics::{
AddressActivityAnalytics, AddressAnalytics, AliasActivityAnalytics, BaseTokenActivityAnalytics,
ClaimedTokensAnalytics, FoundryActivityAnalytics, LedgerOutputAnalytics, LedgerSizeAnalytics,
NftActivityAnalytics, UnlockConditionAnalytics,
FoundryActivityAnalytics, LedgerOutputAnalytics, LedgerSizeAnalytics, NftActivityAnalytics,
UnclaimedTokensAnalytics, UnlockConditionAnalytics,
},
mongodb::MongoDbCollectionExt,
},
Expand Down Expand Up @@ -821,25 +821,25 @@ mod analytics {

/// Gets the number of claimed tokens.
#[tracing::instrument(skip(self), err, level = "trace")]
pub async fn get_claimed_token_analytics(
pub async fn get_unclaimed_token_analytics(
&self,
ledger_index: MilestoneIndex,
) -> Result<ClaimedTokensAnalytics, Error> {
) -> Result<UnclaimedTokensAnalytics, Error> {
Ok(self
.aggregate(
vec![
doc! { "$match": {
"metadata.booked.milestone_index": { "$eq": 0 },
"metadata.spent_metadata.spent.milestone_index": { "$lte": ledger_index },
"metadata.spent_metadata.spent.milestone_index": { "$not": { "$lte": ledger_index } }
} },
doc! { "$group": {
"_id": null,
"claimed_count": { "$sum": 1 },
"claimed_value": { "$sum": { "$toDecimal": "$output.amount" } },
"unclaimed_count": { "$sum": 1 },
"unclaimed_value": { "$sum": { "$toDecimal": "$output.amount" } },
} },
doc! { "$project": {
"claimed_count": 1,
"claimed_value": { "$toString": "$claimed_value" },
"unclaimed_count": 1,
"unclaimed_value": { "$toString": "$unclaimed_value" },
} },
],
None,
Expand Down
30 changes: 15 additions & 15 deletions tests/outputs_claiming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,25 +73,25 @@ mod test_rand {

output_collection.update_spent_outputs(&spent_outputs).await.unwrap();

let claimed = output_collection.get_claimed_token_analytics(1.into()).await.unwrap();
assert_eq!(claimed.claimed_count, 1);
assert_eq!(claimed.claimed_value, d128::from(1));
let unclaimed = output_collection.get_unclaimed_token_analytics(1.into()).await.unwrap();
assert_eq!(unclaimed.unclaimed_count, 4);
assert_eq!(unclaimed.unclaimed_value, d128::from((2..=5).sum::<u32>()));

let claimed = output_collection.get_claimed_token_analytics(2.into()).await.unwrap();
assert_eq!(claimed.claimed_count, 2);
assert_eq!(claimed.claimed_value, d128::from((1..=2).sum::<u32>()));
let unclaimed = output_collection.get_unclaimed_token_analytics(2.into()).await.unwrap();
assert_eq!(unclaimed.unclaimed_count, 3);
assert_eq!(unclaimed.unclaimed_value, d128::from((3..=5).sum::<u32>()));

let claimed = output_collection.get_claimed_token_analytics(3.into()).await.unwrap();
assert_eq!(claimed.claimed_count, 3);
assert_eq!(claimed.claimed_value, d128::from((1..=3).sum::<u32>()));
let unclaimed = output_collection.get_unclaimed_token_analytics(3.into()).await.unwrap();
assert_eq!(unclaimed.unclaimed_count, 2);
assert_eq!(unclaimed.unclaimed_value, d128::from((4..=5).sum::<u32>()));

let claimed = output_collection.get_claimed_token_analytics(4.into()).await.unwrap();
assert_eq!(claimed.claimed_count, 4);
assert_eq!(claimed.claimed_value, d128::from((1..=4).sum::<u32>()));
let unclaimed = output_collection.get_unclaimed_token_analytics(4.into()).await.unwrap();
assert_eq!(unclaimed.unclaimed_count, 1);
assert_eq!(unclaimed.unclaimed_value, d128::from(5));

let claimed = output_collection.get_claimed_token_analytics(5.into()).await.unwrap();
assert_eq!(claimed.claimed_count, 4);
assert_eq!(claimed.claimed_value, d128::from((1..=4).sum::<u32>()));
let unclaimed = output_collection.get_unclaimed_token_analytics(5.into()).await.unwrap();
assert_eq!(unclaimed.unclaimed_count, 1);
assert_eq!(unclaimed.unclaimed_value, d128::from(5));

teardown(db).await;
}
Expand Down

0 comments on commit bf6bca7

Please sign in to comment.