mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-14 12:49:54 +05:00
Adding LastUsed to SMG.sessionEnd
This commit is contained in:
@@ -157,7 +157,11 @@ func (self SMGenericEvent) GetUsage(fieldName string) (time.Duration, error) {
|
||||
if fieldName == utils.META_DEFAULT {
|
||||
fieldName = utils.USAGE
|
||||
}
|
||||
result, _ := utils.ConvertIfaceToString(self[fieldName])
|
||||
valIf, hasVal := self[fieldName]
|
||||
if !hasVal {
|
||||
return nilDuration, utils.ErrNotFound
|
||||
}
|
||||
result, _ := utils.ConvertIfaceToString(valIf)
|
||||
return utils.ParseDurationWithSecs(result)
|
||||
}
|
||||
|
||||
@@ -167,7 +171,7 @@ func (self SMGenericEvent) GetLastUsed(fieldName string) (time.Duration, error)
|
||||
}
|
||||
valStr, hasVal := self[fieldName]
|
||||
if !hasVal {
|
||||
return nilDuration, nil
|
||||
return nilDuration, utils.ErrNotFound
|
||||
}
|
||||
result, _ := utils.ConvertIfaceToString(valStr)
|
||||
return utils.ParseDurationWithSecs(result)
|
||||
|
||||
@@ -42,6 +42,7 @@ type SMGSession struct {
|
||||
callCosts []*engine.CallCost
|
||||
extraDuration time.Duration // keeps the current duration debited on top of what heas been asked
|
||||
lastUsage time.Duration // Keep record of the last debit for LastUsed functionality
|
||||
totalUsage time.Duration
|
||||
}
|
||||
|
||||
// Called in case of automatic debits
|
||||
@@ -87,6 +88,7 @@ func (self *SMGSession) debit(dur time.Duration, lastUsed time.Duration) (time.D
|
||||
}
|
||||
// apply the lastUsed correction
|
||||
dur += lastUsedCorrection
|
||||
self.totalUsage += dur // Should reflect the total usage so far
|
||||
// apply correction from previous run
|
||||
dur -= self.extraDuration
|
||||
self.extraDuration = 0
|
||||
@@ -250,3 +252,7 @@ func (self *SMGSession) saveOperations() error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *SMGSession) TotalUsage() time.Duration {
|
||||
return self.totalUsage
|
||||
}
|
||||
|
||||
@@ -200,7 +200,22 @@ func (self *SMGeneric) SessionStart(gev SMGenericEvent, clnt *rpc2.Client) (time
|
||||
func (self *SMGeneric) SessionEnd(gev SMGenericEvent, clnt *rpc2.Client) error {
|
||||
usage, err := gev.GetUsage(utils.META_DEFAULT)
|
||||
if err != nil {
|
||||
return err
|
||||
if err != utils.ErrNotFound {
|
||||
return err
|
||||
|
||||
}
|
||||
lastUsed, err := gev.GetLastUsed(utils.META_DEFAULT)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var s *SMGSession
|
||||
for _, s = range self.getSession(gev.GetUUID()) {
|
||||
break
|
||||
}
|
||||
if s == nil {
|
||||
return nil
|
||||
}
|
||||
usage = s.TotalUsage() + lastUsed
|
||||
}
|
||||
if err := self.sessionEnd(gev.GetUUID(), usage); err != nil {
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user