fix for cdrlog action

This commit is contained in:
Radu Ioan Fericean
2015-05-25 16:24:41 +03:00
parent bb97e626ab
commit 36595d88f6
7 changed files with 50 additions and 32 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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)
}
}

View File

@@ -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

View File

@@ -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])
}
}

View File

@@ -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,
}
}

View File

@@ -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