From 58086fcf0b6054e2b2f0d073782c16dd0231494e Mon Sep 17 00:00:00 2001 From: DanB Date: Fri, 16 Sep 2016 13:56:44 +0200 Subject: [PATCH] Merge in SMGeneric to store compressed timestamps --- sessionmanager/smg_session.go | 9 +++------ sessionmanager/smgeneric.go | 8 +++++--- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/sessionmanager/smg_session.go b/sessionmanager/smg_session.go index ed50b24b5..bbaf80b64 100644 --- a/sessionmanager/smg_session.go +++ b/sessionmanager/smg_session.go @@ -132,19 +132,14 @@ func (self *SMGSession) debit(dur time.Duration, lastUsed *time.Duration) (time. self.callCosts = append(self.callCosts, cc) self.lastDebit = initialExtraDuration + ccDuration self.totalUsage += self.lastUsage - //utils.Logger.Debug(fmt.Sprintf("TotalUsage: %f", self.totalUsage.Seconds())) - if ccDuration >= dur { // we got what we asked to be debited - //utils.Logger.Debug(fmt.Sprintf("returning normal: %f", requestedDuration.Seconds())) return requestedDuration, nil } - //utils.Logger.Debug(fmt.Sprintf("returning initialExtra: %f + ccDuration: %f", initialExtraDuration.Seconds(), ccDuration.Seconds())) return initialExtraDuration + ccDuration, nil } // Attempts to refund a duration, error on failure func (self *SMGSession) refund(refundDuration time.Duration) error { - //initialRefundDuration := refundDuration firstCC := self.callCosts[0] // use merged cc (from close function) firstCC.Timespans.Decompress() defer firstCC.Timespans.Compress() @@ -183,7 +178,6 @@ func (self *SMGSession) refund(refundDuration time.Duration) error { } } // show only what was actualy refunded (stopped in timespan) - // utils.Logger.Info(fmt.Sprintf("Refund duration: %v", initialRefundDuration-refundDuration)) if len(refundIncrements) > 0 { cd := firstCC.CreateCallDescriptor() cd.Increments = refundIncrements @@ -255,6 +249,9 @@ func (self *SMGSession) saveOperations(originID string) error { return err } } + firstCC.Timespans.Decompress() + firstCC.Timespans.Merge() // Here we could wait a while depending on the size of the timespans + firstCC.Timespans.Compress() smCost := &engine.SMCost{ CGRID: self.eventStart.GetCgrId(self.timezone), CostSource: utils.SESSION_MANAGER_SOURCE, diff --git a/sessionmanager/smgeneric.go b/sessionmanager/smgeneric.go index 1a1758ec5..150b2e4e8 100644 --- a/sessionmanager/smgeneric.go +++ b/sessionmanager/smgeneric.go @@ -305,9 +305,11 @@ func (self *SMGeneric) sessionEnd(sessionId string, usage time.Duration) error { if err := s.close(aTime.Add(usage)); err != nil { utils.Logger.Err(fmt.Sprintf(" Could not close session: %s, runId: %s, error: %s", sessionId, s.runId, err.Error())) } - if err := s.saveOperations(sessionId); err != nil { - utils.Logger.Err(fmt.Sprintf(" Could not save session: %s, runId: %s, error: %s", sessionId, s.runId, err.Error())) - } + go func() { // Call it in goroutine since it could take a while to compress timespans and save them + if err := s.saveOperations(sessionId); err != nil { + utils.Logger.Err(fmt.Sprintf(" Could not save session: %s, runId: %s, error: %s", sessionId, s.runId, err.Error())) + } + }() } return nil, nil }, time.Duration(2)*time.Second, sessionId)