-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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(analytics): add sessionized_metrics
for disputes analytics
#6573
Open
maverox
wants to merge
5
commits into
main
Choose a base branch
from
sessionized_dispute_back_compat
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+447
−23
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
dcabeae
feat(analytics): create module sessionized_metrics for disputes
maverox b1a2203
refactor(analytics): rename the terms for fields in metrics accumulator
maverox 755028f
feat(analytics): implement sessionized_metrics for disputes
maverox 55ad65f
feat(analytics): add sessionizer_dispute table mapping for ckh and sqlx
maverox 386802f
chore: run formatter
hyperswitch-bot[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,8 +5,8 @@ use super::metrics::DisputeMetricRow; | |
#[derive(Debug, Default)] | ||
pub struct DisputeMetricsAccumulator { | ||
pub disputes_status_rate: RateAccumulator, | ||
pub total_amount_disputed: SumAccumulator, | ||
pub total_dispute_lost_amount: SumAccumulator, | ||
pub disputed_amount: PaymentProcessedAmountAccumulator, | ||
pub dispute_lost_amount: PaymentProcessedAmountAccumulator, | ||
} | ||
#[derive(Debug, Default)] | ||
pub struct RateAccumulator { | ||
|
@@ -17,7 +17,7 @@ pub struct RateAccumulator { | |
} | ||
#[derive(Debug, Default)] | ||
#[repr(transparent)] | ||
pub struct SumAccumulator { | ||
pub struct PaymentProcessedAmountAccumulator { | ||
pub total: Option<i64>, | ||
} | ||
|
||
|
@@ -29,7 +29,7 @@ pub trait DisputeMetricAccumulator { | |
fn collect(self) -> Self::MetricOutput; | ||
} | ||
|
||
impl DisputeMetricAccumulator for SumAccumulator { | ||
impl DisputeMetricAccumulator for PaymentProcessedAmountAccumulator { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
type MetricOutput = Option<u64>; | ||
#[inline] | ||
fn add_metrics_bucket(&mut self, metrics: &DisputeMetricRow) { | ||
|
@@ -92,8 +92,8 @@ impl DisputeMetricsAccumulator { | |
disputes_challenged: challenge_rate, | ||
disputes_won: won_rate, | ||
disputes_lost: lost_rate, | ||
total_amount_disputed: self.total_amount_disputed.collect(), | ||
total_dispute_lost_amount: self.total_dispute_lost_amount.collect(), | ||
disputed_amount: self.disputed_amount.collect(), | ||
dispute_lost_amount: self.dispute_lost_amount.collect(), | ||
total_dispute, | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
crates/analytics/src/disputes/metrics/sessionized_metrics/dispute_status_metric.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
use std::collections::HashSet; | ||
|
||
use api_models::analytics::{ | ||
disputes::{DisputeDimensions, DisputeFilters, DisputeMetricsBucketIdentifier}, | ||
Granularity, TimeRange, | ||
}; | ||
use common_utils::errors::ReportSwitchExt; | ||
use error_stack::ResultExt; | ||
use time::PrimitiveDateTime; | ||
|
||
use super::DisputeMetricRow; | ||
use crate::{ | ||
enums::AuthInfo, | ||
query::{Aggregate, GroupByClause, QueryBuilder, QueryFilter, SeriesBucket, ToSql, Window}, | ||
types::{AnalyticsCollection, AnalyticsDataSource, MetricsError, MetricsResult}, | ||
}; | ||
#[derive(Default)] | ||
pub(crate) struct DisputeStatusMetric {} | ||
|
||
#[async_trait::async_trait] | ||
impl<T> super::DisputeMetric<T> for DisputeStatusMetric | ||
where | ||
T: AnalyticsDataSource + super::DisputeMetricAnalytics, | ||
PrimitiveDateTime: ToSql<T>, | ||
AnalyticsCollection: ToSql<T>, | ||
Granularity: GroupByClause<T>, | ||
Aggregate<&'static str>: ToSql<T>, | ||
Window<&'static str>: ToSql<T>, | ||
{ | ||
async fn load_metrics( | ||
&self, | ||
dimensions: &[DisputeDimensions], | ||
auth: &AuthInfo, | ||
filters: &DisputeFilters, | ||
granularity: &Option<Granularity>, | ||
time_range: &TimeRange, | ||
pool: &T, | ||
) -> MetricsResult<HashSet<(DisputeMetricsBucketIdentifier, DisputeMetricRow)>> | ||
where | ||
T: AnalyticsDataSource + super::DisputeMetricAnalytics, | ||
{ | ||
let mut query_builder = QueryBuilder::new(AnalyticsCollection::DisputeSessionized); | ||
|
||
for dim in dimensions { | ||
query_builder.add_select_column(dim).switch()?; | ||
} | ||
|
||
query_builder.add_select_column("dispute_status").switch()?; | ||
|
||
query_builder | ||
.add_select_column(Aggregate::Count { | ||
field: None, | ||
alias: Some("count"), | ||
}) | ||
.switch()?; | ||
query_builder | ||
.add_select_column(Aggregate::Min { | ||
field: "created_at", | ||
alias: Some("start_bucket"), | ||
}) | ||
.switch()?; | ||
query_builder | ||
.add_select_column(Aggregate::Max { | ||
field: "created_at", | ||
alias: Some("end_bucket"), | ||
}) | ||
.switch()?; | ||
|
||
filters.set_filter_clause(&mut query_builder).switch()?; | ||
|
||
auth.set_filter_clause(&mut query_builder).switch()?; | ||
|
||
time_range.set_filter_clause(&mut query_builder).switch()?; | ||
|
||
for dim in dimensions { | ||
query_builder.add_group_by_clause(dim).switch()?; | ||
} | ||
|
||
query_builder | ||
.add_group_by_clause("dispute_status") | ||
.switch()?; | ||
|
||
if let Some(granularity) = granularity.as_ref() { | ||
granularity | ||
.set_group_by_clause(&mut query_builder) | ||
.switch()?; | ||
} | ||
|
||
query_builder | ||
.execute_query::<DisputeMetricRow, _>(pool) | ||
.await | ||
.change_context(MetricsError::QueryBuildingError)? | ||
.change_context(MetricsError::QueryExecutionFailure)? | ||
.into_iter() | ||
.map(|i| { | ||
Ok(( | ||
DisputeMetricsBucketIdentifier::new( | ||
i.dispute_stage.as_ref().map(|i| i.0), | ||
i.connector.clone(), | ||
TimeRange { | ||
start_time: match (granularity, i.start_bucket) { | ||
(Some(g), Some(st)) => g.clip_to_start(st)?, | ||
_ => time_range.start_time, | ||
}, | ||
end_time: granularity.as_ref().map_or_else( | ||
|| Ok(time_range.end_time), | ||
|g| i.end_bucket.map(|et| g.clip_to_end(et)).transpose(), | ||
)?, | ||
}, | ||
), | ||
i, | ||
)) | ||
}) | ||
.collect::<error_stack::Result< | ||
HashSet<(DisputeMetricsBucketIdentifier, DisputeMetricRow)>, | ||
crate::query::PostProcessingError, | ||
>>() | ||
.change_context(MetricsError::PostProcessingFailure) | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
crates/analytics/src/disputes/metrics/sessionized_metrics/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
mod dispute_status_metric; | ||
mod total_amount_disputed; | ||
mod total_dispute_lost_amount; | ||
pub(super) use dispute_status_metric::DisputeStatusMetric; | ||
pub(super) use total_amount_disputed::TotalAmountDisputed; | ||
pub(super) use total_dispute_lost_amount::TotalDisputeLostAmount; | ||
|
||
pub use super::{DisputeMetric, DisputeMetricAnalytics, DisputeMetricRow}; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can name it as
DisputedAmountAccumulator