This commit is contained in:
DanB
2014-07-17 20:42:42 +02:00
4 changed files with 85 additions and 27 deletions

View File

@@ -487,12 +487,16 @@ func (self *ApierV1) AddTriggeredAction(attr AttrAddActionTrigger, reply *string
}
type AttrResetTriggeredAction struct {
Tenant string
Account string
Direction string
BalanceType string
ThresholdType string
ThresholdValue float64
Tenant string
Account string
Direction string
BalanceType string
ThresholdType string
ThresholdValue float64
DestinationId string
BalanceWeight float64
BalanceRatingSubject string
BalanceSharedGroup string
}
func (self *ApierV1) ResetTriggeredActions(attr AttrResetTriggeredAction, reply *string) error {
@@ -500,9 +504,20 @@ func (self *ApierV1) ResetTriggeredActions(attr AttrResetTriggeredAction, reply
attr.Direction = engine.OUTBOUND
}
extraParameters, err := json.Marshal(struct {
ThresholdType string
ThresholdValue float64
}{attr.ThresholdType, attr.ThresholdValue})
ThresholdType string
ThresholdValue float64
DestinationId string
BalanceWeight float64
BalanceRatingSubject string
BalanceSharedGroup string
}{
attr.ThresholdType,
attr.ThresholdValue,
attr.DestinationId,
attr.BalanceWeight,
attr.BalanceRatingSubject,
attr.BalanceSharedGroup,
})
if err != nil {
*reply = err.Error()
return err

View File

@@ -28,18 +28,20 @@ import (
)
type ActionTrigger struct {
Id string // uniquely identify the trigger
BalanceType string
Direction string
ThresholdType string //*min_counter, *max_counter, *min_balance, *max_balance
ThresholdValue float64
Recurrent bool // reset eexcuted flag each run
DestinationId string
BalanceWeight float64
BalanceExpiryTime time.Time
Weight float64
ActionsId string
Executed bool
Id string // uniquely identify the trigger
BalanceType string
Direction string
ThresholdType string //*min_counter, *max_counter, *min_balance, *max_balance
ThresholdValue float64
Recurrent bool // reset eexcuted flag each run
DestinationId string
BalanceWeight float64
BalanceExpiryTime time.Time
BalanceRatingSubject string
BalanceSharedGroup string
Weight float64
ActionsId string
Executed bool
}
func (at *ActionTrigger) Execute(ub *Account) (err error) {
@@ -88,17 +90,25 @@ func (at *ActionTrigger) Match(a *Action) bool {
}
id := a.BalanceType == "" || at.BalanceType == a.BalanceType
direction := a.Direction == "" || at.Direction == a.Direction
thresholdType, thresholdValue := true, true
thresholdType, thresholdValue, destinationId, weight, ratingSubject, sharedGroup := true, true, true, true, true, true
if a.ExtraParameters != "" {
t := struct {
ThresholdType string
ThresholdValue float64
ThresholdType string
ThresholdValue float64
DestinationId string
BalanceWeight float64
BalanceRatingSubject string
BalanceSharedGroup string
}{}
json.Unmarshal([]byte(a.ExtraParameters), &t)
thresholdType = t.ThresholdType == "" || at.ThresholdType == t.ThresholdType
thresholdValue = t.ThresholdValue == 0 || at.ThresholdValue == t.ThresholdValue
destinationId = t.DestinationId == "" || at.DestinationId == t.DestinationId
weight = t.BalanceWeight == 0 || at.BalanceWeight == t.BalanceWeight
ratingSubject = t.BalanceRatingSubject == "" || at.BalanceRatingSubject == t.BalanceRatingSubject
sharedGroup = t.BalanceSharedGroup == "" || at.BalanceSharedGroup == t.BalanceSharedGroup
}
return id && direction && thresholdType && thresholdValue
return id && direction && thresholdType && thresholdValue && destinationId && weight && ratingSubject && sharedGroup
}
// Structure to store actions according to weight

View File

@@ -563,6 +563,23 @@ func TestActionTriggerMatcAllFalse(t *testing.T) {
}
}
func TestActionTriggerMatchAll(t *testing.T) {
at := &ActionTrigger{
Direction: OUTBOUND,
BalanceType: CREDIT,
ThresholdType: TRIGGER_MAX_BALANCE,
ThresholdValue: 2,
DestinationId: "NAT",
BalanceWeight: 1.0,
BalanceRatingSubject: "test1",
BalanceSharedGroup: "test2",
}
a := &Action{Direction: OUTBOUND, BalanceType: CREDIT, ExtraParameters: fmt.Sprintf(`{"ThresholdType":"%v", "ThresholdValue": %v, "DestinationId": "%v", "BalanceWeight": %v, "BalanceRatingSubject": "%v", "BalanceSharedGroup": "%v"}`, TRIGGER_MAX_BALANCE, 2, "NAT", 1.0, "test1", "test2")}
if !at.Match(a) {
t.Errorf("Action trigger [%v] does not match action [%v]", at, a)
}
}
func TestActionTriggerPriotityList(t *testing.T) {
at1 := &ActionTrigger{Weight: 10}
at2 := &ActionTrigger{Weight: 20}

View File

@@ -75,6 +75,10 @@ func (b *Balance) MatchDestination(destinationId string) bool {
}
func (b *Balance) MatchActionTrigger(at *ActionTrigger) bool {
matchesDestination := true
if at.DestinationId != "" {
matchesDestination = b.MatchDestination(at.DestinationId)
}
matchesExpirationDate := true
if !at.BalanceExpiryTime.IsZero() {
matchesExpirationDate = (at.BalanceExpiryTime.Equal(b.ExpirationDate))
@@ -83,9 +87,21 @@ func (b *Balance) MatchActionTrigger(at *ActionTrigger) bool {
if at.BalanceWeight > 0 {
matchesWeight = (at.BalanceWeight == b.Weight)
}
return b.MatchDestination(at.DestinationId) &&
matchesRatingSubject := true
if at.BalanceRatingSubject != "" {
matchesRatingSubject = (at.BalanceRatingSubject == b.RatingSubject)
}
matchesSharedGroup := true
if at.BalanceSharedGroup != "" {
matchesSharedGroup = (at.BalanceSharedGroup == b.SharedGroup)
}
return matchesDestination &&
matchesExpirationDate &&
matchesWeight
matchesWeight &&
matchesRatingSubject &&
matchesSharedGroup
}
func (b *Balance) Clone() *Balance {