-
Notifications
You must be signed in to change notification settings - Fork 0
feat: status page charts #40
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
Open
pthmas
wants to merge
3
commits into
main
Choose a base branch
from
pthmas/status-page-charts
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,266 @@ | ||
| use axum::{ | ||
| extract::{Query, State}, | ||
| Json, | ||
| }; | ||
| use chrono::Utc; | ||
| use serde::{Deserialize, Serialize}; | ||
| use std::sync::Arc; | ||
|
|
||
| use crate::api::error::ApiResult; | ||
| use crate::api::AppState; | ||
|
|
||
| /// Time window for chart queries. | ||
| #[derive(Deserialize, Default, Clone, Copy)] | ||
| pub enum Window { | ||
| #[serde(rename = "1h")] | ||
| OneHour, | ||
| #[serde(rename = "6h")] | ||
| SixHours, | ||
| #[default] | ||
| #[serde(rename = "24h")] | ||
| TwentyFourHours, | ||
| #[serde(rename = "7d")] | ||
| SevenDays, | ||
| #[serde(rename = "1m")] | ||
| OneMonth, | ||
| #[serde(rename = "6m")] | ||
| SixMonths, | ||
| #[serde(rename = "1y")] | ||
| OneYear, | ||
| } | ||
|
|
||
| impl Window { | ||
| pub fn duration_secs(self) -> i64 { | ||
| match self { | ||
| Window::OneHour => 3_600, | ||
| Window::SixHours => 6 * 3_600, | ||
| Window::TwentyFourHours => 24 * 3_600, | ||
| Window::SevenDays => 7 * 24 * 3_600, | ||
| Window::OneMonth => 30 * 24 * 3_600, | ||
| Window::SixMonths => 180 * 24 * 3_600, | ||
| Window::OneYear => 365 * 24 * 3_600, | ||
| } | ||
| } | ||
|
|
||
| pub fn bucket_secs(self) -> i64 { | ||
| match self { | ||
| Window::OneHour => 300, // 5-min buckets → 12 points | ||
| Window::SixHours => 1_800, // 30-min buckets → 12 points | ||
| Window::TwentyFourHours => 3_600, // 1-hour buckets → 24 points | ||
| Window::SevenDays => 43_200, // 12-hour buckets → 14 points | ||
| Window::OneMonth => 86_400, // 1-day buckets → 30 points | ||
| Window::SixMonths => 7 * 86_400, // 1-week buckets → ~26 points | ||
| Window::OneYear => 14 * 86_400, // 2-week buckets → ~26 points | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Deserialize)] | ||
| pub struct WindowQuery { | ||
| #[serde(default)] | ||
| pub window: Window, | ||
| } | ||
|
|
||
| #[derive(Serialize)] | ||
| pub struct BlockChartPoint { | ||
| pub bucket: String, | ||
| pub tx_count: i64, | ||
| pub avg_gas_used: f64, | ||
| } | ||
|
|
||
| #[derive(Serialize)] | ||
| pub struct DailyTxPoint { | ||
| pub day: String, | ||
| pub tx_count: i64, | ||
| } | ||
|
|
||
| #[derive(Serialize)] | ||
| pub struct GasPricePoint { | ||
| pub bucket: String, | ||
| pub avg_gas_price: f64, | ||
| } | ||
|
|
||
| /// GET /api/stats/blocks-chart?window=1h|6h|24h|7d | ||
| /// | ||
| /// Returns tx count and avg gas utilization bucketed over the given window. | ||
| /// Both metrics come from the `blocks` table so a single query serves both charts. | ||
| /// The window is anchored to the latest indexed block timestamp (not NOW()) so | ||
| /// charts show data even when the indexer is behind the live chain head. | ||
| pub async fn get_blocks_chart( | ||
| State(state): State<Arc<AppState>>, | ||
| Query(params): Query<WindowQuery>, | ||
| ) -> ApiResult<Json<Vec<BlockChartPoint>>> { | ||
| let window = params.window; | ||
| let bucket_secs = window.bucket_secs(); | ||
|
|
||
| // Anchor to the latest indexed block timestamp, not wall-clock NOW(). | ||
| // This ensures charts always show data regardless of how far the indexer | ||
| // is behind the live chain head. | ||
| let rows: Vec<(chrono::DateTime<Utc>, i64, f64)> = sqlx::query_as( | ||
| r#" | ||
| WITH latest AS (SELECT MAX(timestamp) AS max_ts FROM blocks), | ||
| bounds AS ( | ||
| SELECT | ||
| max_ts - $2 AS start_ts, | ||
| max_ts AS end_ts | ||
| FROM latest | ||
| ), | ||
| agg AS ( | ||
| SELECT | ||
| (b.start_ts + (((blocks.timestamp - b.start_ts) / $1) * $1))::bigint AS bucket_ts, | ||
| SUM(transaction_count)::bigint AS tx_count, | ||
| COALESCE(AVG(gas_used::float8), 0.0) AS avg_gas_used | ||
| FROM blocks | ||
| CROSS JOIN bounds b | ||
| WHERE blocks.timestamp >= b.start_ts | ||
| AND blocks.timestamp <= b.end_ts | ||
| GROUP BY 1 | ||
| ) | ||
| SELECT | ||
| to_timestamp(gs::float8) AS bucket, | ||
| COALESCE(a.tx_count, 0)::bigint AS tx_count, | ||
| COALESCE(a.avg_gas_used, 0.0) AS avg_gas_used | ||
| FROM bounds b | ||
| CROSS JOIN generate_series(b.start_ts, b.end_ts - $1, $1::bigint) AS gs | ||
| LEFT JOIN agg a ON a.bucket_ts = gs | ||
| ORDER BY gs ASC | ||
| "#, | ||
| ) | ||
| .bind(bucket_secs) | ||
| .bind(window.duration_secs()) | ||
| .fetch_all(&state.pool) | ||
| .await?; | ||
|
|
||
| let points = rows | ||
| .into_iter() | ||
| .map(|(bucket, tx_count, avg_gas_used)| BlockChartPoint { | ||
| bucket: bucket.to_rfc3339(), | ||
| tx_count, | ||
| avg_gas_used, | ||
| }) | ||
| .collect(); | ||
|
|
||
| Ok(Json(points)) | ||
| } | ||
|
|
||
| /// GET /api/stats/daily-txs | ||
| /// | ||
| /// Returns transaction counts per day for the last 14 days. Fixed window. | ||
| pub async fn get_daily_txs( | ||
| State(state): State<Arc<AppState>>, | ||
| ) -> ApiResult<Json<Vec<DailyTxPoint>>> { | ||
| let rows: Vec<(String, i64)> = sqlx::query_as( | ||
| r#" | ||
| WITH latest AS (SELECT MAX(timestamp) AS max_ts FROM transactions) | ||
| SELECT | ||
| to_char(to_timestamp(timestamp)::date, 'YYYY-MM-DD') AS day, | ||
| COUNT(*)::bigint AS tx_count | ||
| FROM transactions, latest | ||
| WHERE timestamp >= max_ts - (14 * 86400) | ||
| GROUP BY to_timestamp(timestamp)::date | ||
| ORDER BY to_timestamp(timestamp)::date ASC | ||
| "#, | ||
| ) | ||
| .fetch_all(&state.pool) | ||
| .await?; | ||
|
|
||
| let points = rows | ||
| .into_iter() | ||
| .map(|(day, tx_count)| DailyTxPoint { day, tx_count }) | ||
| .collect(); | ||
|
|
||
| Ok(Json(points)) | ||
| } | ||
|
|
||
| /// GET /api/stats/gas-price?window=1h|6h|24h|7d | ||
| /// | ||
| /// Returns average gas price (in wei) per bucket over the given window. | ||
| /// Anchored to the latest indexed transaction timestamp (not NOW()). | ||
| pub async fn get_gas_price_chart( | ||
| State(state): State<Arc<AppState>>, | ||
| Query(params): Query<WindowQuery>, | ||
| ) -> ApiResult<Json<Vec<GasPricePoint>>> { | ||
| let window = params.window; | ||
| let bucket_secs = window.bucket_secs(); | ||
|
|
||
| let rows: Vec<(chrono::DateTime<Utc>, Option<f64>)> = sqlx::query_as( | ||
| r#" | ||
| WITH latest AS (SELECT MAX(timestamp) AS max_ts FROM blocks), | ||
| bounds AS ( | ||
| SELECT | ||
| max_ts - $2 AS start_ts, | ||
| max_ts AS end_ts | ||
| FROM latest | ||
| ), | ||
| agg AS ( | ||
| SELECT | ||
| (b.start_ts + (((transactions.timestamp - b.start_ts) / $1) * $1))::bigint AS bucket_ts, | ||
| AVG(gas_price::float8) AS avg_gas_price | ||
| FROM transactions | ||
| CROSS JOIN bounds b | ||
| WHERE transactions.timestamp >= b.start_ts | ||
| AND transactions.timestamp <= b.end_ts | ||
| AND gas_price > 0 | ||
| GROUP BY 1 | ||
| ) | ||
| SELECT | ||
| to_timestamp(gs::float8) AS bucket, | ||
| a.avg_gas_price | ||
| FROM bounds b | ||
| CROSS JOIN generate_series(b.start_ts, b.end_ts - $1, $1::bigint) AS gs | ||
| LEFT JOIN agg a ON a.bucket_ts = gs | ||
| ORDER BY gs ASC | ||
| "#, | ||
| ) | ||
| .bind(bucket_secs) | ||
| .bind(window.duration_secs()) | ||
| .fetch_all(&state.pool) | ||
| .await?; | ||
|
|
||
| let points = rows | ||
| .into_iter() | ||
| .map(|(bucket, avg_gas_price)| GasPricePoint { | ||
| bucket: bucket.to_rfc3339(), | ||
| avg_gas_price: avg_gas_price.unwrap_or(0.0), | ||
| }) | ||
| .collect(); | ||
|
|
||
| Ok(Json(points)) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn window_duration_secs() { | ||
| assert_eq!(Window::OneHour.duration_secs(), 3_600); | ||
| assert_eq!(Window::SixHours.duration_secs(), 6 * 3_600); | ||
| assert_eq!(Window::TwentyFourHours.duration_secs(), 24 * 3_600); | ||
| assert_eq!(Window::SevenDays.duration_secs(), 7 * 24 * 3_600); | ||
| } | ||
|
|
||
| #[test] | ||
| fn window_bucket_secs_gives_reasonable_point_counts() { | ||
| // Each window should yield ~12-28 data points | ||
| for (window, expected_points) in [ | ||
| (Window::OneHour, 12), | ||
| (Window::SixHours, 12), | ||
| (Window::TwentyFourHours, 24), | ||
| (Window::SevenDays, 14), | ||
| (Window::OneMonth, 30), | ||
| (Window::SixMonths, 25), | ||
| (Window::OneYear, 26), | ||
| ] { | ||
| let points = window.duration_secs() / window.bucket_secs(); | ||
| assert_eq!(points, expected_points); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn gas_price_window_supports_7d() { | ||
| // SevenDays is now supported for gas price queries | ||
| assert_eq!(Window::SevenDays.duration_secs(), 7 * 24 * 3_600); | ||
| assert_eq!(Window::SevenDays.bucket_secs(), 43_200); | ||
| } | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.