SessionS package building

This commit is contained in:
DanB
2019-01-27 18:40:56 +01:00
parent 6e6f02297c
commit 73d7ec2a77
3 changed files with 275 additions and 235 deletions

View File

@@ -22,6 +22,7 @@ import (
"sync"
"time"
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/engine"
"github.com/cgrates/cgrates/utils"
)
@@ -139,6 +140,45 @@ func (s *Session) AsActiveSessions(tmz, nodeID string) (aSs []*ActiveSession) {
return
}
// TotalUsage returns the first session run total usage
func (s *Session) TotalUsage() (tDur time.Duration) {
if len(s.SRuns) == 0 {
return
}
s.RLock()
for _, sr := range s.SRuns {
tDur = sr.TotalUsage
break // only first
}
s.RUnlock()
return
}
// AsCGREvents is a thread safe method to return the Session as CGREvents
// there will be one CGREvent for each SRun
func (s *Session) AsCGREvents(cgrCfg *config.CGRConfig) (cgrEvs []*utils.CGREvent, err error) {
if len(s.SRuns) == 0 {
return
}
s.RLock()
cgrEvs = make([]*utils.CGREvent, len(s.SRuns)) // so we can gather all cdr info while under lock
for i, sr := range s.SRuns {
var cdr *engine.CDR
if cdr, err = sr.Event.AsCDR(cgrCfg, s.Tenant,
cgrCfg.GeneralCfg().DefaultTimezone); err != nil {
break // will return with error
}
cdr.Usage = sr.TotalUsage
cgrEvs[i] = &utils.CGREvent{
Tenant: s.Tenant,
ID: utils.UUIDSha1Prefix(),
Event: cdr.AsMapStringIface(),
}
}
s.RUnlock()
return
}
// SRun is one billing run for the Session
type SRun struct {
Event engine.MapEvent // Event received from ChargerS

File diff suppressed because it is too large Load Diff

View File

@@ -771,7 +771,7 @@ const (
SessionSv1GetActiveSessions = "SessionSv1.GetActiveSessions"
SessionSv1ForceDisconnect = "SessionSv1.ForceDisconnect"
SessionSv1GetPassiveSessions = "SessionSv1.GetPassiveSessions"
SessionSv1SetPassiveSessions = "SessionSV1.SetPassiveSessions"
SessionSv1SetPassiveSession = "SessionSV1.SetPassiveSession"
SMGenericV1InitiateSession = "SMGenericV1.InitiateSession"
SMGenericV2InitiateSession = "SMGenericV2.InitiateSession"
SMGenericV2UpdateSession = "SMGenericV2.UpdateSession"