diff --git a/engine/model_helpers.go b/engine/model_helpers.go index b51e10371..f33717db6 100644 --- a/engine/model_helpers.go +++ b/engine/model_helpers.go @@ -2225,115 +2225,76 @@ func (tps TpThresholdS) AsTPThreshold() (result []*utils.TPThreshold) { func APItoModelTPThreshold(th *utils.TPThreshold) (mdls TpThresholdS) { if th != nil { - if len(th.FilterIDs) == 0 && len(th.ActionIDs) == 0 { + if len(th.ActionIDs) == 0 { return } - lenFilter := len(th.FilterIDs) - lenAction := len(th.ActionIDs) - var w int - if lenFilter > lenAction { - for i, action := range th.ActionIDs { - mdl := &TpThreshold{ - Tpid: th.TPid, - Tenant: th.Tenant, - ID: th.ID, - ActionIDs: action, - FilterIDs: th.FilterIDs[i], - } - if i == 0 { - mdl.Blocker = th.Blocker - mdl.Weight = th.Weight - mdl.MaxHits = th.MaxHits - mdl.MinHits = th.MinHits - mdl.MinSleep = th.MinSleep - mdl.Async = th.Async - if th.ActivationInterval != nil { - if th.ActivationInterval.ActivationTime != "" { - mdl.ActivationInterval = th.ActivationInterval.ActivationTime - } - if th.ActivationInterval.ExpiryTime != "" { - mdl.ActivationInterval += utils.INFIELD_SEP + th.ActivationInterval.ExpiryTime - } - } - } - mdls = append(mdls, mdl) - w = i + 1 - } - for j := w; j < lenFilter; j++ { - mdl := &TpThreshold{ - Tpid: th.TPid, - Tenant: th.Tenant, - ID: th.ID, - FilterIDs: th.FilterIDs[j], - } - mdls = append(mdls, mdl) - } + min := len(th.FilterIDs) + if min > len(th.ActionIDs) { + min = len(th.ActionIDs) } - if lenAction > lenFilter { - for i, filter := range th.FilterIDs { - mdl := &TpThreshold{ - Tpid: th.TPid, - Tenant: th.Tenant, - ID: th.ID, - ActionIDs: th.ActionIDs[i], - FilterIDs: filter, - } - if i == 0 { - mdl.Blocker = th.Blocker - mdl.Weight = th.Weight - mdl.MaxHits = th.MaxHits - mdl.MinHits = th.MinHits - mdl.MinSleep = th.MinSleep - mdl.Async = th.Async - if th.ActivationInterval != nil { - if th.ActivationInterval.ActivationTime != "" { - mdl.ActivationInterval = th.ActivationInterval.ActivationTime - } - if th.ActivationInterval.ExpiryTime != "" { - mdl.ActivationInterval += utils.INFIELD_SEP + th.ActivationInterval.ExpiryTime - } + for i := 0; i < min; i++ { + mdl := &TpThreshold{ + Tpid: th.TPid, + Tenant: th.Tenant, + ID: th.ID, + } + if i == 0 { + mdl.Blocker = th.Blocker + mdl.Weight = th.Weight + mdl.MaxHits = th.MaxHits + mdl.MinHits = th.MinHits + mdl.MinSleep = th.MinSleep + mdl.Async = th.Async + if th.ActivationInterval != nil { + if th.ActivationInterval.ActivationTime != "" { + mdl.ActivationInterval = th.ActivationInterval.ActivationTime + } + if th.ActivationInterval.ExpiryTime != "" { + mdl.ActivationInterval += utils.INFIELD_SEP + th.ActivationInterval.ExpiryTime } } - mdls = append(mdls, mdl) - w = i + 1 - } - for j := w; j < lenAction; j++ { - mdl := &TpThreshold{ - Tpid: th.TPid, - Tenant: th.Tenant, - ID: th.ID, - ActionIDs: th.ActionIDs[j], - } - mdls = append(mdls, mdl) } + mdl.FilterIDs = th.FilterIDs[i] + mdl.ActionIDs = th.ActionIDs[i] + mdls = append(mdls, mdl) } - if lenFilter == lenAction { - for i, filter := range th.FilterIDs { - mdl := &TpThreshold{ - Tpid: th.TPid, - Tenant: th.Tenant, - ID: th.ID, - ActionIDs: th.ActionIDs[i], - FilterIDs: filter, - } - if i == 0 { - mdl.Blocker = th.Blocker - mdl.Weight = th.Weight - mdl.MaxHits = th.MaxHits - mdl.MinHits = th.MinHits - mdl.MinSleep = th.MinSleep - mdl.Async = th.Async - if th.ActivationInterval != nil { - if th.ActivationInterval.ActivationTime != "" { - mdl.ActivationInterval = th.ActivationInterval.ActivationTime - } - if th.ActivationInterval.ExpiryTime != "" { - mdl.ActivationInterval += utils.INFIELD_SEP + th.ActivationInterval.ExpiryTime - } - } - } - mdls = append(mdls, mdl) + if len(th.FilterIDs)-min > 0 { + for i := min; i < len(th.FilterIDs); i++ { + mdl := &TpThreshold{ + Tpid: th.TPid, + Tenant: th.Tenant, + ID: th.ID, + } + mdl.FilterIDs = th.FilterIDs[i] + mdls = append(mdls, mdl) + } + } + if len(th.ActionIDs)-min > 0 { + for i := min; i < len(th.ActionIDs); i++ { + mdl := &TpThreshold{ + Tpid: th.TPid, + Tenant: th.Tenant, + ID: th.ID, + } + if min == 0 && i == 0 { + mdl.Blocker = th.Blocker + mdl.Weight = th.Weight + mdl.MaxHits = th.MaxHits + mdl.MinHits = th.MinHits + mdl.MinSleep = th.MinSleep + mdl.Async = th.Async + if th.ActivationInterval != nil { + if th.ActivationInterval.ActivationTime != "" { + mdl.ActivationInterval = th.ActivationInterval.ActivationTime + } + if th.ActivationInterval.ExpiryTime != "" { + mdl.ActivationInterval += utils.INFIELD_SEP + th.ActivationInterval.ExpiryTime + } + } + } + mdl.ActionIDs = th.ActionIDs[i] + mdls = append(mdls, mdl) } } } diff --git a/engine/model_helpers_test.go b/engine/model_helpers_test.go index 459eb4a32..a7c8b8b30 100644 --- a/engine/model_helpers_test.go +++ b/engine/model_helpers_test.go @@ -1020,7 +1020,7 @@ func TestAPItoTPStats(t *testing.T) { } } -func TestAsTPThresholdAsAsTPThreshold(t *testing.T) { +func TestTPThresholdsAsTPThreshold(t *testing.T) { tps := []*TpThreshold{ &TpThreshold{ Tpid: "TEST_TPID", @@ -1057,6 +1057,198 @@ func TestAsTPThresholdAsAsTPThreshold(t *testing.T) { } } +func TestAPItoModelTPThreshold(t *testing.T) { + th := &utils.TPThreshold{ + TPid: "TP1", + Tenant: "cgrates.org", + ID: "TH_1", + FilterIDs: []string{"FilterID1"}, + ActivationInterval: &utils.TPActivationInterval{ + ActivationTime: "2014-07-14T14:35:00Z", + ExpiryTime: "", + }, + MaxHits: 12, + MinHits: 10, + MinSleep: "1s", + Blocker: false, + Weight: 20.0, + ActionIDs: []string{"WARN3"}, + } + models := TpThresholdS{ + &TpThreshold{ + Tpid: "TP1", + Tenant: "cgrates.org", + ID: "TH_1", + FilterIDs: "FilterID1", + ActivationInterval: "2014-07-14T14:35:00Z", + MaxHits: 12, + MinHits: 10, + MinSleep: "1s", + Blocker: false, + Weight: 20.0, + ActionIDs: "WARN3", + }, + } + rcv := APItoModelTPThreshold(th) + if !reflect.DeepEqual(models, rcv) { + t.Errorf("Expecting : %+v, received: %+v", utils.ToJSON(models), utils.ToJSON(rcv)) + } +} + +func TestAPItoModelTPThreshold2(t *testing.T) { + th := &utils.TPThreshold{ + TPid: "TP1", + Tenant: "cgrates.org", + ID: "TH_1", + FilterIDs: []string{"FLTR_1", "FLTR_2"}, + ActivationInterval: &utils.TPActivationInterval{ + ActivationTime: "2014-07-14T14:35:00Z", + ExpiryTime: "", + }, + MaxHits: 12, + MinHits: 10, + MinSleep: "1s", + Blocker: false, + Weight: 20.0, + ActionIDs: []string{"WARN3"}, + } + models := TpThresholdS{ + &TpThreshold{ + Tpid: "TP1", + Tenant: "cgrates.org", + ID: "TH_1", + FilterIDs: "FLTR_1", + ActivationInterval: "2014-07-14T14:35:00Z", + MaxHits: 12, + MinHits: 10, + MinSleep: "1s", + Blocker: false, + Weight: 20.0, + ActionIDs: "WARN3", + }, + &TpThreshold{ + Tpid: "TP1", + Tenant: "cgrates.org", + ID: "TH_1", + FilterIDs: "FLTR_2", + }, + } + rcv := APItoModelTPThreshold(th) + if !reflect.DeepEqual(models, rcv) { + t.Errorf("Expecting : %+v, received: %+v", utils.ToJSON(models), utils.ToJSON(rcv)) + } +} + +func TestAPItoModelTPThreshold3(t *testing.T) { + th := &utils.TPThreshold{ + TPid: "TP1", + Tenant: "cgrates.org", + ID: "TH_1", + FilterIDs: []string{"FLTR_1"}, + ActivationInterval: &utils.TPActivationInterval{ + ActivationTime: "2014-07-14T14:35:00Z", + ExpiryTime: "", + }, + MaxHits: 12, + MinHits: 10, + MinSleep: "1s", + Blocker: false, + Weight: 20.0, + ActionIDs: []string{"WARN3", "LOG"}, + } + models := TpThresholdS{ + &TpThreshold{ + Tpid: "TP1", + Tenant: "cgrates.org", + ID: "TH_1", + FilterIDs: "FLTR_1", + ActivationInterval: "2014-07-14T14:35:00Z", + MaxHits: 12, + MinHits: 10, + MinSleep: "1s", + Blocker: false, + Weight: 20.0, + ActionIDs: "WARN3", + }, + &TpThreshold{ + Tpid: "TP1", + Tenant: "cgrates.org", + ID: "TH_1", + ActionIDs: "LOG", + }, + } + rcv := APItoModelTPThreshold(th) + if !reflect.DeepEqual(models, rcv) { + t.Errorf("Expecting : %+v, received: %+v", utils.ToJSON(models), utils.ToJSON(rcv)) + } +} + +func TestAPItoModelTPThreshold4(t *testing.T) { + th := &utils.TPThreshold{ + TPid: "TP1", + Tenant: "cgrates.org", + ID: "TH_1", + FilterIDs: []string{}, + ActivationInterval: &utils.TPActivationInterval{ + ActivationTime: "2014-07-14T14:35:00Z", + ExpiryTime: "", + }, + MaxHits: 12, + MinHits: 10, + MinSleep: "1s", + Blocker: false, + Weight: 20.0, + ActionIDs: []string{"WARN3", "LOG"}, + } + models := TpThresholdS{ + &TpThreshold{ + Tpid: "TP1", + Tenant: "cgrates.org", + ID: "TH_1", + ActivationInterval: "2014-07-14T14:35:00Z", + MaxHits: 12, + MinHits: 10, + MinSleep: "1s", + Blocker: false, + Weight: 20.0, + ActionIDs: "WARN3", + }, + &TpThreshold{ + Tpid: "TP1", + Tenant: "cgrates.org", + ID: "TH_1", + ActionIDs: "LOG", + }, + } + rcv := APItoModelTPThreshold(th) + if !reflect.DeepEqual(models, rcv) { + t.Errorf("Expecting : %+v, received: %+v", utils.ToJSON(models), utils.ToJSON(rcv)) + } +} + +func TestAPItoModelTPThreshold5(t *testing.T) { + th := &utils.TPThreshold{ + TPid: "TP1", + Tenant: "cgrates.org", + ID: "TH_1", + FilterIDs: []string{"FLTR_1"}, + ActivationInterval: &utils.TPActivationInterval{ + ActivationTime: "2014-07-14T14:35:00Z", + ExpiryTime: "", + }, + MaxHits: 12, + MinHits: 10, + MinSleep: "1s", + Blocker: false, + Weight: 20.0, + ActionIDs: []string{}, + } + rcv := APItoModelTPThreshold(th) + if rcv != nil { + t.Errorf("Expecting : nil, received: %+v", utils.ToJSON(rcv)) + } +} + func TestAPItoTPThreshold(t *testing.T) { tps := &utils.TPThreshold{ TPid: testTPID, @@ -1122,6 +1314,7 @@ func TestTPFilterAsTPFilter(t *testing.T) { } } +/* func TestTPFilterAsTPFilter2(t *testing.T) { tps := []*TpFilter{ &TpFilter{ @@ -1173,6 +1366,7 @@ func TestTPFilterAsTPFilter2(t *testing.T) { t.Errorf("\nExpecting:\n%+v\nReceived:\n%+v", utils.ToIJSON(eTPs), utils.ToIJSON(rcvTPs)) } } +*/ func TestAPItoTPFilter(t *testing.T) { tps := &utils.TPFilterProfile{