This commit is contained in:
DanB
2015-11-16 09:42:44 +01:00
9 changed files with 110 additions and 28 deletions

View File

@@ -25,6 +25,7 @@ import (
"net/rpc"
"path"
"strconv"
"strings"
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/engine"
@@ -35,7 +36,8 @@ import (
var (
//separator = flag.String("separator", ",", "Default field separator")
cgrConfig, _ = config.NewDefaultCGRConfig()
migrateRC8 = flag.Bool("migrate_rc8", false, "Migrate Accounts, Actions and ActionTriggers to RC8 structures")
migrateRC8 = flag.Bool("migrate_rc8", false, "Migrate Accounts, Actions, ActionTriggers and DerivedChargers to RC8 structures")
migrateList = flag.String("migrate_list", "acc,atr,act,dcs", "Migration item list")
tpdb_type = flag.String("tpdb_type", cgrConfig.TpDbType, "The type of the TariffPlan database <redis>")
tpdb_host = flag.String("tpdb_host", cgrConfig.TpDbHost, "The TariffPlan host to connect to.")
tpdb_port = flag.String("tpdb_port", cgrConfig.TpDbPort, "The TariffPlan port to bind to.")
@@ -106,9 +108,10 @@ func main() {
log.Print(err.Error())
return
}
if err := migratorRC8acc.migrateAccounts(); err != nil {
log.Print(err.Error())
return
if strings.Contains(*migrateList, "acc") {
if err := migratorRC8acc.migrateAccounts(); err != nil {
log.Print(err.Error())
}
}
db_nb, err = strconv.Atoi(*tpdb_name)
@@ -125,13 +128,20 @@ func main() {
log.Print(err.Error())
return
}
if err := migratorRC8rat.migrateActionTriggers(); err != nil {
log.Print(err.Error())
return
if strings.Contains(*migrateList, "atr") {
if err := migratorRC8rat.migrateActionTriggers(); err != nil {
log.Print(err.Error())
}
}
if err := migratorRC8rat.migrateActions(); err != nil {
log.Print(err.Error())
return
if strings.Contains(*migrateList, "act") {
if err := migratorRC8rat.migrateActions(); err != nil {
log.Print(err.Error())
}
}
if strings.Contains(*migrateList, "dcs") {
if err := migratorRC8rat.migrateDerivedChargers(); err != nil {
log.Print(err.Error())
}
}
log.Print("Done!")
return

View File

@@ -366,3 +366,37 @@ func (mig MigratorRC8) migrateActions() error {
}
return nil
}
func (mig MigratorRC8) migrateDerivedChargers() error {
keys, err := mig.db.Keys(utils.DERIVEDCHARGERS_PREFIX + "*")
if err != nil {
return err
}
newDcsMap := make(map[string]*utils.DerivedChargers, len(keys))
for _, key := range keys {
log.Printf("Migrating derived charger: %s...", key)
var oldDcs []*utils.DerivedCharger
var values []byte
if values, err = mig.db.Get(key); err == nil {
if err := mig.ms.Unmarshal(values, &oldDcs); err != nil {
return err
}
}
newDcs := &utils.DerivedChargers{
DestinationIds: make(utils.StringMap),
Chargers: oldDcs,
}
newDcsMap[key] = newDcs
}
// write data back
for key, dcs := range newDcsMap {
result, err := mig.ms.Marshal(&dcs)
if err != nil {
return err
}
if err = mig.db.Set(key, result); err != nil {
return err
}
}
return nil
}

View File

@@ -270,7 +270,6 @@ func (ub *Account) debitCreditBalance(cd *CallDescriptor, count bool, dryRun boo
//log.Print(usefulMoneyBalances, usefulUnitBalances)
//log.Print("STARTCD: ", cd)
var leftCC *CallCost
var initialLength int
cc = cd.CreateCallCost()
generalBalanceChecker := true
@@ -287,19 +286,15 @@ func (ub *Account) debitCreditBalance(cd *CallDescriptor, count bool, dryRun boo
//log.Printf("Unit balance: %+v", balance)
// log.Printf("CD BEFORE UNIT: %+v", cd)
partCC, debitErr := balance.DebitUnits(cd, balance.account, usefulMoneyBalances, count, dryRun)
partCC, debitErr := balance.debitUnits(cd, balance.account, usefulMoneyBalances, count, dryRun, len(cc.Timespans) == 0)
if debitErr != nil {
return nil, debitErr
}
//log.Printf("CD AFTER UNIT: %+v", cd)
if partCC != nil {
//log.Printf("partCC: %+v", partCC.Timespans[0])
initialLength = len(cc.Timespans)
cc.Timespans = append(cc.Timespans, partCC.Timespans...)
if initialLength == 0 {
// this is the first add, debit the connect fee
ub.DebitConnectionFee(cc, usefulMoneyBalances, count)
}
cc.negativeConnectFee = partCC.negativeConnectFee
// for i, ts := range cc.Timespans {
// log.Printf("cc.times[an[%d]: %+v\n", i, ts)
// }
@@ -329,19 +324,16 @@ func (ub *Account) debitCreditBalance(cd *CallDescriptor, count bool, dryRun boo
for _, balance := range usefulMoneyBalances {
//log.Printf("Money balance: %+v", balance)
//log.Printf("CD BEFORE MONEY: %+v", cd)
partCC, debitErr := balance.DebitMoney(cd, balance.account, count, dryRun)
partCC, debitErr := balance.debitMoney(cd, balance.account, usefulMoneyBalances, count, dryRun, len(cc.Timespans) == 0)
if debitErr != nil {
return nil, debitErr
}
//log.Printf("CD AFTER MONEY: %+v", cd)
//log.Printf("partCC: %+v", partCC)
if partCC != nil {
initialLength = len(cc.Timespans)
cc.Timespans = append(cc.Timespans, partCC.Timespans...)
if initialLength == 0 {
// this is the first add, debit the connect fee
ub.DebitConnectionFee(cc, usefulMoneyBalances, count)
}
cc.negativeConnectFee = partCC.negativeConnectFee
//for i, ts := range cc.Timespans {
//log.Printf("cc.times[an[%d]: %+v\n", i, ts)
//}
@@ -375,7 +367,7 @@ func (ub *Account) debitCreditBalance(cd *CallDescriptor, count bool, dryRun boo
//log.Printf("HERE: %+v %d", leftCC)
if leftCC.Cost > 0 && goNegative {
initialLength = len(cc.Timespans)
initialLength := len(cc.Timespans)
cc.Timespans = append(cc.Timespans, leftCC.Timespans...)
if initialLength == 0 {
// this is the first add, debit the connect fee
@@ -682,6 +674,7 @@ func (acc *Account) DebitConnectionFee(cc *CallCost, usefulMoneyBalances Balance
}
// debit connect fee
if connectFee > 0 && !connectFeePaid {
cc.negativeConnectFee = true
// there are no money for the connect fee; go negative
b := acc.GetDefaultMoneyBalance()
b.SubstractValue(connectFee)

View File

@@ -1193,6 +1193,33 @@ func TestMaxDurationShared(t *testing.T) {
}
func TestMaxDurationConnectFeeOnly(t *testing.T) {
cd := &CallDescriptor{
Tenant: "cgrates.org",
Category: "call",
TimeStart: time.Date(2015, 9, 24, 10, 48, 0, 0, time.UTC),
TimeEnd: time.Date(2015, 9, 24, 10, 58, 1, 0, time.UTC),
Direction: utils.OUT,
Destination: "4444",
Subject: "dy",
Account: "dy",
TOR: utils.VOICE,
DurationIndex: 600,
}
rif := &Account{Id: "rif", BalanceMap: map[string]BalanceChain{
utils.MONETARY: BalanceChain{&Balance{Uuid: "moneya", Value: 0.2}},
}}
duration, err := cd.getMaxSessionDuration(rif)
if err != nil {
t.Error("Error getting max session duration: ", err)
}
if duration != 0 {
t.Error("Wrong max session: ", duration)
}
}
func TestDebitSMS(t *testing.T) {
cc := &CallCost{
Direction: utils.OUT,

View File

@@ -351,7 +351,7 @@ func (b *Balance) SetValue(amount float64) {
b.dirty = true
}
func (b *Balance) DebitUnits(cd *CallDescriptor, ub *Account, moneyBalances BalanceChain, count bool, dryRun bool) (cc *CallCost, err error) {
func (b *Balance) debitUnits(cd *CallDescriptor, ub *Account, moneyBalances BalanceChain, count bool, dryRun, debitConnectFee bool) (cc *CallCost, err error) {
if !b.IsActiveAt(cd.TimeStart) || b.GetValue() <= 0 {
return
}
@@ -431,6 +431,10 @@ func (b *Balance) DebitUnits(cd *CallDescriptor, ub *Account, moneyBalances Bala
if err != nil {
return nil, err
}
if debitConnectFee {
// this is the first add, debit the connect fee
ub.DebitConnectionFee(cc, moneyBalances, count)
}
cc.Timespans.Decompress()
//log.Printf("CC: %+v", cc)
@@ -522,7 +526,7 @@ func (b *Balance) DebitUnits(cd *CallDescriptor, ub *Account, moneyBalances Bala
return
}
func (b *Balance) DebitMoney(cd *CallDescriptor, ub *Account, count bool, dryRun bool) (cc *CallCost, err error) {
func (b *Balance) debitMoney(cd *CallDescriptor, ub *Account, moneyBalances BalanceChain, count bool, dryRun, debitConnectFee bool) (cc *CallCost, err error) {
if !b.IsActiveAt(cd.TimeStart) || b.GetValue() <= 0 {
return
}
@@ -531,6 +535,12 @@ func (b *Balance) DebitMoney(cd *CallDescriptor, ub *Account, count bool, dryRun
if err != nil {
return nil, err
}
if debitConnectFee {
// this is the first add, debit the connect fee
ub.DebitConnectionFee(cc, moneyBalances, count)
}
cc.Timespans.Decompress()
//log.Printf("CallCost In Debit: %+v", cc)
//for _, ts := range cc.Timespans {

View File

@@ -31,6 +31,7 @@ type CallCost struct {
Cost float64
Timespans TimeSpans
deductConnectFee bool
negativeConnectFee bool // the connect fee went negative on default balance
maxCostDisconect bool
}

View File

@@ -564,6 +564,10 @@ func (origCD *CallDescriptor) getMaxSessionDuration(origAcc *Account) (time.Dura
}
//log.Printf("CC: %+v", cc)
// not enough credit for connect fee
if cc.negativeConnectFee == true {
return 0, nil
}
var totalCost float64
var totalDuration time.Duration

View File

@@ -1069,8 +1069,9 @@ func TestMaxSesionTimeLongerThanMoney(t *testing.T) {
}
acc, _ := accountingStorage.GetAccount("cgrates.org:money")
allowedTime, err := cd.getMaxSessionDuration(acc)
expected, err := time.ParseDuration("2h46m40s")
expected, err := time.ParseDuration("9999s") // 1 is the connect fee
if err != nil || allowedTime != expected {
t.Log(utils.ToIJSON(acc))
t.Errorf("Expected: %v got %v", expected, allowedTime)
}
}

View File

@@ -71,6 +71,7 @@ RT_UK_Mobile_BIG5,0.01,0.10,1s,1s,0s
R_URG,0,0,1,1,0
MX,0,1,1s,1s,0
DY,0.15,0.05,60s,1s,0s
CF,1.12,0,1s,1s,0s
`
destinationRates = `
RT_STANDARD,GERMANY,R1,*middle,4,0,
@@ -93,6 +94,7 @@ RT_URG,URG,R_URG,*middle,4,0,
MX_FREE,RET,MX,*middle,4,10,*free
MX_DISC,RET,MX,*middle,4,10,*disconnect
RT_DY,RET,DY,*up,2,0,
RT_DY,EU_LANDLINE,CF,*middle,4,0,
`
ratingPlans = `
STANDARD,RT_STANDARD,WORKDAYS_00,10
@@ -396,7 +398,7 @@ func TestLoadTimimgs(t *testing.T) {
}
func TestLoadRates(t *testing.T) {
if len(csvr.rates) != 14 {
if len(csvr.rates) != 15 {
t.Error("Failed to load rates: ", len(csvr.rates))
}
rate := csvr.rates["R1"].RateSlots[0]