From 3c4fbaa10f2d18875a83e431b8899b901623aca5 Mon Sep 17 00:00:00 2001 From: DanB Date: Sun, 3 Dec 2017 19:31:59 +0100 Subject: [PATCH] Considering account bundles within *least_cost strategy --- engine/spls_leastcost.go | 16 ++++++++++------ engine/suppliers.go | 36 ++++++++++++++++++++++++++++++++---- utils/consts.go | 1 + 3 files changed, 43 insertions(+), 10 deletions(-) diff --git a/engine/spls_leastcost.go b/engine/spls_leastcost.go index 110596061..45312585d 100644 --- a/engine/spls_leastcost.go +++ b/engine/spls_leastcost.go @@ -41,20 +41,24 @@ func (lcs *LeastCostSorter) SortSuppliers(prflID string, Sorting: lcs.sorting, SortedSuppliers: make([]*SortedSupplier, 0)} for _, s := range suppls { - ec, err := lcs.spS.costForEvent(ev, s.AccountIDs, s.RatingPlanIDs) + costData, err := lcs.spS.costForEvent(ev, s.AccountIDs, s.RatingPlanIDs) if err != nil { return nil, err - } else if ec == nil { + } else if len(costData) == 0 { utils.Logger.Warning( fmt.Sprintf("<%s> profile: %s ignoring supplier with ID: %s, missing cost information", utils.SupplierS, prflID, s.ID)) continue } + srtData := map[string]interface{}{ + utils.Weight: s.Weight, + } + for k, v := range costData { + srtData[k] = v + } sortedSuppls.SortedSuppliers = append(sortedSuppls.SortedSuppliers, &SortedSupplier{ - SupplierID: s.ID, - SortingData: map[string]interface{}{ - utils.Weight: s.Weight, - utils.Cost: ec.GetCost()}}) + SupplierID: s.ID, + SortingData: srtData}) } sortedSuppls.SortCost() return diff --git a/engine/suppliers.go b/engine/suppliers.go index bd2b77f3e..5fe401a7f 100644 --- a/engine/suppliers.go +++ b/engine/suppliers.go @@ -167,8 +167,9 @@ func (spS *SupplierService) matchingSupplierProfilesForEvent(ev *SupplierEvent) } // costForEvent will compute cost out of accounts and rating plans for event +// returns map[string]interface{} with cost and relevant matching information inside func (spS *SupplierService) costForEvent(ev *SupplierEvent, - acntIDs, rpIDs []string) (ec *EventCost, err error) { + acntIDs, rpIDs []string) (costData map[string]interface{}, err error) { if err = ev.CheckMandatoryFields([]string{utils.ACCOUNT, utils.DESTINATION, utils.ANSWER_TIME, utils.USAGE}); err != nil { return @@ -194,6 +195,29 @@ func (spS *SupplierService) costForEvent(ev *SupplierEvent, if usage, err = ev.FieldAsDuration(utils.USAGE); err != nil { return } + for _, anctID := range acntIDs { + cd := &CallDescriptor{ + Direction: utils.OUT, + Category: utils.MetaSuppliers, + Tenant: ev.Tenant, + Subject: subj, + Account: anctID, + Destination: dst, + TimeStart: aTime, + TimeEnd: aTime.Add(usage), + DurationIndex: usage, + } + if maxDur, err := cd.GetMaxSessionDuration(); err != nil { + utils.Logger.Warning( + fmt.Sprintf("<%s> ignoring cost for account: %s, err: %s", + anctID, err.Error())) + } else if maxDur >= usage { + return map[string]interface{}{ + utils.Cost: 0.0, + utils.ACCOUNT: anctID, + }, nil + } + } for _, rp := range rpIDs { // loop through RatingPlans until we find one without errors rPrfl := &RatingProfile{ Id: utils.ConcatenatedKey(utils.OUT, @@ -206,8 +230,8 @@ func (spS *SupplierService) costForEvent(ev *SupplierEvent, }, } // force cache set so it can be picked by calldescriptor for cost calculation - cache.Set(utils.RATING_PROFILE_PREFIX+rPrfl.Id, rPrfl, - true, utils.NonTransactional) + cacheKey := utils.RATING_PROFILE_PREFIX + rPrfl.Id + cache.Set(cacheKey, rPrfl, true, utils.NonTransactional) cd := &CallDescriptor{ Direction: utils.OUT, Category: utils.MetaSuppliers, @@ -220,13 +244,17 @@ func (spS *SupplierService) costForEvent(ev *SupplierEvent, DurationIndex: usage, } cc, err := cd.GetCost() + cache.RemKey(cacheKey, true, utils.NonTransactional) // Remove here so we don't overload memory if err != nil { if err != utils.ErrNotFound { return nil, err } continue } - return NewEventCostFromCallCost(cc, "", ""), nil + ec := NewEventCostFromCallCost(cc, "", "") + return map[string]interface{}{ + utils.Cost: ec.GetCost(), + utils.RatingPlanID: rp}, nil } return } diff --git a/utils/consts.go b/utils/consts.go index 149ad2b4d..0d3d5278a 100755 --- a/utils/consts.go +++ b/utils/consts.go @@ -592,6 +592,7 @@ const ( MetaLeastCost = "*least_cost" Weight = "Weight" Cost = "Cost" + RatingPlanID = "RatingPlanID" ) //Meta