mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
add derived chargers tp and apier methods
This commit is contained in:
@@ -80,7 +80,7 @@ func (self *ApierV1) GetTPAccountActionLoadIds(attrs AttrGetTPAccountActionIds,
|
||||
if missing := utils.MissingStructFields(&attrs, []string{"TPid"}); len(missing) != 0 { //Params missing
|
||||
return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing)
|
||||
}
|
||||
if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_ACCOUNT_ACTIONS, "loadid", nil); err != nil {
|
||||
if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_ACCOUNT_ACTIONS, utils.TPDistinctIds{"loadid"}, nil); err != nil {
|
||||
return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
|
||||
} else if ids == nil {
|
||||
return errors.New(utils.ERR_NOT_FOUND)
|
||||
|
||||
@@ -75,7 +75,7 @@ func (self *ApierV1) GetTPActionIds(attrs AttrGetTPActionIds, reply *[]string) e
|
||||
if missing := utils.MissingStructFields(&attrs, []string{"TPid"}); len(missing) != 0 { //Params missing
|
||||
return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing)
|
||||
}
|
||||
if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_ACTIONS, "id", nil); err != nil {
|
||||
if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_ACTIONS, utils.TPDistinctIds{"id"}, nil); err != nil {
|
||||
return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
|
||||
} else if ids == nil {
|
||||
return errors.New(utils.ERR_NOT_FOUND)
|
||||
|
||||
@@ -73,7 +73,7 @@ func (self *ApierV1) GetTPActionPlanIds(attrs AttrGetTPActionPlanIds, reply *[]s
|
||||
if missing := utils.MissingStructFields(&attrs, []string{"TPid"}); len(missing) != 0 { //Params missing
|
||||
return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing)
|
||||
}
|
||||
if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_ACTION_PLANS, "id", nil); err != nil {
|
||||
if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_ACTION_PLANS, utils.TPDistinctIds{"id"}, nil); err != nil {
|
||||
return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
|
||||
} else if ids == nil {
|
||||
return errors.New(utils.ERR_NOT_FOUND)
|
||||
|
||||
@@ -71,7 +71,7 @@ func (self *ApierV1) GetTPActionTriggerIds(attrs AttrGetTPActionTriggerIds, repl
|
||||
if missing := utils.MissingStructFields(&attrs, []string{"TPid"}); len(missing) != 0 { //Params missing
|
||||
return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing)
|
||||
}
|
||||
if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_ACTION_TRIGGERS, "id", nil); err != nil {
|
||||
if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_ACTION_TRIGGERS, utils.TPDistinctIds{"id"}, nil); err != nil {
|
||||
return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
|
||||
} else if ids == nil {
|
||||
return errors.New(utils.ERR_NOT_FOUND)
|
||||
|
||||
@@ -75,7 +75,7 @@ func (self *ApierV1) GetTPCdrStatsIds(attrs AttrGetTPCdrStatIds, reply *[]string
|
||||
if missing := utils.MissingStructFields(&attrs, []string{"TPid"}); len(missing) != 0 { //Params missing
|
||||
return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing)
|
||||
}
|
||||
if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_CDR_STATS, "id", nil); err != nil {
|
||||
if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_CDR_STATS, utils.TPDistinctIds{"id"}, nil); err != nil {
|
||||
return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
|
||||
} else if ids == nil {
|
||||
return errors.New(utils.ERR_NOT_FOUND)
|
||||
|
||||
@@ -18,13 +18,19 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
|
||||
package apier
|
||||
|
||||
/*
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
)
|
||||
|
||||
// Creates a new DerivedCharges profile within a tariff plan
|
||||
func (self *ApierV1) SetTPDerivedCharges(attrs utils.TPDerivedCharges, reply *string) error {
|
||||
if missing := utils.MissingStructFields(&attrs, []string{"TPid", "DerivedChargesId", "DerivedCharges"}); len(missing) != 0 {
|
||||
func (self *ApierV1) SetTPDerivedChargers(attrs utils.TPDerivedChargers, reply *string) error {
|
||||
if missing := utils.MissingStructFields(&attrs, []string{"TPid", "Direction", "Tenant", "Category", "Account", "Subject"}); len(missing) != 0 {
|
||||
return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing)
|
||||
}
|
||||
for _, action := range attrs.DerivedCharges {
|
||||
/*for _, action := range attrs.DerivedCharges {
|
||||
requiredFields := []string{"Identifier", "Weight"}
|
||||
if action.BalanceType != "" { // Add some inter-dependent parameters - if balanceType then we are not talking about simply calling actions
|
||||
requiredFields = append(requiredFields, "Direction", "Units")
|
||||
@@ -32,30 +38,39 @@ func (self *ApierV1) SetTPDerivedCharges(attrs utils.TPDerivedCharges, reply *st
|
||||
if missing := utils.MissingStructFields(action, requiredFields); len(missing) != 0 {
|
||||
return fmt.Errorf("%s:DerivedCharge:%s:%v", utils.ERR_MANDATORY_IE_MISSING, action.Identifier, missing)
|
||||
}
|
||||
}
|
||||
if err := self.StorDb.SetTPDerivedCharges(attrs.TPid, map[string][]*utils.TPDerivedCharge{attrs.DerivedChargesId: attrs.DerivedCharges}); err != nil {
|
||||
}*/
|
||||
if err := self.StorDb.SetTPDerivedChargers(attrs.TPid, map[string][]*utils.TPDerivedCharger{
|
||||
attrs.GetDerivedChargesId(): attrs.DerivedChargers,
|
||||
}); err != nil {
|
||||
return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
|
||||
}
|
||||
*reply = "OK"
|
||||
return nil
|
||||
}
|
||||
|
||||
type AttrGetTPDerivedCharges struct {
|
||||
TPid string // Tariff plan id
|
||||
DerivedChargesId string // DerivedCharge id
|
||||
type AttrGetTPDerivedChargers struct {
|
||||
TPid string // Tariff plan id
|
||||
DerivedChargersId string // DerivedCharge id
|
||||
}
|
||||
|
||||
// Queries specific DerivedCharge on tariff plan
|
||||
func (self *ApierV1) GetTPDerivedCharges(attrs AttrGetTPDerivedCharges, reply *utils.TPDerivedCharges) error {
|
||||
if missing := utils.MissingStructFields(&attrs, []string{"TPid", "DerivedChargesId"}); len(missing) != 0 { //Params missing
|
||||
func (self *ApierV1) GetTPDerivedChargers(attrs AttrGetTPDerivedChargers, reply *utils.TPDerivedChargers) error {
|
||||
if missing := utils.MissingStructFields(&attrs, []string{"TPid", "DerivedChargersId"}); len(missing) != 0 { //Params missing
|
||||
return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing)
|
||||
}
|
||||
if sgs, err := self.StorDb.GetTpDerivedCharges(attrs.TPid, attrs.DerivedChargesId); err != nil {
|
||||
if sgs, err := self.StorDb.GetTpDerivedChargers(attrs.TPid, attrs.DerivedChargersId); err != nil {
|
||||
return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
|
||||
} else if len(sgs) == 0 {
|
||||
return errors.New(utils.ERR_NOT_FOUND)
|
||||
} else {
|
||||
*reply = utils.TPDerivedCharges{TPid: attrs.TPid, DerivedChargesId: attrs.DerivedChargesId, DerivedCharges: sgs[attrs.DerivedChargesId]}
|
||||
tpdc := utils.TPDerivedChargers{
|
||||
TPid: attrs.TPid,
|
||||
DerivedChargers: sgs[attrs.DerivedChargersId],
|
||||
}
|
||||
if err := tpdc.SetDerivedChargersId(attrs.DerivedChargersId); err != nil {
|
||||
return err
|
||||
}
|
||||
*reply = tpdc
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -65,11 +80,11 @@ type AttrGetTPDerivedChargeIds struct {
|
||||
}
|
||||
|
||||
// Queries DerivedCharges identities on specific tariff plan.
|
||||
func (self *ApierV1) GetTPDerivedChargeIds(attrs AttrGetTPDerivedChargeIds, reply *[]string) error {
|
||||
func (self *ApierV1) GetTPDerivedChargerIds(attrs AttrGetTPDerivedChargeIds, reply *[]string) error {
|
||||
if missing := utils.MissingStructFields(&attrs, []string{"TPid"}); len(missing) != 0 { //Params missing
|
||||
return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing)
|
||||
}
|
||||
if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_DERIVED_CHARGES, "id", nil); err != nil {
|
||||
if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_DERIVED_CHARGERS, utils.TPDistinctIds{"direction", "tenant", "category", "account", "subject", "loadid"}, nil); err != nil {
|
||||
return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
|
||||
} else if ids == nil {
|
||||
return errors.New(utils.ERR_NOT_FOUND)
|
||||
@@ -80,15 +95,14 @@ func (self *ApierV1) GetTPDerivedChargeIds(attrs AttrGetTPDerivedChargeIds, repl
|
||||
}
|
||||
|
||||
// Removes specific DerivedCharges on Tariff plan
|
||||
func (self *ApierV1) RemTPDerivedCharges(attrs AttrGetTPDerivedCharges, reply *string) error {
|
||||
func (self *ApierV1) RemTPDerivedChargers(attrs AttrGetTPDerivedChargers, reply *string) error {
|
||||
if missing := utils.MissingStructFields(&attrs, []string{"TPid", "DerivedChargesId"}); len(missing) != 0 { //Params missing
|
||||
return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing)
|
||||
}
|
||||
if err := self.StorDb.RemTPData(utils.TBL_TP_DERIVED_CHARGES, attrs.TPid, attrs.DerivedChargesId); err != nil {
|
||||
if err := self.StorDb.RemTPData(utils.TBL_TP_DERIVED_CHARGERS, attrs.TPid, attrs.DerivedChargersId); err != nil {
|
||||
return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
|
||||
} else {
|
||||
*reply = "OK"
|
||||
}
|
||||
return nil
|
||||
}
|
||||
*/
|
||||
|
||||
@@ -68,7 +68,7 @@ func (self *ApierV1) GetTPDestinationRateIds(attrs AttrGetTPRateIds, reply *[]st
|
||||
if missing := utils.MissingStructFields(&attrs, []string{"TPid"}); len(missing) != 0 { //Params missing
|
||||
return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing)
|
||||
}
|
||||
if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_DESTINATION_RATES, "id", nil); err != nil {
|
||||
if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_DESTINATION_RATES, utils.TPDistinctIds{"id"}, nil); err != nil {
|
||||
return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
|
||||
} else if ids == nil {
|
||||
return errors.New(utils.ERR_NOT_FOUND)
|
||||
|
||||
@@ -67,7 +67,7 @@ func (self *ApierV1) GetTPDestinationIds(attrs AttrGetTPDestinationIds, reply *[
|
||||
if missing := utils.MissingStructFields(&attrs, []string{"TPid"}); len(missing) != 0 { //Params missing
|
||||
return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing)
|
||||
}
|
||||
if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_DESTINATIONS, "id", nil); err != nil {
|
||||
if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_DESTINATIONS, utils.TPDistinctIds{"id"}, nil); err != nil {
|
||||
return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
|
||||
} else if ids == nil {
|
||||
return errors.New(utils.ERR_NOT_FOUND)
|
||||
|
||||
@@ -68,7 +68,7 @@ func (self *ApierV1) GetTPRateIds(attrs AttrGetTPRateIds, reply *[]string) error
|
||||
if missing := utils.MissingStructFields(&attrs, []string{"TPid"}); len(missing) != 0 { //Params missing
|
||||
return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing)
|
||||
}
|
||||
if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_RATES, "id", nil); err != nil {
|
||||
if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_RATES, utils.TPDistinctIds{"id"}, nil); err != nil {
|
||||
return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
|
||||
} else if ids == nil {
|
||||
return errors.New(utils.ERR_NOT_FOUND)
|
||||
|
||||
@@ -68,7 +68,7 @@ func (self *ApierV1) GetTPRatingPlanIds(attrs AttrGetTPRatingPlanIds, reply *[]s
|
||||
if missing := utils.MissingStructFields(&attrs, []string{"TPid"}); len(missing) != 0 { //Params missing
|
||||
return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing)
|
||||
}
|
||||
if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_RATING_PLANS, "id", nil); err != nil {
|
||||
if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_RATING_PLANS, utils.TPDistinctIds{"id"}, nil); err != nil {
|
||||
return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
|
||||
} else if ids == nil {
|
||||
return errors.New(utils.ERR_NOT_FOUND)
|
||||
|
||||
@@ -76,7 +76,7 @@ func (self *ApierV1) GetTPRatingProfileLoadIds(attrs utils.AttrTPRatingProfileId
|
||||
if missing := utils.MissingStructFields(&attrs, []string{"TPid"}); len(missing) != 0 { //Params missing
|
||||
return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing)
|
||||
}
|
||||
if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_RATE_PROFILES, "loadid", map[string]string{
|
||||
if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_RATE_PROFILES, utils.TPDistinctIds{"loadid"}, map[string]string{
|
||||
"tenant": attrs.Tenant,
|
||||
"tor": attrs.Category,
|
||||
"direction": attrs.Direction,
|
||||
|
||||
@@ -75,7 +75,7 @@ func (self *ApierV1) GetTPSharedGroupIds(attrs AttrGetTPSharedGroupIds, reply *[
|
||||
if missing := utils.MissingStructFields(&attrs, []string{"TPid"}); len(missing) != 0 { //Params missing
|
||||
return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing)
|
||||
}
|
||||
if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_SHARED_GROUPS, "id", nil); err != nil {
|
||||
if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_SHARED_GROUPS, utils.TPDistinctIds{"id"}, nil); err != nil {
|
||||
return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
|
||||
} else if ids == nil {
|
||||
return errors.New(utils.ERR_NOT_FOUND)
|
||||
|
||||
@@ -70,7 +70,7 @@ func (self *ApierV1) GetTPTimingIds(attrs AttrGetTPTimingIds, reply *[]string) e
|
||||
if missing := utils.MissingStructFields(&attrs, []string{"TPid"}); len(missing) != 0 { //Params missing
|
||||
return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing)
|
||||
}
|
||||
if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_TIMINGS, "id", nil); err != nil {
|
||||
if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_TIMINGS, utils.TPDistinctIds{"id"}, nil); err != nil {
|
||||
return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
|
||||
} else if ids == nil {
|
||||
return errors.New(utils.ERR_NOT_FOUND)
|
||||
|
||||
@@ -18,6 +18,13 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
|
||||
package engine
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
)
|
||||
|
||||
// Structs here are one to one mapping of the tables and fields
|
||||
// to be used by gorm orm
|
||||
|
||||
@@ -138,7 +145,7 @@ type TpSharedGroup struct {
|
||||
RatingSubject string
|
||||
}
|
||||
|
||||
type TpDerivedCharges struct {
|
||||
type TpDerivedCharger struct {
|
||||
Tbid int64 `gorm:"primary_key:yes"`
|
||||
Tpid string
|
||||
Loadid string
|
||||
@@ -161,6 +168,20 @@ type TpDerivedCharges struct {
|
||||
DurationField string
|
||||
}
|
||||
|
||||
func (tpdc *TpDerivedCharger) SetDerivedChargersId(id string) error {
|
||||
ids := strings.Split(id, utils.TP_ID_SEP)
|
||||
if len(ids) != 6 {
|
||||
return fmt.Errorf("Wrong TP Derived Charge Id!")
|
||||
}
|
||||
tpdc.Direction = ids[0]
|
||||
tpdc.Tenant = ids[1]
|
||||
tpdc.Category = ids[2]
|
||||
tpdc.Account = ids[3]
|
||||
tpdc.Subject = ids[4]
|
||||
tpdc.Loadid = ids[5]
|
||||
return nil
|
||||
}
|
||||
|
||||
type TpCdrStat struct {
|
||||
Tbid int64 `gorm:"primary_key:yes"`
|
||||
Tpid string
|
||||
|
||||
@@ -136,7 +136,7 @@ type LoadStorage interface {
|
||||
// Apier functions
|
||||
RemTPData(string, string, ...string) error
|
||||
GetTPIds() ([]string, error)
|
||||
GetTPTableIds(string, string, string, map[string]string) ([]string, error)
|
||||
GetTPTableIds(string, string, utils.TPDistinctIds, map[string]string) ([]string, error)
|
||||
|
||||
SetTPTiming(string, *utils.TPTiming) error
|
||||
GetTpTimings(string, string) (map[string]*utils.TPTiming, error)
|
||||
@@ -162,6 +162,9 @@ type LoadStorage interface {
|
||||
SetTPCdrStats(string, map[string][]*utils.TPCdrStat) error
|
||||
GetTpCdrStats(string, string) (map[string][]*utils.TPCdrStat, error)
|
||||
|
||||
SetTPDerivedChargers(string, map[string][]*utils.TPDerivedCharger) error
|
||||
GetTpDerivedChargers(string, string) (map[string][]*utils.TPDerivedCharger, error)
|
||||
|
||||
SetTPLCRs(string, map[string]*LCR) error
|
||||
GetTpLCRs(string, string) (map[string]*LCR, error)
|
||||
|
||||
|
||||
@@ -41,7 +41,7 @@ func NewMySQLStorage(host, port, name, user, password string) (Storage, error) {
|
||||
}
|
||||
db.DB().SetMaxIdleConns(10)
|
||||
db.DB().SetMaxOpenConns(100)
|
||||
db.LogMode(true)
|
||||
//db.LogMode(true)
|
||||
|
||||
return &MySQLStorage{&SQLStorage{Db: db.DB(), db: db}}, nil
|
||||
}
|
||||
|
||||
@@ -81,7 +81,13 @@ func (self *SQLStorage) CreateTablesFromScript(scriptPath string) error {
|
||||
// Return a list with all TPids defined in the system, even if incomplete, isolated in some table.
|
||||
func (self *SQLStorage) GetTPIds() ([]string, error) {
|
||||
rows, err := self.Db.Query(
|
||||
fmt.Sprintf("(SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s)", utils.TBL_TP_TIMINGS, utils.TBL_TP_DESTINATIONS, utils.TBL_TP_RATES, utils.TBL_TP_DESTINATION_RATES, utils.TBL_TP_RATING_PLANS, utils.TBL_TP_RATE_PROFILES))
|
||||
fmt.Sprintf("(SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s)",
|
||||
utils.TBL_TP_TIMINGS,
|
||||
utils.TBL_TP_DESTINATIONS,
|
||||
utils.TBL_TP_RATES,
|
||||
utils.TBL_TP_DESTINATION_RATES,
|
||||
utils.TBL_TP_RATING_PLANS,
|
||||
utils.TBL_TP_RATE_PROFILES))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -103,7 +109,7 @@ func (self *SQLStorage) GetTPIds() ([]string, error) {
|
||||
return ids, nil
|
||||
}
|
||||
|
||||
func (self *SQLStorage) GetTPTableIds(tpid, table, distinct string, filters map[string]string) ([]string, error) {
|
||||
func (self *SQLStorage) GetTPTableIds(tpid, table string, distinct utils.TPDistinctIds, filters map[string]string) ([]string, error) {
|
||||
qry := fmt.Sprintf("SELECT DISTINCT %s FROM %s where tpid='%s'", distinct, table, tpid)
|
||||
for key, value := range filters {
|
||||
if key != "" && value != "" {
|
||||
@@ -120,12 +126,23 @@ func (self *SQLStorage) GetTPTableIds(tpid, table, distinct string, filters map[
|
||||
i := 0
|
||||
for rows.Next() {
|
||||
i++ //Keep here a reference so we know we got at least one
|
||||
var id string
|
||||
err = rows.Scan(&id)
|
||||
|
||||
cols, err := rows.Columns() // Get the column names; remember to check err
|
||||
vals := make([]string, len(cols)) // Allocate enough values
|
||||
ints := make([]interface{}, len(cols)) // Make a slice of []interface{}
|
||||
for i := range ints {
|
||||
ints[i] = &vals[i] // Copy references into the slice
|
||||
}
|
||||
|
||||
err = rows.Scan(ints...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ids = append(ids, id)
|
||||
finalId := vals[0]
|
||||
if len(vals) > 1 {
|
||||
finalId = strings.Join(vals, utils.TP_ID_SEP)
|
||||
}
|
||||
ids = append(ids, finalId)
|
||||
}
|
||||
if i == 0 {
|
||||
return nil, nil
|
||||
@@ -158,30 +175,6 @@ func (self *SQLStorage) RemTPData(table, tpid string, args ...string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Extracts destinations from StorDB on specific tariffplan id
|
||||
/*func (self *SQLStorage) GetTPDestination(tpid, destTag string) (*Destination, error) {
|
||||
rows, err := self.Db.Query(fmt.Sprintf("SELECT prefix FROM %s WHERE tpid='%s' AND id='%s'", utils.TBL_TP_DESTINATIONS, tpid, destTag))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
d := &Destination{Id: destTag}
|
||||
i := 0
|
||||
for rows.Next() {
|
||||
i++ //Keep here a reference so we know we got at least one prefix
|
||||
var pref string
|
||||
err = rows.Scan(&pref)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
d.AddPrefix(pref)
|
||||
}
|
||||
if i == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
return d, nil
|
||||
}*/
|
||||
|
||||
func (self *SQLStorage) SetTPDestination(tpid string, dest *Destination) error {
|
||||
if len(dest.Prefixes) == 0 {
|
||||
return nil
|
||||
@@ -352,6 +345,53 @@ func (self *SQLStorage) SetTPCdrStats(tpid string, css map[string][]*utils.TPCdr
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *SQLStorage) SetTPDerivedChargers(tpid string, sgs map[string][]*utils.TPDerivedCharger) error {
|
||||
if len(sgs) == 0 {
|
||||
return nil //Nothing to set
|
||||
}
|
||||
tx := self.db.Begin()
|
||||
for dcId, dChargers := range sgs {
|
||||
// parse identifiers
|
||||
tmpDc := TpDerivedCharger{}
|
||||
if err := tmpDc.SetDerivedChargersId(dcId); err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
tx.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).
|
||||
Delete(TpDerivedCharger{})
|
||||
for _, dc := range dChargers {
|
||||
newDc := TpDerivedCharger{
|
||||
Tpid: tpid,
|
||||
RunId: dc.RunId,
|
||||
RunFilter: dc.RunFilter,
|
||||
ReqtypeField: dc.ReqtypeField,
|
||||
DirectionField: dc.DirectionField,
|
||||
TenantField: dc.TenantField,
|
||||
CategoryField: dc.CategoryField,
|
||||
AccountField: dc.AccountField,
|
||||
SubjectField: dc.SubjectField,
|
||||
DestinationField: dc.DestinationField,
|
||||
SetupTimeField: dc.SetupTimeField,
|
||||
AnswerTimeField: dc.AnswerTimeField,
|
||||
DurationField: dc.DurationField,
|
||||
}
|
||||
if err := newDc.SetDerivedChargersId(dcId); err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
tx.Save(newDc)
|
||||
}
|
||||
}
|
||||
tx.Commit()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *SQLStorage) SetTPLCRs(tpid string, lcrs map[string]*LCR) error {
|
||||
if len(lcrs) == 0 {
|
||||
return nil //Nothing to set
|
||||
@@ -1288,6 +1328,48 @@ 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) {
|
||||
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)
|
||||
}
|
||||
if err := q.Find(&tpDerivedChargers).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, tpDc := range tpDerivedChargers {
|
||||
dcs[tag] = append(dcs[tag], &utils.TPDerivedCharger{
|
||||
RunId: tpDc.RunId,
|
||||
RunFilter: tpDc.RunFilter,
|
||||
ReqtypeField: tpDc.ReqtypeField,
|
||||
DirectionField: tpDc.DirectionField,
|
||||
TenantField: tpDc.TenantField,
|
||||
CategoryField: tpDc.CategoryField,
|
||||
AccountField: tpDc.AccountField,
|
||||
SubjectField: tpDc.SubjectField,
|
||||
DestinationField: tpDc.DestinationField,
|
||||
SetupTimeField: tpDc.SetupTimeField,
|
||||
AnswerTimeField: tpDc.AnswerTimeField,
|
||||
DurationField: tpDc.DurationField,
|
||||
})
|
||||
}
|
||||
return dcs, nil
|
||||
}
|
||||
|
||||
func (self *SQLStorage) GetTpLCRs(tpid, tag string) (map[string]*LCR, error) {
|
||||
lcrs := make(map[string]*LCR)
|
||||
q := fmt.Sprintf("SELECT * FROM %s WHERE tpid='%s'", utils.TBL_TP_LCRS, tpid)
|
||||
|
||||
@@ -25,6 +25,12 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type TPDistinctIds []string
|
||||
|
||||
func (tpdi TPDistinctIds) String() string {
|
||||
return strings.Join(tpdi, ",")
|
||||
}
|
||||
|
||||
type TPDestination struct {
|
||||
TPid string // Tariff plan id
|
||||
DestinationId string // Destination id
|
||||
@@ -280,6 +286,60 @@ type TPCdrStat struct {
|
||||
ActionTriggers string
|
||||
}
|
||||
|
||||
type TPDerivedChargers struct {
|
||||
TPid string
|
||||
Loadid string
|
||||
Direction string
|
||||
Tenant string
|
||||
Category string
|
||||
Account string
|
||||
Subject string
|
||||
DerivedChargers []*TPDerivedCharger
|
||||
}
|
||||
|
||||
func (tpdc TPDerivedChargers) GetDerivedChargesId() string {
|
||||
return tpdc.Direction +
|
||||
TP_ID_SEP +
|
||||
tpdc.Tenant +
|
||||
TP_ID_SEP +
|
||||
tpdc.Category +
|
||||
TP_ID_SEP +
|
||||
tpdc.Account +
|
||||
TP_ID_SEP +
|
||||
tpdc.Subject +
|
||||
TP_ID_SEP +
|
||||
tpdc.Loadid
|
||||
}
|
||||
|
||||
func (tpdc *TPDerivedChargers) SetDerivedChargersId(id string) error {
|
||||
ids := strings.Split(id, TP_ID_SEP)
|
||||
if len(ids) != 6 {
|
||||
return fmt.Errorf("Wrong TP Derived Charge Id: %s", id)
|
||||
}
|
||||
tpdc.Direction = ids[0]
|
||||
tpdc.Tenant = ids[1]
|
||||
tpdc.Category = ids[2]
|
||||
tpdc.Account = ids[3]
|
||||
tpdc.Subject = ids[4]
|
||||
tpdc.Loadid = ids[5]
|
||||
return nil
|
||||
}
|
||||
|
||||
type TPDerivedCharger struct {
|
||||
RunId string
|
||||
RunFilter string
|
||||
ReqtypeField string
|
||||
DirectionField string
|
||||
TenantField string
|
||||
CategoryField string
|
||||
AccountField string
|
||||
SubjectField string
|
||||
DestinationField string
|
||||
SetupTimeField string
|
||||
AnswerTimeField string
|
||||
DurationField string
|
||||
}
|
||||
|
||||
type TPActionPlan struct {
|
||||
TPid string // Tariff plan id
|
||||
Id string // ActionPlan id
|
||||
|
||||
@@ -34,7 +34,7 @@ const (
|
||||
TBL_TP_ACTION_PLANS = "tp_action_plans"
|
||||
TBL_TP_ACTION_TRIGGERS = "tp_action_triggers"
|
||||
TBL_TP_ACCOUNT_ACTIONS = "tp_account_actions"
|
||||
TBL_TP_DERIVED_CHARGES = "tp_derived_charges"
|
||||
TBL_TP_DERIVED_CHARGERS = "tp_derived_chargers"
|
||||
TBL_CDRS_PRIMARY = "cdrs_primary"
|
||||
TBL_CDRS_EXTRA = "cdrs_extra"
|
||||
TBL_COST_DETAILS = "cost_details"
|
||||
@@ -158,6 +158,7 @@ const (
|
||||
CREATE_CDRS_TABLES_SQL = "create_cdrs_tables.sql"
|
||||
CREATE_TARIFFPLAN_TABLES_SQL = "create_tariffplan_tables.sql"
|
||||
TEST_SQL = "TEST_SQL"
|
||||
TP_ID_SEP = "|"
|
||||
)
|
||||
|
||||
var (
|
||||
|
||||
Reference in New Issue
Block a user