mirror of
				https://github.com/juspay/hyperswitch.git
				synced 2025-10-31 10:06:32 +08:00 
			
		
		
		
	refactor(euclid): log refactors for dynamic routing (#6052)
Co-authored-by: Aprabhat19 <amishaprabhat@gmail.com> Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com> Co-authored-by: Amisha Prabhat <55580080+Aprabhat19@users.noreply.github.com> Co-authored-by: Chethan Rao <70657455+Chethan-rao@users.noreply.github.com>
This commit is contained in:
		| @ -1715,6 +1715,8 @@ async fn payment_response_update_tracker<F: Clone, T: types::Capturable>( | |||||||
|     )?; |     )?; | ||||||
|  |  | ||||||
|     #[cfg(all(feature = "v1", feature = "dynamic_routing"))] |     #[cfg(all(feature = "v1", feature = "dynamic_routing"))] | ||||||
|  |     { | ||||||
|  |         if let Some(dynamic_routing_algorithm) = business_profile.dynamic_routing_algorithm.clone() | ||||||
|         { |         { | ||||||
|             let state = state.clone(); |             let state = state.clone(); | ||||||
|             let business_profile = business_profile.clone(); |             let business_profile = business_profile.clone(); | ||||||
| @ -1726,6 +1728,7 @@ async fn payment_response_update_tracker<F: Clone, T: types::Capturable>( | |||||||
|                         &payment_attempt, |                         &payment_attempt, | ||||||
|                         routable_connectors, |                         routable_connectors, | ||||||
|                         &business_profile, |                         &business_profile, | ||||||
|  |                         dynamic_routing_algorithm, | ||||||
|                     ) |                     ) | ||||||
|                     .await |                     .await | ||||||
|                     .map_err(|e| logger::error!(dynamic_routing_metrics_error=?e)) |                     .map_err(|e| logger::error!(dynamic_routing_metrics_error=?e)) | ||||||
| @ -1734,6 +1737,7 @@ async fn payment_response_update_tracker<F: Clone, T: types::Capturable>( | |||||||
|                 .in_current_span(), |                 .in_current_span(), | ||||||
|             ); |             ); | ||||||
|         } |         } | ||||||
|  |     } | ||||||
|     payment_data.payment_intent = payment_intent; |     payment_data.payment_intent = payment_intent; | ||||||
|     payment_data.payment_attempt = payment_attempt; |     payment_data.payment_attempt = payment_attempt; | ||||||
|     router_data.payment_method_status.and_then(|status| { |     router_data.payment_method_status.and_then(|status| { | ||||||
|  | |||||||
| @ -1253,6 +1253,27 @@ pub async fn toggle_success_based_routing( | |||||||
|                         ), |                         ), | ||||||
|                     }; |                     }; | ||||||
|  |  | ||||||
|  |                     // redact cache for success based routing configs | ||||||
|  |                     let cache_key = format!( | ||||||
|  |                         "{}_{}", | ||||||
|  |                         business_profile.get_id().get_string_repr(), | ||||||
|  |                         algorithm_id.get_string_repr() | ||||||
|  |                     ); | ||||||
|  |                     let cache_entries_to_redact = | ||||||
|  |                         vec![cache::CacheKind::SuccessBasedDynamicRoutingCache( | ||||||
|  |                             cache_key.into(), | ||||||
|  |                         )]; | ||||||
|  |                     let _ = cache::publish_into_redact_channel( | ||||||
|  |                         state.store.get_cache_store().as_ref(), | ||||||
|  |                         cache_entries_to_redact, | ||||||
|  |                     ) | ||||||
|  |                     .await | ||||||
|  |                     .map_err(|e| { | ||||||
|  |                         logger::error!( | ||||||
|  |                             "unable to publish into the redact channel for evicting the success based routing config cache {e:?}" | ||||||
|  |                         ) | ||||||
|  |                     }); | ||||||
|  |  | ||||||
|                     let record = db |                     let record = db | ||||||
|                         .find_routing_algorithm_by_profile_id_algorithm_id( |                         .find_routing_algorithm_by_profile_id_algorithm_id( | ||||||
|                             business_profile.get_id(), |                             business_profile.get_id(), | ||||||
| @ -1351,7 +1372,7 @@ pub async fn success_based_routing_update_configs( | |||||||
|         cache_entries_to_redact, |         cache_entries_to_redact, | ||||||
|     ) |     ) | ||||||
|     .await |     .await | ||||||
|     .map_err(|e| logger::error!("unable to redact the success based routing config cache {e:?}")); |     .map_err(|e| logger::error!("unable to publish into the redact channel for evicting the success based routing config cache {e:?}")); | ||||||
|  |  | ||||||
|     let new_record = record.foreign_into(); |     let new_record = record.foreign_into(); | ||||||
|  |  | ||||||
|  | |||||||
| @ -594,13 +594,8 @@ pub async fn refresh_success_based_routing_cache( | |||||||
| pub async fn fetch_success_based_routing_configs( | pub async fn fetch_success_based_routing_configs( | ||||||
|     state: &SessionState, |     state: &SessionState, | ||||||
|     business_profile: &domain::Profile, |     business_profile: &domain::Profile, | ||||||
|  |     dynamic_routing_algorithm: serde_json::Value, | ||||||
| ) -> RouterResult<routing_types::SuccessBasedRoutingConfig> { | ) -> RouterResult<routing_types::SuccessBasedRoutingConfig> { | ||||||
|     let dynamic_routing_algorithm = business_profile.dynamic_routing_algorithm.clone().ok_or( |  | ||||||
|         errors::ApiErrorResponse::GenericNotFoundError { |  | ||||||
|             message: "unable to find dynamic_routing_algorithm in business profile".to_string(), |  | ||||||
|         }, |  | ||||||
|     )?; |  | ||||||
|  |  | ||||||
|     let dynamic_routing_algorithm_ref = dynamic_routing_algorithm |     let dynamic_routing_algorithm_ref = dynamic_routing_algorithm | ||||||
|         .parse_value::<routing_types::DynamicRoutingAlgorithmRef>("DynamicRoutingAlgorithmRef") |         .parse_value::<routing_types::DynamicRoutingAlgorithmRef>("DynamicRoutingAlgorithmRef") | ||||||
|         .change_context(errors::ApiErrorResponse::InternalServerError) |         .change_context(errors::ApiErrorResponse::InternalServerError) | ||||||
| @ -613,8 +608,12 @@ pub async fn fetch_success_based_routing_configs( | |||||||
|                 .to_string(), |                 .to_string(), | ||||||
|         })? |         })? | ||||||
|         .algorithm_id |         .algorithm_id | ||||||
|  |         // error can be possible when the feature is toggled off. | ||||||
|         .ok_or(errors::ApiErrorResponse::GenericNotFoundError { |         .ok_or(errors::ApiErrorResponse::GenericNotFoundError { | ||||||
|             message: "unable to find algorithm id in success based algorithm config".to_string(), |             message: format!( | ||||||
|  |                 "unable to find algorithm_id in success based algorithm config as the feature is disabled for profile_id: {}", | ||||||
|  |                 business_profile.get_id().get_string_repr() | ||||||
|  |             ), | ||||||
|         })?; |         })?; | ||||||
|  |  | ||||||
|     let key = format!( |     let key = format!( | ||||||
| @ -658,6 +657,7 @@ pub async fn push_metrics_for_success_based_routing( | |||||||
|     payment_attempt: &storage::PaymentAttempt, |     payment_attempt: &storage::PaymentAttempt, | ||||||
|     routable_connectors: Vec<routing_types::RoutableConnectorChoice>, |     routable_connectors: Vec<routing_types::RoutableConnectorChoice>, | ||||||
|     business_profile: &domain::Profile, |     business_profile: &domain::Profile, | ||||||
|  |     dynamic_routing_algorithm: serde_json::Value, | ||||||
| ) -> RouterResult<()> { | ) -> RouterResult<()> { | ||||||
|     let client = state |     let client = state | ||||||
|         .grpc_client |         .grpc_client | ||||||
| @ -675,10 +675,10 @@ pub async fn push_metrics_for_success_based_routing( | |||||||
|     )?; |     )?; | ||||||
|  |  | ||||||
|     let success_based_routing_configs = |     let success_based_routing_configs = | ||||||
|         fetch_success_based_routing_configs(state, business_profile) |         fetch_success_based_routing_configs(state, business_profile, dynamic_routing_algorithm) | ||||||
|             .await |             .await | ||||||
|             .change_context(errors::ApiErrorResponse::InternalServerError) |             .change_context(errors::ApiErrorResponse::InternalServerError) | ||||||
|             .attach_printable("unable to retrieve success_rate configs")?; |             .attach_printable("unable to retrieve success_rate based dynamic routing configs")?; | ||||||
|  |  | ||||||
|     let tenant_business_profile_id = format!( |     let tenant_business_profile_id = format!( | ||||||
|         "{}:{}", |         "{}:{}", | ||||||
| @ -749,41 +749,35 @@ pub async fn push_metrics_for_success_based_routing( | |||||||
|                 "currency", |                 "currency", | ||||||
|                 payment_attempt |                 payment_attempt | ||||||
|                     .currency |                     .currency | ||||||
|                     .ok_or(errors::ApiErrorResponse::InternalServerError) |                     .map_or_else(|| "None".to_string(), |currency| currency.to_string()), | ||||||
|                     .attach_printable("payment currency not found in payment_attempt")? |  | ||||||
|                     .to_string(), |  | ||||||
|             ), |             ), | ||||||
|             ( |             ( | ||||||
|                 "payment_method", |                 "payment_method", | ||||||
|                 payment_attempt |                 payment_attempt.payment_method.map_or_else( | ||||||
|                     .payment_method |                     || "None".to_string(), | ||||||
|                     .ok_or(errors::ApiErrorResponse::InternalServerError) |                     |payment_method| payment_method.to_string(), | ||||||
|                     .attach_printable("payment method not found in payment_attempt")? |                 ), | ||||||
|                     .to_string(), |  | ||||||
|             ), |             ), | ||||||
|             ( |             ( | ||||||
|                 "payment_method_type", |                 "payment_method_type", | ||||||
|                 payment_attempt |                 payment_attempt.payment_method_type.map_or_else( | ||||||
|                     .payment_method_type |                     || "None".to_string(), | ||||||
|                     .ok_or(errors::ApiErrorResponse::InternalServerError) |                     |payment_method_type| payment_method_type.to_string(), | ||||||
|                     .attach_printable("payment method type not found in payment_attempt")? |                 ), | ||||||
|                     .to_string(), |  | ||||||
|             ), |             ), | ||||||
|             ( |             ( | ||||||
|                 "capture_method", |                 "capture_method", | ||||||
|                 payment_attempt |                 payment_attempt.capture_method.map_or_else( | ||||||
|                     .capture_method |                     || "None".to_string(), | ||||||
|                     .ok_or(errors::ApiErrorResponse::InternalServerError) |                     |capture_method| capture_method.to_string(), | ||||||
|                     .attach_printable("capture method not found in payment_attempt")? |                 ), | ||||||
|                     .to_string(), |  | ||||||
|             ), |             ), | ||||||
|             ( |             ( | ||||||
|                 "authentication_type", |                 "authentication_type", | ||||||
|                 payment_attempt |                 payment_attempt.authentication_type.map_or_else( | ||||||
|                     .authentication_type |                     || "None".to_string(), | ||||||
|                     .ok_or(errors::ApiErrorResponse::InternalServerError) |                     |authentication_type| authentication_type.to_string(), | ||||||
|                     .attach_printable("authentication type not found in payment_attempt")? |                 ), | ||||||
|                     .to_string(), |  | ||||||
|             ), |             ), | ||||||
|             ("payment_status", payment_attempt.status.to_string()), |             ("payment_status", payment_attempt.status.to_string()), | ||||||
|             ("conclusive_classification", outcome.to_string()), |             ("conclusive_classification", outcome.to_string()), | ||||||
|  | |||||||
| @ -1014,7 +1014,7 @@ pub async fn toggle_success_based_routing( | |||||||
|             &auth::JWTAuthProfileFromRoute { |             &auth::JWTAuthProfileFromRoute { | ||||||
|                 profile_id: wrapper.profile_id, |                 profile_id: wrapper.profile_id, | ||||||
|                 required_permission: Permission::RoutingWrite, |                 required_permission: Permission::RoutingWrite, | ||||||
|                 minimum_entity_level: EntityType::Merchant, |                 minimum_entity_level: EntityType::Profile, | ||||||
|             }, |             }, | ||||||
|             req.headers(), |             req.headers(), | ||||||
|         ), |         ), | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	 Prajjwal Kumar
					Prajjwal Kumar