Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation
Rust adyenbeginner-friendlyfeaturedfinancefintechhacktoberfesthigh-performanceopen-sourceorchestrationpaymentpayment-gatewaypayment-integrationpayment-processingpaymentspayments-platformrestful-apirustsdkstripeworks-with-react
//! Storage for analytics reports generated by the process tracker.
#[cfg(feature = "olap")]
pub use olap::*;
/// Reports need the `olap` feature; without it the interface is empty.
#[cfg(not(feature = "olap"))]
#[router_derive::track_async_interface]
pub trait AnalyticsReportInterface {}
#[cfg(not(feature = "olap"))]
impl<T: ?Sized> AnalyticsReportInterface for T {}
#[cfg(feature = "olap")]
mod olap {
use analytics::reports::{ReportBind, ReportQuery};
use diesel::sql_types::{Array, Text};
use error_stack::report;
use router_env::instrument;
pub use row::ReportRow;
use storage_impl::{utils::pg_connection_read_replica, MockDb};
use crate::{
core::errors::{self, CustomResult},
db::{kafka_store::KafkaStore, Store},
};
// The derive's generated impl trips `unused_qualifications` and, from Rust 1.99,
// `clippy::redundant_field_names`; this module scopes the allows.
#[allow(unused_qualifications, clippy::redundant_field_names)]
mod row {
use diesel::sql_types::{Array, Nullable, Text};
/// One report row: its cells as text, in the report's header order.
#[derive(Debug, diesel::QueryableByName)]
pub struct ReportRow {
#[diesel(sql_type = Array<Nullable<Text>>)]
pub cells: Vec<Option<String>>,
}
}
#[router_derive::track_async_interface]
#[async_trait::async_trait]
pub trait AnalyticsReportInterface {
/// Runs `query` on the read replica as one read-only cursor, so Postgres scans once, and sends
/// its rows through `pages` at most `page_size` at a time; memory holds a page, never the
/// result. Each fetch is bounded by `fetch_timeout`. Stops early, rolling back, once `pages`
/// is dropped.
async fn stream_analytics_report(
&self,
query: ReportQuery,
page_size: usize,
fetch_timeout: std::time::Duration,
pages: tokio::sync::mpsc::Sender<Vec<ReportRow>>,
) -> CustomResult<(), errors::StorageError>;
}
#[async_trait::async_trait]
impl AnalyticsReportInterface for Store {
#[instrument(skip_all)]
async fn stream_analytics_report(
&self,
query: ReportQuery,
page_size: usize,
fetch_timeout: std::time::Duration,
pages: tokio::sync::mpsc::Sender<Vec<ReportRow>>,
) -> CustomResult<(), errors::StorageError> {
use diesel::{pg::Pg, sql_types::Timestamp};
let conn = pg_connection_read_replica(self).await?;
let mut declare = diesel::sql_query(format!(
"DECLARE analytics_report NO SCROLL CURSOR FOR {}",
query.sql
))
.into_boxed::<Pg>();
for bind in query.binds {
declare = match bind {
ReportBind::Text(value) => declare.bind::<Text, _>(value),
ReportBind::Texts(values) => declare.bind::<Array<Text>, _>(values),
ReportBind::Timestamp(value) => declare.bind::<Timestamp, _>(value),
};
}
conn.stream_read_only_cursor(
declare,
"analytics_report",
page_size,
fetch_timeout,
move |page| pages.blocking_send(page).is_ok(),
)
.await
.map_err(|error: diesel::result::Error| report!(errors::StorageError::from(error)))
}
}
#[async_trait::async_trait]
impl AnalyticsReportInterface for KafkaStore {
async fn stream_analytics_report(
&self,
query: ReportQuery,
page_size: usize,
fetch_timeout: std::time::Duration,
pages: tokio::sync::mpsc::Sender<Vec<ReportRow>>,
) -> CustomResult<(), errors::StorageError> {
AnalyticsReportInterface::stream_analytics_report(
&self.diesel_store,
query,
page_size,
fetch_timeout,
pages,
)
.await
}
}
#[async_trait::async_trait]
impl AnalyticsReportInterface for MockDb {
async fn stream_analytics_report(
&self,
_query: ReportQuery,
_page_size: usize,
_fetch_timeout: std::time::Duration,
_pages: tokio::sync::mpsc::Sender<Vec<ReportRow>>,
) -> CustomResult<(), errors::StorageError> {
Err(errors::StorageError::MockDbError)?
}
}
}