Load DerivedChargers

This commit is contained in:
DanB
2014-09-09 19:11:25 +02:00
parent 886ca68eb8
commit f71aa8483c
9 changed files with 61 additions and 34 deletions

View File

@@ -40,6 +40,7 @@ var cdrstRpc *rpc.Client
func init() {
cdrstCfgPath = path.Join(*dataDir, "conf", "samples", "cdrstatsv1_local_test.cfg")
cdrstCfg, _ = config.NewCGRConfigFromFile(&cdrstCfgPath)
//fmt.Printf("CdrstCfg: %+v\n", cdrstCfg)
}
func TestCDRStatsLclInitDataDb(t *testing.T) {

View File

@@ -68,14 +68,7 @@ func (self *ApierV1) GetTPDerivedChargers(attrs AttrGetTPDerivedChargers, reply
} else if len(sgs) == 0 {
return errors.New(utils.ERR_NOT_FOUND)
} else {
tpdc := utils.TPDerivedChargers{
TPid: attrs.TPid,
DerivedChargers: sgs[attrs.DerivedChargersId],
}
if err := tpdc.SetDerivedChargersId(attrs.DerivedChargersId); err != nil {
return err
}
*reply = tpdc
*reply = *sgs[attrs.DerivedChargersId]
}
return nil
}

View File

@@ -255,7 +255,7 @@ CREATE TABLE tp_derived_chargers (
`account` varchar(24) NOT NULL,
`subject` varchar(64) NOT NULL,
`run_id` varchar(24) NOT NULL,
`run_filters` varchar(24) NOT NULL,
`run_filters` varchar(256) NOT NULL,
`req_type_field` varchar(24) NOT NULL,
`direction_field` varchar(24) NOT NULL,
`tenant_field` varchar(24) NOT NULL,

View File

@@ -825,6 +825,30 @@ func (dbr *DbReader) LoadAccountActionsFiltered(qriedAA *utils.TPAccountActions)
}
func (dbr *DbReader) LoadDerivedChargers() (err error) {
tpDcses, err := dbr.storDb.GetTpDerivedChargers(&utils.TPDerivedChargers{TPid: dbr.tpid})
if err != nil {
return err
}
allDcs := make(map[string]utils.DerivedChargers) // We load in map first so we can pre-process data for errors
for _, tpDcs := range tpDcses {
tag := tpDcs.GetDerivedChargersKey()
if _, hasIt := allDcs[tag]; !hasIt {
allDcs[tag] = make(utils.DerivedChargers, 0)
}
for _, tpDc := range tpDcs.DerivedChargers {
if dc, err := utils.NewDerivedCharger(tpDc.RunId, tpDc.RunFilters, tpDc.ReqTypeField, tpDc.DirectionField, tpDc.TenantField, tpDc.CategoryField,
tpDc.AccountField, tpDc.SubjectField, tpDc.DestinationField, tpDc.SetupTimeField, tpDc.AnswerTimeField, tpDc.UsageField); err != nil {
return err
} else {
allDcs[tag] = append(allDcs[tag], dc)
}
}
}
for dcsKey, dcs := range allDcs {
if err := dbr.accountDb.SetDerivedChargers(dcsKey, dcs); err != nil {
return err
}
}
return nil // Placeholder for now
}

View File

@@ -109,7 +109,7 @@ func TestCreateStorTpTables(t *testing.T) {
}
}
// Loads data from csv files in tp scenarion to ratingDbCsv
// Loads data from csv files in tp scenario to ratingDbCsv
func TestLoadFromCSV(t *testing.T) {
if !*testLocal {
return
@@ -228,6 +228,9 @@ func TestLoadFromStorDb(t *testing.T) {
if err := loader.LoadAccountActions(); err != nil {
t.Error("Failed loading account actions: ", err.Error())
}
if err := loader.LoadDerivedChargers(); err != nil {
t.Error("Failed loading derived chargers: ", err.Error())
}
if err := loader.WriteToDatabase(true, false); err != nil {
t.Error("Could not write data into ratingDb: ", err.Error())
}

View File

@@ -19,7 +19,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package engine
import (
"fmt"
"reflect"
"testing"
@@ -40,9 +39,6 @@ func TestResponderGetDerivedChargers(t *testing.T) {
if err := r.GetDerivedChargers(attrs, &dcs); err != nil {
t.Error("Unexpected error", err.Error())
} else if !reflect.DeepEqual(dcs, cfgedDC) {
for _, dc := range dcs {
fmt.Printf("DerivedCharger: %+v\n", dc)
}
t.Errorf("Expecting: %v, received: %v ", cfgedDC, dcs)
}
}

View File

@@ -163,7 +163,7 @@ type LoadStorage interface {
GetTpCdrStats(string, string) (map[string][]*utils.TPCdrStat, error)
SetTPDerivedChargers(string, map[string][]*utils.TPDerivedCharger) error
GetTpDerivedChargers(*utils.TPDerivedChargers) (map[string][]*utils.TPDerivedCharger, error)
GetTpDerivedChargers(*utils.TPDerivedChargers) (map[string]*utils.TPDerivedChargers, error)
SetTPLCRs(string, map[string]*LCR) error
GetTpLCRs(string, string) (map[string]*LCR, error)

View File

@@ -1365,9 +1365,8 @@ func (self *SQLStorage) GetTpCdrStats(tpid, tag string) (map[string][]*utils.TPC
return css, nil
}
func (self *SQLStorage) GetTpDerivedChargers(dc *utils.TPDerivedChargers) (map[string][]*utils.TPDerivedCharger, error) {
dcs := make(map[string][]*utils.TPDerivedCharger)
func (self *SQLStorage) GetTpDerivedChargers(dc *utils.TPDerivedChargers) (map[string]*utils.TPDerivedChargers, error) {
dcs := make(map[string]*utils.TPDerivedChargers)
var tpDerivedChargers []TpDerivedCharger
q := self.db.Where("tpid = ?", dc.TPid)
if len(dc.Direction) != 0 {
@@ -1391,21 +1390,26 @@ func (self *SQLStorage) GetTpDerivedChargers(dc *utils.TPDerivedChargers) (map[s
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,
RunFilters: tpDc.RunFilters,
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,
UsageField: tpDc.UsageField,
for _, tpDcMdl := range tpDerivedChargers {
tpDc := &utils.TPDerivedChargers{TPid: tpDcMdl.Tpid, Loadid: tpDcMdl.Loadid, Direction: tpDcMdl.Direction, Tenant: tpDcMdl.Tenant, Category: tpDcMdl.Category,
Account: tpDcMdl.Account, Subject: tpDcMdl.Subject}
tag := tpDc.GetDerivedChargesId()
if _, hasIt := dcs[tag]; !hasIt {
dcs[tag] = tpDc
}
dcs[tag].DerivedChargers = append(dcs[tag].DerivedChargers, &utils.TPDerivedCharger{
RunId: tpDcMdl.RunId,
RunFilters: tpDcMdl.RunFilters,
ReqTypeField: tpDcMdl.ReqTypeField,
DirectionField: tpDcMdl.DirectionField,
TenantField: tpDcMdl.TenantField,
CategoryField: tpDcMdl.CategoryField,
AccountField: tpDcMdl.AccountField,
SubjectField: tpDcMdl.SubjectField,
DestinationField: tpDcMdl.DestinationField,
SetupTimeField: tpDcMdl.SetupTimeField,
AnswerTimeField: tpDcMdl.AnswerTimeField,
UsageField: tpDcMdl.UsageField,
})
}
return dcs, nil

View File

@@ -331,7 +331,13 @@ type TPDerivedChargers struct {
DerivedChargers []*TPDerivedCharger
}
func (tpdc TPDerivedChargers) GetDerivedChargesId() string {
// Key used in dataDb to identify DerivedChargers set
func (tpdc *TPDerivedChargers) GetDerivedChargersKey() string {
return DerivedChargersKey(tpdc.Direction, tpdc.Tenant, tpdc.Category, tpdc.Account, tpdc.Subject)
}
func (tpdc *TPDerivedChargers) GetDerivedChargesId() string {
return tpdc.Loadid +
CONCATENATED_KEY_SEP +
tpdc.Direction +