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): elimination routing switch for toggling the feature #6568

Merged
merged 35 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
d983f6c
feat(routing): elimination routing feature
prajjwalkumar17 Nov 13, 2024
2f61113
chore: run formatter
hyperswitch-bot[bot] Nov 14, 2024
ecd8611
clippy lints
prajjwalkumar17 Nov 14, 2024
62cd750
chore: run formatter
hyperswitch-bot[bot] Nov 14, 2024
6dd7e9c
add configs
prajjwalkumar17 Nov 16, 2024
7ef5427
Merge branch 'feat_elimination_routing' of https://github.com/juspay/…
prajjwalkumar17 Nov 16, 2024
bf1aa32
chore: run formatter
hyperswitch-bot[bot] Nov 16, 2024
4c51dd4
toggle api working for Elimination Routing
prajjwalkumar17 Nov 17, 2024
fe43bbe
chore: run formatter
hyperswitch-bot[bot] Nov 17, 2024
21ce449
code refactors
prajjwalkumar17 Nov 18, 2024
6f85320
merge
prajjwalkumar17 Nov 18, 2024
223c19d
refactor old code
prajjwalkumar17 Nov 20, 2024
25a1ddf
add disable feature
prajjwalkumar17 Nov 21, 2024
e6d86b8
Merge branch 'main' into feat_elimination_routing
prajjwalkumar17 Nov 21, 2024
0f63a7f
refactor the code
prajjwalkumar17 Nov 22, 2024
ecbf598
Merge branch 'feat_elimination_routing' of github.com:juspay/hyperswi…
prajjwalkumar17 Nov 22, 2024
06db0e1
refactor code
prajjwalkumar17 Nov 24, 2024
8138814
chore: run formatter
hyperswitch-bot[bot] Nov 24, 2024
9d015f9
minor refactors to make code work
prajjwalkumar17 Nov 24, 2024
e1cf154
final refactors
prajjwalkumar17 Nov 25, 2024
f464b63
chore: run formatter
hyperswitch-bot[bot] Nov 25, 2024
30f4fa8
fix openapi
prajjwalkumar17 Nov 25, 2024
bb9262a
Merge branch 'feat_elimination_routing' of https://github.com/juspay/…
prajjwalkumar17 Nov 25, 2024
1f608b0
docs(openapi): re-generate OpenAPI specification
hyperswitch-bot[bot] Nov 25, 2024
bd62f26
fix spell checks
prajjwalkumar17 Nov 25, 2024
8de6d47
Merge branch 'feat_elimination_routing' of https://github.com/juspay/…
prajjwalkumar17 Nov 25, 2024
31eded0
add cache for elimination routing
prajjwalkumar17 Nov 25, 2024
9116e09
minor refactors
prajjwalkumar17 Nov 25, 2024
e838cad
clippy lints
prajjwalkumar17 Nov 25, 2024
db0b590
clippy lints
prajjwalkumar17 Nov 25, 2024
a4b234b
clippy lints
prajjwalkumar17 Nov 25, 2024
08e3389
fix clippy v2 lints
prajjwalkumar17 Nov 26, 2024
234b94d
chore: run formatter
hyperswitch-bot[bot] Nov 26, 2024
5d13f61
address comments
prajjwalkumar17 Nov 27, 2024
2c3f3c8
Merge branch 'main' into feat_elimination_routing
prajjwalkumar17 Nov 27, 2024
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 api-reference-v2/openapi_spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -1278,7 +1278,7 @@
],
"summary": "Routing - Create",
"description": "Create a routing algorithm",
"operationId": "Create a routing algprithm",
"operationId": "Create a routing algorithm",
"requestBody": {
"content": {
"application/json": {
Expand Down
64 changes: 32 additions & 32 deletions api-reference/openapi_spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -3931,7 +3931,7 @@
],
"summary": "Routing - Toggle success based dynamic routing for profile",
"description": "Create a success based dynamic routing algorithm",
"operationId": "Toggle success based dynamic routing algprithm",
"operationId": "Toggle success based dynamic routing algorithm",
"parameters": [
{
"name": "account_id",
Expand All @@ -3957,7 +3957,7 @@
"description": "Feature to enable for success based routing",
"required": true,
"schema": {
"$ref": "#/components/schemas/SuccessBasedRoutingFeatures"
"$ref": "#/components/schemas/DynamicRoutingFeatures"
}
}
],
Expand Down Expand Up @@ -10236,6 +10236,14 @@
}
}
},
"DynamicRoutingFeatures": {
"type": "string",
"enum": [
"metrics",
"dynamic_connector_selection",
"none"
]
},
"EnabledPaymentMethod": {
"type": "object",
"description": "Object for EnabledPaymentMethod",
Expand Down Expand Up @@ -23852,14 +23860,6 @@
"destination"
]
},
"SuccessBasedRoutingFeatures": {
"type": "string",
"enum": [
"metrics",
"dynamic_connector_selection",
"none"
]
},
"SurchargeDetailsResponse": {
"type": "object",
"required": [
Expand Down Expand Up @@ -24076,6 +24076,28 @@
}
}
},
"ToggleDynamicRoutingPath": {
"type": "object",
"required": [
"profile_id"
],
"properties": {
"profile_id": {
"type": "string"
}
}
},
"ToggleDynamicRoutingQuery": {
"type": "object",
"required": [
"enable"
],
"properties": {
"enable": {
"$ref": "#/components/schemas/DynamicRoutingFeatures"
}
}
},
"ToggleKVRequest": {
"type": "object",
"required": [
Expand Down Expand Up @@ -24109,28 +24131,6 @@
}
}
},
"ToggleSuccessBasedRoutingPath": {
"type": "object",
"required": [
"profile_id"
],
"properties": {
"profile_id": {
"type": "string"
}
}
},
"ToggleSuccessBasedRoutingQuery": {
"type": "object",
"required": [
"enable"
],
"properties": {
"enable": {
"$ref": "#/components/schemas/SuccessBasedRoutingFeatures"
}
}
},
"TouchNGoRedirection": {
"type": "object"
},
Expand Down
6 changes: 3 additions & 3 deletions crates/api_models/src/events/routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::routing::{
RoutingLinkWrapper, RoutingPayloadWrapper, RoutingRetrieveLinkQuery,
RoutingRetrieveLinkQueryWrapper, RoutingRetrieveQuery, SuccessBasedRoutingConfig,
SuccessBasedRoutingPayloadWrapper, SuccessBasedRoutingUpdateConfigQuery,
ToggleSuccessBasedRoutingQuery, ToggleSuccessBasedRoutingWrapper,
ToggleDynamicRoutingQuery, ToggleDynamicRoutingWrapper,
};

impl ApiEventMetric for RoutingKind {
Expand Down Expand Up @@ -79,7 +79,7 @@ impl ApiEventMetric for RoutingRetrieveLinkQueryWrapper {
}
}

impl ApiEventMetric for ToggleSuccessBasedRoutingQuery {
impl ApiEventMetric for ToggleDynamicRoutingQuery {
fn get_api_event_type(&self) -> Option<ApiEventsType> {
Some(ApiEventsType::Routing)
}
Expand All @@ -97,7 +97,7 @@ impl ApiEventMetric for SuccessBasedRoutingPayloadWrapper {
}
}

impl ApiEventMetric for ToggleSuccessBasedRoutingWrapper {
impl ApiEventMetric for ToggleDynamicRoutingWrapper {
fn get_api_event_type(&self) -> Option<ApiEventsType> {
Some(ApiEventsType::Routing)
}
Expand Down
184 changes: 165 additions & 19 deletions crates/api_models/src/routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,18 +523,118 @@ pub struct DynamicAlgorithmWithTimestamp<T> {
#[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize)]
pub struct DynamicRoutingAlgorithmRef {
pub success_based_algorithm: Option<SuccessBasedAlgorithm>,
pub elimination_routing_algorithm: Option<EliminationRoutingAlgorithm>,
}

pub trait DynamicRoutingAlgoAccessor {
fn get_algorithm_id_with_timestamp(
self,
) -> DynamicAlgorithmWithTimestamp<common_utils::id_type::RoutingId>;
fn get_enabled_features(&mut self) -> &mut DynamicRoutingFeatures;
}

impl DynamicRoutingAlgoAccessor for SuccessBasedAlgorithm {
fn get_algorithm_id_with_timestamp(
self,
) -> DynamicAlgorithmWithTimestamp<common_utils::id_type::RoutingId> {
self.algorithm_id_with_timestamp
}
fn get_enabled_features(&mut self) -> &mut DynamicRoutingFeatures {
&mut self.enabled_feature
}
}

impl DynamicRoutingAlgoAccessor for EliminationRoutingAlgorithm {
fn get_algorithm_id_with_timestamp(
self,
) -> DynamicAlgorithmWithTimestamp<common_utils::id_type::RoutingId> {
self.algorithm_id_with_timestamp
}
fn get_enabled_features(&mut self) -> &mut DynamicRoutingFeatures {
&mut self.enabled_feature
}
}

impl EliminationRoutingAlgorithm {
pub fn new(
algorithm_id_with_timestamp: DynamicAlgorithmWithTimestamp<
common_utils::id_type::RoutingId,
>,
) -> Self {
Self {
algorithm_id_with_timestamp,
enabled_feature: DynamicRoutingFeatures::None,
}
}
}

impl SuccessBasedAlgorithm {
pub fn new(
algorithm_id_with_timestamp: DynamicAlgorithmWithTimestamp<
common_utils::id_type::RoutingId,
>,
) -> Self {
Self {
algorithm_id_with_timestamp,
enabled_feature: DynamicRoutingFeatures::None,
}
}
}

impl DynamicRoutingAlgorithmRef {
pub fn update(&mut self, new: Self) {
if let Some(elimination_routing_algorithm) = new.elimination_routing_algorithm {
self.elimination_routing_algorithm = Some(elimination_routing_algorithm)
}
if let Some(success_based_algorithm) = new.success_based_algorithm {
self.success_based_algorithm = Some(success_based_algorithm)
}
}

pub fn update_specific_ref(
&mut self,
algo_type: DynamicRoutingType,
feature_to_enable: DynamicRoutingFeatures,
) {
match algo_type {
DynamicRoutingType::SuccessRateBasedRouting => {
self.success_based_algorithm
.as_mut()
.map(|algo| algo.enabled_feature = feature_to_enable);
}
DynamicRoutingType::EliminationRouting => {
self.elimination_routing_algorithm
.as_mut()
.map(|algo| algo.enabled_feature = feature_to_enable);
}
}
}
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct SuccessBasedAlgorithm {
pub algorithm_id_with_timestamp:
DynamicAlgorithmWithTimestamp<common_utils::id_type::RoutingId>,
#[serde(default)]
pub enabled_feature: SuccessBasedRoutingFeatures,
pub enabled_feature: DynamicRoutingFeatures,
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct EliminationRoutingAlgorithm {
pub algorithm_id_with_timestamp:
DynamicAlgorithmWithTimestamp<common_utils::id_type::RoutingId>,
#[serde(default)]
pub enabled_feature: DynamicRoutingFeatures,
}

impl EliminationRoutingAlgorithm {
pub fn update_enabled_features(&mut self, feature_to_enable: DynamicRoutingFeatures) {
self.enabled_feature = feature_to_enable
}
}

impl SuccessBasedAlgorithm {
pub fn update_enabled_features(&mut self, feature_to_enable: SuccessBasedRoutingFeatures) {
pub fn update_enabled_features(&mut self, feature_to_enable: DynamicRoutingFeatures) {
self.enabled_feature = feature_to_enable
}
}
Expand All @@ -543,26 +643,40 @@ impl DynamicRoutingAlgorithmRef {
pub fn update_algorithm_id(
&mut self,
new_id: common_utils::id_type::RoutingId,
enabled_feature: SuccessBasedRoutingFeatures,
enabled_feature: DynamicRoutingFeatures,
dynamic_routing_type: DynamicRoutingType,
) {
self.success_based_algorithm = Some(SuccessBasedAlgorithm {
algorithm_id_with_timestamp: DynamicAlgorithmWithTimestamp {
algorithm_id: Some(new_id),
timestamp: common_utils::date_time::now_unix_timestamp(),
},
enabled_feature,
})
match dynamic_routing_type {
DynamicRoutingType::SuccessRateBasedRouting => {
self.success_based_algorithm = Some(SuccessBasedAlgorithm {
algorithm_id_with_timestamp: DynamicAlgorithmWithTimestamp {
algorithm_id: Some(new_id),
timestamp: common_utils::date_time::now_unix_timestamp(),
},
enabled_feature,
})
}
DynamicRoutingType::EliminationRouting => {
self.elimination_routing_algorithm = Some(EliminationRoutingAlgorithm {
algorithm_id_with_timestamp: DynamicAlgorithmWithTimestamp {
algorithm_id: Some(new_id),
timestamp: common_utils::date_time::now_unix_timestamp(),
},
enabled_feature,
})
}
};
}
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, ToSchema)]
pub struct ToggleSuccessBasedRoutingQuery {
pub enable: SuccessBasedRoutingFeatures,
pub struct ToggleDynamicRoutingQuery {
pub enable: DynamicRoutingFeatures,
}

#[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize, PartialEq, Eq, ToSchema)]
#[serde(rename_all = "snake_case")]
pub enum SuccessBasedRoutingFeatures {
pub enum DynamicRoutingFeatures {
Metrics,
DynamicConnectorSelection,
#[default]
Expand All @@ -578,26 +692,52 @@ pub struct SuccessBasedRoutingUpdateConfigQuery {
}

#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
pub struct ToggleSuccessBasedRoutingWrapper {
pub struct ToggleDynamicRoutingWrapper {
pub profile_id: common_utils::id_type::ProfileId,
pub feature_to_enable: SuccessBasedRoutingFeatures,
pub feature_to_enable: DynamicRoutingFeatures,
}

#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, ToSchema)]
pub struct ToggleSuccessBasedRoutingPath {
pub struct ToggleDynamicRoutingPath {
#[schema(value_type = String)]
pub profile_id: common_utils::id_type::ProfileId,
}

#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, ToSchema)]
pub struct EliminationRoutingConfig {
pub params: Option<Vec<DynamicRoutingConfigParams>>,
// pub labels: Option<Vec<String>>,
pub elimination_analyser_config: Option<EliminationAnalyserConfig>,
}

#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, ToSchema)]
pub struct EliminationAnalyserConfig {
pub bucket_size: Option<u32>,
pub bucket_ttl_in_mins: Option<f64>,
}

impl Default for EliminationRoutingConfig {
fn default() -> Self {
Self {
params: Some(vec![DynamicRoutingConfigParams::PaymentMethod]),
elimination_analyser_config: Some(EliminationAnalyserConfig {
bucket_size: Some(5),
bucket_ttl_in_mins: Some(2.0),
}),
}
}
}

#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, ToSchema)]
pub struct SuccessBasedRoutingConfig {
pub params: Option<Vec<SuccessBasedRoutingConfigParams>>,
pub params: Option<Vec<DynamicRoutingConfigParams>>,
pub config: Option<SuccessBasedRoutingConfigBody>,
}

impl Default for SuccessBasedRoutingConfig {
fn default() -> Self {
Self {
params: Some(vec![SuccessBasedRoutingConfigParams::PaymentMethod]),
params: Some(vec![DynamicRoutingConfigParams::PaymentMethod]),
config: Some(SuccessBasedRoutingConfigBody {
min_aggregates_size: Some(2),
default_success_rate: Some(100.0),
Expand All @@ -612,7 +752,7 @@ impl Default for SuccessBasedRoutingConfig {
}

#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, ToSchema, strum::Display)]
pub enum SuccessBasedRoutingConfigParams {
pub enum DynamicRoutingConfigParams {
PaymentMethod,
PaymentMethodType,
AuthenticationType,
Expand Down Expand Up @@ -643,6 +783,12 @@ pub struct SuccessBasedRoutingPayloadWrapper {
pub profile_id: common_utils::id_type::ProfileId,
}

#[derive(Debug, Clone, strum::Display, serde::Serialize, serde::Deserialize)]
pub enum DynamicRoutingType {
SuccessRateBasedRouting,
EliminationRouting,
}

impl SuccessBasedRoutingConfig {
pub fn update(&mut self, new: Self) {
if let Some(params) = new.params {
Expand Down
Loading
Loading