-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
7 changed files
with
238 additions
and
2 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
...nd-rust/.sqlx/query-163cc788cf5c4fd22a1f8e0289ad322fa7d92a7dd503a03a517622f2942c4d60.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
35 changes: 35 additions & 0 deletions
35
...nd-rust/.sqlx/query-792aeb42f25e1f70758407b469945187b00c166757ad7f9324abf3f58ac1e821.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
22 changes: 22 additions & 0 deletions
22
...nd-rust/.sqlx/query-d6bccf668d544b1649962a5f0fb358bada7fbdbb4f78de19cc2ab8d0be5a83d5.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |