mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Faster ActiveSessionsCount
This commit is contained in:
@@ -90,7 +90,7 @@ func (self *SMGenericV1) ProcessCDR(ev sessionmanager.SMGenericEvent, reply *str
|
||||
}
|
||||
|
||||
func (self *SMGenericV1) ActiveSessions(attrs utils.AttrSMGGetActiveSessions, reply *[]*sessionmanager.ActiveSession) error {
|
||||
aSessions, err := self.sm.ActiveSessions(attrs.AsMapStringString())
|
||||
aSessions, _, err := self.sm.ActiveSessions(attrs.AsMapStringString(), false)
|
||||
if err != nil {
|
||||
return utils.NewErrServerError(err)
|
||||
}
|
||||
@@ -99,11 +99,11 @@ func (self *SMGenericV1) ActiveSessions(attrs utils.AttrSMGGetActiveSessions, re
|
||||
}
|
||||
|
||||
func (self *SMGenericV1) ActiveSessionsCount(attrs utils.AttrSMGGetActiveSessions, reply *int) error {
|
||||
var aSessions []*sessionmanager.ActiveSession
|
||||
if err := self.ActiveSessions(attrs, &aSessions); err != nil {
|
||||
if _, count, err := self.sm.ActiveSessions(attrs.AsMapStringString(), true); err != nil {
|
||||
return err
|
||||
} else {
|
||||
*reply = count
|
||||
}
|
||||
*reply = len(aSessions)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -592,7 +592,7 @@ func (self *SMGeneric) getSessions() map[string][]*SMGSession {
|
||||
return self.sessions
|
||||
}
|
||||
|
||||
func (self *SMGeneric) ActiveSessions(fltrs map[string]string) (aSessions []*ActiveSession, err error) {
|
||||
func (self *SMGeneric) ActiveSessions(fltrs map[string]string, count bool) (aSessions []*ActiveSession, counter int, err error) {
|
||||
aSessions = make([]*ActiveSession, 0) // Make sure we return at least empty list and not nil
|
||||
// Check first based on indexes so we can downsize the list of matching sessions
|
||||
matchingSessionIDs, checkedFilters := self.getSessionIDsMatchingIndexes(fltrs)
|
||||
@@ -617,7 +617,7 @@ func (self *SMGeneric) ActiveSessions(fltrs map[string]string) (aSessions []*Act
|
||||
for i, s := range remainingSessions {
|
||||
sMp, err := s.eventStart.AsMapStringString()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, 0, err
|
||||
}
|
||||
if _, hasRunID := sMp[utils.MEDI_RUNID]; !hasRunID {
|
||||
sMp[utils.MEDI_RUNID] = utils.META_DEFAULT
|
||||
@@ -635,8 +635,11 @@ func (self *SMGeneric) ActiveSessions(fltrs map[string]string) (aSessions []*Act
|
||||
}
|
||||
}
|
||||
}
|
||||
if count {
|
||||
return nil, len(remainingSessions), nil
|
||||
}
|
||||
for _, s := range remainingSessions {
|
||||
aSessions = append(aSessions, s.AsActiveSession(self.Timezone()))
|
||||
aSessions = append(aSessions, s.AsActiveSession(self.Timezone())) // Expensive for large number of sessions
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -213,27 +213,27 @@ func TestSMGActiveSessions(t *testing.T) {
|
||||
"Extra3": "extra3",
|
||||
}
|
||||
smg.recordSession(smGev2.GetUUID(), &SMGSession{eventStart: smGev2})
|
||||
if aSessions, err := smg.ActiveSessions(nil); err != nil {
|
||||
if aSessions, _, err := smg.ActiveSessions(nil, false); err != nil {
|
||||
t.Error(err)
|
||||
} else if len(aSessions) != 2 {
|
||||
t.Errorf("Received sessions: %%+v", aSessions)
|
||||
}
|
||||
if aSessions, err := smg.ActiveSessions(map[string]string{"Tenant": "itsyscom.com"}); err != nil {
|
||||
if aSessions, _, err := smg.ActiveSessions(map[string]string{"Tenant": "itsyscom.com"}, false); err != nil {
|
||||
t.Error(err)
|
||||
} else if len(aSessions) != 1 {
|
||||
t.Errorf("Received sessions: %%+v", aSessions)
|
||||
}
|
||||
if aSessions, err := smg.ActiveSessions(map[string]string{utils.TOR: "*voice"}); err != nil {
|
||||
if aSessions, _, err := smg.ActiveSessions(map[string]string{utils.TOR: "*voice"}, false); err != nil {
|
||||
t.Error(err)
|
||||
} else if len(aSessions) != 2 {
|
||||
t.Errorf("Received sessions: %%+v", aSessions)
|
||||
}
|
||||
if aSessions, err := smg.ActiveSessions(map[string]string{"Extra3": utils.MetaEmpty}); err != nil {
|
||||
if aSessions, _, err := smg.ActiveSessions(map[string]string{"Extra3": utils.MetaEmpty}, false); err != nil {
|
||||
t.Error(err)
|
||||
} else if len(aSessions) != 1 {
|
||||
t.Errorf("Received sessions: %+v", aSessions)
|
||||
}
|
||||
if aSessions, err := smg.ActiveSessions(map[string]string{utils.SUPPLIER: "supplier2"}); err != nil {
|
||||
if aSessions, _, err := smg.ActiveSessions(map[string]string{utils.SUPPLIER: "supplier2"}, false); err != nil {
|
||||
t.Error(err)
|
||||
} else if len(aSessions) != 1 {
|
||||
t.Errorf("Received sessions: %+v", aSessions)
|
||||
|
||||
Reference in New Issue
Block a user