make sure the aliases are up-to-date after load

This commit is contained in:
Radu Ioan Fericean
2014-03-05 16:54:29 +02:00
parent 71a50dc78e
commit fdeecafe37
7 changed files with 47 additions and 12 deletions

View File

@@ -435,11 +435,7 @@ func TestDebitAndMaxDebit(t *testing.T) {
}
func TestMaxDebitZeroDefinedRate(t *testing.T) {
ap, _ := accountingStorage.GetActionTimings("TOPUP1000UKMOB_AT")
for _, at := range ap {
at.Execute()
}
ap, _ = accountingStorage.GetActionTimings("TOPUP10_AT")
ap, _ := accountingStorage.GetActionTimings("TOPUP10_AT")
for _, at := range ap {
at.Execute()
}
@@ -467,11 +463,7 @@ func TestMaxDebitZeroDefinedRate(t *testing.T) {
}
func TestMaxDebitZeroDefinedRateOnlyMinutes(t *testing.T) {
ap, _ := accountingStorage.GetActionTimings("TOPUP1000UKMOB_AT")
for _, at := range ap {
at.Execute()
}
ap, _ = accountingStorage.GetActionTimings("TOPUP10_AT")
ap, _ := accountingStorage.GetActionTimings("TOPUP10_AT")
for _, at := range ap {
at.Execute()
}

View File

@@ -40,6 +40,7 @@ type CSVReader struct {
actionsTriggers map[string][]*ActionTrigger
aliases map[string]string
accountActions map[string]*Account
dirtyAliases []string // used to clean aliases that might have changed
destinations []*Destination
timings map[string]*utils.TPTiming
rates map[string]*utils.TPRate
@@ -250,6 +251,9 @@ func (csvr *CSVReader) WriteToDatabase(flush, verbose bool) (err error) {
if verbose {
log.Print("Aliases")
}
if err := dataStorage.RemoveAccountAliases(csvr.dirtyAliases); err != nil {
return err
}
for key, alias := range csvr.aliases {
err = dataStorage.SetAlias(key, alias)
if err != nil {
@@ -444,6 +448,7 @@ func (csvr *CSVReader) LoadRatingProfiles() (err error) {
if err != nil {
return fmt.Errorf("Cannot parse activation time from %v", record[4])
}
csvr.dirtyAliases = append(csvr.dirtyAliases, subject)
// extract aliases from subject
aliases := strings.Split(subject, ";")
if len(aliases) > 1 {
@@ -662,6 +667,7 @@ func (csvr *CSVReader) LoadAccountActions() (err error) {
}
for record, err := csvReader.Read(); err == nil; record, err = csvReader.Read() {
tenant, account, direction := record[0], record[1], record[2]
csvr.dirtyAliases = append(csvr.dirtyAliases, account)
// extract aliases from subject
aliases := strings.Split(account, ";")
if len(aliases) > 1 {

View File

@@ -131,12 +131,13 @@ MINI,*topup_reset,*monetary,*out,10,*unlimited,,,10,,,10
MINI,*topup,*minutes,*out,100,*unlimited,NAT,test,10,,,10
SHARED,*topup,*monetary,*out,100,*unlimited,,,10,SG1,,10
TOPUP10_AC,*topup_reset,*monetary,*out,1,*unlimited,*any,,10,,,10
TOPUP10_AC,*topup_reset,*minutes,*out,40,*unlimited,DST_UK_Mobile_BIG5,discounted_minutes,10,,,10
TOPUP10_AC1,*topup_reset,*minutes,*out,40,*unlimited,DST_UK_Mobile_BIG5,discounted_minutes,10,,,10
`
actionTimings = `
MORE_MINUTES,MINI,ONE_TIME_RUN,10
MORE_MINUTES,SHARED,ONE_TIME_RUN,10
TOPUP10_AT,TOPUP10_AC,ASAP,10
TOPUP10_AT,TOPUP10_AC1,ASAP,10
`
actionTriggers = `
STANDARD_TRIGGER,*minutes,*out,*min_counter,10,GERMANY_O2,SOME_1,10
@@ -588,7 +589,7 @@ func TestLoadRatingProfiles(t *testing.T) {
}
func TestLoadActions(t *testing.T) {
if len(csvr.actions) != 3 {
if len(csvr.actions) != 4 {
t.Error("Failed to load actions: ", csvr.actions)
}
as1 := csvr.actions["MINI"]

View File

@@ -36,6 +36,7 @@ type DbReader struct {
actionsTimings map[string][]*ActionTiming
actionsTriggers map[string][]*ActionTrigger
accountActions map[string]*Account
dirtyAliases []string // used to clean aliases that might have changed
destinations []*Destination
aliases map[string]string
timings map[string]*utils.TPTiming
@@ -195,6 +196,9 @@ func (dbr *DbReader) WriteToDatabase(flush, verbose bool) (err error) {
if verbose {
log.Print("Aliases")
}
if err := storage.RemoveAccountAliases(dbr.dirtyAliases); err != nil {
return err
}
for key, alias := range dbr.aliases {
err = storage.SetAlias(key, alias)
if err != nil {
@@ -288,6 +292,7 @@ func (dbr *DbReader) LoadRatingProfiles() error {
return err
}
for _, tpRpf := range mpTpRpfs {
dbr.dirtyAliases = append(dbr.dirtyAliases, tpRpf.Subject)
// extract aliases from subject
aliases := strings.Split(tpRpf.Subject, ";")
if len(aliases) > 1 {
@@ -517,6 +522,7 @@ func (dbr *DbReader) LoadAccountActions() (err error) {
if _, alreadyDefined := dbr.accountActions[aa.KeyId()]; alreadyDefined {
return fmt.Errorf("Duplicate account action found: %s", aa.KeyId())
}
dbr.dirtyAliases = append(dbr.dirtyAliases, aa.Account)
// extract aliases from subject
aliases := strings.Split(aa.Account, ";")
if len(aliases) > 1 {

View File

@@ -78,6 +78,7 @@ type RatingStorage interface {
SetRatingProfile(*RatingProfile) error
GetAlias(string, bool) (string, error)
SetAlias(string, string) error
RemoveAccountAliases([]string) error
GetDestination(string) (*Destination, error)
SetDestination(*Destination) error
}

View File

@@ -221,6 +221,15 @@ func (ms *MapStorage) SetAlias(key, alias string) (err error) {
return
}
func (ms *MapStorage) RemoveAccountAliases(accounts []string) (err error) {
for key, value := range ms.dict {
if strings.HasPrefix(key, ALIAS_PREFIX) && utils.IsSliceMember(accounts, string(value)) {
delete(ms.dict, key)
}
}
return
}
func (ms *MapStorage) GetDestination(key string) (dest *Destination, err error) {
key = DESTINATION_PREFIX + key
if values, ok := ms.dict[key]; ok {

View File

@@ -287,6 +287,26 @@ func (rs *RedisStorage) SetAlias(key, alias string) (err error) {
return
}
func (rs *RedisStorage) RemoveAccountAliases(accounts []string) (err error) {
if alsKeys, err := rs.db.Keys(ALIAS_PREFIX + "*"); err != nil {
return err
} else {
for _, key := range alsKeys {
alias, err := rs.GetAlias(key, true)
if err != nil {
return err
}
if utils.IsSliceMember(accounts, alias) {
if _, err = rs.db.Del(key); err != nil {
return err
}
}
}
}
return
}
func (rs *RedisStorage) GetDestination(key string) (dest *Destination, err error) {
key = DESTINATION_PREFIX + key
var values []byte