From d3060b3fdfda930fccc6d6db253fc8f7403e6d78 Mon Sep 17 00:00:00 2001 From: Kirill Ivanov Date: Mon, 9 Dec 2024 20:56:13 +0900 Subject: [PATCH] adapt total_addresses impl to blockscout --- stats/config/update_groups.json | 2 +- .../stats-server/tests/it/indexing_status.rs | 2 +- .../src/charts/counters/total_addresses.rs | 49 ++++++++++++++++--- 3 files changed, 45 insertions(+), 8 deletions(-) diff --git a/stats/config/update_groups.json b/stats/config/update_groups.json index 8b08037b8..037f4740b 100644 --- a/stats/config/update_groups.json +++ b/stats/config/update_groups.json @@ -3,7 +3,7 @@ "active_accounts_group": "0 0 4 * * * *", "average_block_time_group": "0 */10 * * * * *", "completed_txns_group": "0 5 */3 * * * *", - "total_addresses_group": "0 0 */3 * * * *", + "total_addresses_group": "0 0,30 * * * * *", "total_blocks_group": "0 0 */2 * * * *", "total_tokens_group": "0 0 18 * * * *", "total_txns_group": "0 5 */2 * * * *", diff --git a/stats/stats-server/tests/it/indexing_status.rs b/stats/stats-server/tests/it/indexing_status.rs index 628b45b62..1a108bb8c 100644 --- a/stats/stats-server/tests/it/indexing_status.rs +++ b/stats/stats-server/tests/it/indexing_status.rs @@ -91,6 +91,6 @@ async fn test_not_indexed_ok() { // so they are returned even without calling an update assert_eq!( counters.counters.into_iter().map(|c| c.id).collect_vec(), - vec!["totalBlocks", "totalTxns"] + vec!["totalAddresses", "totalBlocks", "totalTxns"] ) } diff --git a/stats/stats/src/charts/counters/total_addresses.rs b/stats/stats/src/charts/counters/total_addresses.rs index 00513c1a3..3a18a9e9e 100644 --- a/stats/stats/src/charts/counters/total_addresses.rs +++ b/stats/stats/src/charts/counters/total_addresses.rs @@ -1,16 +1,20 @@ use crate::{ + charts::db_interaction::read::query_estimated_table_rows, data_source::{ kinds::{ - local_db::DirectPointLocalDbChartSource, + local_db::{parameters::ValueEstimation, DirectPointLocalDbChartSourceWithEstimate}, remote_db::{PullOne, RemoteDatabaseSource, StatementForOne}, }, types::BlockscoutMigrations, }, - ChartProperties, MissingDatePolicy, Named, + types::timespans::DateValue, + utils::MarkedDbConnection, + ChartError, ChartProperties, MissingDatePolicy, Named, }; -use chrono::NaiveDate; +use blockscout_db::entity::addresses; +use chrono::{NaiveDate, Utc}; use entity::sea_orm_active_enums::ChartType; -use sea_orm::{DbBackend, Statement}; +use sea_orm::{DbBackend, EntityName, Statement}; pub struct TotalAddressesStatement; @@ -57,16 +61,49 @@ impl ChartProperties for Properties { } } -pub type TotalAddresses = DirectPointLocalDbChartSource; +pub struct TotalAddressesEstimation; + +impl ValueEstimation for TotalAddressesEstimation { + async fn estimate(blockscout: &MarkedDbConnection) -> Result, ChartError> { + let now = Utc::now(); + let value = query_estimated_table_rows( + blockscout.connection.as_ref(), + addresses::Entity.table_name(), + ) + .await + .map_err(ChartError::BlockscoutDB)? + .map(|n| u64::try_from(n).unwrap_or(0)) + .unwrap_or(0); + Ok(DateValue { + timespan: now.date_naive(), + value: value.to_string(), + }) + } +} + +pub type TotalAddresses = DirectPointLocalDbChartSourceWithEstimate< + TotalAddressesRemote, + TotalAddressesEstimation, + Properties, +>; #[cfg(test)] mod tests { use super::*; - use crate::tests::simple_test::simple_test_counter; + use crate::{ + counters::TotalTxns, + tests::simple_test::{simple_test_counter, test_counter_fallback}, + }; #[tokio::test] #[ignore = "needs database to run"] async fn update_total_addresses() { simple_test_counter::("update_total_addresses", "33", None).await; } + + #[tokio::test] + #[ignore = "needs database to run"] + async fn total_addresses_fallback() { + test_counter_fallback::("total_addresses_fallback").await; + } }