mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-15 05:09:54 +05:00
Send *usage from routes to accounts and rates
This commit is contained in:
committed by
Dan Christian Bogos
parent
e7ad7b0d3f
commit
1993061c61
@@ -23,15 +23,18 @@ import (
|
||||
|
||||
"github.com/cgrates/birpc/context"
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
"github.com/ericlagergren/decimal"
|
||||
)
|
||||
|
||||
var RoutesProfileCountDftOpt = utils.IntPointer(1)
|
||||
var (
|
||||
RoutesProfileCountDftOpt = utils.IntPointer(1)
|
||||
RoutesUsageDftOpt = decimal.New(int64(time.Minute), 0)
|
||||
)
|
||||
|
||||
const (
|
||||
RoutesContextDftOpt = "*routes"
|
||||
RoutesIgnoreErrorsDftOpt = false
|
||||
RoutesMaxCostDftOpt = utils.EmptyString
|
||||
RoutesUsageDftOpt = time.Minute
|
||||
)
|
||||
|
||||
type RoutesOpts struct {
|
||||
|
||||
@@ -22,17 +22,24 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/cgrates/birpc/context"
|
||||
"github.com/ericlagergren/decimal"
|
||||
|
||||
"github.com/cgrates/cgrates/config"
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
)
|
||||
|
||||
func populateCostForRoutes(ctx *context.Context, cfg *config.CGRConfig,
|
||||
connMgr *ConnManager, routes map[string]*RouteWithWeight,
|
||||
ev *utils.CGREvent, extraOpts *optsGetRoutes) (sortedRoutes []*SortedRoute, err error) {
|
||||
func populateCostForRoutes(ctx *context.Context, cfg *config.CGRConfig, connMgr *ConnManager,
|
||||
fltrS *FilterS, routes map[string]*RouteWithWeight, ev *utils.CGREvent,
|
||||
extraOpts *optsGetRoutes) (sortedRoutes []*SortedRoute, err error) {
|
||||
if len(cfg.RouteSCfg().RateSConns) == 0 {
|
||||
return nil, utils.NewErrMandatoryIeMissing("connIDs")
|
||||
}
|
||||
var usage *decimal.Big
|
||||
if usage, err = GetDecimalBigOpts(ctx, ev.Tenant, ev, fltrS, cfg.RouteSCfg().Opts.Usage,
|
||||
config.RoutesUsageDftOpt, utils.OptsRoutesUsage, utils.MetaUsage); err != nil {
|
||||
return
|
||||
}
|
||||
ev.APIOpts[utils.MetaUsage] = usage
|
||||
sortedRoutes = make([]*SortedRoute, 0, len(routes))
|
||||
for _, route := range routes {
|
||||
if len(route.RateProfileIDs) == 0 && len(route.AccountIDs) == 0 {
|
||||
@@ -121,20 +128,21 @@ func populateCostForRoutes(ctx *context.Context, cfg *config.CGRConfig,
|
||||
return
|
||||
}
|
||||
|
||||
func NewHighestCostSorter(cfg *config.CGRConfig, connMgr *ConnManager) *HightCostSorter {
|
||||
return &HightCostSorter{cfg: cfg, connMgr: connMgr}
|
||||
func NewHighestCostSorter(cfg *config.CGRConfig, connMgr *ConnManager, fltrS *FilterS) *HightCostSorter {
|
||||
return &HightCostSorter{cfg: cfg, connMgr: connMgr, fltrS: fltrS}
|
||||
}
|
||||
|
||||
// HightCostSorter sorts routes based on their cost
|
||||
type HightCostSorter struct {
|
||||
cfg *config.CGRConfig
|
||||
connMgr *ConnManager
|
||||
fltrS *FilterS
|
||||
}
|
||||
|
||||
func (hcs *HightCostSorter) SortRoutes(ctx *context.Context, prflID string, routes map[string]*RouteWithWeight,
|
||||
ev *utils.CGREvent, extraOpts *optsGetRoutes) (sortedRoutes *SortedRoutes, err error) {
|
||||
var sRoutes []*SortedRoute
|
||||
if sRoutes, err = populateCostForRoutes(ctx, hcs.cfg, hcs.connMgr, routes, ev, extraOpts); err != nil {
|
||||
if sRoutes, err = populateCostForRoutes(ctx, hcs.cfg, hcs.connMgr, hcs.fltrS, routes, ev, extraOpts); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -147,20 +155,21 @@ func (hcs *HightCostSorter) SortRoutes(ctx *context.Context, prflID string, rout
|
||||
return
|
||||
}
|
||||
|
||||
func NewLeastCostSorter(cfg *config.CGRConfig, connMgr *ConnManager) *LeastCostSorter {
|
||||
return &LeastCostSorter{cfg: cfg, connMgr: connMgr}
|
||||
func NewLeastCostSorter(cfg *config.CGRConfig, connMgr *ConnManager, fltrS *FilterS) *LeastCostSorter {
|
||||
return &LeastCostSorter{cfg: cfg, connMgr: connMgr, fltrS: fltrS}
|
||||
}
|
||||
|
||||
// LeastCostSorter sorts routes based on their cost
|
||||
type LeastCostSorter struct {
|
||||
cfg *config.CGRConfig
|
||||
connMgr *ConnManager
|
||||
fltrS *FilterS
|
||||
}
|
||||
|
||||
func (lcs *LeastCostSorter) SortRoutes(ctx *context.Context, prflID string, routes map[string]*RouteWithWeight,
|
||||
ev *utils.CGREvent, extraOpts *optsGetRoutes) (sortedRoutes *SortedRoutes, err error) {
|
||||
var sRoutes []*SortedRoute
|
||||
if sRoutes, err = populateCostForRoutes(ctx, lcs.cfg, lcs.connMgr, routes, ev, extraOpts); err != nil {
|
||||
if sRoutes, err = populateCostForRoutes(ctx, lcs.cfg, lcs.connMgr, lcs.fltrS, routes, ev, extraOpts); err != nil {
|
||||
return
|
||||
}
|
||||
sortedRoutes = &SortedRoutes{
|
||||
|
||||
@@ -29,6 +29,7 @@ import (
|
||||
func TestPopulateCostForRoutesConnRefused(t *testing.T) {
|
||||
cfg := config.NewDefaultCGRConfig()
|
||||
connMgr := NewConnManager(cfg)
|
||||
fltrS := NewFilterS(cfg, connMgr, nil)
|
||||
routes := map[string]*RouteWithWeight{
|
||||
"RW": {
|
||||
Route: &Route{
|
||||
@@ -49,7 +50,7 @@ func TestPopulateCostForRoutesConnRefused(t *testing.T) {
|
||||
}
|
||||
extraOpts := &optsGetRoutes{}
|
||||
cfg.RouteSCfg().RateSConns = []string{"*localhost"}
|
||||
_, err := populateCostForRoutes(context.Background(), cfg, connMgr, routes, ev, extraOpts)
|
||||
_, err := populateCostForRoutes(context.Background(), cfg, connMgr, fltrS, routes, ev, extraOpts)
|
||||
errExpect := "RATES_ERROR:dial tcp 127.0.0.1:2012: connect: connection refused"
|
||||
if err.Error() != errExpect {
|
||||
t.Errorf("Expected %v\n but received %v", errExpect, err)
|
||||
|
||||
@@ -111,8 +111,8 @@ func NewRouteService(dm *DataManager,
|
||||
connMgr: connMgr,
|
||||
sorter: RouteSortDispatcher{
|
||||
utils.MetaWeight: NewWeightSorter(cfg),
|
||||
utils.MetaLC: NewLeastCostSorter(cfg, connMgr),
|
||||
utils.MetaHC: NewHighestCostSorter(cfg, connMgr),
|
||||
utils.MetaLC: NewLeastCostSorter(cfg, connMgr, filterS),
|
||||
utils.MetaHC: NewHighestCostSorter(cfg, connMgr, filterS),
|
||||
utils.MetaQOS: NewQOSRouteSorter(cfg, connMgr),
|
||||
utils.MetaReas: NewResourceAscendetSorter(cfg, connMgr),
|
||||
utils.MetaReds: NewResourceDescendentSorter(cfg, connMgr),
|
||||
|
||||
Reference in New Issue
Block a user