From a346235bcf91f832793c5c72eda296b2d6ae4f8a Mon Sep 17 00:00:00 2001 From: TeoV Date: Mon, 17 Feb 2020 13:08:56 +0200 Subject: [PATCH] Add the code for GetCost in SessionSv1.ProcessEvent --- Changelog.tmp | 3 +- apier/v1/costs.go | 6 ++- data/ansible/rpm_packages/main.yaml | 4 +- sessions/sessions.go | 64 +++++++++++++++++++++++++++++ utils/consts.go | 1 + 5 files changed, 72 insertions(+), 6 deletions(-) diff --git a/Changelog.tmp b/Changelog.tmp index 1347e7d1e..eb90bc1a2 100644 --- a/Changelog.tmp +++ b/Changelog.tmp @@ -1,4 +1,3 @@ - [FilterS] Renamed rals_conns to apiers_conns - [FilterS] Updated *destination filter to get ReverseDestination form API -- [SessionS] Added check for missing CGRevent -- [Packages] Change yum dir name to be the same as in reprepro ( nightly ) +- [SessionS] Added check for missing CGRevent \ No newline at end of file diff --git a/apier/v1/costs.go b/apier/v1/costs.go index 5cbce94b8..5628fba3c 100644 --- a/apier/v1/costs.go +++ b/apier/v1/costs.go @@ -56,8 +56,10 @@ func (apier *APIerSv1) GetCost(attrs AttrGetCost, ec *engine.EventCost) error { DurationIndex: usage, } var cc engine.CallCost - if err := apier.Responder.GetCost(&engine.CallDescriptorWithArgDispatcher{CallDescriptor: cd, - ArgDispatcher: attrs.ArgDispatcher}, &cc); err != nil { + if err := apier.Responder.GetCost( + &engine.CallDescriptorWithArgDispatcher{ + CallDescriptor: cd, + ArgDispatcher: attrs.ArgDispatcher}, &cc); err != nil { return utils.NewErrServerError(err) } *ec = *engine.NewEventCostFromCallCost(&cc, "", "") diff --git a/data/ansible/rpm_packages/main.yaml b/data/ansible/rpm_packages/main.yaml index 4130ce3bf..494a037cd 100644 --- a/data/ansible/rpm_packages/main.yaml +++ b/data/ansible/rpm_packages/main.yaml @@ -272,8 +272,8 @@ remote_user: '{{ gouser }}' tasks: - name: set cgrates cgrates_version - set_fact: - cgrates_version: "{{ hostvars['yum']['cgrates_version'] }}" + set_fact: + cgrates_version: "{{ hostvars['yum']['cgrates_version'] }}" - name: Creates directory become: yes diff --git a/sessions/sessions.go b/sessions/sessions.go index fdcdf7cb9..9e9fe0d9c 100644 --- a/sessions/sessions.go +++ b/sessions/sessions.go @@ -2880,6 +2880,7 @@ type V1ProcessEventArgs struct { // V1ProcessEventReply is the reply for the ProcessEvent API type V1ProcessEventReply struct { MaxUsage time.Duration + Cost *float64 // Cost is the cost received from Rater, ignoring accounting part ResourceMessage *string Attributes *engine.AttrSProcessEventReply Suppliers *engine.SortedSuppliers @@ -2926,6 +2927,9 @@ func (v1Rply *V1ProcessEventReply) AsNavigableMap( if v1Rply.StatQueueIDs != nil { cgrReply[utils.CapStatQueues] = *v1Rply.StatQueueIDs } + if v1Rply.Cost != nil { + cgrReply[utils.Cost] = *v1Rply.Cost + } } return config.NewNavigableMap(cgrReply), nil } @@ -2984,6 +2988,66 @@ func (sS *SessionS) BiRPCv1ProcessEvent(clnt rpcclient.ClientConnector, return utils.NewErrAttributeS(err) } } + // check for *cost + if argsFlagsWithParams.HasKey(utils.MetaCost) { + //compose the CallDescriptor with Args + me := engine.MapEvent(args.CGREvent.Event).Clone() + startTime := me.GetTimeIgnoreErrors(utils.AnswerTime, + sS.cgrCfg.GeneralCfg().DefaultTimezone) + if startTime.IsZero() { // AnswerTime not parsable, try SetupTime + startTime = me.GetTimeIgnoreErrors(utils.SetupTime, + sS.cgrCfg.GeneralCfg().DefaultTimezone) + } + category := me.GetStringIgnoreErrors(utils.Category) + if len(category) == 0 { + category = sS.cgrCfg.GeneralCfg().DefaultCategory + } + subject := me.GetStringIgnoreErrors(utils.Subject) + if len(subject) == 0 { + subject = me.GetStringIgnoreErrors(utils.Account) + } + + cd := &engine.CallDescriptor{ + RunID: me.GetStringIgnoreErrors(utils.RunID), + ToR: me.GetStringIgnoreErrors(utils.ToR), + Tenant: args.CGREvent.Tenant, + Category: category, + Subject: subject, + Account: me.GetStringIgnoreErrors(utils.Account), + Destination: me.GetStringIgnoreErrors(utils.Destination), + TimeStart: startTime, + TimeEnd: startTime.Add(me.GetDurationIgnoreErrors(utils.Usage)), + ExtraFields: me.AsMapString(utils.MainCDRFields), + } + var argDsp *utils.ArgDispatcher + //check if we have APIKey in event and in case it has add it in ArgDispatcher + apiKey, errAPIKey := me.GetString(utils.MetaApiKey) + if errAPIKey == nil { + argDsp = &utils.ArgDispatcher{ + APIKey: utils.StringPointer(apiKey), + } + } + //check if we have RouteID in event and in case it has add it in ArgDispatcher + if routeID, err := me.GetString(utils.MetaRouteID); err == nil { + if errAPIKey == utils.ErrNotFound { //in case we don't have APIKey, but we have RouteID we need to initialize the struct + argDsp = &utils.ArgDispatcher{ + RouteID: utils.StringPointer(routeID), + } + } else { + argDsp.RouteID = utils.StringPointer(routeID) + } + } + + var cc engine.CallCost + if err = sS.connMgr.Call(sS.cgrCfg.SessionSCfg().RALsConns, nil, + utils.ResponderGetCost, + &engine.CallDescriptorWithArgDispatcher{CallDescriptor: cd, + ArgDispatcher: argDsp}, &cc); err != nil { + return + } + cc.UpdateCost() + rply.Cost = utils.Float64Pointer(cc.Cost) + } // check for *resources if argsFlagsWithParams.HasKey(utils.MetaResources) { if len(sS.cgrCfg.SessionSCfg().ResSConns) == 0 { diff --git a/utils/consts.go b/utils/consts.go index 69e95e516..96a7b2271 100755 --- a/utils/consts.go +++ b/utils/consts.go @@ -652,6 +652,7 @@ const ( MetaLoaders = "*loaders" TmpSuffix = ".tmp" MetaDiamreq = "*diamreq" + MetaCost = "*cost" ) // Migrator Action