putting credit back

This commit is contained in:
Radu Ioan Fericean
2012-05-18 18:24:03 +03:00
parent 585c048f6f
commit bb5d372094
4 changed files with 49 additions and 23 deletions

View File

@@ -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)
}()
}

View File

@@ -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 := &timespans.CallDescriptor{TOR: lastCC.TOR,
CstmId: lastCC.CstmId,
Subject: lastCC.CstmId,
DestinationPrefix: lastCC.DestinationPrefix,
Amount: -cost,
}
cd.DebitCents()
}
if seconds > 0 {
cd := &timespans.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 {

View File

@@ -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
}

View File

@@ -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())