diff --git a/apier/v1/smgenericv1.go b/apier/v1/smgenericv1.go index d56c1d431..eed4310fb 100644 --- a/apier/v1/smgenericv1.go +++ b/apier/v1/smgenericv1.go @@ -89,6 +89,45 @@ func (self *SMGenericV1) ProcessCdr(ev sessionmanager.SMGenericEvent, reply *str return nil } +func (self *SMGenericV1) ActiveSessions(attrs utils.AttrSMGGetActiveSessions, reply *[]*sessionmanager.ActiveSession) error { + aSessions := make([]*sessionmanager.ActiveSession, 0) + for _, sGrp := range self.sm.Sessions() { // Add sessions to return with filter + for _, s := range sGrp { + as := s.AsActiveSession(self.sm.Timezone()) + if attrs.ToR != nil && *attrs.ToR != as.TOR { + continue + } + if attrs.RunID != nil && *attrs.RunID != as.RunId { + continue + } + if attrs.RequestType != nil && *attrs.RequestType != as.ReqType { + continue + } + if attrs.Tenant != nil && *attrs.Tenant != as.Tenant { + continue + } + if attrs.Category != nil && *attrs.Category != as.Category { + continue + } + if attrs.Account != nil && *attrs.Account != as.Account { + continue + } + if attrs.Subject != nil && *attrs.Subject != as.Subject { + continue + } + if attrs.Destination != nil && *attrs.Destination != as.Destination { + continue + } + if attrs.Supplier != nil && *attrs.Supplier != as.Supplier { + continue + } + aSessions = append(aSessions, as) + } + } + *reply = aSessions + return nil +} + // rpcclient.RpcClientConnection interface func (self *SMGenericV1) Call(serviceMethod string, args interface{}, reply interface{}) error { switch serviceMethod { diff --git a/sessionmanager/smg_session.go b/sessionmanager/smg_session.go index bd449d266..815daa4e3 100644 --- a/sessionmanager/smg_session.go +++ b/sessionmanager/smg_session.go @@ -256,3 +256,40 @@ func (self *SMGSession) saveOperations() error { func (self *SMGSession) TotalUsage() time.Duration { return self.totalUsage } + +func (self *SMGSession) AsActiveSession(timezone string) *ActiveSession { + sTime, _ := self.eventStart.GetSetupTime(utils.META_DEFAULT, timezone) + aTime, _ := self.eventStart.GetAnswerTime(utils.META_DEFAULT, timezone) + usage, _ := self.eventStart.GetUsage(utils.META_DEFAULT) + pdd, _ := self.eventStart.GetPdd(utils.META_DEFAULT) + aSession := &ActiveSession{ + CgrId: self.eventStart.GetCgrId(timezone), + TOR: utils.VOICE, + RunId: self.runId, + AccId: self.eventStart.GetUUID(), + CdrHost: self.eventStart.GetOriginatorIP(utils.META_DEFAULT), + CdrSource: self.eventStart.GetCdrSource(), + ReqType: self.eventStart.GetReqType(utils.META_DEFAULT), + Direction: self.eventStart.GetDirection(utils.META_DEFAULT), + Tenant: self.eventStart.GetTenant(utils.META_DEFAULT), + Category: self.eventStart.GetCategory(utils.META_DEFAULT), + Account: self.eventStart.GetAccount(utils.META_DEFAULT), + Subject: self.eventStart.GetSubject(utils.META_DEFAULT), + Destination: self.eventStart.GetDestination(utils.META_DEFAULT), + SetupTime: sTime, + AnswerTime: aTime, + Usage: usage, + Pdd: pdd, + ExtraFields: self.eventStart.GetExtraFields(), + Supplier: self.eventStart.GetSupplier(utils.META_DEFAULT), + SMId: "CGR-DA", + } + if self.cd != nil { + aSession.LoopIndex = self.cd.LoopIndex + aSession.DurationIndex = self.cd.DurationIndex + aSession.MaxRate = self.cd.MaxRate + aSession.MaxRateUnit = self.cd.MaxRateUnit + aSession.MaxCostSoFar = self.cd.MaxCostSoFar + } + return aSession +} diff --git a/sessionmanager/smgeneric.go b/sessionmanager/smgeneric.go index f18036b44..e80417211 100644 --- a/sessionmanager/smgeneric.go +++ b/sessionmanager/smgeneric.go @@ -344,6 +344,15 @@ func (self *SMGeneric) Connect() error { return nil } +// Used by APIer to retrieve sessions +func (self *SMGeneric) Sessions() map[string][]*SMGSession { + return self.getSessions() +} + +func (self *SMGeneric) Timezone() string { + return self.timezone +} + // System shutdown func (self *SMGeneric) Shutdown() error { for ssId := range self.getSessions() { // Force sessions shutdown diff --git a/utils/apitpdata.go b/utils/apitpdata.go index cc4a47a53..56dcd006f 100644 --- a/utils/apitpdata.go +++ b/utils/apitpdata.go @@ -1156,3 +1156,16 @@ type AliasValue struct { Alias string Weight float64 } + +// AttrSMGGetActiveSessions will filter returned sessions by SMGenericV1 +type AttrSMGGetActiveSessions struct { + ToR *string + RunID *string + RequestType *string + Tenant *string + Category *string + Account *string + Subject *string + Destination *string + Supplier *string +}