From 67b7e00e6ee15d0a4ded2f1b20c8ad0c732ebaa6 Mon Sep 17 00:00:00 2001 From: Ivor Dsouza Date: Wed, 29 Nov 2023 12:46:57 +0530 Subject: [PATCH] error dist response structure refactor --- crates/analytics/src/payments/accumulator.rs | 21 ++++++++++---------- crates/api_models/src/analytics/payments.rs | 9 ++++++++- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/crates/analytics/src/payments/accumulator.rs b/crates/analytics/src/payments/accumulator.rs index 0675d1044cdd..c340f2888f8b 100644 --- a/crates/analytics/src/payments/accumulator.rs +++ b/crates/analytics/src/payments/accumulator.rs @@ -1,4 +1,4 @@ -use api_models::analytics::payments::PaymentMetricsBucketValue; +use api_models::analytics::payments::{ErrorResult, PaymentMetricsBucketValue}; use bigdecimal::ToPrimitive; use diesel_models::enums as storage_enums; use router_env::logger; @@ -71,7 +71,7 @@ pub trait PaymentDistributionAccumulator { } impl PaymentDistributionAccumulator for ErrorDistributionAccumulator { - type DistributionOutput = Option; + type DistributionOutput = Option>; fn add_distribution_bucket(&mut self, distribution: &PaymentDistributionRow) { self.error_vec.push(ErrorDistributionRow { @@ -90,20 +90,19 @@ impl PaymentDistributionAccumulator for ErrorDistributionAccumulator { None } else { self.error_vec.sort_by(|a, b| b.count.cmp(&a.count)); - let mut res: Vec = Vec::new(); - for val in self.error_vec.iter() { + let mut res: Vec = Vec::new(); + for val in self.error_vec.into_iter() { let perc = f64::from(u32::try_from(val.count).ok()?) * 100.0 / f64::from(u32::try_from(val.total).ok()?); - res.push(format!( - "{} ({}%) ({})", - val.error_message, - perc.round(), - val.count - )); + res.push(ErrorResult { + reason: val.error_message, + count: val.count, + percentage: (perc * 100.0).round() / 100.0, + }) } - Some(res.join(" $$ ")) + Some(res) } } } diff --git a/crates/api_models/src/analytics/payments.rs b/crates/api_models/src/analytics/payments.rs index a150fa5fcead..2d7ae262f489 100644 --- a/crates/api_models/src/analytics/payments.rs +++ b/crates/api_models/src/analytics/payments.rs @@ -79,6 +79,13 @@ pub enum PaymentMetrics { ConnectorSuccessRate, } +#[derive(Debug, Default, serde::Serialize)] +pub struct ErrorResult { + pub reason: String, + pub count: i64, + pub percentage: f64, +} + #[derive( Clone, Copy, @@ -194,7 +201,7 @@ pub struct PaymentMetricsBucketValue { pub payment_success_count: Option, pub payment_processed_amount: Option, pub avg_ticket_size: Option, - pub payment_error_message: Option, + pub payment_error_message: Option>, pub retries_count: Option, pub retries_amount_processed: Option, pub connector_success_rate: Option,