From dd91c750284410bbc46057d53addd294aa897fad Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Thu, 3 May 2012 14:33:36 +0300 Subject: [PATCH] implementing get cost 2 --- sessionmanager/session.go | 53 ++++++++++++++++++++++++ sessionmanager/session_test.go | 75 ++++++++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+) create mode 100644 sessionmanager/session.go create mode 100644 sessionmanager/session_test.go diff --git a/sessionmanager/session.go b/sessionmanager/session.go new file mode 100644 index 000000000..4767eff12 --- /dev/null +++ b/sessionmanager/session.go @@ -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 := ×pans.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()) +} diff --git a/sessionmanager/session_test.go b/sessionmanager/session_test.go new file mode 100644 index 000000000..fc74a4d04 --- /dev/null +++ b/sessionmanager/session_test.go @@ -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 +*/ + +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) + } + } +}