diff --git a/apier/apier_local_test.go b/apier/apier_local_test.go index cd400ecd4..a7d3bbb50 100644 --- a/apier/apier_local_test.go +++ b/apier/apier_local_test.go @@ -1608,13 +1608,13 @@ func TestLocalGetScheduledActions(t *testing.T) { } } -func TestLocalGetDataCosts(t *testing.T) { +func TestLocalGetDataCost(t *testing.T) { if !*testLocal { return } - attrs := AttrGetDataCosts{Direction: "*out", Category: "data", Tenant: "cgrates.org", Account: "1001", Subject: "1001", StartTime: time.Now(), Usage: 640113} + attrs := AttrGetDataCost{Direction: "*out", Category: "data", Tenant: "cgrates.org", Account: "1001", Subject: "1001", StartTime: time.Now(), Usage: 640113} var rply *engine.DataCost - if err := rater.Call("ApierV1.GetDataCosts", attrs, &rply); err == nil { + if err := rater.Call("ApierV1.GetDataCost", attrs, &rply); err == nil { t.Error("Should give out error") } else if err.Error() != "SERVER_ERROR:Could not determine rating plans for call" { t.Error("Unexpected error: ", err.Error()) diff --git a/apier/costs.go b/apier/costs.go index 3bdcb129b..93669b524 100644 --- a/apier/costs.go +++ b/apier/costs.go @@ -25,7 +25,7 @@ import ( "time" ) -type AttrGetDataCosts struct { +type AttrGetDataCost struct { Direction string Category string Tenant, Account, Subject string @@ -33,7 +33,7 @@ type AttrGetDataCosts struct { Usage int64 // the call duration so far (till TimeEnd) } -func (apier *ApierV1) GetDataCosts(attrs AttrGetDataCosts, reply *engine.DataCost) error { +func (apier *ApierV1) GetDataCost(attrs AttrGetDataCost, reply *engine.DataCost) error { usageAsDuration := time.Duration(attrs.Usage) * time.Second // Convert to seconds to match the loaded rates cd := engine.CallDescriptor{ Direction: attrs.Direction, diff --git a/console/datacost.go b/console/datacost.go new file mode 100644 index 000000000..70e14b3ed --- /dev/null +++ b/console/datacost.go @@ -0,0 +1,67 @@ +/* +Real-time Charging System for Telecom & ISP environments +Copyright (C) 2012-2014 ITsysCOM GmbH + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see +*/ + +package console + +import ( + "github.com/cgrates/cgrates/apier" + "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/utils" +) + +func init() { + c := &CmdGetDataCost{ + name: "datacost", + rpcMethod: "ApierV1.GetDataCost", + clientArgs: []string{"Direction", "Category", "Tenant", "Account", "Subject", "StartTime", "Usage"}, + } + commands[c.Name()] = c + c.CommandExecuter = &CommandExecuter{c} +} + +// Commander implementation +type CmdGetDataCost struct { + name string + rpcMethod string + rpcParams *apier.AttrGetDataCost + clientArgs []string + *CommandExecuter +} + +func (self *CmdGetDataCost) Name() string { + return self.name +} + +func (self *CmdGetDataCost) RpcMethod() string { + return self.rpcMethod +} + +func (self *CmdGetDataCost) RpcParams() interface{} { + if self.rpcParams == nil { + self.rpcParams = &apier.AttrGetDataCost{Direction: utils.OUT} + } + return self.rpcParams +} + +func (self *CmdGetDataCost) RpcResult() interface{} { + return &engine.DataCost{} +} + +func (self *CmdGetDataCost) ClientArgs() []string { + return self.clientArgs +} diff --git a/data/tariffplans/prepaid1centpsec/SharedGroups.csv b/data/tariffplans/prepaid1centpsec/SharedGroups.csv new file mode 100644 index 000000000..e69de29bb diff --git a/general_tests/datachrg1_test_tmp.go b/general_tests/datachrg1_test_tmp.go new file mode 100644 index 000000000..d389721a2 --- /dev/null +++ b/general_tests/datachrg1_test_tmp.go @@ -0,0 +1,91 @@ +/* +Real-time Charging System for Telecom & ISP environments +Copyright (C) 2012-2014 ITsysCOM GmbH + +This program is free software: you can Storagetribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITH*out ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see +*/ + +package general_tests + +import ( + "testing" + "time" + + "github.com/cgrates/cgrates/cache2go" + "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/utils" +) + +func TestSetStorageDtChrg1(t *testing.T) { + ratingDb, _ = engine.NewMapStorageJson() + engine.SetRatingStorage(ratingDb) + acntDb, _ = engine.NewMapStorageJson() + engine.SetAccountingStorage(acntDb) +} + +func TestLoadCsvTpDtChrg1(t *testing.T) { + timings := `ALWAYS,*any,*any,*any,*any,00:00:00` + rates := `RT_DATA_2c,0,0.002,10,10,0` + destinationRates := `DR_DATA_1,*any,RT_DATA_2c,*up,2` + ratingPlans := `RP_DATA1,DR_DATA_1,ALWAYS,10` + ratingProfiles := `*out,cgrates.org,data,*any,2012-01-01T00:00:00Z,RP_DATA1,` + csvr := engine.NewStringCSVReader(ratingDb, acntDb, ',', "", timings, rates, destinationRates, ratingPlans, ratingProfiles, + "", "", "", "", "", "", "") + if err := csvr.LoadTimings(); err != nil { + t.Fatal(err) + } + if err := csvr.LoadRates(); err != nil { + t.Fatal(err) + } + if err := csvr.LoadDestinationRates(); err != nil { + t.Fatal(err) + } + if err := csvr.LoadRatingPlans(); err != nil { + t.Fatal(err) + } + if err := csvr.LoadRatingProfiles(); err != nil { + t.Fatal(err) + } + csvr.WriteToDatabase(false, false) + ratingDb.CacheRating(nil, nil, nil, nil, nil) + + if cachedRPlans := cache2go.CountEntries(engine.RATING_PLAN_PREFIX); cachedRPlans != 1 { + t.Error("Wrong number of cached rating plans found", cachedRPlans) + } + if cachedRProfiles := cache2go.CountEntries(engine.RATING_PROFILE_PREFIX); cachedRProfiles != 1 { + t.Error("Wrong number of cached rating profiles found", cachedRProfiles) + } +} + +func TestGetDataCostDtChrg1(t *testing.T) { + usedData := 20 + usageDur := time.Duration(usedData) * time.Second + timeStart := time.Date(2014, 3, 4, 6, 0, 0, 0, time.UTC) + cd := &engine.CallDescriptor{ + Direction: "*out", + Category: "data", + Tenant: "cgrates.org", + Subject: "12345", + Account: "12345", + TimeStart: timeStart, + TimeEnd: timeStart.Add(usageDur), + DurationIndex: usageDur, + TOR: utils.DATA, + } + if cc, err := cd.GetCost(); err != nil { + t.Error(err) + } else if cc.Cost != 0.004 { + t.Error("Wrong cost returned: ", cc.Cost) + } +} diff --git a/utils/consts.go b/utils/consts.go index c20cfa9ea..df56a4add 100644 --- a/utils/consts.go +++ b/utils/consts.go @@ -115,6 +115,7 @@ const ( MINUTES = "minutes" NANOSECONDS = "nanoseconds" SECONDS = "seconds" + OUT = "*out" ) var (