@@ -14,7 +14,7 @@ use common_utils::ext_traits::ValueExt;
14
14
use common_utils:: { ext_traits:: Encode , id_type, types:: keymanager:: KeyManagerState } ;
15
15
use diesel_models:: configs;
16
16
#[ cfg( all( feature = "v1" , feature = "dynamic_routing" ) ) ]
17
- use diesel_models:: dynamic_routing_stats:: DynamicRoutingStatsNew ;
17
+ use diesel_models:: dynamic_routing_stats:: { DynamicRoutingStatsNew , DynamicRoutingStatsUpdate } ;
18
18
#[ cfg( all( feature = "dynamic_routing" , feature = "v1" ) ) ]
19
19
use diesel_models:: routing_algorithm;
20
20
use error_stack:: ResultExt ;
@@ -821,28 +821,6 @@ pub async fn push_metrics_with_update_window_for_success_based_routing(
821
821
first_merchant_success_based_connector_label. to_string ( ) ,
822
822
) ;
823
823
824
- let dynamic_routing_stats = DynamicRoutingStatsNew {
825
- payment_id : payment_attempt. payment_id . to_owned ( ) ,
826
- attempt_id : payment_attempt. attempt_id . clone ( ) ,
827
- merchant_id : payment_attempt. merchant_id . to_owned ( ) ,
828
- profile_id : payment_attempt. profile_id . to_owned ( ) ,
829
- amount : payment_attempt. get_total_amount ( ) ,
830
- success_based_routing_connector : first_merchant_success_based_connector_label
831
- . to_string ( ) ,
832
- payment_connector : payment_connector. to_string ( ) ,
833
- payment_method_type : payment_attempt. payment_method_type ,
834
- currency : payment_attempt. currency ,
835
- payment_method : payment_attempt. payment_method ,
836
- capture_method : payment_attempt. capture_method ,
837
- authentication_type : payment_attempt. authentication_type ,
838
- payment_status : payment_attempt. status ,
839
- conclusive_classification : outcome,
840
- created_at : common_utils:: date_time:: now ( ) ,
841
- global_success_based_connector : Some (
842
- first_global_success_based_connector. label . to_string ( ) ,
843
- ) ,
844
- } ;
845
-
846
824
core_metrics:: DYNAMIC_SUCCESS_BASED_ROUTING . add (
847
825
1 ,
848
826
router_env:: metric_attributes!(
@@ -915,12 +893,74 @@ pub async fn push_metrics_with_update_window_for_success_based_routing(
915
893
) ;
916
894
logger:: debug!( "successfully pushed success_based_routing metrics" ) ;
917
895
918
- state
896
+ let duplicate_stats = state
919
897
. store
920
- . insert_dynamic_routing_stat_entry ( dynamic_routing_stats)
898
+ . find_dynamic_routing_stats_optional_by_attempt_id_merchant_id (
899
+ payment_attempt. attempt_id . clone ( ) ,
900
+ & payment_attempt. merchant_id . to_owned ( ) ,
901
+ )
921
902
. await
922
903
. change_context ( errors:: ApiErrorResponse :: InternalServerError )
923
- . attach_printable ( "Unable to push dynamic routing stats to db" ) ?;
904
+ . attach_printable ( "Failed to fetch dynamic_routing_stats entry" ) ?;
905
+
906
+ if duplicate_stats. is_some ( ) {
907
+ let dynamic_routing_update = DynamicRoutingStatsUpdate {
908
+ amount : payment_attempt. get_total_amount ( ) ,
909
+ success_based_routing_connector : first_merchant_success_based_connector_label
910
+ . to_string ( ) ,
911
+ payment_connector : payment_connector. to_string ( ) ,
912
+ payment_method_type : payment_attempt. payment_method_type ,
913
+ currency : payment_attempt. currency ,
914
+ payment_method : payment_attempt. payment_method ,
915
+ capture_method : payment_attempt. capture_method ,
916
+ authentication_type : payment_attempt. authentication_type ,
917
+ payment_status : payment_attempt. status ,
918
+ conclusive_classification : outcome,
919
+ global_success_based_connector : Some (
920
+ first_global_success_based_connector. label . to_string ( ) ,
921
+ ) ,
922
+ } ;
923
+
924
+ state
925
+ . store
926
+ . update_dynamic_routing_stats (
927
+ payment_attempt. attempt_id . clone ( ) ,
928
+ & payment_attempt. merchant_id . to_owned ( ) ,
929
+ dynamic_routing_update,
930
+ )
931
+ . await
932
+ . change_context ( errors:: ApiErrorResponse :: InternalServerError )
933
+ . attach_printable ( "Unable to update dynamic routing stats to db" ) ?;
934
+ } else {
935
+ let dynamic_routing_stats = DynamicRoutingStatsNew {
936
+ payment_id : payment_attempt. payment_id . to_owned ( ) ,
937
+ attempt_id : payment_attempt. attempt_id . clone ( ) ,
938
+ merchant_id : payment_attempt. merchant_id . to_owned ( ) ,
939
+ profile_id : payment_attempt. profile_id . to_owned ( ) ,
940
+ amount : payment_attempt. get_total_amount ( ) ,
941
+ success_based_routing_connector : first_merchant_success_based_connector_label
942
+ . to_string ( ) ,
943
+ payment_connector : payment_connector. to_string ( ) ,
944
+ payment_method_type : payment_attempt. payment_method_type ,
945
+ currency : payment_attempt. currency ,
946
+ payment_method : payment_attempt. payment_method ,
947
+ capture_method : payment_attempt. capture_method ,
948
+ authentication_type : payment_attempt. authentication_type ,
949
+ payment_status : payment_attempt. status ,
950
+ conclusive_classification : outcome,
951
+ created_at : common_utils:: date_time:: now ( ) ,
952
+ global_success_based_connector : Some (
953
+ first_global_success_based_connector. label . to_string ( ) ,
954
+ ) ,
955
+ } ;
956
+
957
+ state
958
+ . store
959
+ . insert_dynamic_routing_stat_entry ( dynamic_routing_stats)
960
+ . await
961
+ . change_context ( errors:: ApiErrorResponse :: InternalServerError )
962
+ . attach_printable ( "Unable to push dynamic routing stats to db" ) ?;
963
+ } ;
924
964
925
965
client
926
966
. update_success_rate (
0 commit comments