From c68332929ba154c92b12c84f9df08ab1449ffb76 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Wed, 11 Nov 2015 21:29:02 +0200 Subject: [PATCH] add MatchedPrefix and MatchedDestId to zero duration calls, fixes #255 --- engine/actions_test.go | 3 -- engine/calldesc.go | 46 +++++++++++++++++++--- engine/calldesc_test.go | 84 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 123 insertions(+), 10 deletions(-) diff --git a/engine/actions_test.go b/engine/actions_test.go index 40fd36390..5a177d01c 100644 --- a/engine/actions_test.go +++ b/engine/actions_test.go @@ -21,7 +21,6 @@ package engine import ( "encoding/json" "fmt" - "log" "reflect" "testing" "time" @@ -1303,9 +1302,7 @@ func TestActionTransactionFuncType(t *testing.T) { }, }, } - log.Print("=========") err = at.Execute() - log.Print("=========") acc, err := accountingStorage.GetAccount("cgrates.org:trans") if err != nil || acc == nil { t.Error("Error getting account: ", acc, err) diff --git a/engine/calldesc.go b/engine/calldesc.go index 45d97fef2..066aaa887 100644 --- a/engine/calldesc.go +++ b/engine/calldesc.go @@ -435,7 +435,7 @@ Creates a CallCost structure with the cost information calculated for the receiv func (cd *CallDescriptor) GetCost() (*CallCost, error) { cd.account = nil // make sure it's not cached cc, err := cd.getCost() - if err != nil { + if err != nil || cd.GetDuration() == 0 { return cc, err } @@ -474,8 +474,19 @@ func (cd *CallDescriptor) GetCost() (*CallCost, error) { func (cd *CallDescriptor) getCost() (*CallCost, error) { // check for 0 duration - if cd.TimeEnd.Sub(cd.TimeStart) == 0 { - return cd.CreateCallCost(), nil + if cd.GetDuration() == 0 { + cc := cd.CreateCallCost() + // add RatingInfo + err := cd.LoadRatingPlans() + if err == nil && len(cd.RatingInfos) > 0 { + ts := &TimeSpan{ + TimeStart: cd.TimeStart, + TimeEnd: cd.TimeEnd, + } + ts.setRatingInfo(cd.RatingInfos[0]) + cc.Timespans = append(cc.Timespans, ts) + } + return cc, nil } if cd.DurationIndex < cd.TimeEnd.Sub(cd.TimeStart) { cd.DurationIndex = cd.TimeEnd.Sub(cd.TimeStart) @@ -621,8 +632,19 @@ func (cd *CallDescriptor) GetMaxSessionDuration() (duration time.Duration, err e // Interface method used to add/substract an amount of cents or bonus seconds (as returned by GetCost method) // from user's money balance. func (cd *CallDescriptor) debit(account *Account, dryRun bool, goNegative bool) (cc *CallCost, err error) { - if cd.TimeEnd.Sub(cd.TimeStart) == 0 { - return cd.CreateCallCost(), nil + if cd.GetDuration() == 0 { + cc = cd.CreateCallCost() + // add RatingInfo + err := cd.LoadRatingPlans() + if err == nil && len(cd.RatingInfos) > 0 { + ts := &TimeSpan{ + TimeStart: cd.TimeStart, + TimeEnd: cd.TimeEnd, + } + ts.setRatingInfo(cd.RatingInfos[0]) + cc.Timespans = append(cc.Timespans, ts) + } + return cc, nil } if !dryRun { defer accountingStorage.SetAccount(account) @@ -678,6 +700,20 @@ func (cd *CallDescriptor) MaxDebit() (cc *CallCost, err error) { remainingDuration, err := cd.getMaxSessionDuration(account) //log.Print("AFTER MAX SESSION: ", cd) if err != nil || remainingDuration == 0 { + if cd.GetDuration() == 0 { + cc = cd.CreateCallCost() + // add RatingInfo + err := cd.LoadRatingPlans() + if err == nil && len(cd.RatingInfos) > 0 { + ts := &TimeSpan{ + TimeStart: cd.TimeStart, + TimeEnd: cd.TimeEnd, + } + ts.setRatingInfo(cd.RatingInfos[0]) + cc.Timespans = append(cc.Timespans, ts) + } + return cc, nil + } cc, err = new(CallCost), fmt.Errorf("no more credit: %v", err) return 0, err } diff --git a/engine/calldesc_test.go b/engine/calldesc_test.go index 3fa4d287a..34013e26f 100644 --- a/engine/calldesc_test.go +++ b/engine/calldesc_test.go @@ -581,6 +581,88 @@ func TestGetCostRoundingIssue(t *testing.T) { } } +func TestGetCostRatingInfoOnZeroTime(t *testing.T) { + ap, _ := ratingStorage.GetActionPlans("TOPUP10_AT", false) + for _, at := range ap { + at.Execute() + } + cd := &CallDescriptor{ + Direction: "*out", + Category: "call", + Tenant: "cgrates.org", + Subject: "dy", + Account: "dy", + Destination: "0723123113", + TimeStart: time.Date(2015, 10, 26, 13, 29, 27, 0, time.UTC), + TimeEnd: time.Date(2015, 10, 26, 13, 29, 27, 0, time.UTC), + MaxCostSoFar: 0, + } + cc, err := cd.GetCost() + if err != nil || + len(cc.Timespans) != 1 || + cc.Timespans[0].MatchedDestId != "RET" || + cc.Timespans[0].MatchedSubject != "*out:cgrates.org:call:dy" || + cc.Timespans[0].MatchedPrefix != "0723" || + cc.Timespans[0].RatingPlanId != "DY_PLAN" { + t.Error("MatchedInfo not added:", utils.ToIJSON(cc)) + } +} + +func TestDebitRatingInfoOnZeroTime(t *testing.T) { + ap, _ := ratingStorage.GetActionPlans("TOPUP10_AT", false) + for _, at := range ap { + at.Execute() + } + cd := &CallDescriptor{ + Direction: "*out", + Category: "call", + Tenant: "cgrates.org", + Subject: "dy", + Account: "dy", + Destination: "0723123113", + TimeStart: time.Date(2015, 10, 26, 13, 29, 27, 0, time.UTC), + TimeEnd: time.Date(2015, 10, 26, 13, 29, 27, 0, time.UTC), + MaxCostSoFar: 0, + } + cc, err := cd.Debit() + if err != nil || + cc == nil || + len(cc.Timespans) != 1 || + cc.Timespans[0].MatchedDestId != "RET" || + cc.Timespans[0].MatchedSubject != "*out:cgrates.org:call:dy" || + cc.Timespans[0].MatchedPrefix != "0723" || + cc.Timespans[0].RatingPlanId != "DY_PLAN" { + t.Error("MatchedInfo not added:", utils.ToIJSON(cc)) + } +} + +func TestMaxDebitRatingInfoOnZeroTime(t *testing.T) { + ap, _ := ratingStorage.GetActionPlans("TOPUP10_AT", false) + for _, at := range ap { + at.Execute() + } + cd := &CallDescriptor{ + Direction: "*out", + Category: "call", + Tenant: "cgrates.org", + Subject: "dy", + Account: "dy", + Destination: "0723123113", + TimeStart: time.Date(2015, 10, 26, 13, 29, 27, 0, time.UTC), + TimeEnd: time.Date(2015, 10, 26, 13, 29, 27, 0, time.UTC), + MaxCostSoFar: 0, + } + cc, err := cd.MaxDebit() + if err != nil || + len(cc.Timespans) != 1 || + cc.Timespans[0].MatchedDestId != "RET" || + cc.Timespans[0].MatchedSubject != "*out:cgrates.org:call:dy" || + cc.Timespans[0].MatchedPrefix != "0723" || + cc.Timespans[0].RatingPlanId != "DY_PLAN" { + t.Error("MatchedInfo not added:", utils.ToIJSON(cc)) + } +} + func TestGetCostMaxDebitRoundingIssue(t *testing.T) { ap, _ := ratingStorage.GetActionPlans("TOPUP10_AT", false) for _, at := range ap { @@ -888,7 +970,6 @@ func TestDebitAndMaxDebit(t *testing.T) { t.Error("Error debiting and/or maxdebiting: ", err1, err2) } if !reflect.DeepEqual(cc1, cc2) { - t.Log("===============================") t.Logf("CC1: %+v", cc1) for _, ts := range cc1.Timespans { t.Logf("TS: %+v", ts) @@ -897,7 +978,6 @@ func TestDebitAndMaxDebit(t *testing.T) { for _, ts := range cc2.Timespans { t.Logf("TS: %+v", ts) } - t.Log("===============================") t.Error("Debit and MaxDebit differ") } }