Skip to content

Commit

Permalink
error dist response structure refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ivor11 committed Nov 29, 2023
1 parent 7dc22a9 commit 67b7e00
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
21 changes: 10 additions & 11 deletions crates/analytics/src/payments/accumulator.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -71,7 +71,7 @@ pub trait PaymentDistributionAccumulator {
}

impl PaymentDistributionAccumulator for ErrorDistributionAccumulator {
type DistributionOutput = Option<String>;
type DistributionOutput = Option<Vec<ErrorResult>>;

fn add_distribution_bucket(&mut self, distribution: &PaymentDistributionRow) {
self.error_vec.push(ErrorDistributionRow {
Expand All @@ -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<String> = Vec::new();
for val in self.error_vec.iter() {
let mut res: Vec<ErrorResult> = 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)
}
}
}
Expand Down
9 changes: 8 additions & 1 deletion crates/api_models/src/analytics/payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -194,7 +201,7 @@ pub struct PaymentMetricsBucketValue {
pub payment_success_count: Option<u64>,
pub payment_processed_amount: Option<u64>,
pub avg_ticket_size: Option<f64>,
pub payment_error_message: Option<String>,
pub payment_error_message: Option<Vec<ErrorResult>>,
pub retries_count: Option<u64>,
pub retries_amount_processed: Option<u64>,
pub connector_success_rate: Option<f64>,
Expand Down

0 comments on commit 67b7e00

Please sign in to comment.