ApierV1.DebitUsage method, rename ApierV1.GetMaxSessionTime->ApierV1.GetMaxUsage

This commit is contained in:
DanB
2015-07-01 13:47:18 +02:00
parent be59d92ee4
commit 2659742310
4 changed files with 69 additions and 30 deletions

View File

@@ -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
}

View File

@@ -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);

View File

@@ -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
}

View File

@@ -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)
}
}