Skip to content

Commit

Permalink
Account metrics (#285)
Browse files Browse the repository at this point in the history
* Account metrics

* Coalesce the max
  • Loading branch information
Victor-N-Suadicani authored Nov 6, 2024
1 parent fd09fa2 commit 28aa9e2
Show file tree
Hide file tree
Showing 7 changed files with 238 additions and 2 deletions.

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

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

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

3 changes: 3 additions & 0 deletions backend-rust/migrations/0001_initialize.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ CREATE TABLE accounts(
-- credential_registration_id
);

-- Important for performance when joining accounts with its associated creation block.
CREATE INDEX accounts_created_block_idx ON accounts (created_block);

-- Add foreign key constraint now that the account table is created.
ALTER TABLE transactions
ADD CONSTRAINT fk_transaction_sender
Expand Down
5 changes: 3 additions & 2 deletions backend-rust/src/graphql_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#![allow(unused_variables)]

mod account_metrics;
mod transaction_metrics;

// TODO remove this macro, when done with first iteration
Expand All @@ -15,6 +16,7 @@ macro_rules! todo_api {
};
}

use account_metrics::AccountMetricsQuery;
use anyhow::Context as _;
use async_graphql::{
http::GraphiQLSource,
Expand Down Expand Up @@ -63,7 +65,7 @@ pub struct ApiServiceConfig {
}

#[derive(MergedObject, Default)]
pub struct Query(BaseQuery, TransactionMetricsQuery);
pub struct Query(BaseQuery, AccountMetricsQuery, TransactionMetricsQuery);

pub struct Service {
pub schema: Schema<Query, EmptyMutation, Subscription>,
Expand Down Expand Up @@ -754,7 +756,6 @@ LIMIT 30", // WHERE slot_time > (LOCALTIMESTAMP - $1::interval)
})
}

// accountsMetrics(period: MetricsPeriod!): AccountsMetrics
// bakerMetrics(period: MetricsPeriod!): BakerMetrics!
// rewardMetrics(period: MetricsPeriod!): RewardMetrics!
// rewardMetricsForAccount(accountId: ID! period: MetricsPeriod!):
Expand Down
113 changes: 113 additions & 0 deletions backend-rust/src/graphql_api/account_metrics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
use std::sync::Arc;

use async_graphql::{Context, Object, SimpleObject};
use sqlx::postgres::types::PgInterval;

use super::{get_pool, ApiError, ApiResult, DateTime, MetricsPeriod, TimeSpan};

#[derive(SimpleObject)]
struct AccountMetrics {
/// Total number of accounts created (all time).
last_cumulative_accounts_created: i64,

/// Total number of accounts created in requested period.
accounts_created: i64,

buckets: AccountMetricsBuckets,
}

#[derive(SimpleObject)]
struct AccountMetricsBuckets {
/// The width (time interval) of each bucket.
bucket_width: TimeSpan,

/// Start of the bucket time period. Intended x-axis value.
#[graphql(name = "x_Time")]
x_time: Vec<DateTime>,

/// Total number of accounts created (all time) at the end of the bucket
/// period. Intended y-axis value.
#[graphql(name = "y_LastCumulativeAccountsCreated")]
y_last_cumulative_accounts_created: Vec<i64>,

/// Number of accounts created within bucket time period. Intended y-axis
/// value.
#[graphql(name = "y_AccountsCreated")]
y_accounts_created: Vec<i64>,
}

#[derive(Default)]
pub(crate) struct AccountMetricsQuery;

#[Object]
impl AccountMetricsQuery {
async fn account_metrics(
&self,
ctx: &Context<'_>,
period: MetricsPeriod,
) -> ApiResult<AccountMetrics> {
let pool = get_pool(ctx)?;

let last_cumulative_accounts_created =
sqlx::query_scalar!("SELECT COALESCE(MAX(index), 0) FROM accounts")
.fetch_one(pool)
.await?
.expect("coalesced");

// The full period interval, e.g. 7 days.
let period_interval: PgInterval = period
.as_duration()
.try_into()
.map_err(|e| ApiError::DurationOutOfRange(Arc::new(e)))?;

let cumulative_accounts_created_before_period = sqlx::query_scalar!(
"SELECT COALESCE(MAX(index), 0)
FROM accounts
LEFT JOIN blocks ON created_block = height
WHERE slot_time < (now() - $1::interval)",
period_interval,
)
.fetch_one(pool)
.await?
.expect("coalesced");

let accounts_created =
last_cumulative_accounts_created - cumulative_accounts_created_before_period;

let bucket_width = period.bucket_width();

// The bucket interval, e.g. 6 hours.
let bucket_interval: PgInterval =
bucket_width.try_into().map_err(|err| ApiError::DurationOutOfRange(Arc::new(err)))?;

let rows = sqlx::query_file!(
"src/graphql_api/account_metrics.sql",
period_interval,
bucket_interval,
)
.fetch_all(pool)
.await?;

let x_time = rows
.iter()
.map(|r| r.bucket_time.expect("generated by generate_series so never null"))
.collect();
let y_last_cumulative_accounts_created =
rows.iter().map(|r| r.end_index.expect("coalesced")).collect();
let y_accounts_created = rows
.iter()
.map(|r| r.end_index.expect("coalesced") - r.start_index.expect("coalesced"))
.collect();

Ok(AccountMetrics {
last_cumulative_accounts_created,
accounts_created,
buckets: AccountMetricsBuckets {
bucket_width: TimeSpan(bucket_width),
x_time,
y_last_cumulative_accounts_created,
y_accounts_created,
},
})
}
}
42 changes: 42 additions & 0 deletions backend-rust/src/graphql_api/account_metrics.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
-- Counts accounts in buckets by counting the cumulative total number of
-- accounts (i.e. the account index) at or before (i.e. <=) the start of the
-- bucket and the same number just before (i.e. <) the next bucket. The
-- difference between the two numbers should give the total number of accounts
-- created within the bucket.
SELECT
-- The bucket time is the starting time of the bucket.
bucket_time,
-- Number of accounts at or before the bucket.
COALESCE(before_bucket.index, 0) as start_index,
-- Number of accounts at the end of the bucket.
COALESCE(after_bucket.index, 0) as end_index
FROM
-- We generate a time series of all the buckets where accounts will be counted.
-- $1 is the full period, $2 is the bucket interval.
-- For the rest of the comments, let's go with the example of a full period of 7 days with 6 hour buckets.
generate_series(
-- The first bucket starts 7 days ago.
now() - $1::interval,
-- The final bucket starts 6 hours ago, since the bucket time is the start of the bucket.
now() - $2::interval,
-- Each bucket is seperated by 6 hours.
$2::interval
) AS bucket_time
LEFT JOIN LATERAL (
-- Selects the index at or before the start of the bucket.
SELECT index
FROM accounts
LEFT JOIN blocks ON created_block = height
WHERE slot_time <= bucket_time
ORDER BY slot_time DESC
LIMIT 1
) before_bucket ON true
LEFT JOIN LATERAL (
-- Selects the index at the end of the bucket.
SELECT index
FROM accounts
LEFT JOIN blocks ON created_block = height
WHERE slot_time < bucket_time + $2::interval
ORDER BY slot_time DESC
LIMIT 1
) after_bucket ON true

0 comments on commit 28aa9e2

Please sign in to comment.