From e39187eab95d9b1db36e4f0c1df630b195184f52 Mon Sep 17 00:00:00 2001 From: DanB Date: Mon, 28 Apr 2014 12:06:42 +0200 Subject: [PATCH] TOR -> Category in relation with DerivedCharging --- apier/derivedcharging.go | 26 ++++++++++++++++++++++---- engine/handler_derivedcharging.go | 4 ++-- engine/handler_derivedcharging_test.go | 2 +- engine/loader_csv_test.go | 8 ++++---- mediator/mediator.go | 4 ++-- sessionmanager/fssessionmanager.go | 6 +++--- utils/apitpdata.go | 2 +- utils/derivedchargers.go | 4 ++-- utils/derivedchargers_test.go | 2 +- 9 files changed, 38 insertions(+), 20 deletions(-) diff --git a/apier/derivedcharging.go b/apier/derivedcharging.go index 3756823cd..138eb5d50 100644 --- a/apier/derivedcharging.go +++ b/apier/derivedcharging.go @@ -38,17 +38,35 @@ func (self *ApierV1) DerivedChargers(attrs utils.AttrDerivedChargers, reply *uti } type AttrSetDerivedChargers struct { - Tenant, Tor, Direction, Account, Subject string - DerivedChargers utils.DerivedChargers + Direction, Tenant, Category, Account, Subject string + DerivedChargers utils.DerivedChargers } func (self *ApierV1) SetDerivedChargers(attrs AttrSetDerivedChargers, reply *utils.DerivedChargers) (err error) { - if missing := utils.MissingStructFields(&attrs, []string{"Tenant", "Direction", "Account", "Subject", "DerivedChargers"}); len(missing) != 0 { + if missing := utils.MissingStructFields(&attrs, []string{"Tenant", "Category", "Direction", "Account", "Subject", "DerivedChargers"}); len(missing) != 0 { return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing) } - if err := self.AccountDb.SetDerivedChargers(utils.DerivedChargersKey(attrs.Tenant, attrs.Tor, attrs.Direction, attrs.Account, attrs.Subject), + if err := self.AccountDb.SetDerivedChargers(utils.DerivedChargersKey(attrs.Direction, attrs.Tenant, attrs.Category, attrs.Account, attrs.Subject), attrs.DerivedChargers); err != nil { return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error()) } return nil } + +/* +type AttrRemDerivedChargers struct { + Tenant, Category, Direction, Account, Subject string +} + +func (self *ApierV1) RemDerivedChargers(attrs AttrRemDerivedChargers, reply *string) error { + if missing := utils.MissingStructFields(&attrs, ]string{"Tenant", "Category", "Direction", "Account", "Subject"}); len(missing) != 0 { //Params missing + return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing) + } + if err := self.StorDb.RemTPData(utils.DerivedChargersKey(attrs.Tenant, attrs.Category, attrs.Direction, attrs.Account, attrs.Subject), nil); err != nil { + return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error()) + } else { + *reply = "OK" + } + return nil +} +*/ diff --git a/engine/handler_derivedcharging.go b/engine/handler_derivedcharging.go index dcd30c8e9..6178d2d7d 100644 --- a/engine/handler_derivedcharging.go +++ b/engine/handler_derivedcharging.go @@ -27,8 +27,8 @@ import ( func HandleGetDerivedChargers(acntStorage AccountingStorage, cfg *config.CGRConfig, attrs utils.AttrDerivedChargers) (utils.DerivedChargers, error) { var dcs utils.DerivedChargers var err error - strictKey := utils.DerivedChargersKey(attrs.Tenant, attrs.Tor, attrs.Direction, attrs.Account, attrs.Subject) - anySubjKey := utils.DerivedChargersKey(attrs.Tenant, attrs.Tor, attrs.Direction, attrs.Account, utils.ANY) + strictKey := utils.DerivedChargersKey(attrs.Direction, attrs.Tenant, attrs.Category, attrs.Account, attrs.Subject) + anySubjKey := utils.DerivedChargersKey(attrs.Direction, attrs.Tenant, attrs.Category, attrs.Account, utils.ANY) for _, dcKey := range []string{strictKey, anySubjKey} { if dcsDb, err := acntStorage.GetDerivedChargers(dcKey, false); err != nil && err.Error() != utils.ERR_NOT_FOUND { return nil, err diff --git a/engine/handler_derivedcharging_test.go b/engine/handler_derivedcharging_test.go index 10f0f20cb..df7f4c355 100644 --- a/engine/handler_derivedcharging_test.go +++ b/engine/handler_derivedcharging_test.go @@ -59,7 +59,7 @@ func TestHandleGetConfiguredDC(t *testing.T) { // Receive composed derived chargers func TestHandleGetStoredDC(t *testing.T) { - keyCharger1 := utils.DerivedChargersKey("cgrates.org", "call", "*out", "rif", "rif") + keyCharger1 := utils.DerivedChargersKey("*out", "cgrates.org", "call", "rif", "rif") charger1 := utils.DerivedChargers{ &utils.DerivedCharger{RunId: "extra1", ReqTypeField: "^prepaid", DirectionField: "*default", TenantField: "*default", TorField: "*default", AccountField: "rif", SubjectField: "rif", DestinationField: "*default", SetupTimeField: "*default", AnswerTimeField: "*default", DurationField: "*default"}, diff --git a/engine/loader_csv_test.go b/engine/loader_csv_test.go index 987806287..99ebcabe8 100644 --- a/engine/loader_csv_test.go +++ b/engine/loader_csv_test.go @@ -168,10 +168,10 @@ vdf,emptyY,*out,TOPUP_EMPTY_AT, ` derivedCharges = ` -#Tenant,Tor,Direction,Account,Subject,RunId,ReqTypeField,DirectionField,TenantField,TorField,AccountField,SubjectField,DestinationField,SetupTimeField,AnswerTimeField,DurationField -cgrates.org,call,*out,dan,dan,extra1,^prepaid,,,,rif,rif,,,, -cgrates.org,call,*out,dan,dan,extra2,,,,,ivo,ivo,,,, -cgrates.org,call,*out,dan,*any,extra1,,,,,rif2,rif2,,,, +#Direction,Tenant,Category,Account,Subject,RunId,ReqTypeField,DirectionField,TenantField,TorField,AccountField,SubjectField,DestinationField,SetupTimeField,AnswerTimeField,DurationField +*out,cgrates.org,call,dan,dan,extra1,^prepaid,,,,rif,rif,,,, +*out,cgrates.org,call,dan,dan,extra2,,,,,ivo,ivo,,,, +*out,cgrates.org,call,dan,*any,extra1,,,,,rif2,rif2,,,, ` ) diff --git a/mediator/mediator.go b/mediator/mediator.go index ff60e9dd8..cf415e632 100644 --- a/mediator/mediator.go +++ b/mediator/mediator.go @@ -114,8 +114,8 @@ func (self *Mediator) RateCdr(dbcdr utils.RawCDR) error { if err != nil { return err } - cdrs := []*utils.StoredCdr{rtCdr} // Start with initial dbcdr, will add here all to be mediated - attrsDC := utils.AttrDerivedChargers{Tenant: rtCdr.Tenant, Tor: rtCdr.TOR, Direction: rtCdr.Direction, + cdrs := []*utils.StoredCdr{rtCdr} // Start with initial dbcdr, will add here all to be mediated + attrsDC := utils.AttrDerivedChargers{Direction: rtCdr.Direction, Tenant: rtCdr.Tenant, Category: rtCdr.TOR, Account: rtCdr.Account, Subject: rtCdr.Subject} var dcs utils.DerivedChargers if err := self.connector.GetDerivedChargers(attrsDC, &dcs); err != nil { diff --git a/sessionmanager/fssessionmanager.go b/sessionmanager/fssessionmanager.go index 858739e6c..aea91ab48 100644 --- a/sessionmanager/fssessionmanager.go +++ b/sessionmanager/fssessionmanager.go @@ -151,7 +151,7 @@ func (sm *FSSessionManager) OnHeartBeat(ev Event) { func (sm *FSSessionManager) OnChannelPark(ev Event) { var maxCallDuration time.Duration // This will be the maximum duration this channel will be allowed to last var durInitialized bool - attrsDC := utils.AttrDerivedChargers{Tenant: ev.GetTenant(utils.META_DEFAULT), Tor: ev.GetTOR(utils.META_DEFAULT), Direction: ev.GetDirection(utils.META_DEFAULT), + attrsDC := utils.AttrDerivedChargers{Tenant: ev.GetTenant(utils.META_DEFAULT), Category: ev.GetTOR(utils.META_DEFAULT), Direction: ev.GetDirection(utils.META_DEFAULT), Account: ev.GetAccount(utils.META_DEFAULT), Subject: ev.GetSubject(utils.META_DEFAULT)} var dcs utils.DerivedChargers if err := sm.connector.GetDerivedChargers(attrsDC, &dcs); err != nil { @@ -219,7 +219,7 @@ func (sm *FSSessionManager) OnChannelAnswer(ev Event) { if _, err := fsock.FS.SendApiCmd(fmt.Sprintf("uuid_setvar %s cgr_reqtype %s\n\n", ev.GetUUID(), ev.GetReqType(""))); err != nil { engine.Logger.Err(fmt.Sprintf("Error on attempting to overwrite cgr_type in chan variables: %v", err)) } - attrsDC := utils.AttrDerivedChargers{Tenant: ev.GetTenant(utils.META_DEFAULT), Tor: ev.GetTOR(utils.META_DEFAULT), + attrsDC := utils.AttrDerivedChargers{Tenant: ev.GetTenant(utils.META_DEFAULT), Category: ev.GetTOR(utils.META_DEFAULT), Direction: ev.GetDirection(utils.META_DEFAULT), Account: ev.GetAccount(utils.META_DEFAULT), Subject: ev.GetSubject(utils.META_DEFAULT)} var dcs utils.DerivedChargers if err := sm.connector.GetDerivedChargers(attrsDC, &dcs); err != nil { @@ -242,7 +242,7 @@ func (sm *FSSessionManager) OnChannelHangupComplete(ev Event) { sm.RemoveSession(s.uuid) // Unreference it early so we avoid concurrency } defer s.Close(ev) // Stop loop and save the costs deducted so far to database - attrsDC := utils.AttrDerivedChargers{Tenant: ev.GetTenant(utils.META_DEFAULT), Tor: ev.GetTOR(utils.META_DEFAULT), Direction: ev.GetDirection(utils.META_DEFAULT), + attrsDC := utils.AttrDerivedChargers{Tenant: ev.GetTenant(utils.META_DEFAULT), Category: ev.GetTOR(utils.META_DEFAULT), Direction: ev.GetDirection(utils.META_DEFAULT), Account: ev.GetAccount(utils.META_DEFAULT), Subject: ev.GetSubject(utils.META_DEFAULT)} var dcs utils.DerivedChargers if err := sm.connector.GetDerivedChargers(attrsDC, &dcs); err != nil { diff --git a/utils/apitpdata.go b/utils/apitpdata.go index 76bb55342..1b182ab43 100644 --- a/utils/apitpdata.go +++ b/utils/apitpdata.go @@ -379,5 +379,5 @@ type AttrGetDestination struct { } type AttrDerivedChargers struct { - Tenant, Tor, Direction, Account, Subject string + Direction, Tenant, Category, Account, Subject string } diff --git a/utils/derivedchargers.go b/utils/derivedchargers.go index 9d3373643..8d6393f32 100644 --- a/utils/derivedchargers.go +++ b/utils/derivedchargers.go @@ -116,8 +116,8 @@ type DerivedCharger struct { rsrDurationField *RSRField } -func DerivedChargersKey(tenant, tor, direction, account, subject string) string { - return ConcatenatedKey(tenant, tor, direction, account, subject) +func DerivedChargersKey(direction, tenant, category, account, subject string) string { + return ConcatenatedKey(direction, tenant, category, account, subject) } type DerivedChargers []*DerivedCharger diff --git a/utils/derivedchargers_test.go b/utils/derivedchargers_test.go index 52c05e608..3ce9e35f6 100644 --- a/utils/derivedchargers_test.go +++ b/utils/derivedchargers_test.go @@ -105,7 +105,7 @@ func TestNewDerivedCharger(t *testing.T) { } func TestDerivedChargersKey(t *testing.T) { - if dcKey := DerivedChargersKey("cgrates.org", "call", "*out", "dan", "dan"); dcKey != "cgrates.org:call:*out:dan:dan" { + if dcKey := DerivedChargersKey("*out", "cgrates.org", "call", "dan", "dan"); dcKey != "*out:cgrates.org:call:dan:dan" { t.Error("Unexpected derived chargers key: ", dcKey) } }