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): Back3nd chart5 Migration (Part 2: yesterday's transactions) #1120

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions stats/stats/src/charts/counters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod total_native_coin_transfers;
mod total_tokens;
mod total_txns;
mod total_verified_contracts;
mod yesterday_txns;

#[cfg(test)]
mod mock;
Expand All @@ -28,6 +29,7 @@ pub use total_native_coin_transfers::TotalNativeCoinTransfers;
pub use total_tokens::TotalTokens;
pub use total_txns::TotalTxns;
pub use total_verified_contracts::TotalVerifiedContracts;
pub use yesterday_txns::YesterdayTxns;

#[cfg(test)]
pub use mock::MockCounter;
94 changes: 94 additions & 0 deletions stats/stats/src/charts/counters/yesterday_txns.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
use std::ops::Range;

use crate::{
data_source::{
kinds::{
local_db::DirectPointLocalDbChartSource,
remote_db::{RemoteDatabaseSource, RemoteQueryBehaviour, StatementFromRange},
},
UpdateContext,
},
lines::NewTxnsStatement,
types::TimespanValue,
utils::day_start,
ChartProperties, MissingDatePolicy, Named, UpdateError,
};
use chrono::{DateTime, Days, NaiveDate, Utc};
use entity::sea_orm_active_enums::ChartType;
use sea_orm::FromQueryResult;

pub struct YesterdayTxnsQuery;

impl RemoteQueryBehaviour for YesterdayTxnsQuery {
type Output = TimespanValue<NaiveDate, String>;

async fn query_data(
cx: &UpdateContext<'_>,
_range: Option<Range<DateTime<Utc>>>,
) -> Result<Self::Output, UpdateError> {
let today = cx.time.date_naive();
let yesterday = today
.checked_sub_days(Days::new(1))
.ok_or(UpdateError::Internal(
"Update time is incorrect: ~ minimum possible date".into(),
))?;
let yesterday_range = day_start(&yesterday)..day_start(&today);
let query = NewTxnsStatement::get_statement(
Some(yesterday_range),
&cx.blockscout_applied_migrations,
);
let data = Self::Output::find_by_statement(query)
.one(cx.blockscout)
.await
.map_err(UpdateError::BlockscoutDB)?
// no transactions for yesterday
.unwrap_or(TimespanValue::with_zero_value(yesterday));
Ok(data)
}
}

pub type YesterdayTxnsRemote = RemoteDatabaseSource<YesterdayTxnsQuery>;

pub struct Properties;

impl Named for Properties {
fn name() -> String {
"yesterdayTxns".into()
}
}

impl ChartProperties for Properties {
type Resolution = NaiveDate;

fn chart_type() -> ChartType {
ChartType::Counter
}
fn missing_date_policy() -> MissingDatePolicy {
MissingDatePolicy::FillPrevious
bragov4ik marked this conversation as resolved.
Show resolved Hide resolved
}
}

pub type YesterdayTxns = DirectPointLocalDbChartSource<YesterdayTxnsRemote, Properties>;

#[cfg(test)]
mod tests {
use super::*;
use crate::tests::{point_construction::dt, simple_test::simple_test_counter};

#[tokio::test]
#[ignore = "needs database to run"]
async fn update_yesterday_txns_1() {
simple_test_counter::<YesterdayTxns>("update_yesterday_txns_1", "0", None).await;
}

#[tokio::test]
#[ignore = "needs database to run"]
async fn update_yesterday_txns_2() {
simple_test_counter::<YesterdayTxns>(
"update_yesterday_txns_2",
"12",
Some(dt("2022-11-11T00:00:00")),
)
.await;
}
}
1 change: 1 addition & 0 deletions stats/stats/src/charts/lines/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ pub use new_native_coin_transfers::{
NewNativeCoinTransfers, NewNativeCoinTransfersInt, NewNativeCoinTransfersMonthly,
NewNativeCoinTransfersWeekly, NewNativeCoinTransfersYearly,
};
pub(crate) use new_txns::NewTxnsStatement;
bragov4ik marked this conversation as resolved.
Show resolved Hide resolved
pub use new_txns::{NewTxns, NewTxnsInt, NewTxnsMonthly, NewTxnsWeekly, NewTxnsYearly};
pub use new_verified_contracts::{
NewVerifiedContracts, NewVerifiedContractsMonthly, NewVerifiedContractsWeekly,
Expand Down
Loading