New supplier data in tariffplan files to replicate *least_cost strategy

This commit is contained in:
DanB
2017-12-01 18:43:41 +01:00
parent e194ab86bd
commit 705453f97f
7 changed files with 77 additions and 28 deletions

View File

@@ -103,7 +103,7 @@ func TestSMGV1CacheStats(t *testing.T) {
expectedStats := &utils.CacheStats{Destinations: 5, ReverseDestinations: 7, RatingPlans: 4, RatingProfiles: 10,
Actions: 9, ActionPlans: 4, AccountActionPlans: 5, SharedGroups: 1, DerivedChargers: 1,
LcrProfiles: 5, CdrStats: 6, Users: 3, Aliases: 1, ReverseAliases: 2, ResourceProfiles: 3, Resources: 3, StatQueues: 1,
StatQueueProfiles: 1, Thresholds: 7, ThresholdProfiles: 7, Filters: 16, SupplierProfiles: 2}
StatQueueProfiles: 1, Thresholds: 7, ThresholdProfiles: 7, Filters: 16, SupplierProfiles: 3}
var args utils.AttrCacheStats
if err := smgV1Rpc.Call("ApierV1.GetCacheStats", args, &rcvStats); err != nil {
t.Error("Got error on ApierV1.GetCacheStats: ", err.Error())

View File

@@ -47,7 +47,8 @@ var sTestsSupplierSV1 = []func(t *testing.T){
testV1SplSStartEngine,
testV1SplSRpcConn,
testV1SplSFromFolder,
testV1SplSGetWeightSuppliers,
//testV1SplSGetWeightSuppliers,
testV1SplSGetLeastCostSuppliers,
testV1SplSStopEngine,
}
@@ -133,13 +134,61 @@ func testV1SplSGetWeightSuppliers(t *testing.T) {
&engine.SortedSupplier{
SupplierID: "supplier2",
SortingData: map[string]interface{}{
"Weight": 20.0,
utils.Weight: 20.0,
},
},
&engine.SortedSupplier{
SupplierID: "supplier1",
SortingData: map[string]interface{}{
"Weight": 10.0,
utils.Weight: 10.0,
},
},
},
}
var suplsReply engine.SortedSuppliers
if err := splSv1Rpc.Call(utils.SupplierSv1GetSuppliers,
ev, &suplsReply); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(eSpls, suplsReply) {
t.Errorf("Expecting: %s, received: %s",
utils.ToJSON(eSpls), utils.ToJSON(suplsReply))
}
}
func testV1SplSGetLeastCostSuppliers(t *testing.T) {
ev := &engine.SupplierEvent{
Tenant: "cgrates.org",
ID: "testV1SplSGetLeastCostSuppliers",
Event: map[string]interface{}{
utils.ACCOUNT: "1001",
utils.DESTINATION: "1002",
utils.ANSWER_TIME: time.Date(2017, 12, 1, 14, 25, 0, 0, time.UTC),
utils.USAGE: "1m20s",
},
}
eSpls := engine.SortedSuppliers{
ProfileID: "SPL_LEASTCOST_1",
Sorting: utils.MetaWeight,
SortedSuppliers: []*engine.SortedSupplier{
&engine.SortedSupplier{
SupplierID: "supplier3",
SortingData: map[string]interface{}{
utils.Weight: 25.0,
utils.Cost: 0.2,
},
},
&engine.SortedSupplier{
SupplierID: "supplier1",
SortingData: map[string]interface{}{
utils.Weight: 10.0,
utils.Cost: 0.2,
},
},
&engine.SortedSupplier{
SupplierID: "supplier2",
SortingData: map[string]interface{}{
utils.Weight: 20.0,
utils.Cost: 0.2,
},
},
},

View File

@@ -3,3 +3,6 @@ cgrates.org,SPP_1,FLTR_ACNT_dan;FLTR_DST_DE,2017-07-29T15:00:00Z,*lowest_cost,,s
cgrates.org,SPL_WEIGHT_1,FLTR_DST_DE;FLTR_ACNT_1007,2017-11-27T00:00:00Z,*weight,,supplier1,,,,,,10,false,10
cgrates.org,SPL_WEIGHT_1,FLTR_DST_DE,,,,supplier2,,,,,,20,,
cgrates.org,SPL_WEIGHT_1,FLTR_ACNT_1007,,,,supplier3,FLTR_ACNT_dan,,,,,15,,
cgrates.org,SPL_LEASTCOST_1,FLTR_1,2017-11-27T00:00:00Z,*weight,,supplier1,,RP_SPECIAL_1002,,,,10,false,10
cgrates.org,SPL_LEASTCOST_1,,,,,supplier2,,RP_RETAIL1,,,,20,,
cgrates.org,SPL_LEASTCOST_1,,,,,supplier3,,RP_SPECIAL_1002,,,,15,,
1 #Tenant ID FilterIDs ActivationInterval Sorting SortingParams SupplierID SupplierFilterIDs SupplierAccountIDs SupplierRatingPlanIDs SupplierResourceIDs SupplierStatIDs SupplierWeight Blocker Weight
3 cgrates.org SPL_WEIGHT_1 FLTR_DST_DE;FLTR_ACNT_1007 2017-11-27T00:00:00Z *weight supplier1 10 false 10
4 cgrates.org SPL_WEIGHT_1 FLTR_DST_DE supplier2 20
5 cgrates.org SPL_WEIGHT_1 FLTR_ACNT_1007 supplier3 FLTR_ACNT_dan 15
6 cgrates.org SPL_LEASTCOST_1 FLTR_1 2017-11-27T00:00:00Z *weight supplier1 RP_SPECIAL_1002 10 false 10
7 cgrates.org SPL_LEASTCOST_1 supplier2 RP_RETAIL1 20
8 cgrates.org SPL_LEASTCOST_1 supplier3 RP_SPECIAL_1002 15

View File

@@ -26,11 +26,6 @@ import (
"github.com/cgrates/cgrates/utils"
)
const (
Weight = "Weight"
Cost = "Cost"
)
// SuppliersReply is returned as part of GetSuppliers call
type SortedSuppliers struct {
ProfileID string // Profile matched
@@ -47,7 +42,7 @@ type SortedSupplier struct {
// SortWeight is part of sort interface, sort based on Weight
func (sSpls *SortedSuppliers) SortWeight() {
sort.Slice(sSpls.SortedSuppliers, func(i, j int) bool {
return sSpls.SortedSuppliers[i].SortingData[Weight].(float64) > sSpls.SortedSuppliers[j].SortingData[Weight].(float64)
return sSpls.SortedSuppliers[i].SortingData[utils.Weight].(float64) > sSpls.SortedSuppliers[j].SortingData[utils.Weight].(float64)
})
}
@@ -55,10 +50,10 @@ func (sSpls *SortedSuppliers) SortWeight() {
// sort based on Cost with fallback on Weight
func (sSpls *SortedSuppliers) SortCost() {
sort.Slice(sSpls.SortedSuppliers, func(i, j int) bool {
if sSpls.SortedSuppliers[i].SortingData[Cost].(float64) == sSpls.SortedSuppliers[j].SortingData[Cost].(float64) {
return sSpls.SortedSuppliers[i].SortingData[Weight].(float64) > sSpls.SortedSuppliers[j].SortingData[Weight].(float64)
if sSpls.SortedSuppliers[i].SortingData[utils.Cost].(float64) == sSpls.SortedSuppliers[j].SortingData[utils.Cost].(float64) {
return sSpls.SortedSuppliers[i].SortingData[utils.Weight].(float64) > sSpls.SortedSuppliers[j].SortingData[utils.Weight].(float64)
}
return sSpls.SortedSuppliers[i].SortingData[Cost].(float64) < sSpls.SortedSuppliers[j].SortingData[Cost].(float64)
return sSpls.SortedSuppliers[i].SortingData[utils.Cost].(float64) < sSpls.SortedSuppliers[j].SortingData[utils.Cost].(float64)
})
}
@@ -172,7 +167,7 @@ func (ws *WeightSorter) SortSuppliers(prflID string,
for i, s := range suppls {
sortedSuppls.SortedSuppliers[i] = &SortedSupplier{
SupplierID: s.ID,
SortingData: map[string]interface{}{Weight: s.Weight}}
SortingData: map[string]interface{}{utils.Weight: s.Weight}}
}
sortedSuppls.SortWeight()
return

View File

@@ -30,22 +30,22 @@ func TestLibSuppliersSortCost(t *testing.T) {
&SortedSupplier{
SupplierID: "supplier1",
SortingData: map[string]interface{}{
Cost: 0.1,
Weight: 10.0,
utils.Cost: 0.1,
utils.Weight: 10.0,
},
},
&SortedSupplier{
SupplierID: "supplier2",
SortingData: map[string]interface{}{
Cost: 0.1,
Weight: 20.0,
utils.Cost: 0.1,
utils.Weight: 20.0,
},
},
&SortedSupplier{
SupplierID: "supplier3",
SortingData: map[string]interface{}{
Cost: 0.05,
Weight: 10.0,
utils.Cost: 0.05,
utils.Weight: 10.0,
},
},
},
@@ -56,22 +56,22 @@ func TestLibSuppliersSortCost(t *testing.T) {
&SortedSupplier{
SupplierID: "supplier3",
SortingData: map[string]interface{}{
Cost: 0.05,
Weight: 10.0,
utils.Cost: 0.05,
utils.Weight: 10.0,
},
},
&SortedSupplier{
SupplierID: "supplier2",
SortingData: map[string]interface{}{
Cost: 0.1,
Weight: 20.0,
utils.Cost: 0.1,
utils.Weight: 20.0,
},
},
&SortedSupplier{
SupplierID: "supplier1",
SortingData: map[string]interface{}{
Cost: 0.1,
Weight: 10.0,
utils.Cost: 0.1,
utils.Weight: 10.0,
},
},
},

View File

@@ -46,8 +46,8 @@ func (lcs *LeastCostSorter) SortSuppliers(prflID string,
sortedSuppls.SortedSuppliers[i] = &SortedSupplier{
SupplierID: s.ID,
SortingData: map[string]interface{}{
Weight: s.Weight,
Cost: ec.GetCost()}}
utils.Weight: s.Weight,
utils.Cost: ec.GetCost()}}
}
sortedSuppls.SortCost()
return

View File

@@ -590,6 +590,8 @@ const (
SupplierS = "SupplierS"
MetaWeight = "*weight"
MetaLeastCost = "*least_cost"
Weight = "Weight"
Cost = "Cost"
)
//Meta