added db loader for shared balances

This commit is contained in:
Radu Ioan Fericean
2014-02-12 16:23:03 +02:00
parent c447fa49dc
commit 6ad5794bfc
6 changed files with 119 additions and 2 deletions

View File

@@ -111,6 +111,23 @@ CREATE TABLE `tp_rating_profiles` (
UNIQUE KEY `tpid_tag_tenant_tor_dir_subj_atime` (`tpid`,`loadid`, `tenant`,`tor`,`direction`,`subject`,`activation_time`)
);
--
-- Table structure for table `tp_shared_groups`
--
DROP TABLE IF EXISTS `tp_shared_groups`;
CREATE TABLE `tp_shared_groups` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`tpid` varchar(64) NOT NULL,
`tag` varchar(64) NOT NULL,
`account` varchar(24) NOT NULL,
`strategy` varchar(24) NOT NULL,
`rate_subject` varchar(24) NOT NULL,
PRIMARY KEY (`id`),
KEY `tpid` (`tpid`),
UNIQUE KEY `unique_shared_group` (`tpid`,`tag`,`account`,`strategy`,`rate_subject`)
);
--
-- Table structure for table `tp_actions`
--

View File

@@ -91,4 +91,11 @@ CREATE TABLE action IF NOT EXISTS(
direction TEXT,
units FLOAT8,
weight FLOAT8
);
);
CREATE TABLE sharedgroup IF NOT EXISTS(
id SERIAL PRIMARY KEY,
account TEXT,
strategy TEXT,
ratesubject TEXT,
);

View File

@@ -389,7 +389,8 @@ func (dbr *DbReader) LoadRatingProfileFiltered(qriedRpf *utils.TPRatingProfile)
}
func (dbr *DbReader) LoadSharedGroups() (err error) {
return nil
dbr.sharedGroups, err = dbr.storDb.GetTpSharedGroups(dbr.tpid, "")
return err
}
func (dbr *DbReader) LoadActions() (err error) {

View File

@@ -142,6 +142,10 @@ type LoadStorage interface {
GetTpRatingProfiles(*utils.TPRatingProfile) (map[string]*utils.TPRatingProfile, error)
GetTPRatingProfileIds(*utils.AttrTPRatingProfileIds) ([]string, error)
SetTPSharedGroups(string, map[string]*SharedGroup) error
GetTpSharedGroups(string, string) (map[string]*SharedGroup, error)
GetTPSharedGroupIds(string) ([]string, error)
SetTPActions(string, map[string][]*utils.TPAction) error
GetTpActions(string, string) (map[string][]*utils.TPAction, error)
GetTPActionIds(string) ([]string, error)

View File

@@ -403,6 +403,52 @@ func (self *SQLStorage) GetTPRatingProfileIds(filters *utils.AttrTPRatingProfile
return ids, nil
}
func (self *SQLStorage) SetTPSharedGroups(tpid string, sgs map[string]*SharedGroup) error {
if len(sgs) == 0 {
return nil //Nothing to set
}
var buffer bytes.Buffer
buffer.WriteString(fmt.Sprintf("INSERT INTO %s (tpid,tag,account,strategy,rate_subject) VALUES ", utils.TBL_TP_SHARED_GROUPS))
i := 0
for sgId, sg := range sgs {
for account, params := range sg.AccountParameters {
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')",
tpid, sgId, account, params.Strategy, params.RateSubject))
i++
}
}
buffer.WriteString(" ON DUPLICATE KEY UPDATE account=values(account),strategy=values(strategy),rate_subject=values(rate_subject)")
if _, err := self.Db.Exec(buffer.String()); err != nil {
return err
}
return nil
}
func (self *SQLStorage) GetTPSharedGroupIds(tpid string) ([]string, error) {
rows, err := self.Db.Query(fmt.Sprintf("SELECT DISTINCT tag FROM %s where tpid='%s'", utils.TBL_TP_SHARED_GROUPS, tpid))
if err != nil {
return nil, err
}
defer rows.Close()
ids := []string{}
i := 0
for rows.Next() {
i++ //Keep here a reference so we know we got at least one
var id string
if err = rows.Scan(&id); err != nil {
return nil, err
}
ids = append(ids, id)
}
if i == 0 {
return nil, nil
}
return ids, nil
}
func (self *SQLStorage) SetTPActions(tpid string, acts map[string][]*utils.TPAction) error {
if len(acts) == 0 {
return nil //Nothing to set
@@ -1054,6 +1100,47 @@ func (self *SQLStorage) GetTpRatingProfiles(qryRpf *utils.TPRatingProfile) (map[
return rpfs, nil
}
func (self *SQLStorage) GetTpSharedGroups(tpid, tag string) (map[string]*SharedGroup, error) {
sgs := make(map[string]*SharedGroup)
q := fmt.Sprintf("SELECT * FROM %s WHERE tpid='%s'", utils.TBL_TP_SHARED_GROUPS, tpid)
if tag != "" {
q += fmt.Sprintf(" AND tag='%s'", tag)
}
rows, err := self.Db.Query(q)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var id int
var tpid, tag, account, strategy, rateSubject string
if err := rows.Scan(&id, &tpid, &tag, &account, &strategy, &rateSubject); err != nil {
return nil, err
}
sg, found := sgs[tag]
if found {
sg.AccountParameters[account] = &SharingParameters{
Strategy: strategy,
RateSubject: rateSubject,
}
} else {
sg = &SharedGroup{
Id: tag,
AccountParameters: map[string]*SharingParameters{
account: &SharingParameters{
Strategy: strategy,
RateSubject: rateSubject,
},
},
}
}
csvr.sharedGroups[tag] = sg
}
return sgs, nil
}
func (self *SQLStorage) GetTpActions(tpid, tag string) (map[string][]*utils.TPAction, error) {
as := make(map[string][]*utils.TPAction)
q := fmt.Sprintf("SELECT * FROM %s WHERE tpid='%s'", utils.TBL_TP_ACTIONS, tpid)

View File

@@ -26,6 +26,7 @@ const (
TBL_TP_DESTINATION_RATES = "tp_destination_rates"
TBL_TP_RATING_PLANS = "tp_rating_plans"
TBL_TP_RATE_PROFILES = "tp_rating_profiles"
TBL_TP_SHARED_GROUPS = "tp_shared_groups"
TBL_TP_ACTIONS = "tp_actions"
TBL_TP_ACTION_PLANS = "tp_action_plans"
TBL_TP_ACTION_TRIGGERS = "tp_action_triggers"