From 785e7e9386a6193f0c37bc8f650d6499e577917a Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Fri, 18 Dec 2015 14:31:44 +0200 Subject: [PATCH] fixes and updated migrator --- apier/v1/apier_local_test.go | 1 - cmd/cgr-loader/migrator_rc8.go | 50 ++++++++++++++++++++++++++++++++-- engine/tp_reader.go | 2 ++ scheduler/scheduler.go | 1 + 4 files changed, 50 insertions(+), 4 deletions(-) diff --git a/apier/v1/apier_local_test.go b/apier/v1/apier_local_test.go index 2ec712d3f..12144cfbf 100644 --- a/apier/v1/apier_local_test.go +++ b/apier/v1/apier_local_test.go @@ -1134,7 +1134,6 @@ func TestApierGetAccount(t *testing.T) { if !*testLocal { return } - time.Sleep(100 * time.Millisecond) // give scheduler time to react var reply *engine.Account attrs := &utils.AttrGetAccount{Tenant: "cgrates.org", Account: "1001"} if err := rater.Call("ApierV2.GetAccount", attrs, &reply); err != nil { diff --git a/cmd/cgr-loader/migrator_rc8.go b/cmd/cgr-loader/migrator_rc8.go index 6eab368ac..672167a5f 100644 --- a/cmd/cgr-loader/migrator_rc8.go +++ b/cmd/cgr-loader/migrator_rc8.go @@ -125,6 +125,26 @@ type Action struct { Balance *Balance } +type ActionPlan struct { + Uuid string // uniquely identify the timing + Id string // informative purpose only + AccountIds []string + Timing *engine.RateInterval + Weight float64 + ActionsId string + actions Actions + stCache time.Time // cached time of the next start +} + +func (at *ActionPlan) IsASAP() bool { + if at.Timing == nil { + return false + } + return at.Timing.Timing.StartTime == utils.ASAP +} + +type ActionPlans []*ActionPlan + func (mig MigratorRC8) migrateAccounts() error { keys, err := mig.db.Cmd("KEYS", OLD_ACCOUNT_PREFIX+"*").List() if err != nil { @@ -431,10 +451,10 @@ func (mig MigratorRC8) migrateActionPlans() error { if err != nil { return err } - aplsMap := make(map[string]engine.ActionPlans, len(keys)) + aplsMap := make(map[string]ActionPlans, len(keys)) for _, key := range keys { log.Printf("Migrating action plans: %s...", key) - var apls engine.ActionPlans + var apls ActionPlans var values []byte if values, err = mig.db.Cmd("GET", key).Bytes(); err == nil { if err := mig.ms.Unmarshal(values, &apls); err != nil { @@ -456,7 +476,31 @@ func (mig MigratorRC8) migrateActionPlans() error { aplsMap[key] = apls } // write data back - for key, apl := range aplsMap { + newAplMap := make(map[string]*engine.ActionPlan) + for key, apls := range aplsMap { + for _, apl := range apls { + newApl, exists := newAplMap[key] + if !exists { + newApl = &engine.ActionPlan{ + Id: apl.Id, + AccountIDs: make(map[string]struct{}), + } + newAplMap[key] = newApl + } + if !apl.IsASAP() { + for _, accID := range apl.AccountIds { + newApl.AccountIDs[accID] = struct{}{} + } + } + newApl.ActionTimings = append(newApl.ActionTimings, &engine.ActionTiming{ + Uuid: utils.GenUUID(), + Timing: apl.Timing, + ActionsID: apl.ActionsId, + Weight: apl.Weight, + }) + } + } + for key, apl := range newAplMap { result, err := mig.ms.Marshal(apl) if err != nil { return err diff --git a/engine/tp_reader.go b/engine/tp_reader.go index 23b8e4fbc..d5d4fc4a5 100644 --- a/engine/tp_reader.go +++ b/engine/tp_reader.go @@ -582,6 +582,7 @@ func (tpr *TpReader) LoadActionPlans() (err error) { } } actPln.ActionTimings = append(actPln.ActionTimings, &ActionTiming{ + Uuid: utils.GenUUID(), Weight: at.Weight, Timing: &RateInterval{ Timing: &RITiming{ @@ -713,6 +714,7 @@ func (tpr *TpReader) LoadAccountActionsFiltered(qriedAA *TpAccountAction) error } } actionPlan.ActionTimings = append(actionPlan.ActionTimings, &ActionTiming{ + Uuid: utils.GenUUID(), Weight: at.Weight, Timing: &RateInterval{ Timing: &RITiming{ diff --git a/scheduler/scheduler.go b/scheduler/scheduler.go index 45583931f..ac5e35d63 100644 --- a/scheduler/scheduler.go +++ b/scheduler/scheduler.go @@ -133,6 +133,7 @@ func (s *Scheduler) loadActionPlans() { continue } at.SetAccountIDs(actionPlan.AccountIDs) // copy the accounts + at.SetActionPlanID(actionPlan.Id) s.queue = append(s.queue, at) }