Skip to content

Commit

Permalink
👌 chore: added func chanspy check cdr validation #12
Browse files Browse the repository at this point in the history
  • Loading branch information
pnguyen215 committed May 3, 2023
1 parent 30a3a88 commit f2b29ee
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 13 deletions.
56 changes: 49 additions & 7 deletions pkg/ami/ami_cdr.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ami

import (
"fmt"
"log"
"strconv"
"strings"
"time"
Expand All @@ -13,6 +14,7 @@ import (
func NewAMICdr() *AMICdr {
r := &AMICdr{}
r.SetEvent(config.AmiListenerEventCdr)
r.SetExtenSplitterSymbol("-")
return r
}

Expand Down Expand Up @@ -217,6 +219,21 @@ func (r *AMICdr) SetUserExten(value string) *AMICdr {
return r
}

func (r *AMICdr) SetPhoneNumber(value string) *AMICdr {
r.PhoneNumber = utils.TrimAllSpace(value)
return r
}

func (r *AMICdr) SetExtenSplitterSymbol(value string) *AMICdr {
r.ExtenSplitterSymbol = utils.TrimAllSpace(value)
return r
}

func (r *AMICdr) SetPlaybackUrl(value string) *AMICdr {
r.PlaybackUrl = value
return r
}

func (r *AMICdr) Json() string {
return utils.ToJson(r)
}
Expand Down Expand Up @@ -293,6 +310,10 @@ func (r *AMICdr) IsCdrOutboundNormal() bool {
return strings.EqualFold(r.TypeDirection, config.AmiTypeOutboundNormalDirection)
}

func (r *AMICdr) IsCdrOutboundChanSpy() bool {
return strings.EqualFold(r.TypeDirection, config.AmiLastApplicationChanSpy)
}

func ParseCdr(e *AMIMessage, d *AMIDictionary) *AMICdr {
if d == nil {
d = NewDictionary()
Expand Down Expand Up @@ -331,21 +352,42 @@ func ParseCdr(e *AMIMessage, d *AMIDictionary) *AMICdr {
r.SetFlowCall(flow)
r.SetDirection(config.AmiOutboundDirection)
r.SetTypeDirection(config.AmiTypeOutboundNormalDirection)
r.SetUserExten(strings.Split(r.Channel, "-")[0])
r.SetUserExten(strings.Split(r.Channel, r.ExtenSplitterSymbol)[0])
r.SetPhoneNumber(phone)
} else {
valid := strings.EqualFold(r.LastApplication, config.AmiLastApplicationDial)
if valid { // from dial
var inCase bool = false
// from outbound chan-spy
if strings.EqualFold(r.LastApplication, config.AmiLastApplicationChanSpy) {
inCase = true
flow := fmt.Sprintf(form, r.Channel, r.LastData)
r.SetFlowCall(flow)
r.SetTypeDirection(config.AmiTypeChanSpyDirection)
r.SetDirection(config.AmiOutboundDirection)
r.SetUserExten(strings.Split(r.Channel, r.ExtenSplitterSymbol)[0])
}
// from inbound dial
if strings.EqualFold(r.LastApplication, config.AmiLastApplicationDial) {
inCase = true
flow := fmt.Sprintf(form, r.Source, r.DestinationChannel)
r.SetFlowCall(flow)
r.SetDirection(config.AmiInboundDirection)
r.SetTypeDirection(config.AmiTypeInboundDialDirection)
r.SetUserExten(strings.Split(r.DestinationChannel, "-")[0])
} else { // from queue
r.SetUserExten(strings.Split(r.DestinationChannel, r.ExtenSplitterSymbol)[0])
r.SetPhoneNumber(r.Source)
}
// from inbound queue
if strings.EqualFold(r.LastApplication, config.AmiLastApplicationQueue) {
inCase = true
flow := fmt.Sprintf(form, r.Source, r.Channel)
r.SetFlowCall(flow)
r.SetDirection(config.AmiInboundDirection)
r.SetTypeDirection(config.AmiTypeInboundQueueDirection)
r.SetUserExten(strings.Split(r.Channel, "-")[0])
r.SetUserExten(strings.Split(r.Channel, r.ExtenSplitterSymbol)[0])
r.SetPhoneNumber(r.Source)
}
if !inCase {
log.Printf("ParseCdr, CDR exception case = %v", utils.ToJson(r))
}
r.SetDirection(config.AmiInboundDirection)
}
return r
}
11 changes: 7 additions & 4 deletions pkg/ami/ami_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,10 +434,13 @@ type AMICdr struct {
// Type of call direction
// `inbound`
// `outbound`
Direction string `json:"direction"`
FlowCall string `json:"flow_call"`
TypeDirection string `json:"type_direction"`
UserExtension string `json:"user_exten,omitempty"`
Direction string `json:"direction"`
FlowCall string `json:"flow_call"`
TypeDirection string `json:"type_direction"`
UserExtension string `json:"user_exten,omitempty"`
PhoneNumber string `json:"phone_number,omitempty"`
PlaybackUrl string `json:"playback_url,omitempty"` // the only cdr has status answered
ExtenSplitterSymbol string `json:"-"` // default exten splitter symbol: -, example: SIP/1000-00098fec then split by -
}

type AMIPayloadChanspy struct {
Expand Down
6 changes: 4 additions & 2 deletions pkg/ami/config/ami_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@ const (
AmiTypeOutboundNormalDirection = "outbound_normal"
AmiTypeInboundDialDirection = "inbound_dial" // from application on user local machine
AmiTypeInboundQueueDirection = "inbound_queue" // from queue, not real user
AmiTypeChanSpyDirection = "chan_spy" //
)

var (
Expand Down Expand Up @@ -630,8 +631,9 @@ const (
)

const (
AmiLastApplicationDial = "Dial"
AmiLastApplicationQueue = "Queue"
AmiLastApplicationDial = "Dial"
AmiLastApplicationQueue = "Queue"
AmiLastApplicationChanSpy = "ChanSpy"
)

const (
Expand Down

0 comments on commit f2b29ee

Please sign in to comment.