From 26597423101c3ed8188af5bd355c2310f134decb Mon Sep 17 00:00:00 2001 From: DanB Date: Wed, 1 Jul 2015 13:47:18 +0200 Subject: [PATCH] ApierV1.DebitUsage method, rename ApierV1.GetMaxSessionTime->ApierV1.GetMaxUsage --- apier/v1/callsetup.go | 45 +++++++++---------- .../opensips/etc/opensips/opensips.cfg | 2 +- engine/storedcdr.go | 33 ++++++++++++-- engine/storedcdr_test.go | 19 +++++++- 4 files changed, 69 insertions(+), 30 deletions(-) diff --git a/apier/v1/callsetup.go b/apier/v1/callsetup.go index 62e7a9115..0b548da60 100644 --- a/apier/v1/callsetup.go +++ b/apier/v1/callsetup.go @@ -26,36 +26,33 @@ import ( "github.com/cgrates/cgrates/utils" ) -// Returns MaxSessionTime in seconds, -1 for no limit -func (self *ApierV1) GetMaxSessionTime(auth engine.MaxUsageReq, maxSessionTime *float64) error { - if auth.TOR == "" { - auth.TOR = utils.VOICE +// Returns MaxUsage (for calls in seconds), -1 for no limit +func (self *ApierV1) GetMaxUsage(usageRecord engine.UsageRecord, maxUsage *float64) error { + if usageRecord.TOR == "" { + usageRecord.TOR = utils.VOICE } - if auth.ReqType == "" { - auth.ReqType = self.Config.DefaultReqType + if usageRecord.ReqType == "" { + usageRecord.ReqType = self.Config.DefaultReqType } - if auth.Direction == "" { - auth.Direction = utils.OUT + if usageRecord.Direction == "" { + usageRecord.Direction = utils.OUT } - if auth.Tenant == "" { - auth.Tenant = self.Config.DefaultTenant + if usageRecord.Tenant == "" { + usageRecord.Tenant = self.Config.DefaultTenant } - if auth.Category == "" { - auth.Category = self.Config.DefaultCategory + if usageRecord.Category == "" { + usageRecord.Category = self.Config.DefaultCategory } - if auth.Subject == "" { - auth.Subject = auth.Account + if usageRecord.Subject == "" { + usageRecord.Subject = usageRecord.Account } - if auth.Subject == "" { - auth.Subject = auth.Account + if usageRecord.SetupTime == "" { + usageRecord.SetupTime = utils.META_NOW } - if auth.SetupTime == "" { - auth.SetupTime = utils.META_NOW + if usageRecord.Usage == "" { + usageRecord.Usage = strconv.FormatFloat(self.Config.MaxCallDuration.Seconds(), 'f', -1, 64) } - if auth.Usage == "" { - auth.Usage = strconv.FormatFloat(self.Config.MaxCallDuration.Seconds(), 'f', -1, 64) - } - storedCdr, err := auth.AsStoredCdr() + storedCdr, err := usageRecord.AsStoredCdr() if err != nil { return utils.NewErrServerError(err) } @@ -64,9 +61,9 @@ func (self *ApierV1) GetMaxSessionTime(auth engine.MaxUsageReq, maxSessionTime * return err } if maxDur == -1.0 { - *maxSessionTime = -1.0 + *maxUsage = -1.0 return nil } - *maxSessionTime = time.Duration(maxDur).Seconds() + *maxUsage = time.Duration(maxDur).Seconds() return nil } diff --git a/data/tutorials/osips_async/opensips/etc/opensips/opensips.cfg b/data/tutorials/osips_async/opensips/etc/opensips/opensips.cfg index f250bd0a9..2a52b73dc 100644 --- a/data/tutorials/osips_async/opensips/etc/opensips/opensips.cfg +++ b/data/tutorials/osips_async/opensips/etc/opensips/opensips.cfg @@ -133,7 +133,7 @@ route[CGR_AUTH_REQ] { # Code to produce the json $json(cgr_auth) := "{}"; $json(cgr_auth/id) = "1"; - $json(cgr_auth/method) = "ApierV1.GetMaxSessionTime"; + $json(cgr_auth/method) = "ApierV1.GetMaxUsage"; $json(cgr_auth/params) := "[{}]"; $json(cgr_auth/params[0]/ReqType) = $avp(cgr_reqtype); $json(cgr_auth/params[0]/Account) = $avp(cgr_account); diff --git a/engine/storedcdr.go b/engine/storedcdr.go index d309a78d7..3f112fdbd 100644 --- a/engine/storedcdr.go +++ b/engine/storedcdr.go @@ -621,8 +621,8 @@ type ExternalCdr struct { Rated bool // Mark the CDR as rated so we do not process it during mediation } -// Used when authorizing requests from outside, eg ApierV1.GetMaxSessionTime -type MaxUsageReq struct { +// Used when authorizing requests from outside, eg ApierV1.GetMaxUsage +type UsageRecord struct { TOR string ReqType string Direction string @@ -636,7 +636,7 @@ type MaxUsageReq struct { Usage string } -func (self *MaxUsageReq) AsStoredCdr() (*StoredCdr, error) { +func (self *UsageRecord) AsStoredCdr() (*StoredCdr, error) { var err error storedCdr := &StoredCdr{TOR: self.TOR, ReqType: self.ReqType, Direction: self.Direction, Tenant: self.Tenant, Category: self.Category, Account: self.Account, Subject: self.Subject, Destination: self.Destination} @@ -651,3 +651,30 @@ func (self *MaxUsageReq) AsStoredCdr() (*StoredCdr, error) { } return storedCdr, nil } + +func (self *UsageRecord) AsCallDescriptor() (*CallDescriptor, error) { + var err error + timeStr := self.AnswerTime + if len(timeStr) == 0 { // In case of auth, answer time will not be defined, so take it out of setup one + timeStr = self.SetupTime + } + startTime, err := utils.ParseTimeDetectLayout(timeStr) + if err != nil { + return nil, err + } + usage, err := utils.ParseDurationWithSecs(self.Usage) + if err != nil { + return nil, err + } + return &CallDescriptor{ + TOR: self.TOR, + Direction: self.Direction, + Tenant: self.Tenant, + Category: self.Category, + Subject: self.Subject, + Account: self.Account, + Destination: self.Destination, + TimeStart: startTime, + TimeEnd: startTime.Add(usage), + }, nil +} diff --git a/engine/storedcdr_test.go b/engine/storedcdr_test.go index d6fb13810..3163f851a 100644 --- a/engine/storedcdr_test.go +++ b/engine/storedcdr_test.go @@ -520,8 +520,8 @@ func TestStoredCdrEventFields(t *testing.T) { } } -func TestMaxUsageReqAsStoredCdr(t *testing.T) { - setupReq := &MaxUsageReq{TOR: utils.VOICE, ReqType: utils.META_RATED, Direction: "*out", Tenant: "cgrates.org", Category: "call", +func TesUsageReqAsStoredCdr(t *testing.T) { + setupReq := &UsageRecord{TOR: utils.VOICE, ReqType: utils.META_RATED, Direction: "*out", Tenant: "cgrates.org", Category: "call", Account: "1001", Subject: "1001", Destination: "1002", SetupTime: "2013-11-07T08:42:20Z", AnswerTime: "2013-11-07T08:42:26Z", Usage: "0.00000001", } @@ -534,3 +534,18 @@ func TestMaxUsageReqAsStoredCdr(t *testing.T) { t.Errorf("Expected: %+v, received: %+v", eStorCdr, storedCdr) } } + +func TestUsageReqAsCD(t *testing.T) { + req := &UsageRecord{TOR: utils.VOICE, ReqType: utils.META_RATED, Direction: "*out", Tenant: "cgrates.org", Category: "call", + Account: "1001", Subject: "1001", Destination: "1002", + SetupTime: "2013-11-07T08:42:20Z", AnswerTime: "2013-11-07T08:42:26Z", Usage: "0.00000001", + } + eCD := &CallDescriptor{TOR: req.TOR, Direction: req.Direction, + Tenant: req.Tenant, Category: req.Category, Account: req.Account, Subject: req.Subject, Destination: req.Destination, + TimeStart: time.Date(2013, 11, 7, 8, 42, 26, 0, time.UTC), TimeEnd: time.Date(2013, 11, 7, 8, 42, 26, 0, time.UTC).Add(time.Duration(10))} + if cd, err := req.AsCallDescriptor(); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(eCD, cd) { + t.Errorf("Expected: %+v, received: %+v", eCD, cd) + } +}