This commit is contained in:
DanB
2014-04-09 17:38:35 +02:00
5 changed files with 55 additions and 5 deletions

View File

@@ -19,6 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package apier
import (
"encoding/json"
"errors"
"fmt"
"path"
@@ -433,6 +434,54 @@ func (self *ApierV1) AddTriggeredAction(attr AttrAddActionTrigger, reply *string
return nil
}
type AttrResetTriggeredAction struct {
Tenant string
Account string
Direction string
BalanceType string
ThresholdType string
ThresholdValue float64
}
func (self *ApierV1) ResetTriggeredActions(attr AttrResetTriggeredAction, reply *string) error {
if attr.Direction == "" {
attr.Direction = engine.OUTBOUND
}
extraParameters, err := json.Marshal(struct {
ThresholdType string
ThresholdValue float64
}{attr.ThresholdType, attr.ThresholdValue})
if err != nil {
*reply = err.Error()
return err
}
a := &engine.Action{
BalanceType: attr.BalanceType,
Direction: attr.Direction,
ExtraParameters: string(extraParameters),
}
accID := utils.BalanceKey(attr.Tenant, attr.Account, attr.Direction)
_, err = engine.AccLock.Guard(accID, func() (float64, error) {
acc, err := self.AccountDb.GetAccount(accID)
if err != nil {
return 0, err
}
acc.ResetActionTriggers(a)
if err = self.AccountDb.SetAccount(acc); err != nil {
return 0, err
}
return 0, nil
})
if err != nil {
*reply = err.Error()
return err
}
*reply = OK
return nil
}
// Process dependencies and load a specific AccountActions profile from storDb into dataDb.
func (self *ApierV1) LoadAccountActions(attrs utils.TPAccountActions, reply *string) error {
if missing := utils.MissingStructFields(&attrs, []string{"TPid", "LoadId", "Tenant", "Account", "Direction"}); len(missing) != 0 {

View File

@@ -381,7 +381,7 @@ func (ub *Account) executeActionTriggers(a *Action) {
// Mark all action trigers as ready for execution
// If the action is not nil it acts like a filter
func (ub *Account) resetActionTriggers(a *Action) {
func (ub *Account) ResetActionTriggers(a *Action) {
for _, at := range ub.ActionTriggers {
if !at.Match(a) {
continue

View File

@@ -830,7 +830,7 @@ func TestAccountExecuteTriggeredActions(t *testing.T) {
t.Error("Error executing triggered actions", ub.BalanceMap[CREDIT+OUTBOUND][0].Value, ub.BalanceMap[MINUTES+OUTBOUND][0].Value)
}
// we can reset them
ub.resetActionTriggers(nil)
ub.ResetActionTriggers(nil)
ub.countUnits(&Action{BalanceType: CREDIT, Direction: OUTBOUND, Balance: &Balance{Value: 10}})
if ub.BalanceMap[CREDIT+OUTBOUND][0].Value != 120 || ub.BalanceMap[MINUTES+OUTBOUND][0].Value != 30 {
t.Error("Error executing triggered actions", ub.BalanceMap[CREDIT+OUTBOUND][0].Value, ub.BalanceMap[MINUTES+OUTBOUND][0].Value)

View File

@@ -111,7 +111,7 @@ func logAction(ub *Account, a *Action) (err error) {
}
func resetTriggersAction(ub *Account, a *Action) (err error) {
ub.resetActionTriggers(a)
ub.ResetActionTriggers(a)
return
}
@@ -194,7 +194,7 @@ func genericReset(ub *Account) error {
ub.BalanceMap[k] = BalanceChain{&Balance{Value: 0}}
}
ub.UnitCounters = make([]*UnitsCounter, 0)
ub.resetActionTriggers(nil)
ub.ResetActionTriggers(nil)
return nil
}

View File

@@ -21,8 +21,9 @@ package engine
import (
"encoding/json"
"fmt"
"github.com/cgrates/cgrates/utils"
"sort"
"github.com/cgrates/cgrates/utils"
)
type ActionTrigger struct {