From fd045e8b38796ece555968395e88c486edbb3731 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Sun, 24 Aug 2014 13:56:13 +0300 Subject: [PATCH] add derived chargers tp and apier methods --- apier/tpaccountactions.go | 2 +- apier/tpactions.go | 2 +- apier/tpactiontimings.go | 2 +- apier/tpactiontriggers.go | 2 +- apier/tpcdrstats.go | 2 +- apier/tpderivedcharges.go | 50 ++++++++----- apier/tpdestinationrates.go | 2 +- apier/tpdestinations.go | 2 +- apier/tprates.go | 2 +- apier/tpratingplans.go | 2 +- apier/tpratingprofiles.go | 2 +- apier/tpsharedgroups.go | 2 +- apier/tptimings.go | 2 +- engine/models.go | 23 +++++- engine/storage_interface.go | 5 +- engine/storage_mysql.go | 2 +- engine/storage_sql.go | 140 ++++++++++++++++++++++++++++-------- utils/apitpdata.go | 60 ++++++++++++++++ utils/consts.go | 3 +- 19 files changed, 244 insertions(+), 63 deletions(-) diff --git a/apier/tpaccountactions.go b/apier/tpaccountactions.go index 4003197f8..106630ca3 100644 --- a/apier/tpaccountactions.go +++ b/apier/tpaccountactions.go @@ -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) diff --git a/apier/tpactions.go b/apier/tpactions.go index 210e534b0..0737675a4 100644 --- a/apier/tpactions.go +++ b/apier/tpactions.go @@ -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) diff --git a/apier/tpactiontimings.go b/apier/tpactiontimings.go index b711df170..a55134c25 100644 --- a/apier/tpactiontimings.go +++ b/apier/tpactiontimings.go @@ -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) diff --git a/apier/tpactiontriggers.go b/apier/tpactiontriggers.go index b1a92ec65..9d1db099f 100644 --- a/apier/tpactiontriggers.go +++ b/apier/tpactiontriggers.go @@ -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) diff --git a/apier/tpcdrstats.go b/apier/tpcdrstats.go index e4990eae4..82284362c 100644 --- a/apier/tpcdrstats.go +++ b/apier/tpcdrstats.go @@ -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) diff --git a/apier/tpderivedcharges.go b/apier/tpderivedcharges.go index 57b40a09f..525abda09 100644 --- a/apier/tpderivedcharges.go +++ b/apier/tpderivedcharges.go @@ -18,13 +18,19 @@ along with this program. If not, see 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 } -*/ diff --git a/apier/tpdestinationrates.go b/apier/tpdestinationrates.go index d920481b2..3147f69bf 100644 --- a/apier/tpdestinationrates.go +++ b/apier/tpdestinationrates.go @@ -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) diff --git a/apier/tpdestinations.go b/apier/tpdestinations.go index 7e9d22b97..af4129bdb 100644 --- a/apier/tpdestinations.go +++ b/apier/tpdestinations.go @@ -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) diff --git a/apier/tprates.go b/apier/tprates.go index d4d080109..83c8f44a6 100644 --- a/apier/tprates.go +++ b/apier/tprates.go @@ -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) diff --git a/apier/tpratingplans.go b/apier/tpratingplans.go index 1a30dc346..8cb5ff03f 100644 --- a/apier/tpratingplans.go +++ b/apier/tpratingplans.go @@ -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) diff --git a/apier/tpratingprofiles.go b/apier/tpratingprofiles.go index 120cd32f2..3d156fb3c 100644 --- a/apier/tpratingprofiles.go +++ b/apier/tpratingprofiles.go @@ -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, diff --git a/apier/tpsharedgroups.go b/apier/tpsharedgroups.go index 84b3f11ae..23b788f2c 100644 --- a/apier/tpsharedgroups.go +++ b/apier/tpsharedgroups.go @@ -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) diff --git a/apier/tptimings.go b/apier/tptimings.go index 2b79dcf06..26fd12d77 100644 --- a/apier/tptimings.go +++ b/apier/tptimings.go @@ -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) diff --git a/engine/models.go b/engine/models.go index 8b23111a1..672f3fda4 100644 --- a/engine/models.go +++ b/engine/models.go @@ -18,6 +18,13 @@ along with this program. If not, see 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 diff --git a/engine/storage_interface.go b/engine/storage_interface.go index 62373e53f..3af698efb 100644 --- a/engine/storage_interface.go +++ b/engine/storage_interface.go @@ -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) diff --git a/engine/storage_mysql.go b/engine/storage_mysql.go index 3d3d29a3d..9463b71a1 100644 --- a/engine/storage_mysql.go +++ b/engine/storage_mysql.go @@ -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 } diff --git a/engine/storage_sql.go b/engine/storage_sql.go index fcc390109..1d29d603b 100644 --- a/engine/storage_sql.go +++ b/engine/storage_sql.go @@ -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) diff --git a/utils/apitpdata.go b/utils/apitpdata.go index f42e02d5c..f8e200307 100644 --- a/utils/apitpdata.go +++ b/utils/apitpdata.go @@ -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 diff --git a/utils/consts.go b/utils/consts.go index 20f4b76be..18c16eba9 100644 --- a/utils/consts.go +++ b/utils/consts.go @@ -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 (