add account actions tp and apier methods

This commit is contained in:
Radu Ioan Fericean
2014-08-25 14:49:09 +03:00
parent d0597921df
commit 7dc72725fc
8 changed files with 201 additions and 85 deletions

View File

@@ -168,6 +168,9 @@ func (self *SQLStorage) RemTPData(table, tpid string, args ...string) error {
case utils.TBL_TP_ACCOUNT_ACTIONS:
q = fmt.Sprintf("DELETE FROM %s WHERE tpid='%s' AND loadid='%s' AND tenant='%s' AND account='%s' AND direction='%s'",
table, tpid, args[0], args[1], args[2], args[3])
case utils.TBL_TP_DERIVED_CHARGERS:
q = fmt.Sprintf("DELETE FROM %s WHERE tpid='%s' AND loadid='%s' AND direction='%s' AND tenant='%s' AND category='%s' AND account='%s' AND subject='%s'",
table, tpid, args[0], args[1], args[2], args[3], args[4], args[5])
}
if _, err := self.Db.Exec(q); err != nil {
return err
@@ -561,26 +564,33 @@ func (self *SQLStorage) SetTPActionTriggers(tpid string, ats map[string][]*utils
}
// Sets a group of account actions. Map key has the role of grouping within a tpid
func (self *SQLStorage) SetTPAccountActions(tpid string, aa map[string]*utils.TPAccountActions) error {
if len(aa) == 0 {
func (self *SQLStorage) SetTPAccountActions(tpid string, aas map[string]*utils.TPAccountActions) error {
if len(aas) == 0 {
return nil //Nothing to set
}
var buffer bytes.Buffer
buffer.WriteString(fmt.Sprintf("INSERT INTO %s (tpid, loadid, tenant, account, direction, action_plan_id, action_triggers_id) VALUES ", utils.TBL_TP_ACCOUNT_ACTIONS))
i := 0
for _, aActs := range aa {
if i != 0 { //Consecutive values after the first will be prefixed with "," as separator
buffer.WriteRune(',')
}
buffer.WriteString(fmt.Sprintf("('%s','%s','%s','%s','%s','%s','%s')",
tpid, aActs.LoadId, aActs.Tenant, aActs.Account, aActs.Direction, aActs.ActionPlanId, aActs.ActionTriggersId))
i++
}
buffer.WriteString(" ON DUPLICATE KEY UPDATE action_plan_id=values(action_plan_id), action_triggers_id=values(action_triggers_id)")
if _, err := self.Db.Exec(buffer.String()); err != nil {
return err
tx := self.db.Begin()
for _, aa := range aas {
// parse identifiers
tx.Where("tpid = ?", tpid).
Where("direction = ?", aa.Direction).
Where("tenant = ?", aa.Tenant).
Where("account = ?", aa.Account).
Where("loadid = ?", aa.LoadId).
Delete(TpAccountAction{})
tx.Save(TpAccountAction{
Tpid: aa.TPid,
Loadid: aa.LoadId,
Tenant: aa.Tenant,
Account: aa.Account,
Direction: aa.Direction,
ActionPlanId: aa.ActionPlanId,
ActionTriggersId: aa.ActionTriggersId,
})
}
tx.Commit()
return nil
}
func (self *SQLStorage) LogCallCost(cgrid, source, runid string, cc *CallCost) (err error) {
@@ -1331,29 +1341,33 @@ func (self *SQLStorage) GetTpCdrStats(tpid, tag string) (map[string][]*utils.TPC
return css, nil
}
func (self *SQLStorage) GetTpDerivedChargers(tpid, tag string) (map[string][]*utils.TPDerivedCharger, error) {
func (self *SQLStorage) GetTpDerivedChargers(dc *utils.TPDerivedChargers) (map[string][]*utils.TPDerivedCharger, error) {
dcs := make(map[string][]*utils.TPDerivedCharger)
var tpDerivedChargers []TpDerivedCharger
q := self.db.Where("tpid = ?", tpid)
if len(tag) != 0 {
// parse identifiers
tmpDc := TpDerivedCharger{}
if err := tmpDc.SetDerivedChargersId(tag); err != nil {
return nil, err
}
q = q.Where("tpid = ?", tpid).
Where("direction = ?", tmpDc.Direction).
Where("tenant = ?", tmpDc.Tenant).
Where("account = ?", tmpDc.Account).
Where("category = ?", tmpDc.Category).
Where("subject = ?", tmpDc.Subject).
Where("loadid = ?", tmpDc.Loadid)
q := self.db.Where("tpid = ?", dc.TPid)
if len(dc.Direction) != 0 {
q = q.Where("direction = ?", dc.Direction)
}
if len(dc.Tenant) != 0 {
q = q.Where("tenant = ?", dc.Tenant)
}
if len(dc.Account) != 0 {
q = q.Where("account = ?", dc.Account)
}
if len(dc.Category) != 0 {
q = q.Where("category = ?", dc.Category)
}
if len(dc.Subject) != 0 {
q = q.Where("subject = ?", dc.Subject)
}
if len(dc.Loadid) != 0 {
q = q.Where("loadid = ?", dc.Loadid)
}
if err := q.Find(&tpDerivedChargers).Error; err != nil {
return nil, err
}
tag := dc.GetDerivedChargesId()
for _, tpDc := range tpDerivedChargers {
dcs[tag] = append(dcs[tag], &utils.TPDerivedCharger{
RunId: tpDc.RunId,
@@ -1494,40 +1508,35 @@ func (self *SQLStorage) GetTpActionTriggers(tpid, tag string) (map[string][]*uti
}
func (self *SQLStorage) GetTpAccountActions(aaFltr *utils.TPAccountActions) (map[string]*utils.TPAccountActions, error) {
q := fmt.Sprintf("SELECT loadid, tenant, account, direction, action_plan_id, action_triggers_id FROM %s WHERE tpid='%s'", utils.TBL_TP_ACCOUNT_ACTIONS, aaFltr.TPid)
if len(aaFltr.LoadId) != 0 {
q += fmt.Sprintf(" AND loadid='%s'", aaFltr.LoadId)
aas := make(map[string]*utils.TPAccountActions)
var tpAccActs []TpAccountAction
q := self.db.Where("tpid = ?", aaFltr.TPid)
if len(aaFltr.Direction) != 0 {
q = q.Where("direction = ?", aaFltr.Direction)
}
if len(aaFltr.Tenant) != 0 {
q += fmt.Sprintf(" AND tenant='%s'", aaFltr.Tenant)
q = q.Where("tenant = ?", aaFltr.Tenant)
}
if len(aaFltr.Account) != 0 {
q += fmt.Sprintf(" AND account='%s'", aaFltr.Account)
q = q.Where("account = ?", aaFltr.Account)
}
if len(aaFltr.Direction) != 0 {
q += fmt.Sprintf(" AND direction='%s'", aaFltr.Direction)
if len(aaFltr.LoadId) != 0 {
q = q.Where("loadid = ?", aaFltr.LoadId)
}
rows, err := self.Db.Query(q)
if err != nil {
if err := q.Find(&tpAccActs).Error; err != nil {
return nil, err
}
defer rows.Close()
aa := make(map[string]*utils.TPAccountActions)
for rows.Next() {
var aaLoadId, tenant, account, direction, action_plan_tag, action_triggers_tag string
if err := rows.Scan(&aaLoadId, &tenant, &account, &direction, &action_plan_tag, &action_triggers_tag); err != nil {
return nil, err
}
for _, tpAa := range tpAccActs {
aacts := &utils.TPAccountActions{
TPid: aaFltr.TPid,
LoadId: aaLoadId,
Tenant: tenant,
Account: account,
Direction: direction,
ActionPlanId: action_plan_tag,
ActionTriggersId: action_triggers_tag,
TPid: tpAa.Tpid,
LoadId: tpAa.Loadid,
Tenant: tpAa.Tenant,
Account: tpAa.Account,
Direction: tpAa.Direction,
ActionPlanId: tpAa.ActionPlanId,
ActionTriggersId: tpAa.ActionTriggersId,
}
aa[aacts.KeyId()] = aacts
aas[aacts.KeyId()] = aacts
}
return aa, nil
return aas, nil
}