From 64c1cd67f0088158d71f31e3ad3e747d2ff17668 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Thu, 25 Jul 2013 12:16:15 +0300 Subject: [PATCH] implemented global rounding method and precision for get cost --- cmd/cgr-engine/cgr-engine.go | 3 ++- engine/calldesc.go | 40 ++++++++++++------------------------ 2 files changed, 15 insertions(+), 28 deletions(-) diff --git a/cmd/cgr-engine/cgr-engine.go b/cmd/cgr-engine/cgr-engine.go index 02a52c356..7dd2e999a 100644 --- a/cmd/cgr-engine/cgr-engine.go +++ b/cmd/cgr-engine/cgr-engine.go @@ -26,8 +26,8 @@ import ( "github.com/cgrates/cgrates/balancer2go" "github.com/cgrates/cgrates/cdrs" "github.com/cgrates/cgrates/config" - "github.com/cgrates/cgrates/mediator" "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/mediator" "github.com/cgrates/cgrates/scheduler" "github.com/cgrates/cgrates/sessionmanager" "github.com/cgrates/cgrates/utils" @@ -252,6 +252,7 @@ func main() { } defer loggerDb.Close() engine.SetStorageLogger(loggerDb) + engine.SetRoundingMethodAndDecimals(cfg.RaterRoundingMethod, cfg.RaterRoundingDecimals) if cfg.SMDebitInterval > 0 { if dp, err := time.ParseDuration(fmt.Sprintf("%vs", cfg.SMDebitInterval)); err == nil { diff --git a/engine/calldesc.go b/engine/calldesc.go index 1cdbe480c..532b658bb 100644 --- a/engine/calldesc.go +++ b/engine/calldesc.go @@ -24,7 +24,6 @@ import ( "github.com/cgrates/cgrates/cache2go" "github.com/cgrates/cgrates/utils" "log/syslog" - "math" "strings" "time" ) @@ -51,27 +50,12 @@ var ( storageGetter, _ = NewMapStorage() //storageGetter, _ = NewMongoStorage(db_server, "27017", "cgrates_test", "", "") //storageGetter, _ = NewRedisStorage(db_server+":6379", 11, "") - storageLogger = storageGetter - debitPeriod = 10 * time.Second + storageLogger = storageGetter + debitPeriod = 10 * time.Second + roundingMethod = "*middle" + roundingDecimals = 4 ) -/* -Utility function for rounding a float to a certain number of decimals (not present in math). -*/ -func round(val float64, prec int) float64 { - - var rounder float64 - intermed := val * math.Pow(10, float64(prec)) - - if val >= 0.5 { - rounder = math.Ceil(intermed) - } else { - rounder = math.Floor(intermed) - } - - return rounder / math.Pow(10, float64(prec)) -} - /* The input stucture that contains call information. */ @@ -102,9 +86,7 @@ func (cd *CallDescriptor) GetUserBalanceKey() string { return fmt.Sprintf("%s:%s:%s", cd.Direction, cd.Tenant, subj) } -/* -Gets and caches the user balance information. -*/ +// Gets and caches the user balance information. func (cd *CallDescriptor) getUserBalance() (ub *UserBalance, err error) { if cd.userBalance == nil { cd.userBalance, err = storageGetter.GetUserBalance(cd.GetUserBalanceKey()) @@ -112,13 +94,17 @@ func (cd *CallDescriptor) getUserBalance() (ub *UserBalance, err error) { return cd.userBalance, err } -/* -Exported method to set the storage getter. -*/ +// Exported method to set the storage getter. func SetDataStorage(sg DataStorage) { storageGetter = sg } +// Sets the global rounding method and decimal precision for GetCost method +func SetRoundingMethodAndDecimals(rm string, rd int) { + roundingMethod = rm + roundingDecimals = rd +} + /* Sets the database for logging (can be de same as storage getter or different db) */ @@ -285,7 +271,7 @@ func (cd *CallDescriptor) GetCost() (*CallCost, error) { } cost += ts.getCost(cd) } - + cost = utils.Round(cost, roundingDecimals, roundingMethod) cc := &CallCost{ Direction: cd.Direction, TOR: cd.TOR,