OPS-465: Fix pinned routes weight (#132)

* OPS-465: Fix pinned routes weight

* Get rid of useless test
This commit is contained in:
ndiezel0 2024-05-16 16:52:14 +05:00 committed by GitHub
parent 1d6d7a647f
commit ef25bdc20a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 74 additions and 51 deletions

View File

@ -30,7 +30,6 @@
-export([routes_selected_for_low_risk_score/1]).
-export([terminal_priority_for_shop/1]).
-export([gather_pinned_route/1]).
-export([choose_pinned_route/1]).
-export([choose_route_w_override/1]).
-define(PROVIDER_MIN_ALLOWED, ?cash(1000, <<"RUB">>)).
@ -73,7 +72,6 @@ groups() ->
terminal_priority_for_shop,
gather_pinned_route,
choose_pinned_route,
choose_route_w_override
]}
].
@ -796,34 +794,6 @@ gather_pinned_route(_C) ->
Routes
).
-spec choose_pinned_route(config()) -> test_return().
choose_pinned_route(_C) ->
Currency = ?cur(<<"RUB">>),
PaymentTool = {payment_terminal, #domain_PaymentTerminal{payment_service = ?pmt_srv(<<"euroset-ref">>)}},
Pin1 = #{
currency => Currency,
payment_tool => PaymentTool,
client_ip => undefined
},
Pin2 = #{
currency => Currency,
payment_tool => PaymentTool
},
Pin3 = #{
currency => Currency,
payment_tool => PaymentTool
},
Route1 = hg_route:new(?prv(1), ?trm(1), 0, 0, Pin1),
Route2 = hg_route:new(?prv(1), ?trm(1), 0, 0, Pin2),
Route3 = hg_route:new(?prv(1), ?trm(1), 0, 0, Pin3),
Routes = [
Route1,
Route2,
Route3
],
[ChosenRoute | _] = lists:sort(Routes),
{ChosenRoute, _} = hg_routing:choose_route(Routes).
-spec choose_route_w_override(config()) -> test_return().
choose_route_w_override(_C) ->
%% without overrides

View File

@ -347,23 +347,42 @@ find_best_routes([First | Rest]) ->
select_better_route({LeftScore, _} = Left, {RightScore, _} = Right) ->
LeftPin = LeftScore#domain_PaymentRouteScores.route_pin,
RightPin = RightScore#domain_PaymentRouteScores.route_pin,
case {LeftPin, RightPin} of
_ when LeftPin /= ?ZERO, RightPin /= ?ZERO ->
select_better_pinned_route(Left, Right);
_ ->
select_better_regular_route(Left, Right)
end.
Res =
case {LeftPin, RightPin} of
_ when LeftPin /= ?ZERO, RightPin /= ?ZERO, LeftPin == RightPin ->
select_better_pinned_route(Left, Right);
_ ->
select_better_regular_route(Left, Right)
end,
Res.
select_better_pinned_route({LeftScore, _Route1} = Left, {RightScore, _Route2} = Right) ->
case max(LeftScore, RightScore) of
LeftScore ->
select_better_pinned_route({LeftScore0, _Route1} = Left, {RightScore0, _Route2} = Right) ->
LeftScore1 = LeftScore0#domain_PaymentRouteScores{
random_condition = 0
},
RightScore1 = RightScore0#domain_PaymentRouteScores{
random_condition = 0
},
case max(LeftScore1, RightScore1) of
LeftScore1 ->
Left;
RightScore ->
RightScore1 ->
Right
end.
select_better_regular_route(Left, Right) ->
max(Left, Right).
select_better_regular_route({LeftScore0, LRoute}, {RightScore0, RRoute}) ->
LeftScore1 = LeftScore0#domain_PaymentRouteScores{
route_pin = 0
},
RightScore1 = RightScore0#domain_PaymentRouteScores{
route_pin = 0
},
case max({LeftScore1, LRoute}, {RightScore1, RRoute}) of
{LeftScore1, LRoute} ->
{LeftScore0, LRoute};
{RightScore1, RRoute} ->
{RightScore0, RRoute}
end.
select_better_route_ideal(Left, Right) ->
IdealLeft = set_ideal_score(Left),
@ -525,20 +544,18 @@ score_route_ext({Route, ProviderStatus}) ->
score_route(Route) ->
PriorityRate = hg_route:priority(Route),
Pin = hg_route:pin(Route),
{RandomCondition, PinHash} =
case Pin of
#{} when map_size(Pin) == 0 ->
{hg_route:weight(Route), ?ZERO};
_ ->
{?ZERO, erlang:phash2(Pin)}
end,
#domain_PaymentRouteScores{
terminal_priority_rating = PriorityRate,
route_pin = PinHash,
random_condition = RandomCondition,
route_pin = get_pin_hash(Pin),
random_condition = hg_route:weight(Route),
blacklist_condition = 0
}.
get_pin_hash(Pin) when map_size(Pin) == 0 ->
?ZERO;
get_pin_hash(Pin) ->
erlang:phash2(Pin).
get_availability_score({alive, FailRate}) -> {1, 1.0 - FailRate};
get_availability_score({dead, FailRate}) -> {0, 1.0 - FailRate}.
@ -868,6 +885,42 @@ pin_random_test() ->
lists:seq(0, 1000)
).
-spec pin_weight_test() -> _.
pin_weight_test() ->
Pin0 = #{
email => <<"example@mail.com">>
},
Pin1 = #{
email => <<"example2@mail.com">>
},
Scores = {{alive, 0.0}, {normal, 0.0}},
Route1 = {hg_route:new(?prv(1), ?trm(1), 50, 1, Pin0), Scores},
Route2 = {hg_route:new(?prv(2), ?trm(2), 50, 1, Pin1), Scores},
{_, DiffTimes} = lists:foldl(
fun(_I, {Acc, Iter}) ->
BalancedRoutes = balance_routes([Route1, Route2]),
ScoredRoutes = score_routes(BalancedRoutes),
{{_, ChosenScoredRoute}, _IdealRoute} = find_best_routes(ScoredRoutes),
case Acc of
undefined ->
{ChosenScoredRoute, Iter};
_ ->
ChosenTerminal = hg_route:terminal_ref(ChosenScoredRoute),
case hg_route:terminal_ref(Acc) of
ChosenTerminal ->
{Acc, Iter};
_ ->
{Acc, Iter + 1}
end
end
end,
{undefined, 0},
lists:seq(0, 1000)
),
?assertNotEqual(0, DiffTimes),
?assertEqual(true, DiffTimes > 300),
?assertEqual(true, DiffTimes < 700).
-spec balance_routes_test_() -> [testcase()].
balance_routes_test_() ->
Status = {{alive, 0.0}, {normal, 0.0}},