diff --git a/sessionmanager/postgreslogger.go b/sessionmanager/postgreslogger.go new file mode 100644 index 000000000..f17cb808f --- /dev/null +++ b/sessionmanager/postgreslogger.go @@ -0,0 +1,63 @@ +/* +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 ( + "database/sql" + "encoding/json" + "fmt" + _ "github.com/bmizerany/pq" + "github.com/rif/cgrates/timespans" + "log" +) + +type PostgresLogger struct { + db *sql.DB +} + +func NewPostgresLogger(dbName, user, pass string) *PostgresLogger { + db, err := sql.Open("postgres", fmt.Sprintf("dbname=%s user=%s password=%s sslmode=disable", dbName, user, pass)) + if err != nil { + log.Printf("Failed to open the database: %v", err) + } + return &PostgresLogger{db} +} + +func (psl *PostgresLogger) Close() { + psl.db.Close() +} + +func (psl *PostgresLogger) Log(uuid string, cc *timespans.CallCost) { + tss, err := json.Marshal(cc.Timespans) + if err != nil { + log.Printf("Error marshalling timespans to json: %v", err) + } + _, err = psl.db.Exec(fmt.Sprintf("INSERT INTO callcosts VALUES ('%s', '%s', '%s', '%s', '%s', %v, %v, '%s')", + uuid, + cc.TOR, + cc.CstmId, + cc.Subject, + cc.DestinationPrefix, + cc.Cost, + cc.ConnectFee, + tss)) + if err != nil { + log.Printf("failed to execute insert statement: %v", err) + } +} diff --git a/sessionmanager/session.go b/sessionmanager/session.go index 096ac43f5..d98336d47 100644 --- a/sessionmanager/session.go +++ b/sessionmanager/session.go @@ -25,6 +25,10 @@ import ( "time" ) +var ( + postgresLogger = NewPostgresLogger("gosqltest", "rif", "testus") +) + // Session type holding the call information fields, a session delegate for specific // actions and a channel to signal end of the debit loop. type Session struct { @@ -112,6 +116,7 @@ func (s *Session) SaveMOperations() { for _, cc := range s.CallCosts[1:] { firstCC.Merge(cc) } + postgresLogger.Log(s.uuid, firstCC) log.Print(firstCC) }() } diff --git a/timespans/callcost_test.go b/timespans/callcost_test.go index d875dd2c0..a79432d59 100644 --- a/timespans/callcost_test.go +++ b/timespans/callcost_test.go @@ -68,11 +68,11 @@ func TestMultipleResultMerge(t *testing.T) { t.Errorf("expected 6 was %v", cc2.Cost) } cc1.Merge(cc2) - if len(cc1.Timespans) != 1 || cc1.Timespans[0].GetDuration().Seconds() != 60 { + if len(cc1.Timespans) != 2 || cc1.Timespans[0].GetDuration().Seconds() != 60 { t.Error("wrong resulted timespan") } - if cc1.Cost != 12 { - t.Errorf("Exdpected 12 was %v", cc1.Cost) + if cc1.Cost != 18 { + t.Errorf("Exdpected 18 was %v", cc1.Cost) } } diff --git a/timespans/calldesc.go b/timespans/calldesc.go index 3022aa973..acf431a73 100644 --- a/timespans/calldesc.go +++ b/timespans/calldesc.go @@ -55,6 +55,7 @@ type CallDescriptor struct { CstmId, Subject, DestinationPrefix string TimeStart, TimeEnd time.Time Amount float64 + FallbackSubject string // the subject to check for destination if not found on primary subject ActivationPeriods []*ActivationPeriod storageGetter StorageGetter userBudget *UserBudget diff --git a/timespans/timespans_test.go b/timespans/timespans_test.go index ff8bf0c4c..610bc9793 100644 --- a/timespans/timespans_test.go +++ b/timespans/timespans_test.go @@ -191,16 +191,16 @@ func TestTimespanGetCost(t *testing.T) { t2 := time.Date(2012, time.February, 5, 17, 55, 0, 0, time.UTC) ts1 := TimeSpan{TimeStart: t1, TimeEnd: t2} cd := &CallDescriptor{Subject: "other", storageGetter: getter} - if ts1.GetCost(cd) != 0 { + if ts1.getCost(cd) != 0 { t.Error("No interval and still kicking") } ts1.Interval = &Interval{Price: 1} - if ts1.GetCost(cd) != 600 { - t.Error("Expected 10 got ", ts1.GetCost(cd)) + if ts1.getCost(cd) != 600 { + t.Error("Expected 10 got ", ts1.getCost(cd)) } ts1.Interval.BillingUnit = .1 - if ts1.GetCost(cd) != 6000 { - t.Error("Expected 6000 got ", ts1.GetCost(cd)) + if ts1.getCost(cd) != 6000 { + t.Error("Expected 6000 got ", ts1.getCost(cd)) } }