From c281916089b4f2ff11f52b10c94bf5166c3abe25 Mon Sep 17 00:00:00 2001 From: DanB Date: Fri, 10 Apr 2015 08:39:09 +0200 Subject: [PATCH] Adding responder GetLCR static sample --- engine/calldesc.go | 1 + engine/responder_test.go | 147 +++++++++++++++++++++++++++++++++++++++ engine/storage_mysql.go | 3 +- engine/storage_redis.go | 6 +- 4 files changed, 152 insertions(+), 5 deletions(-) diff --git a/engine/calldesc.go b/engine/calldesc.go index b7c4eb5e4..9de2d74b7 100644 --- a/engine/calldesc.go +++ b/engine/calldesc.go @@ -660,6 +660,7 @@ func (cd *CallDescriptor) GetLCRFromStorage() (*LCR, error) { utils.LCRKey(cd.Direction, cd.Tenant, cd.Category, utils.ANY, utils.ANY), utils.LCRKey(cd.Direction, cd.Tenant, utils.ANY, utils.ANY, utils.ANY), utils.LCRKey(cd.Direction, utils.ANY, utils.ANY, utils.ANY, utils.ANY), + utils.LCRKey(utils.ANY, utils.ANY, utils.ANY, utils.ANY, utils.ANY), } for _, key := range keyVariants { if lcr, err := dataStorage.GetLCR(key, false); err != nil && err.Error() != utils.ERR_NOT_FOUND { diff --git a/engine/responder_test.go b/engine/responder_test.go index f5f5ee34c..f3bf8b945 100644 --- a/engine/responder_test.go +++ b/engine/responder_test.go @@ -160,3 +160,150 @@ func TestGetSessionRuns(t *testing.T) { t.Errorf("Received: %+v", sesRuns) } } + +/* +func TestGetLCRStatic(t *testing.T) { + dstDe := &Destination{Id: "GERMANY", Prefixes: []string{"+49"}} + if err := dataStorage.SetDestination(dstDe); err != nil { + t.Error(err) + } + rp1 := &RatingPlan{ + Id: "RP1", + Timings: map[string]*RITiming{ + "30eab300": &RITiming{ + Years: utils.Years{}, + Months: utils.Months{}, + MonthDays: utils.MonthDays{}, + WeekDays: utils.WeekDays{}, + StartTime: "00:00:00", + }, + }, + Ratings: map[string]*RIRate{ + "b457f86d": &RIRate{ + ConnectFee: 0, + Rates: []*Rate{ + &Rate{ + GroupIntervalStart: 0, + Value: 0.01, + RateIncrement: time.Second, + RateUnit: time.Second, + }, + }, + RoundingMethod: utils.ROUNDING_MIDDLE, + RoundingDecimals: 4, + }, + }, + DestinationRates: map[string]RPRateList{ + dstDe.Id: []*RPRate{ + &RPRate{ + Timing: "30eab300", + Rating: "b457f86d", + Weight: 10, + }, + }, + }, + } + rp2 := &RatingPlan{ + Id: "RP2", + Timings: map[string]*RITiming{ + "30eab300": &RITiming{ + Years: utils.Years{}, + Months: utils.Months{}, + MonthDays: utils.MonthDays{}, + WeekDays: utils.WeekDays{}, + StartTime: "00:00:00", + }, + }, + Ratings: map[string]*RIRate{ + "b457f86d": &RIRate{ + ConnectFee: 0, + Rates: []*Rate{ + &Rate{ + GroupIntervalStart: 0, + Value: 0.02, + RateIncrement: time.Second, + RateUnit: time.Second, + }, + }, + RoundingMethod: utils.ROUNDING_MIDDLE, + RoundingDecimals: 4, + }, + }, + DestinationRates: map[string]RPRateList{ + "GERMANY": []*RPRate{ + &RPRate{ + Timing: "30eab300", + Rating: "b457f86d", + Weight: 10, + }, + }, + }, + } + if err := dataStorage.SetRatingPlan(rp1); err != nil { + t.Error(err) + } + danRpfl := &RatingProfile{Id: "*out:cgrates.org:call:dan", + RatingPlanActivations: RatingPlanActivations{&RatingPlanActivation{ + ActivationTime: time.Date(2015, 01, 01, 8, 0, 0, 0, time.UTC), + RatingPlanId: rp1.Id, + FallbackKeys: []string{}, + CdrStatQueueIds: []string{}, + }}, + } + rifRpfl := &RatingProfile{Id: "*out:cgrates.org:call:rif", + RatingPlanActivations: RatingPlanActivations{&RatingPlanActivation{ + ActivationTime: time.Date(2015, 01, 01, 8, 0, 0, 0, time.UTC), + RatingPlanId: rp2.Id, + FallbackKeys: []string{}, + CdrStatQueueIds: []string{}, + }}, + } + for _, rpfl := range []*RatingProfile{danRpfl, rifRpfl} { + if err := dataStorage.SetRatingProfile(rpfl); err != nil { + t.Error(err) + } + } + lcrSet := &LCR{Direction: utils.ANY, Tenant: utils.ANY, Category: utils.ANY, Account: utils.ANY, Subject: utils.ANY, + Activations: []*LCRActivation{ + &LCRActivation{ + ActivationTime: time.Date(2015, 01, 01, 8, 0, 0, 0, time.UTC), + Entries: []*LCREntry{ + &LCREntry{DestinationId: utils.ANY, RPCategory: "call", Strategy: LCR_STRATEGY_STATIC, StrategyParams: "dan;rif", Weight: 10.0}}, + }, + }, + } + if err := dataStorage.SetLCR(lcrSet); err != nil { + t.Error(err) + } + if err := dataStorage.CacheRating([]string{DESTINATION_PREFIX + dstDe.Id}, + []string{RATING_PLAN_PREFIX + rp1.Id, RATING_PLAN_PREFIX + rp2.Id}, + []string{RATING_PROFILE_PREFIX + danRpfl.Id, RATING_PROFILE_PREFIX + rifRpfl.Id}, + []string{}, + []string{LCR_PREFIX + lcrSet.GetId()}); err != nil { + t.Error(err) + } + cd := &CallDescriptor{ + TimeStart: time.Date(2015, 04, 06, 17, 40, 0, 0, time.UTC), + TimeEnd: time.Date(2015, 04, 06, 17, 41, 0, 0, time.UTC), + Tenant: "cgrates.org", + Direction: utils.OUT, + Category: "call", + Destination: "+4986517174963", + Account: "dan", + Subject: "dan", + } + eLcr := &LCRCost{ + Entry: &LCREntry{DestinationId: utils.ANY, RPCategory: "call", Strategy: LCR_STRATEGY_STATIC, StrategyParams: "dan;rif", Weight: 10.0}, + SupplierCosts: []*LCRSupplierCost{ + &LCRSupplierCost{Supplier: "*out:cgrates.org:call:dan", Cost: 0.6, Duration: "60s"}, + &LCRSupplierCost{Supplier: "*out:cgrates.org:call:rif", Cost: 1.2, Duration: "60s"}, + }, + } + var lcr LCRCost + if err := rsponder.GetLCR(cd, &lcr); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(eLcr, lcr) { + t.Errorf("Received: %+v", lcr) + } +} +*/ diff --git a/engine/storage_mysql.go b/engine/storage_mysql.go index effa7a032..5ef249472 100644 --- a/engine/storage_mysql.go +++ b/engine/storage_mysql.go @@ -34,8 +34,7 @@ func NewMySQLStorage(host, port, name, user, password string, maxConn, maxIdleCo if err != nil { return nil, err } - err = db.DB().Ping() - if err != nil { + if err = db.DB().Ping(); err != nil { return nil, err } db.DB().SetMaxIdleConns(maxIdleConn) diff --git a/engine/storage_redis.go b/engine/storage_redis.go index 16fbf2152..abf71b18e 100644 --- a/engine/storage_redis.go +++ b/engine/storage_redis.go @@ -139,14 +139,14 @@ func (rs *RedisStorage) CacheRating(dKeys, rpKeys, rpfKeys, alsKeys, lcrKeys []s Logger.Info("Finished rating profile caching.") } if lcrKeys == nil { - Logger.Info("Caching LCRs") + Logger.Info("Caching LCR rules.") if lcrKeys, err = rs.db.Keys(LCR_PREFIX + "*"); err != nil { cache2go.RollbackTransaction() return err } cache2go.RemPrefixKey(LCR_PREFIX) } else if len(lcrKeys) != 0 { - Logger.Info(fmt.Sprintf("Caching LCR: %v", lcrKeys)) + Logger.Info(fmt.Sprintf("Caching LCR rules: %v", lcrKeys)) } for _, key := range lcrKeys { cache2go.RemKey(key) @@ -156,7 +156,7 @@ func (rs *RedisStorage) CacheRating(dKeys, rpKeys, rpfKeys, alsKeys, lcrKeys []s } } if len(lcrKeys) != 0 { - Logger.Info("Finished rating profile caching.") + Logger.Info("Finished LCR rules caching.") } if alsKeys == nil { Logger.Info("Caching all rating subject aliases.")