Add UpdateTime filed at Account structure

This commit is contained in:
TeoV
2020-01-10 13:47:47 +02:00
parent ce01ee2f32
commit 0d14876431
9 changed files with 52 additions and 40 deletions

View File

@@ -42,6 +42,7 @@ type Account struct {
ActionTriggers ActionTriggers
AllowNegative bool
Disabled bool
UpdateTime time.Time
executingTriggers bool
}

View File

@@ -742,7 +742,7 @@ func (iDB *InternalDB) SetAccountDrv(acc *Account) (err error) {
acc = ac
}
}
acc.UpdateTime = time.Now()
iDB.db.Set(utils.CacheAccounts, acc.ID, acc, nil,
cacheCommit(utils.NonTransactional), utils.NonTransactional)
return

View File

@@ -1173,6 +1173,7 @@ func (ms *MongoStorage) SetAccountDrv(acc *Account) error {
acc = ac
}
}
acc.UpdateTime = time.Now()
return ms.query(func(sctx mongo.SessionContext) (err error) {
_, err = ms.getCol(ColAcc).UpdateOne(sctx, bson.M{"id": acc.ID},
bson.M{"$set": acc},

View File

@@ -26,6 +26,7 @@ import (
"io/ioutil"
"strconv"
"sync"
"time"
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/guardian"
@@ -753,21 +754,22 @@ func (rs *RedisStorage) GetAccountDrv(key string) (*Account, error) {
return ub, nil
}
func (rs *RedisStorage) SetAccountDrv(ub *Account) (err error) {
func (rs *RedisStorage) SetAccountDrv(acc *Account) (err error) {
// never override existing account with an empty one
// UPDATE: if all balances expired and were cleaned it makes
// sense to write empty balance map
if len(ub.BalanceMap) == 0 {
if ac, err := rs.GetAccountDrv(ub.ID); err == nil && !ac.allBalancesExpired() {
ac.ActionTriggers = ub.ActionTriggers
ac.UnitCounters = ub.UnitCounters
ac.AllowNegative = ub.AllowNegative
ac.Disabled = ub.Disabled
ub = ac
if len(acc.BalanceMap) == 0 {
if ac, err := rs.GetAccountDrv(acc.ID); err == nil && !ac.allBalancesExpired() {
ac.ActionTriggers = acc.ActionTriggers
ac.UnitCounters = acc.UnitCounters
ac.AllowNegative = acc.AllowNegative
ac.Disabled = acc.Disabled
acc = ac
}
}
result, err := rs.ms.Marshal(ub)
err = rs.Cmd(redis_SET, utils.ACCOUNT_PREFIX+ub.ID, result).Err
acc.UpdateTime = time.Now()
result, err := rs.ms.Marshal(acc)
err = rs.Cmd(redis_SET, utils.ACCOUNT_PREFIX+acc.ID, result).Err
return
}

View File

@@ -241,8 +241,14 @@ func testAcc2ITMigrate(t *testing.T) {
if err != nil {
t.Error("Error when getting Accounts ", err.Error())
}
if !reflect.DeepEqual(testAccount, result) {
t.Errorf("Expecting: %+v, received: %+v", testAccount, result)
if !reflect.DeepEqual(testAccount.ID, result.ID) {
t.Errorf("Expecting: %+v, received: %+v", testAccount.ID, result.ID)
} else if !reflect.DeepEqual(testAccount.ActionTriggers, result.ActionTriggers) {
t.Errorf("Expecting: %+v, received: %+v", testAccount.ActionTriggers, result.ActionTriggers)
} else if !reflect.DeepEqual(testAccount.BalanceMap, result.BalanceMap) {
t.Errorf("Expecting: %+v, received: %+v", testAccount.BalanceMap, result.BalanceMap)
} else if !reflect.DeepEqual(testAccount.UnitCounters, result.UnitCounters) {
t.Errorf("Expecting: %+v, received: %+v", testAccount.UnitCounters, result.UnitCounters)
}
//check if old account was deleted
if _, err = acc2Migrator.dmIN.getv1Account(); err != utils.ErrNoMoreData {

View File

@@ -311,8 +311,14 @@ func testAccITMigrateAndMove(t *testing.T) {
if err != nil {
t.Error("Error when getting Accounts ", err.Error())
}
if !reflect.DeepEqual(testAccount, result) {
t.Errorf("Expecting: %+v, received: %+v", testAccount, result)
if !reflect.DeepEqual(testAccount.ID, result.ID) {
t.Errorf("Expecting: %+v, received: %+v", testAccount.ID, result.ID)
} else if !reflect.DeepEqual(testAccount.ActionTriggers, result.ActionTriggers) {
t.Errorf("Expecting: %+v, received: %+v", testAccount.ActionTriggers, result.ActionTriggers)
} else if !reflect.DeepEqual(testAccount.BalanceMap, result.BalanceMap) {
t.Errorf("Expecting: %+v, received: %+v", testAccount.BalanceMap, result.BalanceMap)
} else if !reflect.DeepEqual(testAccount.UnitCounters, result.UnitCounters) {
t.Errorf("Expecting: %+v, received: %+v", testAccount.UnitCounters, result.UnitCounters)
}
//check if old account was deleted
if _, err = accMigrator.dmIN.getv1Account(); err != utils.ErrNoMoreData {
@@ -339,8 +345,14 @@ func testAccITMigrateAndMove(t *testing.T) {
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(testAccount, result) {
t.Errorf("Expecting: %+v, received: %+v", testAccount, result)
if !reflect.DeepEqual(testAccount.ID, result.ID) {
t.Errorf("Expecting: %+v, received: %+v", testAccount.ID, result.ID)
} else if !reflect.DeepEqual(testAccount.ActionTriggers, result.ActionTriggers) {
t.Errorf("Expecting: %+v, received: %+v", testAccount.ActionTriggers, result.ActionTriggers)
} else if !reflect.DeepEqual(testAccount.BalanceMap, result.BalanceMap) {
t.Errorf("Expecting: %+v, received: %+v", testAccount.BalanceMap, result.BalanceMap)
} else if !reflect.DeepEqual(testAccount.UnitCounters, result.UnitCounters) {
t.Errorf("Expecting: %+v, received: %+v", testAccount.UnitCounters, result.UnitCounters)
}
//check if old account was deleted
result, err = accMigrator.dmIN.DataManager().GetAccount(testAccount.ID)

View File

@@ -58,10 +58,6 @@ func TestCdrsReload(t *testing.T) {
close(chS.GetPrecacheChannel(utils.CacheTimings))
cfg.ChargerSCfg().Enabled = true
internalChan := make(chan rpcclient.ClientConnector, 1)
internalChan <- nil
cacheSChan := make(chan rpcclient.ClientConnector, 1)
cacheSChan <- chS
server := utils.NewServer()
srvMngr := servmanager.NewServiceManager(cfg, engineShutdown)
db := NewDataDBService(cfg, nil)
@@ -78,7 +74,6 @@ func TestCdrsReload(t *testing.T) {
cdrS := NewCDRServer(cfg, db, stordb, filterSChan, server,
make(chan rpcclient.ClientConnector, 1),
nil)
engine.NewConnManager(cfg, nil)
srvMngr.AddServices(cdrS, ralS, schS, chrS,
NewLoaderService(cfg, db, filterSChan, server, engineShutdown,
make(chan rpcclient.ClientConnector, 1), nil), db, stordb)

View File

@@ -58,10 +58,6 @@ func TestRalsReload(t *testing.T) {
close(chS.GetPrecacheChannel(utils.CacheTimings))
cfg.ThresholdSCfg().Enabled = true
internalChan := make(chan rpcclient.ClientConnector, 1)
internalChan <- nil
cacheSChan := make(chan rpcclient.ClientConnector, 1)
cacheSChan <- chS
server := utils.NewServer()
srvMngr := servmanager.NewServiceManager(cfg, engineShutdown)
db := NewDataDBService(cfg, nil)
@@ -75,7 +71,6 @@ func TestRalsReload(t *testing.T) {
make(chan rpcclient.ClientConnector, 1),
make(chan rpcclient.ClientConnector, 1),
schS, engineShutdown, nil)
engine.NewConnManager(cfg, nil)
srvMngr.AddServices(ralS, schS, tS,
NewLoaderService(cfg, db, filterSChan, server, engineShutdown, make(chan rpcclient.ClientConnector, 1), nil), db, stordb)
if err = srvMngr.StartServices(); err != nil {

View File

@@ -1091,7 +1091,7 @@ Date: Fri Dec 30 19:48:09 2016 +0100
Fixes for db driver to avoid returning new values in case of errors
`
eVers := "CGRateS 0.9.1~rc8 git+73014da (2016-12-30T19:48:09+01:00)"
eVers := "CGRateS@v0.9.1~rc8-20161230184809-73014daa0c1d"
if vers, err := GetCGRVersion(); err != nil {
t.Error(err)
} else if vers != eVers {
@@ -1100,35 +1100,35 @@ Date: Fri Dec 30 19:48:09 2016 +0100
GitLastLog = ""
if vers, err := GetCGRVersion(); err != nil {
t.Error(err)
} else if vers != "CGRateS 0.9.1~rc8" {
t.Errorf("Expecting: <CGRateS 0.9.1~rc8>, received: <%s>", vers)
} else if vers != "CGRateS@v0.9.1~rc8" {
t.Errorf("Expecting: <CGRateS@v0.9.1~rc8>, received: <%s>", vers)
}
GitLastLog = "\n"
if vers, err := GetCGRVersion(); err == nil || err.Error() != "Building version - error: <EOF> reading line from file" {
t.Error(err)
} else if vers != "CGRateS 0.9.1~rc8" {
t.Errorf("Expecting: <CGRateS 0.9.1~rc8>, received: <%s>", vers)
} else if vers != "CGRateS@v0.9.1~rc8" {
t.Errorf("Expecting: <CGRateS@v0.9.1~rc8>, received: <%s>", vers)
}
GitLastLog = `commit . . .
`
if vers, err := GetCGRVersion(); err == nil || err.Error() != "Building version - cannot extract commit hash" {
t.Error(err)
} else if vers != "CGRateS 0.9.1~rc8" {
t.Errorf("Expecting: <CGRateS 0.9.1~rc8>, received: <%s>", vers)
} else if vers != "CGRateS@v0.9.1~rc8" {
t.Errorf("Expecting: <CGRateS@v0.9.1~rc8>, received: <%s>", vers)
}
GitLastLog = `Date: : :
`
if vers, err := GetCGRVersion(); err == nil || err.Error() != "Building version - cannot split commit date" {
t.Error(err)
} else if vers != "CGRateS 0.9.1~rc8" {
t.Errorf("Expecting: <CGRateS 0.9.1~rc8>, received: <%s>", vers)
} else if vers != "CGRateS@v0.9.1~rc8" {
t.Errorf("Expecting: <CGRateS@v0.9.1~rc8>, received: <%s>", vers)
}
GitLastLog = `Date: wrong format
`
if vers, err := GetCGRVersion(); err == nil || err.Error() != `Building version - error: <parsing time "wrong format" as "Mon Jan 2 15:04:05 2006 -0700": cannot parse "wrong format" as "Mon"> compiling commit date` {
t.Error(err)
} else if vers != "CGRateS 0.9.1~rc8" {
t.Errorf("Expecting: <CGRateS 0.9.1~rc8>, received: <%s>", vers)
} else if vers != "CGRateS@v0.9.1~rc8" {
t.Errorf("Expecting: <CGRateS@v0.9.1~rc8>, received: <%s>", vers)
}
GitLastLog = `ommit 73014daa0c1d7edcb532d5fe600b8a20d588cdf8
Author: DanB <danb@cgrates.org>
@@ -1138,8 +1138,8 @@ Date: Fri Dec 30 19:48:09 2016 +0100
`
if vers, err := GetCGRVersion(); err == nil || err.Error() != "Cannot find commitHash or commitDate information" {
t.Error(err)
} else if vers != "CGRateS 0.9.1~rc8" {
t.Errorf("Expecting: <CGRateS 0.9.1~rc8>, received: <%s>", vers)
} else if vers != "CGRateS@v0.9.1~rc8" {
t.Errorf("Expecting: <CGRateS@v0.9.1~rc8>, received: <%s>", vers)
}
}