ApierV1.GetCost and ApierV1.GetDataCost

This commit is contained in:
DanB
2017-11-27 14:09:21 +01:00
parent 31b1f2bcc1
commit b1bff453ea
3 changed files with 54 additions and 14 deletions

View File

@@ -1568,7 +1568,8 @@ func TestApierITGetScheduledActions(t *testing.T) {
}
func TestApierITGetDataCost(t *testing.T) {
attrs := AttrGetDataCost{Direction: "*out", Category: "data", Tenant: "cgrates.org", Account: "1001", Subject: "1001", StartTime: time.Now(), Usage: 640113}
attrs := AttrGetDataCost{Category: "data", Tenant: "cgrates.org",
Subject: "1001", AnswerTime: time.Now(), Usage: 640113}
var rply *engine.DataCost
if err := rater.Call("ApierV1.GetDataCost", attrs, &rply); err != nil {
t.Error("Unexpected nil error received: ", err.Error())
@@ -1577,6 +1578,17 @@ func TestApierITGetDataCost(t *testing.T) {
}
}
func TestApierITGetCost(t *testing.T) {
attrs := AttrGetCost{Category: "data", Tenant: "cgrates.org",
Subject: "1001", AnswerTime: time.Now(), Usage: "640113"}
var rply *engine.EventCost
if err := rater.Call("ApierV1.GetCost", attrs, &rply); err != nil {
t.Error("Unexpected nil error received: ", err.Error())
} else if *rply.Cost != 128.0240 {
t.Errorf("Unexpected cost received: %f", *rply.Cost)
}
}
// Test LoadTPFromStorDb
func TestApierInitDataDb2(t *testing.T) {
if err := engine.InitDataDb(cfg); err != nil {

View File

@@ -25,25 +25,54 @@ import (
"github.com/cgrates/cgrates/utils"
)
type AttrGetCost struct {
Tenant string
Category string
Subject string
AnswerTime time.Time
Usage string
}
func (apier *ApierV1) GetCost(attrs AttrGetCost, ec *engine.EventCost) error {
usage, err := utils.ParseDurationWithNanosecs(attrs.Usage)
if err != nil {
return err
}
cd := &engine.CallDescriptor{
Direction: utils.OUT,
Category: attrs.Category,
Tenant: attrs.Tenant,
Subject: attrs.Subject,
TimeStart: attrs.AnswerTime,
TimeEnd: attrs.AnswerTime.Add(usage),
DurationIndex: usage,
}
var cc engine.CallCost
if err := apier.Responder.GetCost(cd, &cc); err != nil {
return utils.NewErrServerError(err)
}
*ec = *engine.NewEventCostFromCallCost(&cc, "", "")
ec.Compute()
return nil
}
type AttrGetDataCost struct {
Direction string
Category string
Tenant, Account, Subject string
StartTime time.Time
Usage int64 // the call duration so far (till TimeEnd)
Tenant string
Category string
Subject string
AnswerTime time.Time
Usage time.Duration // the call duration so far (till TimeEnd)
}
func (apier *ApierV1) GetDataCost(attrs AttrGetDataCost, reply *engine.DataCost) error {
usageAsDuration := time.Duration(attrs.Usage) * time.Nanosecond // Convert to seconds to match the loaded rates
cd := &engine.CallDescriptor{
Direction: attrs.Direction,
Direction: utils.OUT,
Category: attrs.Category,
Tenant: attrs.Tenant,
Account: attrs.Account,
Subject: attrs.Subject,
TimeStart: attrs.StartTime,
TimeEnd: attrs.StartTime.Add(usageAsDuration),
DurationIndex: usageAsDuration,
TimeStart: attrs.AnswerTime,
TimeEnd: attrs.AnswerTime.Add(attrs.Usage),
DurationIndex: attrs.Usage,
TOR: utils.DATA,
}
var cc engine.CallCost

View File

@@ -21,7 +21,6 @@ package console
import (
"github.com/cgrates/cgrates/apier/v1"
"github.com/cgrates/cgrates/engine"
"github.com/cgrates/cgrates/utils"
)
func init() {
@@ -53,7 +52,7 @@ func (self *CmdGetDataCost) RpcMethod() string {
func (self *CmdGetDataCost) RpcParams(reset bool) interface{} {
if reset || self.rpcParams == nil {
self.rpcParams = &v1.AttrGetDataCost{Direction: utils.OUT}
self.rpcParams = new(v1.AttrGetDataCost)
}
return self.rpcParams
}