writing to postgresql

This commit is contained in:
Radu Ioan Fericean
2012-05-21 17:49:35 +03:00
parent 6281d03275
commit 4aca30122b
5 changed files with 77 additions and 8 deletions

View File

@@ -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 <http://www.gnu.org/licenses/>
*/
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)
}
}

View File

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

View File

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

View File

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

View File

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