implementing get cost 2

This commit is contained in:
Radu Ioan Fericean
2012-05-03 14:33:36 +03:00
parent 9b00d43f93
commit dd91c75028
2 changed files with 128 additions and 0 deletions

53
sessionmanager/session.go Normal file
View File

@@ -0,0 +1,53 @@
package sessionmanager
import (
"fmt"
"github.com/rif/cgrates/timespans"
"log"
"time"
)
type Session struct {
customer, subject string
destinations []string
startTimes []time.Time
}
func (s *Session) AddCallToSession(destination string, startTime time.Time) {
s.destinations = append(s.destinations, destination)
s.startTimes = append(s.startTimes, startTime)
}
func (s *Session) GetSessionDurationFrom(now time.Time) (d time.Duration) {
seconds := 0.0
for _, st := range s.startTimes {
seconds += now.Sub(st).Seconds()
}
d, err := time.ParseDuration(fmt.Sprintf("%ds", int(seconds)))
if err != nil {
log.Printf("Cannot parse session duration %v", seconds)
}
return
}
func (s *Session) GetSessionDuration() time.Duration {
return s.GetSessionDurationFrom(time.Now())
}
func (s *Session) GetSessionCostFrom(now time.Time) (callCosts []*timespans.CallCost, err error) {
for i, st := range s.startTimes {
cd := &timespans.CallDescriptor{TOR: 1, CstmId: s.customer, Subject: s.subject, DestinationPrefix: s.destinations[i], TimeStart: st, TimeEnd: now}
cd.SetStorageGetter(storageGetter)
if cc, err := cd.GetCost(); err != nil {
callCosts = append(callCosts, cc)
} else {
break
}
}
return
}
func (s *Session) GetSessionCost() (callCosts []*timespans.CallCost, err error) {
return s.GetSessionCostFrom(time.Now())
}

View File

@@ -0,0 +1,75 @@
/*
Rating system designed to be used in VoIP Carriers World
Copyright (C) 2012 Radu Ioan Fericean
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package sessionmanager
import (
"log"
"testing"
"time"
)
func TestSessionDurationSingle(t *testing.T) {
s := &Session{}
s.AddCallToSession("", time.Now())
twoSeconds, _ := time.ParseDuration("2s")
if d := s.GetSessionDurationFrom(time.Now().Add(twoSeconds)); d.Seconds() < 2 || d.Seconds() > 3 {
t.Errorf("Wrong duration %v", d)
}
}
func TestSessionDurationMultiple(t *testing.T) {
s := &Session{}
s.AddCallToSession("", time.Now())
s.AddCallToSession("", time.Now())
s.AddCallToSession("", time.Now())
twoSeconds, _ := time.ParseDuration("2s")
if d := s.GetSessionDurationFrom(time.Now().Add(twoSeconds)); d.Seconds() < 6 || d.Seconds() > 7 {
t.Errorf("Wrong duration %v", d)
}
}
func TestSessionCostSingle(t *testing.T) {
s := &Session{customer: "vdf", subject: "rif"}
s.AddCallToSession("0723", time.Now())
twoSeconds, _ := time.ParseDuration("2s")
if ccs, err := s.GetSessionCostFrom(time.Now().Add(twoSeconds)); err != nil {
t.Errorf("Get cost returned error %v", err)
} else {
log.Print(ccs)
for i, cc := range ccs {
log.Printf("here: %d. %v", i, cc.Cost)
}
}
}
func TestSessionCostMultiple(t *testing.T) {
s := &Session{customer: "vdf", subject: "rif"}
s.AddCallToSession("0723", time.Now())
s.AddCallToSession("0723", time.Now())
s.AddCallToSession("0723", time.Now())
twoSeconds, _ := time.ParseDuration("2s")
if ccs, err := s.GetSessionCostFrom(time.Now().Add(twoSeconds)); err != nil {
t.Errorf("Get cost returned error %v", err)
} else {
log.Print(ccs)
for i, cc := range ccs {
log.Printf("here: %d. %v", i, cc.Cost)
}
}
}