From c3967341b44e38d9fb3a331d2da3b4e474c69387 Mon Sep 17 00:00:00 2001 From: Kirill Ivanov Date: Mon, 9 Dec 2024 19:01:44 +0900 Subject: [PATCH] make total_txns an independent sql using query from backend --- stats/stats/src/charts/counters/total_txns.rs | 57 +++++++++++++++---- 1 file changed, 45 insertions(+), 12 deletions(-) diff --git a/stats/stats/src/charts/counters/total_txns.rs b/stats/stats/src/charts/counters/total_txns.rs index ee2be324c..cd3f9dc1f 100644 --- a/stats/stats/src/charts/counters/total_txns.rs +++ b/stats/stats/src/charts/counters/total_txns.rs @@ -1,19 +1,48 @@ use crate::{ charts::db_interaction::read::query_estimated_table_rows, - data_source::kinds::{ - data_manipulation::{map::MapToString, sum_point::Sum}, - local_db::{parameters::ValueEstimation, DirectPointLocalDbChartSourceWithEstimate}, + data_source::{ + kinds::{ + local_db::{parameters::ValueEstimation, DirectPointLocalDbChartSourceWithEstimate}, + remote_db::{RemoteDatabaseSource, RemoteQueryBehaviour}, + }, + UpdateContext, }, - lines::NewTxnsInt, + range::UniversalRange, types::timespans::DateValue, utils::MarkedDbConnection, ChartError, ChartProperties, MissingDatePolicy, Named, }; use blockscout_db::entity::transactions; -use chrono::{NaiveDate, Utc}; +use chrono::{DateTime, NaiveDate, Utc}; use entity::sea_orm_active_enums::ChartType; -use sea_orm::EntityName; +use sea_orm::{EntityName, EntityTrait, PaginatorTrait, QuerySelect}; + +pub struct TotalTxnsQueryBehaviour; + +impl RemoteQueryBehaviour for TotalTxnsQueryBehaviour { + type Output = DateValue; + + async fn query_data( + cx: &UpdateContext<'_>, + _range: UniversalRange>, + ) -> Result { + let now = cx.time; + let value = transactions::Entity::find() + .select_only() + .count(cx.blockscout.connection.as_ref()) + .await + .map_err(ChartError::BlockscoutDB)?; + + let data = DateValue:: { + timespan: now.date_naive(), + value: value.to_string(), + }; + Ok(data) + } +} + +pub type TotalTxnsRemote = RemoteDatabaseSource; pub struct Properties; @@ -53,11 +82,15 @@ impl ValueEstimation for TotalTxnsEstimation { } } -pub type TotalTxns = DirectPointLocalDbChartSourceWithEstimate< - MapToString>, - TotalTxnsEstimation, - Properties, ->; +// We will need it to update on not fully indexed data soon, therefore this counter is +// separated from `NewTxns`. +// +// Separate query not reliant on previous computation helps this counter to work in such +// environments. +// +// todo: make it dependant again if #845 is resolved +pub type TotalTxns = + DirectPointLocalDbChartSourceWithEstimate; #[cfg(test)] mod tests { @@ -67,7 +100,7 @@ mod tests { #[tokio::test] #[ignore = "needs database to run"] async fn update_total_txns() { - simple_test_counter::("update_total_txns", "47", None).await; + simple_test_counter::("update_total_txns", "48", None).await; } #[tokio::test]