From eeee8b99452a699bf882fc3c378e5e0387fc41f3 Mon Sep 17 00:00:00 2001 From: TeoV Date: Mon, 12 Mar 2018 10:56:37 -0400 Subject: [PATCH] Add cgr_acd variable in freeswitch (used for GetSuppliers) --- agents/fsevent.go | 25 +++++++++++++++++++++++++ agents/fsevent_test.go | 3 ++- sessions/sessions.go | 3 +++ utils/consts.go | 1 + 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/agents/fsevent.go b/agents/fsevent.go index 713d3b7a5..80edc4b5b 100644 --- a/agents/fsevent.go +++ b/agents/fsevent.go @@ -72,6 +72,7 @@ const ( VAR_CGR_CMPUTELCR = "variable_" + utils.CGR_COMPUTELCR FsConnID = "FsConnID" // used to share connID info in event for remote disconnects VarAnswerEpoch = "variable_answer_epoch" + VarCGRACD = "variable_" + utils.CGR_ACD ) func NewFSEvent(strEv string) (fsev FSEvent) { @@ -93,6 +94,7 @@ func (fsev FSEvent) String() (result string) { func (fsev FSEvent) GetName() string { return fsev[NAME] } + func (fsev FSEvent) GetDirection(fieldName string) string { if strings.HasPrefix(fieldName, utils.STATIC_VALUE_PREFIX) { // Static value return fieldName[len(utils.STATIC_VALUE_PREFIX):] @@ -138,18 +140,22 @@ func (fsev FSEvent) GetCategory(fieldName string) string { } return utils.FirstNonEmpty(fsev[fieldName], fsev[CATEGORY], config.CgrConfig().DefaultCategory) } + func (fsev FSEvent) GetUUID() string { return fsev[UUID] } + func (fsev FSEvent) GetSessionIds() []string { return []string{fsev.GetUUID()} } + func (fsev FSEvent) GetTenant(fieldName string) string { if strings.HasPrefix(fieldName, utils.STATIC_VALUE_PREFIX) { // Static value return fieldName[len(utils.STATIC_VALUE_PREFIX):] } return utils.FirstNonEmpty(fsev[fieldName], fsev[CSTMID], config.CgrConfig().DefaultTenant) } + func (fsev FSEvent) GetReqType(fieldName string) string { var reqTypeDetected = "" // Used to automatically disable processing of the request if fsev["variable_process_cdr"] == "false" { // FS will not generated CDR here @@ -162,6 +168,7 @@ func (fsev FSEvent) GetReqType(fieldName string) string { } return utils.FirstNonEmpty(fsev[fieldName], fsev[REQTYPE], reqTypeDetected, config.CgrConfig().DefaultReqType) } + func (fsev FSEvent) MissingParameter(timezone string) string { if strings.TrimSpace(fsev.GetDirection(utils.META_DEFAULT)) == "" { return utils.Direction @@ -189,6 +196,7 @@ func (fsev FSEvent) MissingParameter(timezone string) string { } return "" } + func (fsev FSEvent) GetSetupTime(fieldName, timezone string) (t time.Time, err error) { fsSTimeStr, hasKey := fsev[SETUP_TIME] if hasKey && fsSTimeStr != "0" { @@ -201,6 +209,7 @@ func (fsev FSEvent) GetSetupTime(fieldName, timezone string) (t time.Time, err e } return utils.ParseTimeDetectLayout(sTimeStr, timezone) } + func (fsev FSEvent) GetAnswerTime(fieldName, timezone string) (t time.Time, err error) { fsATimeStr, hasKey := fsev[ANSWER_TIME] if hasKey && fsATimeStr != "0" { @@ -241,6 +250,21 @@ func (fsev FSEvent) GetPdd(fieldName string) (time.Duration, error) { return utils.ParseDurationWithSecs(PDDStr) } +func (fsev FSEvent) GetADC(fieldName string) (time.Duration, error) { + var ACDStr string + if utils.IsSliceMember([]string{utils.ACD, utils.META_DEFAULT}, fieldName) { + ACDStr = utils.FirstNonEmpty(fsev[VarCGRACD]) + if len(ACDStr) != 0 { + ACDStr = ACDStr + "s" // ACD is in seconds and CGR expects it in seconds + } + } else if strings.HasPrefix(fieldName, utils.STATIC_VALUE_PREFIX) { // Static value + ACDStr = fieldName[len(utils.STATIC_VALUE_PREFIX):] + } else { + ACDStr = fsev[fieldName] + } + return utils.ParseDurationWithSecs(ACDStr) +} + func (fsev FSEvent) GetSupplier(fieldName string) string { if strings.HasPrefix(fieldName, utils.STATIC_VALUE_PREFIX) { // Static value return fieldName[len(utils.STATIC_VALUE_PREFIX):] @@ -360,6 +384,7 @@ func (fsev FSEvent) AsMapStringInterface(timezone string) map[string]interface{} mp[utils.AnswerTime], _ = fsev.GetAnswerTime(utils.META_DEFAULT, timezone) mp[utils.Usage], _ = fsev.GetDuration(utils.META_DEFAULT) mp[utils.PDD], _ = fsev.GetPdd(utils.META_DEFAULT) + mp[utils.ACD], _ = fsev.GetADC(utils.META_DEFAULT) mp[utils.COST] = -1 mp[utils.SUPPLIER] = fsev.GetSupplier(utils.META_DEFAULT) mp[utils.DISCONNECT_CAUSE] = fsev.GetDisconnectCause(utils.META_DEFAULT) diff --git a/agents/fsevent_test.go b/agents/fsevent_test.go index 420845d11..62f524b0f 100644 --- a/agents/fsevent_test.go +++ b/agents/fsevent_test.go @@ -300,6 +300,7 @@ variable_progressmsec: 28 variable_answermsec: 500 variable_waitmsec: 500 variable_progress_mediamsec: 28 +variable_cgr_acd: 30 variable_flow_billmsec: 65539 variable_uduration: 65539698 variable_billusec: 65039704 @@ -603,9 +604,9 @@ func TestFsEvAsMapStringInterface(t *testing.T) { expectedMap[utils.Subject] = "1001" expectedMap[utils.Cost] = -1 expectedMap[utils.PDD] = time.Duration(28) * time.Millisecond + expectedMap[utils.ACD] = time.Duration(30) * time.Second expectedMap[utils.DISCONNECT_CAUSE] = "NORMAL_CLEARING" expectedMap[utils.SUPPLIER] = "supplier1" - if storedMap := ev.AsMapStringInterface(""); !reflect.DeepEqual(expectedMap, storedMap) { t.Errorf("Expecting: %+v, received: %+v", expectedMap, storedMap) } diff --git a/sessions/sessions.go b/sessions/sessions.go index d8dc0a192..73e335777 100644 --- a/sessions/sessions.go +++ b/sessions/sessions.go @@ -1400,6 +1400,9 @@ func (smg *SMGeneric) BiRPCv1AuthorizeEvent(clnt rpcclient.RpcClientConnection, } cgrEv := args.CGREvent.Clone() cgrEv.Event[utils.Usage] = time.Duration(time.Minute) + if acd, has := cgrEv.Event[utils.ACD]; has { + cgrEv.Event[utils.Usage] = acd + } var splsReply engine.SortedSuppliers sArgs := &engine.ArgsGetSuppliers{ CGREvent: *cgrEv, diff --git a/utils/consts.go b/utils/consts.go index e3f64fef3..d108c7ad0 100755 --- a/utils/consts.go +++ b/utils/consts.go @@ -522,6 +522,7 @@ const ( Error = "Error" MetaCGRReply = "*cgrReply" CacheS = "CacheS" + CGR_ACD = "cgr_acd" ) //MetaMetrics