From bb5d372094c686bb731a661504e24bce93e5d1ba Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Fri, 18 May 2012 18:24:03 +0300 Subject: [PATCH] putting credit back --- sessionmanager/session.go | 6 +++-- sessionmanager/sessiondelegate.go | 37 ++++++++++++++++++++++++++++++- timespans/callcost.go | 9 ++++---- timespans/callcost_test.go | 20 ++++------------- 4 files changed, 49 insertions(+), 23 deletions(-) diff --git a/sessionmanager/session.go b/sessionmanager/session.go index fcd71831e..096ac43f5 100644 --- a/sessionmanager/session.go +++ b/sessionmanager/session.go @@ -108,8 +108,10 @@ func (s *Session) String() string { // func (s *Session) SaveMOperations() { go func() { - for _, cc := range s.CallCosts { - log.Print(cc) + firstCC := s.CallCosts[0] + for _, cc := range s.CallCosts[1:] { + firstCC.Merge(cc) } + log.Print(firstCC) }() } diff --git a/sessionmanager/sessiondelegate.go b/sessionmanager/sessiondelegate.go index 41b9599b7..3cb6d43cd 100644 --- a/sessionmanager/sessiondelegate.go +++ b/sessionmanager/sessiondelegate.go @@ -59,7 +59,41 @@ func (dsd *DirectSessionDelegate) OnChannelAnswer(ev Event, s *Session) { } func (dsd *DirectSessionDelegate) OnChannelHangupComplete(ev Event, s *Session) { - log.Print("direct hangup") + lastCC := s.CallCosts[len(s.CallCosts)-1] + // put credit back + start := time.Now() + end := lastCC.Timespans[len(lastCC.Timespans)-1].TimeEnd + cost := 0 + seconds := 0 + for _, ts := range lastCC.Timespans { + if ts.TimeEnd > start { + sec := ts.TimeEnd.Sub(start) + if ts.Interval.BillingUnit > 0 { + cost = (sec / ts.Interval.BillingUnit) * ts.Interval.Price + } else { + cost = sec * ts.Interval.Price + } + } + } + if cost > 0 { + cd := ×pans.CallDescriptor{TOR: lastCC.TOR, + CstmId: lastCC.CstmId, + Subject: lastCC.CstmId, + DestinationPrefix: lastCC.DestinationPrefix, + Amount: -cost, + } + cd.DebitCents() + } + if seconds > 0 { + cd := ×pans.CallDescriptor{TOR: lastCC.TOR, + CstmId: lastCC.CstmId, + Subject: lastCC.CstmId, + DestinationPrefix: lastCC.DestinationPrefix, + Amount: -seconds, + } + cd.DebitSeconds() + } + log.Print("Rambursed %v cents, %v seconds") } func (dsd *DirectSessionDelegate) LoopAction(s *Session, cd *timespans.CallDescriptor) { @@ -69,6 +103,7 @@ func (dsd *DirectSessionDelegate) LoopAction(s *Session, cd *timespans.CallDescr log.Printf("Could not complete debit opperation: %v", err) } s.CallCosts = append(s.CallCosts, cc) + log.Print(cc) cd.Amount = DEBIT_PERIOD.Seconds() remainingSeconds, err := cd.GetMaxSessionTime() if remainingSeconds == -1 && err == nil { diff --git a/timespans/callcost.go b/timespans/callcost.go index 1fd3daf56..1305cfdac 100644 --- a/timespans/callcost.go +++ b/timespans/callcost.go @@ -42,17 +42,18 @@ func (cc *CallCost) String() (r string) { } // Merges the received timespan if they are similar (same activation period, same interval, same minute info. -func (cc *CallCost) Merge(other *CallCost) *CallCost { +func (cc *CallCost) Merge(other *CallCost) { ts := cc.Timespans[len(cc.Timespans)-1] otherTs := other.Timespans[0] if reflect.DeepEqual(ts.ActivationPeriod, otherTs.ActivationPeriod) && reflect.DeepEqual(ts.MinuteInfo, otherTs.MinuteInfo) && reflect.DeepEqual(ts.Interval, otherTs.Interval) { - cc.Cost += other.Cost // extend the last timespan with ts.TimeEnd = ts.TimeEnd.Add(otherTs.GetDuration()) // add the rest of the timspans cc.Timespans = append(cc.Timespans, other.Timespans[1:]...) - return nil + } else { + // just add all timespans + cc.Timespans = append(cc.Timespans, other.Timespans...) } - return other + cc.Cost += other.Cost } diff --git a/timespans/callcost_test.go b/timespans/callcost_test.go index fae7c94ad..d875dd2c0 100644 --- a/timespans/callcost_test.go +++ b/timespans/callcost_test.go @@ -41,10 +41,7 @@ func TestSingleResultMerge(t *testing.T) { if cc2.Cost != 12 { t.Errorf("expected 12 was %v", cc2.Cost) } - result := cc1.Merge(cc2) - if result != nil { - t.Error("expected nil result") - } + cc1.Merge(cc2) if len(cc1.Timespans) != 1 || cc1.Timespans[0].GetDuration().Seconds() != 120 { t.Error("wrong resulted timespan") } @@ -70,10 +67,7 @@ func TestMultipleResultMerge(t *testing.T) { if cc2.Cost != 6 { t.Errorf("expected 6 was %v", cc2.Cost) } - result := cc1.Merge(cc2) - if result == nil { - t.Error("expected non nil result") - } + cc1.Merge(cc2) if len(cc1.Timespans) != 1 || cc1.Timespans[0].GetDuration().Seconds() != 60 { t.Error("wrong resulted timespan") } @@ -99,10 +93,7 @@ func TestMultipleInputLeftMerge(t *testing.T) { if cc2.Cost != 6 { t.Errorf("expected 6 was %v", cc2.Cost) } - result := cc1.Merge(cc2) - if result != nil { - t.Error("expected nil result") - } + cc1.Merge(cc2) if len(cc1.Timespans) != 2 || cc1.Timespans[1].GetDuration().Seconds() != 120 { t.Error("wrong resulted timespan") } @@ -128,10 +119,7 @@ func TestMultipleInputRightMerge(t *testing.T) { if cc2.Cost != 18 { t.Errorf("expected 18 was %v", cc2.Cost) } - result := cc1.Merge(cc2) - if result != nil { - t.Error("expected nil result") - } + cc1.Merge(cc2) if len(cc1.Timespans) != 2 || cc1.Timespans[0].GetDuration().Seconds() != 120 { t.Error("wrong resulted timespan") t.Log(cc1.Timespans[0].GetDuration())