Skip to content
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(routing): Contract based routing integration #6761

Open
wants to merge 54 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
c003cfe
feat: Integrate contract routing
Sarthak1799 Dec 3, 2024
7d1cbe3
Merge branch 'main' of github.com:juspay/hyperswitch into contracts-d…
Sarthak1799 Dec 3, 2024
354314a
chore: Fixed some implementaions
Sarthak1799 Dec 3, 2024
df8bab3
feat: Implemented cache related stuff
Sarthak1799 Dec 4, 2024
fb830b8
fix: Fixed errors
Sarthak1799 Dec 4, 2024
063d978
feat: Added new endpoint for contracts
Sarthak1799 Dec 5, 2024
a0b334c
feat: Implemented config routes for contracts
Sarthak1799 Dec 5, 2024
1bf3f9b
Merge branch 'main' of github.com:juspay/hyperswitch into contracts-d…
Sarthak1799 Dec 5, 2024
535532c
chore: run formatter
hyperswitch-bot[bot] Dec 5, 2024
f5454d6
fix: Fixed errors
Sarthak1799 Dec 5, 2024
025f70c
refactor: Refactored routing callers into single function
Sarthak1799 Dec 7, 2024
e5ad76b
chore: run formatter
hyperswitch-bot[bot] Dec 7, 2024
c3cf07a
fix: Fixed clippy errors
Sarthak1799 Dec 8, 2024
e17beee
chore: run formatter
hyperswitch-bot[bot] Dec 8, 2024
59e573c
refactor: Changed order of contract functions
Sarthak1799 Dec 9, 2024
3c471f9
fix: Fixed metrics
Sarthak1799 Dec 12, 2024
0a5c5e1
fix: Fixed clippy errors
Sarthak1799 Dec 12, 2024
198a308
chore: run formatter
hyperswitch-bot[bot] Dec 12, 2024
5ec8b3f
fix: Fixed errors
Sarthak1799 Dec 12, 2024
f9f0e27
Merge branch 'main' of github.com:juspay/hyperswitch into contracts-d…
Sarthak1799 Dec 13, 2024
17044b1
chore: run formatter
hyperswitch-bot[bot] Dec 13, 2024
584db45
chore: Refactored a bit
Sarthak1799 Dec 16, 2024
a1732d4
chore: run formatter
hyperswitch-bot[bot] Dec 16, 2024
9630a45
chore: Resolved comments
Sarthak1799 Dec 17, 2024
6a01879
Merge branch 'main' of github.com:juspay/hyperswitch into contracts-d…
Sarthak1799 Dec 18, 2024
01907d5
chore: Resolved comments
Sarthak1799 Dec 18, 2024
cbc9478
fix: Fixed errors
Sarthak1799 Dec 18, 2024
f4dec0b
chore: Resolved comments
Sarthak1799 Dec 19, 2024
130ce8e
chore: Resolved comments
Sarthak1799 Dec 20, 2024
7a03ea2
Merge branch 'main' of github.com:juspay/hyperswitch into contracts-d…
Sarthak1799 Dec 23, 2024
6241147
chore: Resolved comments
Sarthak1799 Dec 23, 2024
da5c476
chore: run formatter
hyperswitch-bot[bot] Dec 23, 2024
acdf379
fix: Fixed errors
Sarthak1799 Dec 23, 2024
a646967
refactor: Added db call for stats
Sarthak1799 Dec 24, 2024
c031d24
fix: Resolved comments
Sarthak1799 Dec 24, 2024
b45a01f
chore: run formatter
hyperswitch-bot[bot] Dec 24, 2024
6eb3a8d
chore: Resolved comments
Sarthak1799 Dec 26, 2024
0f4535b
chore: run formatter
hyperswitch-bot[bot] Dec 26, 2024
27e1639
Merge branch 'main' of github.com:juspay/hyperswitch into contracts-d…
Sarthak1799 Jan 15, 2025
8d0810b
Merge branch 'main' of github.com:juspay/hyperswitch into contracts-d…
Sarthak1799 Jan 15, 2025
17648f3
chore: Resolved comments
Sarthak1799 Jan 16, 2025
835b4e0
fix: Fixed errors
Sarthak1799 Jan 16, 2025
bcfe3d2
chore: Resolved comments
Sarthak1799 Jan 16, 2025
1429f81
chore: Resolved comments
Sarthak1799 Jan 16, 2025
b0c3bf0
Merge branch 'main' of github.com:juspay/hyperswitch into contracts-d…
Sarthak1799 Jan 16, 2025
eecb4b0
chore: Resolved comments
Sarthak1799 Jan 16, 2025
55dc858
fix: Fixed errors
Sarthak1799 Jan 16, 2025
d332edf
refactor: Removed profile form dynamic routing fucntion calls
Sarthak1799 Jan 17, 2025
978d315
fix: Fixed errors
Sarthak1799 Jan 19, 2025
6a25c78
chore: Resolved comments
Sarthak1799 Jan 19, 2025
e622670
chore: Resolved comments
Sarthak1799 Jan 19, 2025
7457811
chore: Resolved comments
Sarthak1799 Jan 20, 2025
405eaa5
fix: Fixed metrics population
Sarthak1799 Jan 20, 2025
4f12340
Merge branch 'main' of github.com:juspay/hyperswitch into contracts-d…
Sarthak1799 Jan 21, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/api_models/src/routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,7 @@ pub struct ContractBasedRoutingPayloadWrapper {

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct ContractBasedRoutingSetupPayloadWrapper {
pub config: ContractBasedRoutingConfig,
pub config: Option<ContractBasedRoutingConfig>,
pub profile_id: common_utils::id_type::ProfileId,
pub features_to_enable: DynamicRoutingFeatures,
}
Expand Down
136 changes: 74 additions & 62 deletions crates/router/src/core/routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1470,7 +1470,7 @@ pub async fn contract_based_dynamic_routing_setup(
merchant_account: domain::MerchantAccount,
profile_id: common_utils::id_type::ProfileId,
feature_to_enable: routing_types::DynamicRoutingFeatures,
config: routing_types::ContractBasedRoutingConfig,
config: Option<routing_types::ContractBasedRoutingConfig>,
) -> RouterResult<service_api::ApplicationResponse<routing_types::RoutingDictionaryRecord>> {
let db = state.store.as_ref();
let key_manager_state = &(&state).into();
Expand All @@ -1488,29 +1488,6 @@ pub async fn contract_based_dynamic_routing_setup(
id: profile_id.get_string_repr().to_owned(),
})?;

// validate the contained mca_ids
if let Some(info_vec) = &config.label_info {
for info in info_vec {
let mca = db
.find_by_merchant_connector_account_merchant_id_merchant_connector_id(
key_manager_state,
merchant_account.get_id(),
&info.mca_id,
&key_store,
)
.await
.change_context(errors::ApiErrorResponse::MerchantConnectorAccountNotFound {
id: info.mca_id.get_string_repr().to_owned(),
})?;

utils::when(mca.connector_name != info.label, || {
Err(errors::ApiErrorResponse::InvalidRequestData {
message: "Incorrect mca configuration received".to_string(),
})
})?;
}
}

let mut dynamic_routing_algo_ref: Option<routing_types::DynamicRoutingAlgorithmRef> =
business_profile
.dynamic_routing_algorithm
Expand Down Expand Up @@ -1545,6 +1522,25 @@ pub async fn contract_based_dynamic_routing_setup(
},
)?;

if feature_to_enable == routing::DynamicRoutingFeatures::None {
let algorithm = dynamic_routing_algo_ref
.clone()
.get_required_value("dynamic_routing_algo_ref")
.attach_printable("Failed to get dynamic_routing_algo_ref")?;
return helpers::disable_dynamic_routing_algorithm(
&state,
key_store,
business_profile,
algorithm,
routing_types::DynamicRoutingType::ContractBasedRouting,
)
.await;
}

let config = config
.get_required_value("ContractBasedRoutingConfig")
.attach_printable("Failed to get ContractBasedRoutingConfig from request")?;

let merchant_id = business_profile.merchant_id.clone();
let algorithm_id = common_utils::generate_routing_id_of_default_length();
let timestamp = common_utils::date_time::now();
Expand Down Expand Up @@ -1589,47 +1585,63 @@ pub async fn contract_based_dynamic_routing_setup(
}
};

match feature_to_enable {
routing::DynamicRoutingFeatures::Metrics
| routing::DynamicRoutingFeatures::DynamicConnectorSelection => {
let record = db
.insert_routing_algorithm(algo)
.await
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("Unable to insert record in routing algorithm table")?;

helpers::update_business_profile_active_dynamic_algorithm_ref(
db,
key_manager_state,
&key_store,
business_profile,
final_algorithm,
)
.await?;
// validate the contained mca_ids
if let Some(info_vec) = &config.label_info {
let validation_futures: Vec<_> = info_vec
.iter()
.map(|info| async {
let mca_id = info.mca_id.clone();
let label = info.label.clone();
let mca = db
.find_by_merchant_connector_account_merchant_id_merchant_connector_id(
key_manager_state,
merchant_account.get_id(),
&mca_id,
&key_store,
)
.await
.change_context(errors::ApiErrorResponse::MerchantConnectorAccountNotFound {
id: mca_id.get_string_repr().to_owned(),
})?;

utils::when(mca.connector_name != label, || {
Err(error_stack::Report::new(
errors::ApiErrorResponse::InvalidRequestData {
message: "Incorrect mca configuration received".to_string(),
},
))
})?;

// Should we also update at dynamic routing service?
let new_record = record.foreign_into();
Ok::<_, error_stack::Report<errors::ApiErrorResponse>>(())
})
.collect();

metrics::ROUTING_CREATE_SUCCESS_RESPONSE.add(
1,
router_env::metric_attributes!((
"profile_id",
profile_id.get_string_repr().to_string()
)),
);
Ok(service_api::ApplicationResponse::Json(new_record))
}
routing::DynamicRoutingFeatures::None => {
helpers::disable_dynamic_routing_algorithm(
&state,
key_store,
business_profile,
final_algorithm,
routing_types::DynamicRoutingType::ContractBasedRouting,
)
.await
}
futures::future::try_join_all(validation_futures).await?;
}

let record = db
.insert_routing_algorithm(algo)
.await
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("Unable to insert record in routing algorithm table")?;

helpers::update_business_profile_active_dynamic_algorithm_ref(
db,
key_manager_state,
&key_store,
business_profile,
final_algorithm,
)
.await?;

// Should we also update at dynamic routing service?
Sarthak1799 marked this conversation as resolved.
Show resolved Hide resolved
let new_record = record.foreign_into();

metrics::ROUTING_CREATE_SUCCESS_RESPONSE.add(
1,
router_env::metric_attributes!(("profile_id", profile_id.get_string_repr().to_string())),
);
Ok(service_api::ApplicationResponse::Json(new_record))
}

#[cfg(all(feature = "v1", feature = "dynamic_routing"))]
Expand Down
8 changes: 0 additions & 8 deletions crates/router/src/core/routing/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1066,12 +1066,6 @@ pub async fn push_metrics_with_update_window_for_contract_based_routing(
))?
.0, first_contract_based_connector.score, first_contract_based_connector.current_count );

let outcome = get_dynamic_routing_based_metrics_outcome_for_payment(
payment_status_attribute,
payment_connector.to_string(),
first_contract_based_connector.to_string(),
);

core_metrics::DYNAMIC_CONTRACT_BASED_ROUTING.add(
prajjwalkumar17 marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this metric be pushed only if payment_status_attribute is charged?

1,
router_env::metric_attributes!(
Expand Down Expand Up @@ -1134,8 +1128,6 @@ pub async fn push_metrics_with_update_window_for_contract_based_routing(
|authentication_type| authentication_type.to_string(),
),
),
("payment_status", payment_attempt.status.to_string()),
("conclusive_classification", outcome.to_string()),
),
);
logger::debug!("successfully pushed contract_based_routing metrics");
Expand Down
4 changes: 2 additions & 2 deletions crates/router/src/routes/routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1101,11 +1101,11 @@ pub async fn contract_based_routing_setup_config(
req: HttpRequest,
path: web::Path<routing_types::ToggleDynamicRoutingPath>,
query: web::Query<api_models::routing::ToggleDynamicRoutingQuery>,
json_payload: web::Json<routing_types::ContractBasedRoutingConfig>,
json_payload: Option<web::Json<routing_types::ContractBasedRoutingConfig>>,
) -> impl Responder {
let flow = Flow::ToggleDynamicRouting;
let routing_payload_wrapper = routing_types::ContractBasedRoutingSetupPayloadWrapper {
config: json_payload.into_inner(),
config: json_payload.map(|json| json.into_inner()),
profile_id: path.into_inner().profile_id,
features_to_enable: query.into_inner().enable,
};
Expand Down
Loading