From d74b77200788539d5fca0710be9897b75034697d Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Thu, 4 Jun 2015 14:58:32 +0300 Subject: [PATCH] more test fixes and renamed internal ActionPlan --- engine/{action_timing.go => action_plan.go} | 42 ++++++++++----------- engine/loader_csv_test.go | 4 +- engine/models.go | 28 +++++++------- engine/storage_csv.go | 12 +++--- engine/storage_interface.go | 8 ++-- engine/storage_map.go | 12 +++--- engine/storage_mongo.go | 10 ++--- engine/storage_redis.go | 2 +- engine/storage_sql.go | 2 +- engine/tp_reader.go | 22 +++++------ 10 files changed, 71 insertions(+), 71 deletions(-) rename engine/{action_timing.go => action_plan.go} (89%) diff --git a/engine/action_timing.go b/engine/action_plan.go similarity index 89% rename from engine/action_timing.go rename to engine/action_plan.go index ead505e58..4216cc03b 100644 --- a/engine/action_timing.go +++ b/engine/action_plan.go @@ -33,7 +33,7 @@ const ( ASAP = "*asap" ) -type ActionTiming struct { +type ActionPlan struct { Uuid string // uniquely identify the timing Id string // informative purpose only AccountIds []string @@ -44,9 +44,9 @@ type ActionTiming struct { stCache time.Time // cached time of the next start } -type ActionPlan []*ActionTiming +type ActionPlans []*ActionPlan -func (at *ActionTiming) GetNextStartTime(now time.Time) (t time.Time) { +func (at *ActionPlan) GetNextStartTime(now time.Time) (t time.Time) { if !at.stCache.IsZero() { return at.stCache } @@ -69,7 +69,7 @@ func (at *ActionTiming) GetNextStartTime(now time.Time) (t time.Time) { } // To be deleted after the above solution proves reliable -func (at *ActionTiming) GetNextStartTimeOld(now time.Time) (t time.Time) { +func (at *ActionPlan) GetNextStartTimeOld(now time.Time) (t time.Time) { if !at.stCache.IsZero() { return at.stCache } @@ -219,15 +219,15 @@ YEARS: return } -func (at *ActionTiming) resetStartTimeCache() { +func (at *ActionPlan) resetStartTimeCache() { at.stCache = time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC) } -func (at *ActionTiming) SetActions(as Actions) { +func (at *ActionPlan) SetActions(as Actions) { at.actions = as } -func (at *ActionTiming) getActions() (as []*Action, err error) { +func (at *ActionPlan) getActions() (as []*Action, err error) { if at.actions == nil { at.actions, err = accountingStorage.GetActions(at.ActionsId, false) } @@ -235,7 +235,7 @@ func (at *ActionTiming) getActions() (as []*Action, err error) { return at.actions, err } -func (at *ActionTiming) Execute() (err error) { +func (at *ActionPlan) Execute() (err error) { if len(at.AccountIds) == 0 { // nothing to do if no accounts set return } @@ -276,26 +276,26 @@ func (at *ActionTiming) Execute() (err error) { } } } - storageLogger.LogActionTiming(SCHED_SOURCE, at, aac) + storageLogger.LogActionPlan(SCHED_SOURCE, at, aac) return } -func (at *ActionTiming) IsASAP() bool { +func (at *ActionPlan) IsASAP() bool { return at.Timing.Timing.StartTime == ASAP } // Structure to store actions according to weight -type ActionTimingPriotityList []*ActionTiming +type ActionPlanPriotityList []*ActionPlan -func (atpl ActionTimingPriotityList) Len() int { +func (atpl ActionPlanPriotityList) Len() int { return len(atpl) } -func (atpl ActionTimingPriotityList) Swap(i, j int) { +func (atpl ActionPlanPriotityList) Swap(i, j int) { atpl[i], atpl[j] = atpl[j], atpl[i] } -func (atpl ActionTimingPriotityList) Less(i, j int) bool { +func (atpl ActionPlanPriotityList) Less(i, j int) bool { if atpl[i].GetNextStartTime(time.Now()).Equal(atpl[j].GetNextStartTime(time.Now())) { // higher weights earlyer in the list return atpl[i].Weight > atpl[j].Weight @@ -303,23 +303,23 @@ func (atpl ActionTimingPriotityList) Less(i, j int) bool { return atpl[i].GetNextStartTime(time.Now()).Before(atpl[j].GetNextStartTime(time.Now())) } -func (atpl ActionTimingPriotityList) Sort() { +func (atpl ActionPlanPriotityList) Sort() { sort.Sort(atpl) } -func (at *ActionTiming) String_DISABLED() string { +func (at *ActionPlan) String_DISABLED() string { return at.Id + " " + at.GetNextStartTime(time.Now()).String() + ",w: " + strconv.FormatFloat(at.Weight, 'f', -1, 64) } -// Helper to remove ActionTiming members based on specific filters, empty data means no always match -func RemActionTiming(ats ActionPlan, actionTimingId, balanceId string) ActionPlan { +// Helper to remove ActionPlan members based on specific filters, empty data means no always match +func RemActionPlan(ats ActionPlans, actionTimingId, balanceId string) ActionPlans { for idx, at := range ats { - if len(actionTimingId) != 0 && at.Uuid != actionTimingId { // No Match for ActionTimingId, no need to move further + if len(actionTimingId) != 0 && at.Uuid != actionTimingId { // No Match for ActionPlanId, no need to move further continue } if len(balanceId) == 0 { // No account defined, considered match for complete removal if len(ats) == 1 { // Removing last item, by init empty - return make([]*ActionTiming, 0) + return make([]*ActionPlan, 0) } ats[idx], ats = ats[len(ats)-1], ats[:len(ats)-1] continue @@ -328,7 +328,7 @@ func RemActionTiming(ats ActionPlan, actionTimingId, balanceId string) ActionPla if blncId == balanceId { if len(at.AccountIds) == 1 { // Only one balance, remove complete at if len(ats) == 1 { // Removing last item, by init empty - return make([]*ActionTiming, 0) + return make([]*ActionPlan, 0) } ats[idx], ats = ats[len(ats)-1], ats[:len(ats)-1] } else { diff --git a/engine/loader_csv_test.go b/engine/loader_csv_test.go index 7baa92913..92c0e0047 100644 --- a/engine/loader_csv_test.go +++ b/engine/loader_csv_test.go @@ -898,7 +898,7 @@ func TestLoadLCRs(t *testing.T) { }, } if !reflect.DeepEqual(lcr, expected) { - t.Errorf("Error loading lcr %+v: ", lcr) + t.Errorf("Error loading lcr %+v: ", lcr.Activations[0].Entries[0]) } } @@ -924,7 +924,7 @@ func TestLoadActionTimings(t *testing.T) { ActionsId: "MINI", } if !reflect.DeepEqual(atm, expected) { - t.Errorf("Error loading action timing:\n%+v\n%+v", atm, expected) + t.Errorf("Error loading action timing:\n%+v", atm) } } diff --git a/engine/models.go b/engine/models.go index 0b817c6fd..78337d722 100644 --- a/engine/models.go +++ b/engine/models.go @@ -119,17 +119,17 @@ func (rpf *TpRatingProfile) GetRatingProfileId() string { type TpLcrRule struct { Id int64 Tpid string - Direction string `index:"0" re:""` - Tenant string `index:"1" re:""` - Category string `index:"2" re:""` - Account string `index:"3" re:""` - Subject string `index:"4" re:""` - DestinationTag string `index:"5" re:""` - RpCategory string `index:"6" re:""` - Strategy string `index:"7" re:""` - StrategyParams string `index:"8" re:""` - ActivationTime string `index:"9" re:""` - Weight float64 + Direction string `index:"0" re:""` + Tenant string `index:"1" re:""` + Category string `index:"2" re:""` + Account string `index:"3" re:""` + Subject string `index:"4" re:""` + DestinationTag string `index:"5" re:""` + RpCategory string `index:"6" re:""` + Strategy string `index:"7" re:""` + StrategyParams string `index:"8" re:""` + ActivationTime string `index:"9" re:""` + Weight float64 `index:"10" re:""` CreatedAt time.Time } @@ -238,9 +238,9 @@ type TpSharedGroup struct { Id int64 Tpid string Tag string `index:"0" re:""` - Account string `index:"0" re:""` - Strategy string `index:"0" re:""` - RatingSubject string `index:"0" re:""` + Account string `index:"1" re:""` + Strategy string `index:"2" re:""` + RatingSubject string `index:"3" re:""` CreatedAt time.Time } diff --git a/engine/storage_csv.go b/engine/storage_csv.go index 9f2f132ef..9e9cabb2d 100644 --- a/engine/storage_csv.go +++ b/engine/storage_csv.go @@ -326,10 +326,10 @@ func (csvs *CSVStorage) GetTpActionTriggers(tpid, tag string) ([]TpActionTrigger log.Print("bad line in action triggers csv: ", err) return nil, err } - if tpRate, err := csvLoad(TpActionTrigger{}, record); err != nil { + if tpAt, err := csvLoad(TpActionTrigger{}, record); err != nil { return nil, err } else { - tpActionTriggers = append(tpActionTriggers, tpRate.(TpActionTrigger)) + tpActionTriggers = append(tpActionTriggers, tpAt.(TpActionTrigger)) } } return tpActionTriggers, nil @@ -386,9 +386,9 @@ func (csvs *CSVStorage) GetTpDerivedChargers(filter *TpDerivedCharger) ([]TpDeri } func (csvs *CSVStorage) GetTpCdrStats(tpid, tag string) ([]TpCdrStat, error) { - csvReader, fp, err := csvs.readerFunc(csvs.derivedChargersFn, csvs.sep, getColumnCount(TpCdrStat{})) + csvReader, fp, err := csvs.readerFunc(csvs.cdrStatsFn, csvs.sep, getColumnCount(TpCdrStat{})) if err != nil { - log.Print("Could not load derivedChargers file: ", err) + log.Print("Could not load cdr stats file: ", err) // allow writing of the other values return nil, nil } @@ -401,10 +401,10 @@ func (csvs *CSVStorage) GetTpCdrStats(tpid, tag string) ([]TpCdrStat, error) { log.Print("bad line in cdr stats csv: ", err) return nil, err } - if tpRate, err := csvLoad(TpCdrStat{}, record); err != nil { + if tpCdrStat, err := csvLoad(TpCdrStat{}, record); err != nil { return nil, err } else { - tpCdrStats = append(tpCdrStats, tpRate.(TpCdrStat)) + tpCdrStats = append(tpCdrStats, tpCdrStat.(TpCdrStat)) } } return tpCdrStats, nil diff --git a/engine/storage_interface.go b/engine/storage_interface.go index 9481b86e0..f584a8a2a 100644 --- a/engine/storage_interface.go +++ b/engine/storage_interface.go @@ -103,9 +103,9 @@ type AccountingStorage interface { SetAccAlias(string, string) error RemoveAccAliases([]*TenantAccount) error GetAccountAliases(string, string, bool) ([]string, error) - GetActionTimings(string) (ActionPlan, error) - SetActionTimings(string, ActionPlan) error - GetAllActionTimings() (map[string]ActionPlan, error) + GetActionTimings(string) (ActionPlans, error) + SetActionTimings(string, ActionPlans) error + GetAllActionTimings() (map[string]ActionPlans, error) GetDerivedChargers(string, bool) (utils.DerivedChargers, error) SetDerivedChargers(string, utils.DerivedChargers) error } @@ -125,7 +125,7 @@ type LogStorage interface { //GetAllActionTimingsLogs() (map[string]ActionsTimings, error) LogError(uuid, source, runid, errstr string) error LogActionTrigger(ubId, source string, at *ActionTrigger, as Actions) error - LogActionTiming(source string, at *ActionTiming, as Actions) error + LogActionPlan(source string, at *ActionPlan, as Actions) error } type LoadStorage interface { diff --git a/engine/storage_map.go b/engine/storage_map.go index 232ec06d1..3690e9056 100644 --- a/engine/storage_map.go +++ b/engine/storage_map.go @@ -513,7 +513,7 @@ func (ms *MapStorage) SetAccount(ub *Account) (err error) { return } -func (ms *MapStorage) GetActionTimings(key string) (ats ActionPlan, err error) { +func (ms *MapStorage) GetActionTimings(key string) (ats ActionPlans, err error) { if values, ok := ms.dict[ACTION_TIMING_PREFIX+key]; ok { err = ms.ms.Unmarshal(values, &ats) } else { @@ -522,7 +522,7 @@ func (ms *MapStorage) GetActionTimings(key string) (ats ActionPlan, err error) { return } -func (ms *MapStorage) SetActionTimings(key string, ats ActionPlan) (err error) { +func (ms *MapStorage) SetActionTimings(key string, ats ActionPlans) (err error) { if len(ats) == 0 { // delete the key delete(ms.dict, ACTION_TIMING_PREFIX+key) @@ -533,13 +533,13 @@ func (ms *MapStorage) SetActionTimings(key string, ats ActionPlan) (err error) { return } -func (ms *MapStorage) GetAllActionTimings() (ats map[string]ActionPlan, err error) { - ats = make(map[string]ActionPlan) +func (ms *MapStorage) GetAllActionTimings() (ats map[string]ActionPlans, err error) { + ats = make(map[string]ActionPlans) for key, value := range ms.dict { if !strings.HasPrefix(key, ACTION_TIMING_PREFIX) { continue } - var tempAts ActionPlan + var tempAts ActionPlans err = ms.ms.Unmarshal(value, &tempAts) ats[key[len(ACTION_TIMING_PREFIX):]] = tempAts } @@ -626,7 +626,7 @@ func (ms *MapStorage) LogActionTrigger(ubId, source string, at *ActionTrigger, a return } -func (ms *MapStorage) LogActionTiming(source string, at *ActionTiming, as Actions) (err error) { +func (ms *MapStorage) LogActionPlan(source string, at *ActionPlan, as Actions) (err error) { mat, err := ms.ms.Marshal(at) if err != nil { return diff --git a/engine/storage_mongo.go b/engine/storage_mongo.go index 33623b2e0..050fa3d16 100644 --- a/engine/storage_mongo.go +++ b/engine/storage_mongo.go @@ -113,10 +113,10 @@ type LogCostEntry struct { } type LogTimingEntry struct { - ActionTiming *ActionTiming - Actions Actions - LogTime time.Time - Source string + ActionPlan *ActionPlan + Actions Actions + LogTime time.Time + Source string } type LogTriggerEntry struct { @@ -233,7 +233,7 @@ func (ms *MongoStorage) LogActionTrigger(ubId, source string, at *ActionTrigger, return ms.db.C("actlog").Insert(&LogTriggerEntry{ubId, at, as, time.Now(), source}) } -func (ms *MongoStorage) LogActionTiming(source string, at *ActionTiming, as Actions) (err error) { +func (ms *MongoStorage) LogActionPlan(source string, at *ActionPlan, as Actions) (err error) { return ms.db.C("actlog").Insert(&LogTimingEntry{at, as, time.Now(), source}) } diff --git a/engine/storage_redis.go b/engine/storage_redis.go index abf71b18e..688cfea6d 100644 --- a/engine/storage_redis.go +++ b/engine/storage_redis.go @@ -774,7 +774,7 @@ func (rs *RedisStorage) LogActionTrigger(ubId, source string, at *ActionTrigger, return } -func (rs *RedisStorage) LogActionTiming(source string, at *ActionTiming, as Actions) (err error) { +func (rs *RedisStorage) LogActionPlan(source string, at *ActionPlan, as Actions) (err error) { mat, err := rs.ms.Marshal(at) if err != nil { return diff --git a/engine/storage_sql.go b/engine/storage_sql.go index 7d4bc03f2..26cc42b91 100644 --- a/engine/storage_sql.go +++ b/engine/storage_sql.go @@ -591,7 +591,7 @@ func (self *SQLStorage) GetCallCostLog(cgrid, source, runid string) (*CallCost, func (self *SQLStorage) LogActionTrigger(ubId, source string, at *ActionTrigger, as Actions) (err error) { return } -func (self *SQLStorage) LogActionTiming(source string, at *ActionTiming, as Actions) (err error) { +func (self *SQLStorage) LogActionPlan(source string, at *ActionPlan, as Actions) (err error) { return } func (self *SQLStorage) LogError(uuid, source, runid, errstr string) (err error) { return } diff --git a/engine/tp_reader.go b/engine/tp_reader.go index ab8d062c7..3fd2797c3 100644 --- a/engine/tp_reader.go +++ b/engine/tp_reader.go @@ -16,7 +16,7 @@ type TpReader struct { accountingStorage AccountingStorage lr LoadReader actions map[string][]*Action - actionsTimings map[string][]*ActionTiming + actionsTimings map[string][]*ActionPlan actionsTriggers map[string][]*ActionTrigger accountActions map[string]*Account dirtyRpAliases []*TenantRatingSubject // used to clean aliases that might have changed @@ -42,7 +42,7 @@ func NewTpReader(rs RatingStorage, as AccountingStorage, lr LoadReader, tpid str accountingStorage: as, lr: lr, actions: make(map[string][]*Action), - actionsTimings: make(map[string][]*ActionTiming), + actionsTimings: make(map[string][]*ActionPlan), actionsTriggers: make(map[string][]*ActionTrigger), rates: make(map[string]*utils.TPRate), destinations: make(map[string]*Destination), @@ -402,7 +402,7 @@ func (tpr *TpReader) LoadLCRs() (err error) { } act.Entries = append(act.Entries, &LCREntry{ DestinationId: tpLcr.DestinationTag, - RPCategory: tpLcr.Category, + RPCategory: tpLcr.RpCategory, Strategy: tpLcr.Strategy, StrategyParams: tpLcr.StrategyParams, Weight: tpLcr.Weight, @@ -491,7 +491,7 @@ func (tpr *TpReader) LoadActionPlans() (err error) { if !exists { return fmt.Errorf("actionTiming: Could not load the timing for tag: %v", at.TimingId) } - actTmg := &ActionTiming{ + actTmg := &ActionPlan{ Uuid: utils.GenUUID(), Id: atId, Weight: at.Weight, @@ -580,10 +580,10 @@ func (tpr *TpReader) LoadAccountActionsFiltered(qriedAA *TpAccountAction) error if accountAction.ActionPlanId != "" { // get old userBalanceIds var exitingAccountIds []string - existingActionTimings, err := tpr.accountingStorage.GetActionTimings(accountAction.ActionPlanId) - if err == nil && len(existingActionTimings) > 0 { + existingActionPlans, err := tpr.accountingStorage.GetActionPlans(accountAction.ActionPlanId) + if err == nil && len(existingActionPlans) > 0 { // all action timings from a specific tag shuld have the same list of user balances from the first one - exitingAccountIds = existingActionTimings[0].AccountIds + exitingAccountIds = existingActionPlans[0].AccountIds } tpap, err := tpr.lr.GetTpActionPlans(tpr.tpid, accountAction.ActionPlanId) @@ -596,7 +596,7 @@ func (tpr *TpReader) LoadAccountActionsFiltered(qriedAA *TpAccountAction) error if err != nil { return err } - var actionTimings []*ActionTiming + var actionTimings []*ActionPlan ats := aps[accountAction.ActionPlanId] for _, at := range ats { // Check action exists before saving it inside actionTiming key @@ -617,7 +617,7 @@ func (tpr *TpReader) LoadAccountActionsFiltered(qriedAA *TpAccountAction) error return err } t := tm[at.TimingId] - actTmg := &ActionTiming{ + actTmg := &ActionPlan{ Uuid: utils.GenUUID(), Id: accountAction.ActionPlanId, Weight: at.Weight, @@ -648,7 +648,7 @@ func (tpr *TpReader) LoadAccountActionsFiltered(qriedAA *TpAccountAction) error } // write action timings - err = tpr.accountingStorage.SetActionTimings(accountAction.ActionPlanId, actionTimings) + err = tpr.accountingStorage.SetActionPlans(accountAction.ActionPlanId, actionTimings) if err != nil { return errors.New(err.Error() + " (SetActionPlan): " + accountAction.ActionPlanId) } @@ -988,7 +988,7 @@ func (tpr *TpReader) WriteToDatabase(flush, verbose bool) (err error) { log.Print("Action Plans:") } for k, ats := range tpr.actionsTimings { - err = tpr.accountingStorage.SetActionTimings(k, ats) + err = tpr.accountingStorage.SetActionPlans(k, ats) if err != nil { return err }