-
Notifications
You must be signed in to change notification settings - Fork 4.2k
feat(debit_routing): add debit_routing_savings
in analytics payment attempt
#8519
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4c5524f
feat(debit_routing): add in analytics payment attempt
ShankarSinghC 8c8e230
Merge branch 'main' of https://github.com/juspay/hyperswitch into deb…
ShankarSinghC 48202fc
chore: run formatter
hyperswitch-bot[bot] 4cf9a95
address pr comments
ShankarSinghC afbff01
address pr comments
ShankarSinghC 22c46d9
chore: run formatter
hyperswitch-bot[bot] 047df67
Merge branch 'main' of https://github.com/juspay/hyperswitch into deb…
ShankarSinghC 6c0e4a4
Merge branch 'debit-routing/analytics/add-savings' of https://github.…
ShankarSinghC a2979c7
address pr comments
ShankarSinghC c0a1a9a
refactor
ShankarSinghC 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,151 @@ | ||
use std::collections::HashSet; | ||
|
||
use api_models::analytics::{ | ||
payments::{PaymentDimensions, PaymentFilters, PaymentMetricsBucketIdentifier}, | ||
Granularity, TimeRange, | ||
}; | ||
use common_utils::errors::ReportSwitchExt; | ||
use diesel_models::enums as storage_enums; | ||
use error_stack::ResultExt; | ||
use time::PrimitiveDateTime; | ||
|
||
use super::PaymentMetricRow; | ||
use crate::{ | ||
enums::AuthInfo, | ||
query::{Aggregate, GroupByClause, QueryBuilder, QueryFilter, SeriesBucket, ToSql, Window}, | ||
types::{AnalyticsCollection, AnalyticsDataSource, MetricsError, MetricsResult}, | ||
}; | ||
|
||
#[derive(Default)] | ||
pub(super) struct DebitRouting; | ||
|
||
#[async_trait::async_trait] | ||
impl<T> super::PaymentMetric<T> for DebitRouting | ||
where | ||
T: AnalyticsDataSource + super::PaymentMetricAnalytics, | ||
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: &[PaymentDimensions], | ||
auth: &AuthInfo, | ||
filters: &PaymentFilters, | ||
granularity: Option<Granularity>, | ||
time_range: &TimeRange, | ||
pool: &T, | ||
) -> MetricsResult<HashSet<(PaymentMetricsBucketIdentifier, PaymentMetricRow)>> { | ||
let mut query_builder: QueryBuilder<T> = QueryBuilder::new(AnalyticsCollection::Payment); | ||
|
||
for dim in dimensions.iter() { | ||
query_builder.add_select_column(dim).switch()?; | ||
} | ||
query_builder | ||
.add_select_column(Aggregate::Count { | ||
field: None, | ||
alias: Some("count"), | ||
}) | ||
.switch()?; | ||
query_builder | ||
.add_select_column(Aggregate::Sum { | ||
field: "debit_routing_savings", | ||
alias: Some("total"), | ||
}) | ||
.switch()?; | ||
query_builder.add_select_column("currency").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) | ||
.attach_printable("Error filtering time range") | ||
.switch()?; | ||
|
||
for dim in dimensions.iter() { | ||
query_builder | ||
.add_group_by_clause(dim) | ||
.attach_printable("Error grouping by dimensions") | ||
.switch()?; | ||
} | ||
|
||
query_builder | ||
.add_group_by_clause("currency") | ||
.attach_printable("Error grouping by currency") | ||
.switch()?; | ||
|
||
if let Some(granularity) = granularity { | ||
granularity | ||
.set_group_by_clause(&mut query_builder) | ||
.attach_printable("Error adding granularity") | ||
.switch()?; | ||
} | ||
|
||
query_builder | ||
.add_filter_clause( | ||
PaymentDimensions::PaymentStatus, | ||
storage_enums::AttemptStatus::Charged, | ||
) | ||
.switch()?; | ||
|
||
query_builder | ||
.execute_query::<PaymentMetricRow, _>(pool) | ||
.await | ||
.change_context(MetricsError::QueryBuildingError)? | ||
.change_context(MetricsError::QueryExecutionFailure)? | ||
.into_iter() | ||
.map(|i| { | ||
Ok(( | ||
PaymentMetricsBucketIdentifier::new( | ||
i.currency.as_ref().map(|i| i.0), | ||
None, | ||
i.connector.clone(), | ||
i.authentication_type.as_ref().map(|i| i.0), | ||
i.payment_method.clone(), | ||
i.payment_method_type.clone(), | ||
i.client_source.clone(), | ||
i.client_version.clone(), | ||
i.profile_id.clone(), | ||
i.card_network.clone(), | ||
i.merchant_id.clone(), | ||
i.card_last_4.clone(), | ||
i.card_issuer.clone(), | ||
i.error_reason.clone(), | ||
i.routing_approach.as_ref().map(|i| i.0), | ||
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<(PaymentMetricsBucketIdentifier, PaymentMetricRow)>, | ||
crate::query::PostProcessingError, | ||
>>() | ||
.change_context(MetricsError::PostProcessingFailure) | ||
} | ||
} |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Can add
Self::DebitRouting
here and can remove this commentThis is for the forex conversion of other currencies to
USD
fordebit_routing_savings