Skip to content

Commit

Permalink
feat(analytics): Add refund sessionized metrics for Analytics V2 dash…
Browse files Browse the repository at this point in the history
…board (#6616)

Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com>
  • Loading branch information
tsdk02 and hyperswitch-bot[bot] authored Dec 5, 2024
1 parent de80121 commit 774a53e
Show file tree
Hide file tree
Showing 29 changed files with 1,527 additions and 78 deletions.
15 changes: 14 additions & 1 deletion crates/analytics/src/clickhouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ use super::{
distribution::PaymentDistributionRow, filters::PaymentFilterRow, metrics::PaymentMetricRow,
},
query::{Aggregate, ToSql, Window},
refunds::{filters::RefundFilterRow, metrics::RefundMetricRow},
refunds::{
distribution::RefundDistributionRow, filters::RefundFilterRow, metrics::RefundMetricRow,
},
sdk_events::{filters::SdkEventFilter, metrics::SdkEventMetricRow},
types::{AnalyticsCollection, AnalyticsDataSource, LoadRow, QueryExecutionError},
};
Expand Down Expand Up @@ -170,6 +172,7 @@ impl super::payment_intents::filters::PaymentIntentFilterAnalytics for Clickhous
impl super::payment_intents::metrics::PaymentIntentMetricAnalytics for ClickhouseClient {}
impl super::refunds::metrics::RefundMetricAnalytics for ClickhouseClient {}
impl super::refunds::filters::RefundFilterAnalytics for ClickhouseClient {}
impl super::refunds::distribution::RefundDistributionAnalytics for ClickhouseClient {}
impl super::frm::metrics::FrmMetricAnalytics for ClickhouseClient {}
impl super::frm::filters::FrmFilterAnalytics for ClickhouseClient {}
impl super::sdk_events::filters::SdkEventFilterAnalytics for ClickhouseClient {}
Expand Down Expand Up @@ -300,6 +303,16 @@ impl TryInto<RefundFilterRow> for serde_json::Value {
}
}

impl TryInto<RefundDistributionRow> for serde_json::Value {
type Error = Report<ParsingError>;

fn try_into(self) -> Result<RefundDistributionRow, Self::Error> {
serde_json::from_value(self).change_context(ParsingError::StructParseFailure(
"Failed to parse RefundDistributionRow in clickhouse results",
))
}
}

impl TryInto<FrmMetricRow> for serde_json::Value {
type Error = Report<ParsingError>;

Expand Down
115 changes: 113 additions & 2 deletions crates/analytics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use hyperswitch_interfaces::secrets_interface::{
secret_state::{RawSecret, SecretStateContainer, SecuredSecret},
SecretManagementInterface, SecretsManagementError,
};
use refunds::distribution::{RefundDistribution, RefundDistributionRow};
pub use types::AnalyticsDomain;
pub mod lambda_utils;
pub mod utils;
Expand All @@ -52,7 +53,7 @@ use api_models::analytics::{
sdk_events::{
SdkEventDimensions, SdkEventFilters, SdkEventMetrics, SdkEventMetricsBucketIdentifier,
},
Distribution, Granularity, TimeRange,
Granularity, PaymentDistributionBody, RefundDistributionBody, TimeRange,
};
use clickhouse::ClickhouseClient;
pub use clickhouse::ClickhouseConfig;
Expand Down Expand Up @@ -215,7 +216,7 @@ impl AnalyticsProvider {

pub async fn get_payment_distribution(
&self,
distribution: &Distribution,
distribution: &PaymentDistributionBody,
dimensions: &[PaymentDimensions],
auth: &AuthInfo,
filters: &PaymentFilters,
Expand Down Expand Up @@ -528,6 +529,116 @@ impl AnalyticsProvider {
.await
}

pub async fn get_refund_distribution(
&self,
distribution: &RefundDistributionBody,
dimensions: &[RefundDimensions],
auth: &AuthInfo,
filters: &RefundFilters,
granularity: &Option<Granularity>,
time_range: &TimeRange,
) -> types::MetricsResult<Vec<(RefundMetricsBucketIdentifier, RefundDistributionRow)>> {
// Metrics to get the fetch time for each payment metric
metrics::request::record_operation_time(
async {
match self {
Self::Sqlx(pool) => {
distribution.distribution_for
.load_distribution(
distribution,
dimensions,
auth,
filters,
granularity,
time_range,
pool,
)
.await
}
Self::Clickhouse(pool) => {
distribution.distribution_for
.load_distribution(
distribution,
dimensions,
auth,
filters,
granularity,
time_range,
pool,
)
.await
}
Self::CombinedCkh(sqlx_pool, ckh_pool) => {
let (ckh_result, sqlx_result) = tokio::join!(distribution.distribution_for
.load_distribution(
distribution,
dimensions,
auth,
filters,
granularity,
time_range,
ckh_pool,
),
distribution.distribution_for
.load_distribution(
distribution,
dimensions,
auth,
filters,
granularity,
time_range,
sqlx_pool,
));
match (&sqlx_result, &ckh_result) {
(Ok(ref sqlx_res), Ok(ref ckh_res)) if sqlx_res != ckh_res => {
router_env::logger::error!(clickhouse_result=?ckh_res, postgres_result=?sqlx_res, "Mismatch between clickhouse & postgres payments analytics distribution")
},
_ => {}

};

ckh_result
}
Self::CombinedSqlx(sqlx_pool, ckh_pool) => {
let (ckh_result, sqlx_result) = tokio::join!(distribution.distribution_for
.load_distribution(
distribution,
dimensions,
auth,
filters,
granularity,
time_range,
ckh_pool,
),
distribution.distribution_for
.load_distribution(
distribution,
dimensions,
auth,
filters,
granularity,
time_range,
sqlx_pool,
));
match (&sqlx_result, &ckh_result) {
(Ok(ref sqlx_res), Ok(ref ckh_res)) if sqlx_res != ckh_res => {
router_env::logger::error!(clickhouse_result=?ckh_res, postgres_result=?sqlx_res, "Mismatch between clickhouse & postgres payments analytics distribution")
},
_ => {}

};

sqlx_result
}
}
},
&metrics::METRIC_FETCH_TIME,
&distribution.distribution_for,
self,
)
.await
}

pub async fn get_frm_metrics(
&self,
metric: &FrmMetrics,
Expand Down
6 changes: 3 additions & 3 deletions crates/analytics/src/payments/distribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use api_models::analytics::{
payments::{
PaymentDimensions, PaymentDistributions, PaymentFilters, PaymentMetricsBucketIdentifier,
},
Distribution, Granularity, TimeRange,
Granularity, PaymentDistributionBody, TimeRange,
};
use diesel_models::enums as storage_enums;
use time::PrimitiveDateTime;
Expand Down Expand Up @@ -53,7 +53,7 @@ where
#[allow(clippy::too_many_arguments)]
async fn load_distribution(
&self,
distribution: &Distribution,
distribution: &PaymentDistributionBody,
dimensions: &[PaymentDimensions],
auth: &AuthInfo,
filters: &PaymentFilters,
Expand All @@ -75,7 +75,7 @@ where
{
async fn load_distribution(
&self,
distribution: &Distribution,
distribution: &PaymentDistributionBody,
dimensions: &[PaymentDimensions],
auth: &AuthInfo,
filters: &PaymentFilters,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use api_models::analytics::{
payments::{PaymentDimensions, PaymentFilters, PaymentMetricsBucketIdentifier},
Distribution, Granularity, TimeRange,
Granularity, PaymentDistributionBody, TimeRange,
};
use common_utils::errors::ReportSwitchExt;
use diesel_models::enums as storage_enums;
Expand Down Expand Up @@ -31,7 +31,7 @@ where
{
async fn load_distribution(
&self,
distribution: &Distribution,
distribution: &PaymentDistributionBody,
dimensions: &[PaymentDimensions],
auth: &AuthInfo,
filters: &PaymentFilters,
Expand Down
3 changes: 2 additions & 1 deletion crates/analytics/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use api_models::{
frm::{FrmDimensions, FrmTransactionType},
payment_intents::PaymentIntentDimensions,
payments::{PaymentDimensions, PaymentDistributions},
refunds::{RefundDimensions, RefundType},
refunds::{RefundDimensions, RefundDistributions, RefundType},
sdk_events::{SdkEventDimensions, SdkEventNames},
Granularity,
},
Expand Down Expand Up @@ -488,6 +488,7 @@ impl_to_sql_for_to_string!(
PaymentIntentDimensions,
&PaymentDistributions,
RefundDimensions,
&RefundDistributions,
FrmDimensions,
PaymentMethod,
PaymentMethodType,
Expand Down
1 change: 1 addition & 0 deletions crates/analytics/src/refunds.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod accumulator;
mod core;

pub mod distribution;
pub mod filters;
pub mod metrics;
pub mod types;
Expand Down
Loading

0 comments on commit 774a53e

Please sign in to comment.