From 554d84266febdced15bb0c0c73b278c99b3835b6 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Wed, 20 May 2015 14:11:39 +0300 Subject: [PATCH] higher weights are better --- engine/action.go | 3 ++- engine/action_timing.go | 3 ++- engine/action_trigger.go | 3 ++- engine/actions_local_test.go | 5 +++-- engine/actions_test.go | 8 ++++---- engine/balances.go | 1 + engine/calldesc_test.go | 8 +++++--- engine/lcr.go | 1 + engine/rateinterval.go | 3 ++- engine/timespans.go | 5 +++-- 10 files changed, 25 insertions(+), 15 deletions(-) diff --git a/engine/action.go b/engine/action.go index df55f4ce6..7c4cd6022 100644 --- a/engine/action.go +++ b/engine/action.go @@ -502,7 +502,8 @@ func (apl Actions) Swap(i, j int) { apl[i], apl[j] = apl[j], apl[i] } -func (apl Actions) Less(i, j int) bool { +// we need higher weights earlyer in the list +func (apl Actions) Less(j, i int) bool { return apl[i].Weight < apl[j].Weight } diff --git a/engine/action_timing.go b/engine/action_timing.go index bc11ef486..ead505e58 100644 --- a/engine/action_timing.go +++ b/engine/action_timing.go @@ -297,7 +297,8 @@ func (atpl ActionTimingPriotityList) Swap(i, j int) { func (atpl ActionTimingPriotityList) Less(i, j int) bool { if atpl[i].GetNextStartTime(time.Now()).Equal(atpl[j].GetNextStartTime(time.Now())) { - return atpl[i].Weight < atpl[j].Weight + // higher weights earlyer in the list + return atpl[i].Weight > atpl[j].Weight } return atpl[i].GetNextStartTime(time.Now()).Before(atpl[j].GetNextStartTime(time.Now())) } diff --git a/engine/action_trigger.go b/engine/action_trigger.go index edc8bd803..1dd490f3b 100644 --- a/engine/action_trigger.go +++ b/engine/action_trigger.go @@ -151,7 +151,8 @@ func (atpl ActionTriggerPriotityList) Swap(i, j int) { atpl[i], atpl[j] = atpl[j], atpl[i] } -func (atpl ActionTriggerPriotityList) Less(i, j int) bool { +//we need higher weights earlyer in the list +func (atpl ActionTriggerPriotityList) Less(j, i int) bool { return atpl[i].Weight < atpl[j].Weight } diff --git a/engine/actions_local_test.go b/engine/actions_local_test.go index 1fbbb1664..9f6390400 100644 --- a/engine/actions_local_test.go +++ b/engine/actions_local_test.go @@ -19,12 +19,13 @@ along with this program. If not, see package engine import ( - "github.com/cgrates/cgrates/config" - "github.com/cgrates/cgrates/utils" "net/rpc" "net/rpc/jsonrpc" "path" "testing" + + "github.com/cgrates/cgrates/config" + "github.com/cgrates/cgrates/utils" ) var actsLclCfg *config.CGRConfig diff --git a/engine/actions_test.go b/engine/actions_test.go index 539c0bb7b..7fd6935ae 100644 --- a/engine/actions_test.go +++ b/engine/actions_test.go @@ -463,7 +463,7 @@ func TestActionTimingPriotityListWeight(t *testing.T) { StartTime: "00:00:00", }, }, - Weight: 10.0, + Weight: 20, } at2 := &ActionTiming{ Timing: &RateInterval{ @@ -473,7 +473,7 @@ func TestActionTimingPriotityListWeight(t *testing.T) { StartTime: "00:00:00", }, }, - Weight: 20.0, + Weight: 10, } var atpl ActionTimingPriotityList atpl = append(atpl, at2, at1) @@ -634,9 +634,9 @@ func TestActionTriggerMatchAll(t *testing.T) { } func TestActionTriggerPriotityList(t *testing.T) { - at1 := &ActionTrigger{Weight: 10} + at1 := &ActionTrigger{Weight: 30} at2 := &ActionTrigger{Weight: 20} - at3 := &ActionTrigger{Weight: 30} + at3 := &ActionTrigger{Weight: 10} var atpl ActionTriggerPriotityList atpl = append(atpl, at2, at1, at3) atpl.Sort() diff --git a/engine/balances.go b/engine/balances.go index b69f16670..4a5f0d054 100644 --- a/engine/balances.go +++ b/engine/balances.go @@ -521,6 +521,7 @@ func (bc BalanceChain) Swap(i, j int) { bc[i], bc[j] = bc[j], bc[i] } +// we need the better ones at the beginning func (bc BalanceChain) Less(j, i int) bool { return bc[i].precision < bc[j].precision || (bc[i].precision == bc[j].precision && bc[i].Weight < bc[j].Weight) diff --git a/engine/calldesc_test.go b/engine/calldesc_test.go index 610236409..cd0642a3b 100644 --- a/engine/calldesc_test.go +++ b/engine/calldesc_test.go @@ -42,10 +42,12 @@ func populateDB() { &Action{ActionType: "*topup", BalanceType: utils.MONETARY, Direction: OUTBOUND, Balance: &Balance{Value: 10}}, &Action{ActionType: "*topup", BalanceType: utils.VOICE, Direction: OUTBOUND, Balance: &Balance{Weight: 20, Value: 10, DestinationIds: "NAT"}}, } + ats1 := []*Action{ - &Action{ActionType: "*topup", BalanceType: utils.MONETARY, Direction: OUTBOUND, Balance: &Balance{Value: 10}, Weight: 20}, - &Action{ActionType: "*reset_account", Weight: 10}, + &Action{ActionType: "*topup", BalanceType: utils.MONETARY, Direction: OUTBOUND, Balance: &Balance{Value: 10}, Weight: 10}, + &Action{ActionType: "*reset_account", Weight: 20}, } + minu := &Account{ Id: "*out:vdf:minu", BalanceMap: map[string]BalanceChain{ @@ -235,7 +237,7 @@ func TestCalldescHolliday(t *testing.T) { }, &RateInterval{ Timing: &RITiming{Months: utils.Months{time.May}, MonthDays: utils.MonthDays{1}, StartTime: "00:00:00"}, - Weight: 5, + Weight: 20, }, }, }, diff --git a/engine/lcr.go b/engine/lcr.go index 8a025919a..8f7b858c4 100644 --- a/engine/lcr.go +++ b/engine/lcr.go @@ -158,6 +158,7 @@ func (es LCREntriesSorter) Swap(i, j int) { es[i], es[j] = es[j], es[i] } +// we need the best earlyer in the list func (es LCREntriesSorter) Less(j, i int) bool { return es[i].Weight < es[j].Weight || (es[i].Weight == es[j].Weight && es[i].precision < es[j].precision) diff --git a/engine/rateinterval.go b/engine/rateinterval.go index 19f6149ea..9927ba5e8 100644 --- a/engine/rateinterval.go +++ b/engine/rateinterval.go @@ -364,7 +364,8 @@ func (il RateIntervalList) Swap(i, j int) { il[i], il[j] = il[j], il[i] } -func (il RateIntervalList) Less(i, j int) bool { +// we need higher weights earlyer in the list +func (il RateIntervalList) Less(j, i int) bool { return il[i].Weight < il[j].Weight } diff --git a/engine/timespans.go b/engine/timespans.go index c923f98f3..0da7e3040 100644 --- a/engine/timespans.go +++ b/engine/timespans.go @@ -248,6 +248,7 @@ or if the Weights are equal and new price is lower then spans's interval price */ func (ts *TimeSpan) SetRateInterval(i *RateInterval) { //log.Printf("SETRATEINTERVAL: %+v", i.Timing) + // higher weights are better if ts.RateInterval == nil || ts.RateInterval.Weight < i.Weight { ts.RateInterval = i //log.Printf("RET TS: %+v", ts.RateInterval.Timing) @@ -548,9 +549,9 @@ func (ts *TimeSpan) hasBetterRateIntervalThan(interval *RateInterval) bool { //log.Print("StartTime: ", ts.TimeStart) //log.Printf("OWN: %+v", ts.RateInterval) //log.Printf("OTHER: %+v", interval) - // the lower the weight the better + // the higher the weight the better if ts.RateInterval != nil && - ts.RateInterval.Weight < interval.Weight { + ts.RateInterval.Weight > interval.Weight { return true } // check interval is closer than the new one