Thresholds.ProcessEvent returning also number of threshold hits, integration tests

This commit is contained in:
DanB
2017-10-11 20:13:58 +02:00
parent 5b6b3d3c22
commit 4b41e0c6ea
3 changed files with 20 additions and 80 deletions

View File

@@ -54,8 +54,8 @@ func (tSv1 *ThresholdSV1) GetThreshold(tntID *utils.TenantID, t *engine.Threshol
}
// ProcessEvent will process an Event
func (tSv1 *ThresholdSV1) ProcessEvent(ev *engine.ThresholdEvent, reply *string) error {
return tSv1.tS.V1ProcessEvent(ev, reply)
func (tSv1 *ThresholdSV1) ProcessEvent(ev *engine.ThresholdEvent, hits *int) error {
return tSv1.tS.V1ProcessEvent(ev, hits)
}
// GetThresholdProfile returns a Threshold Profile

View File

@@ -80,7 +80,7 @@ var sTestsThresholdSV1 = []func(t *testing.T){
testV1TSRpcConn,
testV1TSFromFolder,
testV1TSGetThresholds,
//testV1STSProcessEvent,
testV1TSProcessEvent,
//testV1TSGetThresholdsAfterRestart,
//testV1STSSetThresholdProfile,
//testV1STSUpdateThresholdProfile,
@@ -164,79 +164,18 @@ func testV1TSGetThresholds(t *testing.T) {
}
}
/*
func testV1TSProcessEvent(t *testing.T) {
var hits int
for _, ev := range tEvs {
if err := tSv1Rpc.Call("ThresholdSV1.ProcessEvent", ev, &hits); err != nil {
t.Error(err)
}
}
func testV1STSProcessEvent(t *testing.T) {
var reply string
ev1 := engine.StatEvent{
Tenant: "cgrates.org",
ID: "event1",
Fields: map[string]interface{}{
utils.ACCOUNT: "1001",
utils.ANSWER_TIME: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC),
utils.USAGE: time.Duration(135 * time.Second),
utils.COST: 123.0,
utils.PDD: time.Duration(12 * time.Second)}}
if err := tSv1Rpc.Call("StatSV1.ProcessEvent", &ev1, &reply); err != nil {
t.Error(err)
} else if reply != utils.OK {
t.Errorf("received reply: %s", reply)
}
//process with one event (should be N/A becaus MinItems is 2)
expectedMetrics := map[string]string{
utils.MetaASR: utils.NOT_AVAILABLE,
utils.MetaACD: utils.NOT_AVAILABLE,
utils.MetaTCC: utils.NOT_AVAILABLE,
utils.MetaTCD: utils.NOT_AVAILABLE,
utils.MetaACC: utils.NOT_AVAILABLE,
utils.MetaPDD: utils.NOT_AVAILABLE,
}
var metrics map[string]string
if err := tSv1Rpc.Call("StatSV1.GetQueueStringMetrics", &utils.TenantID{Tenant: "cgrates.org", ID: "Stats1"}, &metrics); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(expectedMetrics, metrics) {
t.Errorf("expecting: %+v, received reply: %s", expectedMetrics, metrics)
}
ev2 := engine.StatEvent{
Tenant: "cgrates.org",
ID: "event2",
Fields: map[string]interface{}{
utils.ACCOUNT: "1002",
utils.ANSWER_TIME: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC),
utils.USAGE: time.Duration(45 * time.Second)}}
if err := tSv1Rpc.Call("StatSV1.ProcessEvent", &ev2, &reply); err != nil {
t.Error(err)
} else if reply != utils.OK {
t.Errorf("received reply: %s", reply)
}
ev3 := &engine.StatEvent{
Tenant: "cgrates.org",
ID: "event3",
Fields: map[string]interface{}{
utils.ACCOUNT: "1002",
utils.SETUP_TIME: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC),
utils.USAGE: 0}}
if err := tSv1Rpc.Call("StatSV1.ProcessEvent", &ev3, &reply); err != nil {
t.Error(err)
} else if reply != utils.OK {
t.Errorf("received reply: %s", reply)
}
expectedMetrics2 := map[string]string{
utils.MetaASR: "66.66667%",
utils.MetaACD: "1m30s",
utils.MetaACC: "61.5",
utils.MetaTCD: "3m0s",
utils.MetaTCC: "123",
utils.MetaPDD: "4s",
}
var metrics2 map[string]string
if err := tSv1Rpc.Call("StatSV1.GetQueueStringMetrics", &utils.TenantID{Tenant: "cgrates.org", ID: "Stats1"}, &metrics2); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(expectedMetrics2, metrics2) {
t.Errorf("expecting: %+v, received reply: %s", expectedMetrics2, metrics2)
}
}
/*
func testV1TSGetThresholdsAfterRestart(t *testing.T) {
time.Sleep(time.Second)
if _, err := engine.StopStartEngine(tSv1CfgPath, thdsDelay); err != nil {

View File

@@ -287,13 +287,12 @@ func (tS *ThresholdService) matchingThresholdsForEvent(ev *ThresholdEvent) (ts T
}
// processEvent processes a new event, dispatching to matching thresholds
func (tS *ThresholdService) processEvent(ev *ThresholdEvent) (err error) {
func (tS *ThresholdService) processEvent(ev *ThresholdEvent) (hits int, err error) {
matchTs, err := tS.matchingThresholdsForEvent(ev)
if err != nil {
return err
} else if len(matchTs) == 0 {
return utils.ErrNotFound
return 0, err
}
hits = len(matchTs)
var withErrors bool
for _, t := range matchTs {
err = t.ProcessEvent(ev, tS.dm)
@@ -335,12 +334,14 @@ func (tS *ThresholdService) processEvent(ev *ThresholdEvent) (err error) {
}
// V1ProcessEvent implements ThresholdService method for processing an Event
func (tS *ThresholdService) V1ProcessEvent(ev *ThresholdEvent, reply *string) (err error) {
func (tS *ThresholdService) V1ProcessEvent(ev *ThresholdEvent, reply *int) (err error) {
if missing := utils.MissingStructFields(ev, []string{"Tenant", "ID"}); len(missing) != 0 { //Params missing
return utils.NewErrMandatoryIeMissing(missing...)
}
if err = tS.processEvent(ev); err == nil {
*reply = utils.OK
if hits, err := tS.processEvent(ev); err != nil {
return err
} else {
*reply = hits
}
return
}