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(stats): Backend charts & Migration 6: Transactions page #1158

Merged
merged 17 commits into from
Dec 25, 2024
Merged
4 changes: 2 additions & 2 deletions stats/Cargo.lock

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

2 changes: 1 addition & 1 deletion stats/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ members = [

[workspace.dependencies]
blockscout-client = { git = "https://github.com/blockscout/blockscout-rs/", rev = "506b821" }
blockscout-service-launcher = { version = "0.13.1" }
blockscout-service-launcher = { version = "0.15.0" }
rstest = "0.23.0"
trait-variant = "0.1.2"
wiremock = "0.6.2"
Expand Down
18 changes: 18 additions & 0 deletions stats/config/charts.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,24 @@
"description": "Average time taken in seconds for a block to be included in the blockchain",
"units": "s"
},
"new_txns_24h": {
"title": "Transactions",
"description": "Number of new transactions within last 24 hours"
},
"pending_txns": {
"title": "Pending transactions",
"description": " "
},
bragov4ik marked this conversation as resolved.
Show resolved Hide resolved
"txns_fee_24h": {
"title": "Transactions fees",
"description": "Sum of {{native_coin_symbol}} spent on gas fees within last 24 hours",
"units": "{{native_coin_symbol}}"
},
"average_txn_fee_24h": {
"title": "Avg. transaction fee",
"description": "Average amount of {{native_coin_symbol}} spent on gas fees per transaction within last 24 hours",
"units": "{{native_coin_symbol}}"
},
"completed_txns": {
"title": "Completed txns",
"description": "Number of transactions with success status"
Expand Down
6 changes: 5 additions & 1 deletion stats/config/layout.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
"total_txns",
"total_operational_txns",
"total_verified_contracts",
"yesterday_txns"
"yesterday_txns",
"new_txns_24h",
"pending_txns",
"txns_fee_24h",
"average_txn_fee_24h"
],
"line_chart_categories": [
{
Expand Down
4 changes: 3 additions & 1 deletion stats/config/update_groups.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"active_accounts_group": "0 0 4 * * * *",
"average_block_time_group": "0 */10 * * * * *",
"completed_txns_group": "0 5 */3 * * * *",
"pending_txns_group": "0 */5 * * * * *",
"total_addresses_group": "0 0,30 * * * * *",
"total_blocks_group": "0 0 */2 * * * *",
"total_tokens_group": "0 0 18 * * * *",
Expand Down Expand Up @@ -37,6 +38,7 @@
"new_txns_group": "0 10 */3 * * * *",
"new_verified_contracts_group": "0 30 */3 * * * *",
"native_coin_holders_growth_group": "0 0 7,17,22 * * * *",
"new_native_coin_transfers_group": "0 0 3,13 * * * *"
"new_native_coin_transfers_group": "0 0 3,13 * * * *",
"txns_stats_24h_group": "0 30 * * * * *"
}
}
14 changes: 7 additions & 7 deletions stats/stats-server/src/read_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,30 @@ use async_trait::async_trait;
use chrono::{DateTime, NaiveDate, Utc};
use futures::{stream::FuturesOrdered, StreamExt};
use proto_v1::stats_service_server::StatsService;
use sea_orm::DbErr;
use sea_orm::{DatabaseConnection, DbErr};
use stats::{
data_source::{types::BlockscoutMigrations, UpdateContext, UpdateParameters},
query_dispatch::{CounterHandle, LineHandle, QuerySerializedDyn},
range::UniversalRange,
types::Timespan,
utils::{day_start, MarkedDbConnection},
utils::day_start,
ChartError, RequestedPointsLimit, ResolutionKind,
};
use stats_proto::blockscout::stats::v1 as proto_v1;
use tonic::{Request, Response, Status};

#[derive(Clone)]
pub struct ReadService {
db: MarkedDbConnection,
blockscout: MarkedDbConnection,
db: Arc<DatabaseConnection>,
blockscout: Arc<DatabaseConnection>,
charts: Arc<RuntimeSetup>,
limits: ReadLimits,
}

impl ReadService {
pub async fn new(
db: MarkedDbConnection,
blockscout: MarkedDbConnection,
db: Arc<DatabaseConnection>,
blockscout: Arc<DatabaseConnection>,
charts: Arc<RuntimeSetup>,
limits: ReadLimits,
) -> Result<Self, DbErr> {
Expand Down Expand Up @@ -104,7 +104,7 @@ impl ReadService {
points_limit: Option<RequestedPointsLimit>,
query_time: DateTime<Utc>,
) -> Result<Data, ChartError> {
let migrations = BlockscoutMigrations::query_from_db(self.blockscout.connection.as_ref())
let migrations = BlockscoutMigrations::query_from_db(&self.blockscout)
.await
.map_err(ChartError::BlockscoutDB)?;
let context = UpdateContext::from_params_now_or_override(UpdateParameters {
Expand Down
2 changes: 2 additions & 0 deletions stats/stats-server/src/runtime_setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ impl RuntimeSetup {
Arc::new(ActiveAccountsGroup),
Arc::new(AverageBlockTimeGroup),
Arc::new(CompletedTxnsGroup),
Arc::new(PendingTxnsGroup),
Arc::new(TotalAddressesGroup),
Arc::new(TotalBlocksGroup),
Arc::new(TotalTokensGroup),
Expand Down Expand Up @@ -294,6 +295,7 @@ impl RuntimeSetup {
Arc::new(NewVerifiedContractsGroup),
Arc::new(NativeCoinHoldersGrowthGroup),
Arc::new(NewNativeCoinTransfersGroup),
Arc::new(TxnsStats24hGroup),
]
}

Expand Down
12 changes: 4 additions & 8 deletions stats/stats-server/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use anyhow::Context;
use blockscout_endpoint_swagger::route_swagger;
use blockscout_service_launcher::launcher::{self, LaunchSettings};
use sea_orm::{ConnectOptions, Database};
use stats::{metrics, utils::MarkedDbConnection};
use stats::metrics;
use stats_proto::blockscout::stats::v1::{
health_actix::route_health,
health_server::HealthServer,
Expand Down Expand Up @@ -80,9 +80,7 @@ pub async fn stats(mut settings: Settings) -> Result<(), anyhow::Error> {
settings.run_migrations,
)
.await?;
let db = MarkedDbConnection::main_connection(Arc::new(
Database::connect(opt).await.context("stats DB")?,
));
let db = Arc::new(Database::connect(opt).await.context("stats DB")?);

let mut opt = ConnectOptions::new(settings.blockscout_db_url.clone());
opt.sqlx_logging_level(tracing::log::LevelFilter::Debug);
Expand All @@ -92,9 +90,7 @@ pub async fn stats(mut settings: Settings) -> Result<(), anyhow::Error> {
tracing::log::LevelFilter::Warn,
Duration::from_secs(3600),
);
let blockscout = MarkedDbConnection::main_connection(Arc::new(
Database::connect(opt).await.context("blockscout DB")?,
));
let blockscout = Arc::new(Database::connect(opt).await.context("blockscout DB")?);

let charts = Arc::new(RuntimeSetup::new(
charts_config,
Expand All @@ -106,7 +102,7 @@ pub async fn stats(mut settings: Settings) -> Result<(), anyhow::Error> {
for group_entry in charts.update_groups.values() {
group_entry
.group
.create_charts_with_mutexes(db.connection.as_ref(), None, &group_entry.enabled_members)
.create_charts_with_mutexes(&db, None, &group_entry.enabled_members)
.await?;
}

Expand Down
26 changes: 11 additions & 15 deletions stats/stats-server/src/update_service.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
use crate::runtime_setup::{RuntimeSetup, UpdateGroupEntry};
use chrono::Utc;
use cron::Schedule;
use sea_orm::DbErr;
use stats::{
data_source::types::{BlockscoutMigrations, UpdateParameters},
utils::MarkedDbConnection,
};
use sea_orm::{DatabaseConnection, DbErr};
use stats::data_source::types::{BlockscoutMigrations, UpdateParameters};
use std::sync::Arc;
use tokio::task::JoinHandle;

const FAILED_UPDATERS_UNTIL_PANIC: u64 = 3;

pub struct UpdateService {
db: MarkedDbConnection,
blockscout: MarkedDbConnection,
db: Arc<DatabaseConnection>,
blockscout: Arc<DatabaseConnection>,
charts: Arc<RuntimeSetup>,
}

Expand All @@ -29,8 +26,8 @@ fn time_till_next_call(schedule: &Schedule) -> std::time::Duration {

impl UpdateService {
pub async fn new(
db: MarkedDbConnection,
blockscout: MarkedDbConnection,
db: Arc<DatabaseConnection>,
blockscout: Arc<DatabaseConnection>,
charts: Arc<RuntimeSetup>,
) -> Result<Self, DbErr> {
Ok(Self {
Expand Down Expand Up @@ -107,12 +104,11 @@ impl UpdateService {
force_update = force_full,
"updating group of charts"
);
let Ok(active_migrations) =
BlockscoutMigrations::query_from_db(self.blockscout.connection.as_ref())
.await
.inspect_err(|err| {
tracing::error!("error during blockscout migrations detection: {:?}", err)
})
let Ok(active_migrations) = BlockscoutMigrations::query_from_db(&self.blockscout)
.await
.inspect_err(|err| {
tracing::error!("error during blockscout migrations detection: {:?}", err)
})
bragov4ik marked this conversation as resolved.
Show resolved Hide resolved
else {
return;
};
Expand Down
4 changes: 4 additions & 0 deletions stats/stats-server/tests/it/counters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ async fn test_counters_ok() {
"totalTxns",
"totalVerifiedContracts",
"yesterdayTxns",
"newTxns24h",
"pendingTxns",
"txnsFee24h",
"averageTxnFee24h",
]
.into_iter()
.collect();
Expand Down
5 changes: 3 additions & 2 deletions stats/stats/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,23 @@ rust_decimal = "1.27"
# Dependencies for test-utils only
pretty_assertions = { version= "1.2", optional = true }
tracing-subscriber = { version = "0.3", features = ["env-filter"], optional = true }
blockscout-service-launcher = { version = "0.13.1", features = [ "database-0_12", "test-database" ], optional = true }
blockscout-service-launcher = { workspace = true, features = [ "database-0_12", "test-database" ], optional = true }
wiremock = { workspace = true, optional = true }

[dev-dependencies]
sea-orm = { version = "0.12", features = [
"sqlx-postgres",
"sqlx-sqlite",
"runtime-tokio-rustls",
"mock"
] }
rust_decimal = "1.27"
rust_decimal_macros = "1.27"

# test-utils
pretty_assertions = "1.2"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
blockscout-service-launcher = { version = "0.13.1", features = [ "database-0_12", "test-database" ] }
blockscout-service-launcher = { workspace = true, features = [ "database-0_12", "test-database" ] }
wiremock = { workspace = true }

[features]
Expand Down
7 changes: 3 additions & 4 deletions stats/stats/src/charts/counters/average_block_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ async fn query_average_block_time(
) -> Result<Option<TimespanValue<NaiveDate, f64>>, ChartError> {
let query = average_block_time_statement(offset);
let block_timestamps = BlockTimestamp::find_by_statement(query)
.all(cx.blockscout.connection.as_ref())
.all(cx.blockscout)
.await
.map_err(ChartError::BlockscoutDB)?;
Ok(calculate_average_block_time(block_timestamps))
Expand Down Expand Up @@ -149,7 +149,6 @@ mod tests {
mock_blockscout::fill_many_blocks,
simple_test::{get_counter, prepare_chart_test, simple_test_counter},
},
utils::MarkedDbConnection,
};

#[tokio::test]
Expand Down Expand Up @@ -198,8 +197,8 @@ mod tests {
};
fill_many_blocks(&blockscout, current_time.naive_utc(), &block_times).await;
let mut parameters = UpdateParameters {
db: &MarkedDbConnection::from_test_db(&db).unwrap(),
blockscout: &MarkedDbConnection::from_test_db(&blockscout).unwrap(),
db: &db,
blockscout: &blockscout,
blockscout_applied_migrations: BlockscoutMigrations::latest(),
update_time_override: Some(current_time),
force_full: true,
Expand Down
Loading
Loading