diff --git a/apier/v1/apier_it_test.go b/apier/v1/apier_it_test.go index 67aea47f3..9b18b2a68 100644 --- a/apier/v1/apier_it_test.go +++ b/apier/v1/apier_it_test.go @@ -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 { diff --git a/apier/v1/costs.go b/apier/v1/costs.go index 37e93c4c0..6b7fadc83 100644 --- a/apier/v1/costs.go +++ b/apier/v1/costs.go @@ -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 diff --git a/console/datacost.go b/console/datacost.go index 551394427..c70d755ca 100644 --- a/console/datacost.go +++ b/console/datacost.go @@ -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 }