From 36595d88f65956d442f5dda67655a11a39ff12c6 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Mon, 25 May 2015 16:24:41 +0300 Subject: [PATCH] fix for cdrlog action --- data/docker/devel/run.sh | 3 -- engine/account.go | 14 +++++---- engine/account_test.go | 4 +-- engine/action.go | 56 +++++++++++++++++++++++------------- engine/actions_local_test.go | 2 +- engine/balances.go | 1 + run_devel_docker.sh | 2 +- 7 files changed, 50 insertions(+), 32 deletions(-) delete mode 100755 data/docker/devel/run.sh diff --git a/data/docker/devel/run.sh b/data/docker/devel/run.sh deleted file mode 100755 index c3bd43585..000000000 --- a/data/docker/devel/run.sh +++ /dev/null @@ -1,3 +0,0 @@ -#/usr/bin/env sh - -docker run --rm -p 3306:3306 -p 6379:6379 -p 2012:2012 -p 2013:2013 -p 2080:2080 -itv `pwd`:/root/code/src/github.com/cgrates/cgrates --name cgr cgrates diff --git a/engine/account.go b/engine/account.go index 81fd4dc04..fb2e2e246 100644 --- a/engine/account.go +++ b/engine/account.go @@ -96,11 +96,13 @@ func (ub *Account) getCreditForPrefix(cd *CallDescriptor) (duration time.Duratio // Returns the remaining credit in user's balance. func (ub *Account) debitBalanceAction(a *Action, reset bool) error { if a == nil { - return errors.New("nil minute action!") + return errors.New("nil minute action") } if a.Balance.Uuid == "" { a.Balance.Uuid = utils.GenUUID() } + bClone := a.Balance.Clone() + if ub.BalanceMap == nil { ub.BalanceMap = make(map[string]BalanceChain, 1) } @@ -115,15 +117,17 @@ func (ub *Account) debitBalanceAction(a *Action, reset bool) error { if reset { b.Value = 0 } - b.SubstractAmount(a.Balance.Value) + b.SubstractAmount(bClone.Value) found = true } } // if it is not found then we add it to the list if !found { - a.Balance.Value = -a.Balance.Value - a.Balance.dirty = true // Mark the balance as dirty since we have modified and it should be checked by action triggers - ub.BalanceMap[id] = append(ub.BalanceMap[id], a.Balance) + if bClone.Value != 0 { + bClone.Value = -bClone.Value + } + bClone.dirty = true // Mark the balance as dirty since we have modified and it should be checked by action triggers + ub.BalanceMap[id] = append(ub.BalanceMap[id], bClone) } if a.Balance.SharedGroup != "" { // add shared group member diff --git a/engine/account_test.go b/engine/account_test.go index 1ce98a279..31d30827e 100644 --- a/engine/account_test.go +++ b/engine/account_test.go @@ -940,8 +940,8 @@ func TestAccountdebitBalance(t *testing.T) { newMb := &Balance{Weight: 20, DestinationIds: "NEW"} a := &Action{BalanceType: utils.VOICE, Direction: OUTBOUND, Balance: newMb} ub.debitBalanceAction(a, false) - if len(ub.BalanceMap[utils.VOICE+OUTBOUND]) != 3 || ub.BalanceMap[utils.VOICE+OUTBOUND][2] != newMb { - t.Error("Error adding minute bucket!", len(ub.BalanceMap[utils.VOICE+OUTBOUND]), ub.BalanceMap[utils.VOICE+OUTBOUND]) + if len(ub.BalanceMap[utils.VOICE+OUTBOUND]) != 3 || ub.BalanceMap[utils.VOICE+OUTBOUND][2].Uuid != newMb.Uuid { + t.Errorf("Error adding minute bucket! %d %+v %+v", len(ub.BalanceMap[utils.VOICE+OUTBOUND]), ub.BalanceMap[utils.VOICE+OUTBOUND][2], newMb) } } diff --git a/engine/action.go b/engine/action.go index 214611bc5..290f2656d 100644 --- a/engine/action.go +++ b/engine/action.go @@ -70,6 +70,19 @@ const ( CDRLOG = "*cdrlog" ) +func (a *Action) Clone() *Action { + return &Action{ + Id: a.Id, + ActionType: a.ActionType, + BalanceType: a.BalanceType, + Direction: a.Direction, + ExtraParameters: a.ExtraParameters, + ExpirationString: a.ExpirationString, + Weight: a.Weight, + Balance: a.Balance.Clone(), + } +} + type actionTypeFunc func(*Account, *StatsQueueTriggered, *Action, Actions) error func getActionFunc(typ string) (actionTypeFunc, bool) { @@ -254,7 +267,7 @@ func cdrLogAction(acc *Account, sq *StatsQueueTriggered, a *Action, acs Actions) func resetTriggersAction(ub *Account, sq *StatsQueueTriggered, a *Action, acs Actions) (err error) { if ub == nil { - return errors.New("Nil user balance") + return errors.New("nil user balance") } ub.ResetActionTriggers(a) return @@ -262,7 +275,7 @@ func resetTriggersAction(ub *Account, sq *StatsQueueTriggered, a *Action, acs Ac func setRecurrentAction(ub *Account, sq *StatsQueueTriggered, a *Action, acs Actions) (err error) { if ub == nil { - return errors.New("Nil user balance") + return errors.New("nil user balance") } ub.SetRecurrent(a, true) return @@ -270,7 +283,7 @@ func setRecurrentAction(ub *Account, sq *StatsQueueTriggered, a *Action, acs Act func unsetRecurrentAction(ub *Account, sq *StatsQueueTriggered, a *Action, acs Actions) (err error) { if ub == nil { - return errors.New("Nil user balance") + return errors.New("nil user balance") } ub.SetRecurrent(a, false) return @@ -278,7 +291,7 @@ func unsetRecurrentAction(ub *Account, sq *StatsQueueTriggered, a *Action, acs A func allowNegativeAction(ub *Account, sq *StatsQueueTriggered, a *Action, acs Actions) (err error) { if ub == nil { - return errors.New("Nil user balance") + return errors.New("nil user balance") } ub.AllowNegative = true return @@ -286,7 +299,7 @@ func allowNegativeAction(ub *Account, sq *StatsQueueTriggered, a *Action, acs Ac func denyNegativeAction(ub *Account, sq *StatsQueueTriggered, a *Action, acs Actions) (err error) { if ub == nil { - return errors.New("Nil user balance") + return errors.New("nil user balance") } ub.AllowNegative = false return @@ -294,33 +307,35 @@ func denyNegativeAction(ub *Account, sq *StatsQueueTriggered, a *Action, acs Act func resetAccountAction(ub *Account, sq *StatsQueueTriggered, a *Action, acs Actions) (err error) { if ub == nil { - return errors.New("Nil user balance") + return errors.New("nil user balance") } return genericReset(ub) } func topupResetAction(ub *Account, sq *StatsQueueTriggered, a *Action, acs Actions) (err error) { if ub == nil { - return errors.New("Nil user balance") + return errors.New("nil user balance") } if ub.BalanceMap == nil { // Init the map since otherwise will get error if nil ub.BalanceMap = make(map[string]BalanceChain, 0) } - genericMakeNegative(a) - return genericDebit(ub, a, true) + c := a.Clone() + genericMakeNegative(c) + return genericDebit(ub, c, true) } func topupAction(ub *Account, sq *StatsQueueTriggered, a *Action, acs Actions) (err error) { if ub == nil { - return errors.New("Nil user balance") + return errors.New("nil user balance") } - genericMakeNegative(a) - return genericDebit(ub, a, false) + c := a.Clone() + genericMakeNegative(c) + return genericDebit(ub, c, false) } func debitResetAction(ub *Account, sq *StatsQueueTriggered, a *Action, acs Actions) (err error) { if ub == nil { - return errors.New("Nil user balance") + return errors.New("nil user balance") } if ub.BalanceMap == nil { // Init the map since otherwise will get error if nil ub.BalanceMap = make(map[string]BalanceChain, 0) @@ -330,14 +345,15 @@ func debitResetAction(ub *Account, sq *StatsQueueTriggered, a *Action, acs Actio func debitAction(ub *Account, sq *StatsQueueTriggered, a *Action, acs Actions) (err error) { if ub == nil { - return errors.New("Nil user balance") + return errors.New("nil user balance") } - return genericDebit(ub, a, false) + err = genericDebit(ub, a, false) + return } func resetCounterAction(ub *Account, sq *StatsQueueTriggered, a *Action, acs Actions) (err error) { if ub == nil { - return errors.New("Nil user balance") + return errors.New("nil user balance") } uc := ub.getUnitCounter(a) if uc == nil { @@ -350,7 +366,7 @@ func resetCounterAction(ub *Account, sq *StatsQueueTriggered, a *Action, acs Act func resetCountersAction(ub *Account, sq *StatsQueueTriggered, a *Action, acs Actions) (err error) { if ub == nil { - return errors.New("Nil user balance") + return errors.New("nil user balance") } ub.UnitCounters = make([]*UnitsCounter, 0) ub.initCounters() @@ -365,7 +381,7 @@ func genericMakeNegative(a *Action) { func genericDebit(ub *Account, a *Action, reset bool) (err error) { if ub == nil { - return errors.New("Nil user balance") + return errors.New("nil user balance") } if ub.BalanceMap == nil { ub.BalanceMap = make(map[string]BalanceChain) @@ -376,7 +392,7 @@ func genericDebit(ub *Account, a *Action, reset bool) (err error) { func enableUserAction(ub *Account, sq *StatsQueueTriggered, a *Action, acs Actions) (err error) { if ub == nil { - return errors.New("Nil user balance") + return errors.New("nil user balance") } ub.Disabled = false return @@ -384,7 +400,7 @@ func enableUserAction(ub *Account, sq *StatsQueueTriggered, a *Action, acs Actio func disableUserAction(ub *Account, sq *StatsQueueTriggered, a *Action, acs Actions) (err error) { if ub == nil { - return errors.New("Nil user balance") + return errors.New("nil user balance") } ub.Disabled = true return diff --git a/engine/actions_local_test.go b/engine/actions_local_test.go index 9f6390400..0b0e7a3ef 100644 --- a/engine/actions_local_test.go +++ b/engine/actions_local_test.go @@ -118,7 +118,7 @@ func TestActionsLocalSetCdrlogActions(t *testing.T) { rcvedCdrs[0].Usage != "1" || rcvedCdrs[0].MediationRunId != utils.META_DEFAULT || rcvedCdrs[0].Cost != attrsAA.Actions[0].Units { - t.Error("Received: ", rcvedCdrs[0]) + t.Errorf("Received: %+v", rcvedCdrs[0]) } } diff --git a/engine/balances.go b/engine/balances.go index 4a5f0d054..2c3cc202c 100644 --- a/engine/balances.go +++ b/engine/balances.go @@ -180,6 +180,7 @@ func (b *Balance) Clone() *Balance { SharedGroup: b.SharedGroup, TimingIDs: b.TimingIDs, Timings: b.Timings, // should not be a problem with aliasing + dirty: b.dirty, } } diff --git a/run_devel_docker.sh b/run_devel_docker.sh index 81ff9396d..c580dfda3 100755 --- a/run_devel_docker.sh +++ b/run_devel_docker.sh @@ -1,3 +1,3 @@ #!/usr/bin/env sh -docker run --rm -p 3306:3306 -p 6379:6379 -p 2012:2012 -itv /home/rif/Documents/prog/go/src/github.com/cgrates/cgrates:/root/code/src/github.com/cgrates/cgrates --name cgr cgrates +docker run --rm -p 3306:3306 -p 6379:6379 -p 2012:2012 -p 2013:2013 -p 2080:2080 -itv `pwd`:/root/code/src/github.com/cgrates/cgrates --name cgr cgrates