diff --git a/cdrs/cdrs.go b/cdrs/cdrs.go index 4e8193562..63c055b03 100644 --- a/cdrs/cdrs.go +++ b/cdrs/cdrs.go @@ -24,7 +24,6 @@ import ( "github.com/cgrates/cgrates/mediator" "github.com/cgrates/cgrates/rater" "io/ioutil" - "log" "net/http" ) @@ -38,8 +37,8 @@ func cdrHandler(w http.ResponseWriter, r *http.Request) { body, _ := ioutil.ReadAll(r.Body) if fsCdr, err := new(FSCdr).New(body); err == nil { storage.SetCdr(fsCdr) - if cfg.CDRSMediator == 'internal' { - medi.MediateCdrFromDB(fsCdr.GetAccount(), storage) + if cfg.CDRSMediator == "internal" { + medi.MediateCdrFromDB(fsCdr, storage) } else { //TODO: use the connection to mediator } diff --git a/cdrs/fscdr.go b/cdrs/fscdr.go index fefbec562..370be6f9f 100644 --- a/cdrs/fscdr.go +++ b/cdrs/fscdr.go @@ -20,6 +20,7 @@ package cdrs import ( "encoding/json" + "errors" "github.com/cgrates/cgrates/rater" "github.com/cgrates/cgrates/utils" "strconv" @@ -44,6 +45,7 @@ const ( END_TIME = "end_stamp" USERNAME = "user_name" FS_IP = "sip_local_network_addr" + RATE = "rate" ) type FSCdr map[string]string @@ -65,6 +67,15 @@ func (fsCdr FSCdr) New(body []byte) (rater.CDR, error) { return nil, err } +func (fsCdr FSCdr) GetCgrId() string { + return "" +} +func (fsCdr FSCdr) GetAccId() string { + return "" +} +func (fsCdr FSCdr) GetCdrHost() string { + return "" +} func (fsCdr FSCdr) GetDirection() string { //TODO: implement direction return "OUT" @@ -118,3 +129,15 @@ func (fsCdr FSCdr) GetEndTime() (t time.Time, err error) { t = time.Unix(0, st*1000) return } + +func (fsCdr FSCdr) GetRate() (float64, error) { + rate, ok := fsCdr[RATE] + if !ok { + return -1, errors.New("Not found") + } + return strconv.ParseFloat(rate, 64) +} + +func (fsCdr FSCdr) SetRate(rate float64) { + fsCdr[RATE] = strconv.FormatFloat(rate, 'f', -1, 64) +} diff --git a/data/storage/postgres/create_rater_tables.sql b/data/storage/postgres/create_rater_tables.sql new file mode 100644 index 000000000..118e742a8 --- /dev/null +++ b/data/storage/postgres/create_rater_tables.sql @@ -0,0 +1,94 @@ +CREATE TABLE ratingprofile IF NOT EXISTS ( + id SERIAL PRIMARY KEY, + fallbackkey VARCHAR(512), +); +CREATE TABLE ratingdestinations IF NOT EXISTS ( + id SERIAL PRIMARY KEY, + ratingprofile INTEGER REFERENCES ratingprofile(id) ON DELETE CASCADE, + destination INTEGER REFERENCES destination(id) ON DELETE CASCADE +); +CREATE TABLE destination IF NOT EXISTS ( + id SERIAL PRIMARY KEY, + ratingprofile INTEGER REFERENCES ratingprofile(id) ON DELETE CASCADE, + name VARCHAR(512), + prefixes TEXT +); +CREATE TABLE activationprofile IF NOT EXISTS( + id SERIAL PRIMARY KEY, + destination INTEGER REFERENCES destination(id) ON DELETE CASCADE, + activationtime TIMESTAMP +); +CREATE TABLE interval IF NOT EXISTS( + id SERIAL PRIMARY KEY, + activationprofile INTEGER REFERENCES activationprofile(id) ON DELETE CASCADE, + years TEXT, + months TEXT, + monthdays TEXT, + weekdays TEXT, + starttime TIMESTAMP, + endtime TIMESTAMP, + weight FLOAT8, + connectfee FLOAT8, + price FLOAT8, + pricedunits FLOAT8, + rateincrements FLOAT8 +); +CREATE TABLE minutebucket IF NOT EXISTS( + id SERIAL PRIMARY KEY, + destination INTEGER REFERENCES destination(id) ON DELETE CASCADE, + seconds FLOAT8, + weight FLOAT8, + price FLOAT8, + percent FLOAT8 +); +CREATE TABLE unitcounter IF NOT EXISTS( + id SERIAL PRIMARY KEY, + direction TEXT, + balance TEXT, + units FLOAT8 +); +CREATE TABLE unitcounterbucket IF NOT EXISTS( + id SERIAL PRIMARY KEY, + unitcounter INTEGER REFERENCES unitcounter(id) ON DELETE CASCADE, + minutebucket INTEGER REFERENCES minutebucket(id) ON DELETE CASCADE +); +CREATE TABLE actiontrigger IF NOT EXISTS( + id SERIAL PRIMARY KEY, + destination INTEGER REFERENCES destination(id) ON DELETE CASCADE, + actions INTEGER REFERENCES action(id) ON DELETE CASCADE, + balance TEXT, + direction TEXT, + thresholdvalue FLOAT8, + weight FLOAT8, + executed BOOL +); +CREATE TABLE balance IF NOT EXISTS( + id SERIAL PRIMARY KEY, + name TEXT; + value FLOAT8 +); +CREATE TABLE userbalance IF NOT EXISTS( + id SERIAL PRIMARY KEY, + unitcounter INTEGER REFERENCES unitcounter(id) ON DELETE CASCADE, + minutebucket INTEGER REFERENCES minutebucket(id) ON DELETE CASCADE + actiontriggers INTEGER REFERENCES actiontrigger(id) ON DELETE CASCADE, + balances INTEGER REFERENCES balance(id) ON DELETE CASCADE, + type TEXT +); +CREATE TABLE actiontiming IF NOT EXISTS( + id SERIAL PRIMARY KEY, + tag TEXT, + userbalances INTEGER REFERENCES userbalance(id) ON DELETE CASCADE, + timing INTEGER REFERENCES interval(id) ON DELETE CASCADE, + actions INTEGER REFERENCES action(id) ON DELETE CASCADE, + weight FLOAT8 +); +CREATE TABLE action IF NOT EXISTS( + id SERIAL PRIMARY KEY, + minutebucket INTEGER REFERENCES minutebucket(id) ON DELETE CASCADE, + actiontype TEXT, + balance TEXT, + direction TEXT, + units FLOAT8, + weight FLOAT8 +); \ No newline at end of file diff --git a/mediator/mediator.go b/mediator/mediator.go index 56a95cde6..935c29498 100644 --- a/mediator/mediator.go +++ b/mediator/mediator.go @@ -225,7 +225,7 @@ func (m *Mediator) getCostsFromRater(record []string, runIdx int) (cc *rater.Cal /* Calculates price for the specified cdr and writes the new cdr with price to the storage. If the cdr is nil then it will fetch it from the storage. */ -func (m *Mediator) MediateCdrFromDB(cdrID string, cdr rater.CDR, db rater.DataStorage) error { +func (m *Mediator) MediateCdrFromDB(cdr rater.CDR, db rater.DataStorage) error { cc := &rater.CallCost{} startTime, err := cdr.GetStartTime() if err != nil { @@ -247,5 +247,5 @@ func (m *Mediator) MediateCdrFromDB(cdrID string, cdr rater.CDR, db rater.DataSt if err := m.connector.GetCost(cd, cc); err != nil { return err } - return db.SetMediatedCdr(cdr, cc) + return db.SetRatedCdr(cdr, cc) } diff --git a/rater/cdr.go b/rater/cdr.go index 8fa743d4a..a355bf803 100644 --- a/rater/cdr.go +++ b/rater/cdr.go @@ -24,6 +24,9 @@ import ( type CDR interface { New([]byte) (CDR, error) + GetCgrId() string + GetAccId() string + GetCdrHost() string GetDirection() string GetOrigId() string GetSubject() string @@ -38,4 +41,6 @@ type CDR interface { GetEndTime() (time.Time, error) GetFallbackSubj() string GetExtraParameters() string + GetRate() (float64, error) + SetRate(float64) } diff --git a/rater/storage_gosexy.go b/rater/storage_gosexy.go index 6f704fd2a..982301388 100644 --- a/rater/storage_gosexy.go +++ b/rater/storage_gosexy.go @@ -218,17 +218,10 @@ func (rs *GosexyStorage) LogError(uuid, source, errstr string) (err error) { return } -func (rs *GosexyStorage) GetCdr(string) (CDR, error) { - return nil, nil -} func (rs *GosexyStorage) SetCdr(CDR) error { return nil } -func (rs *GosexyStorage) SetMediatedCdr(CDR, *CallCost) error { +func (rs *GosexyStorage) SetRatedCdr(CDR, *CallCost) error { return nil } - -func (rs *GosexyStorage) GetMediatedCdr(string) (CDR, error) { - return nil, nil -} diff --git a/rater/storage_interface.go b/rater/storage_interface.go index 200ac8b94..a257c7ba1 100644 --- a/rater/storage_interface.go +++ b/rater/storage_interface.go @@ -37,10 +37,12 @@ const ( LOG_ACTION_TIMMING_PREFIX = "ltm_" LOG_ACTION_TRIGGER_PREFIX = "ltr_" LOG_ERR = "ler_" + LOG_CDR = "cdr_" + LOG_MEDIATED_CDR = "mcd_" // sources SESSION_MANAGER_SOURCE = "SMR" MEDIATOR_SOURCE = "MED" - SCHED_SOURCE = "MED" + SCHED_SOURCE = "SCH" RATER_SOURCE = "RAT" ) @@ -61,10 +63,8 @@ type DataStorage interface { GetActionTimings(string) ([]*ActionTiming, error) SetActionTimings(string, []*ActionTiming) error GetAllActionTimings() (map[string][]*ActionTiming, error) - GetCdr(string) (CDR, error) SetCdr(CDR) error - SetMediatedCdr(CDR, *CallCost) error - GetMediatedCdr(string) (CDR, error) + SetRatedCdr(CDR, *CallCost) error //GetAllActionTimingsLogs() (map[string][]*ActionTiming, error) LogCallCost(uuid, source string, cc *CallCost) error LogError(uuid, source, errstr string) error diff --git a/rater/storage_map.go b/rater/storage_map.go index 7bf8760d2..a0a05d0fc 100644 --- a/rater/storage_map.go +++ b/rater/storage_map.go @@ -183,17 +183,10 @@ func (ms *MapStorage) LogError(uuid, source, errstr string) (err error) { return nil } -func (ms *MapStorage) GetCdr(string) (CDR, error) { - return nil, nil -} func (ms *MapStorage) SetCdr(CDR) error { return nil } -func (ms *MapStorage) SetMediatedCdr(CDR, *CallCost) error { +func (ms *MapStorage) SetRatedCdr(CDR, *CallCost) error { return nil } - -func (ms *MapStorage) GetMediatedCdr(string) (CDR, error) { - return nil, nil -} diff --git a/rater/storage_mongo.go b/rater/storage_mongo.go index da46e2416..7652601be 100644 --- a/rater/storage_mongo.go +++ b/rater/storage_mongo.go @@ -208,17 +208,10 @@ func (ms *MongoStorage) LogError(uuid, source, errstr string) (err error) { return ms.db.C("errlog").Insert(&LogErrEntry{uuid, errstr, source}) } -func (ms *MongoStorage) GetCdr(string) (CDR, error) { - return nil, nil -} func (ms *MongoStorage) SetCdr(CDR) error { return nil } -func (ms *MongoStorage) SetMediatedCdr(CDR, *CallCost) error { +func (ms *MongoStorage) SetRatedCdr(CDR, *CallCost) error { return nil } - -func (ms *MongoStorage) GetMediatedCdr(string) (CDR, error) { - return nil, nil -} diff --git a/rater/storage_mysql.go b/rater/storage_mysql.go index a3b157823..c198522a9 100644 --- a/rater/storage_mysql.go +++ b/rater/storage_mysql.go @@ -29,105 +29,6 @@ type MySQLStorage struct { Db *sql.DB } -var ( - mysql_schema = ` -CREATE TABLE ratingprofile IF NOT EXISTS ( - id SERIAL PRIMARY KEY, - fallbackkey VARCHAR(512), -); -CREATE TABLE ratingdestinations IF NOT EXISTS ( - id SERIAL PRIMARY KEY, - ratingprofile INTEGER REFERENCES ratingprofile(id) ON DELETE CASCADE, - destination INTEGER REFERENCES destination(id) ON DELETE CASCADE -); -CREATE TABLE destination IF NOT EXISTS ( - id SERIAL PRIMARY KEY, - ratingprofile INTEGER REFERENCES ratingprofile(id) ON DELETE CASCADE, - name VARCHAR(512), - prefixes TEXT -); -CREATE TABLE activationprofile IF NOT EXISTS( - id SERIAL PRIMARY KEY, - destination INTEGER REFERENCES destination(id) ON DELETE CASCADE, - activationtime TIMESTAMP -); -CREATE TABLE interval IF NOT EXISTS( - id SERIAL PRIMARY KEY, - activationprofile INTEGER REFERENCES activationprofile(id) ON DELETE CASCADE, - years TEXT, - months TEXT, - monthdays TEXT, - weekdays TEXT, - starttime TIMESTAMP, - endtime TIMESTAMP, - weight FLOAT8, - connectfee FLOAT8, - price FLOAT8, - pricedunits FLOAT8, - rateincrements FLOAT8 -); -CREATE TABLE minutebucket IF NOT EXISTS( - id SERIAL PRIMARY KEY, - destination INTEGER REFERENCES destination(id) ON DELETE CASCADE, - seconds FLOAT8, - weight FLOAT8, - price FLOAT8, - percent FLOAT8 -); -CREATE TABLE unitcounter IF NOT EXISTS( - id SERIAL PRIMARY KEY, - direction TEXT, - balance TEXT, - units FLOAT8 -); -CREATE TABLE unitcounterbucket IF NOT EXISTS( - id SERIAL PRIMARY KEY, - unitcounter INTEGER REFERENCES unitcounter(id) ON DELETE CASCADE, - minutebucket INTEGER REFERENCES minutebucket(id) ON DELETE CASCADE -); -CREATE TABLE actiontrigger IF NOT EXISTS( - id SERIAL PRIMARY KEY, - destination INTEGER REFERENCES destination(id) ON DELETE CASCADE, - actions INTEGER REFERENCES action(id) ON DELETE CASCADE, - balance TEXT, - direction TEXT, - thresholdvalue FLOAT8, - weight FLOAT8, - executed BOOL -); -CREATE TABLE balance IF NOT EXISTS( - id SERIAL PRIMARY KEY, - name TEXT; - value FLOAT8 -); -CREATE TABLE userbalance IF NOT EXISTS( - id SERIAL PRIMARY KEY, - unitcounter INTEGER REFERENCES unitcounter(id) ON DELETE CASCADE, - minutebucket INTEGER REFERENCES minutebucket(id) ON DELETE CASCADE - actiontriggers INTEGER REFERENCES actiontrigger(id) ON DELETE CASCADE, - balances INTEGER REFERENCES balance(id) ON DELETE CASCADE, - type TEXT -); -CREATE TABLE actiontiming IF NOT EXISTS( - id SERIAL PRIMARY KEY, - tag TEXT, - userbalances INTEGER REFERENCES userbalance(id) ON DELETE CASCADE, - timing INTEGER REFERENCES interval(id) ON DELETE CASCADE, - actions INTEGER REFERENCES action(id) ON DELETE CASCADE, - weight FLOAT8 -); -CREATE TABLE action IF NOT EXISTS( - id SERIAL PRIMARY KEY, - minutebucket INTEGER REFERENCES minutebucket(id) ON DELETE CASCADE, - actiontype TEXT, - balance TEXT, - direction TEXT, - units FLOAT8, - weight FLOAT8 -); -` -) - func NewMySQLStorage(host, port, name, user, password string) (DataStorage, error) { db, err := sql.Open("mysql", "cgrates:testus@tcp(192.168.0.17:3306)/cgrates?charset=utf8") if err != nil { @@ -186,7 +87,7 @@ func (mys *MySQLStorage) LogCallCost(uuid, source string, cc *CallCost) (err err if err != nil { Logger.Err(fmt.Sprintf("Error marshalling timespans to json: %v", err)) } - _, err = mys.Db.Exec(fmt.Sprintf("INSERT INTO cdr VALUES ('%s', '%s','%s', '%s', '%s', '%s', '%s', '%s', %v, %v, '%s')", + _, err = mys.Db.Exec(fmt.Sprintf("INSERT INTO call_costs VALUES ('%s', '%s','%s', '%s', '%s', '%s', '%s', '%s', %v, %v, '%s')", uuid, source, cc.Direction, @@ -205,7 +106,7 @@ func (mys *MySQLStorage) LogCallCost(uuid, source string, cc *CallCost) (err err } func (mys *MySQLStorage) GetCallCostLog(uuid, source string) (cc *CallCost, err error) { - row := mys.Db.QueryRow(fmt.Sprintf("SELECT * FROM cdr WHERE uuid='%s' AND source='%s'", uuid, source)) + row := mys.Db.QueryRow(fmt.Sprintf("SELECT * FROM call_costs WHERE uuid='%s' AND source='%s'", uuid, source)) var uuid_found string var timespansJson string err = row.Scan(&uuid_found, &cc.Direction, &cc.Tenant, &cc.TOR, &cc.Subject, &cc.Destination, &cc.Cost, &cc.ConnectFee, ×pansJson) @@ -221,17 +122,57 @@ func (mys *MySQLStorage) LogActionTiming(source string, at *ActionTiming, as []* } func (mys *MySQLStorage) LogError(uuid, source, errstr string) (err error) { return } -func (mys *MySQLStorage) GetCdr(string) (CDR, error) { - return nil, nil -} -func (mys *MySQLStorage) SetCdr(CDR) error { - return nil +func (mys *MySQLStorage) SetCdr(cdr CDR) (err error) { + startTime, err := cdr.GetStartTime() + if err != nil { + return err + } + endTime, err := cdr.GetEndTime() + if err != nil { + return err + } + _, err = mys.Db.Exec(fmt.Sprintf("INSERT INTO cdrs_primary VALUES ('%s', '%s','%s', '%s', '%s', '%s', '%s', '%s', %v, %v, '%s')", + cdr.GetCgrId(), + cdr.GetAccId(), + cdr.GetCdrHost(), + cdr.GetReqType(), + cdr.GetDirection(), + cdr.GetTenant(), + cdr.GetTOR(), + cdr.GetAccount(), + cdr.GetSubject(), + cdr.GetDestination(), + startTime, + endTime, //duration + )) + if err != nil { + Logger.Err(fmt.Sprintf("failed to execute cdr insert statement: %v", err)) + } + _, err = mys.Db.Exec(fmt.Sprintf("INSERT INTO cdrs_extra VALUES ('%s', '%s')", + cdr.GetCgrId(), + cdr.GetExtraParameters(), + )) + if err != nil { + Logger.Err(fmt.Sprintf("failed to execute cdr insert statement: %v", err)) + } + + return } -func (mys *MySQLStorage) SetMediatedCdr(CDR, *CallCost) error { - return nil -} +func (mys *MySQLStorage) SetRatedCdr(cdr CDR, callcost *CallCost) (err error) { + rate, err := cdr.GetRate() + if err != nil { + return err + } + _, err = mys.Db.Exec(fmt.Sprintf("INSERT INTO cdrs_extra VALUES ('%s', '%s', '%s', '%s')", + cdr.GetCgrId(), + rate, + "cgrcostid", + "cdrsrc", + )) + if err != nil { + Logger.Err(fmt.Sprintf("failed to execute cdr insert statement: %v", err)) + } -func (mys *MySQLStorage) GetMediatedCdr(string) (CDR, error) { - return nil, nil + return } diff --git a/rater/storage_postgres.go b/rater/storage_postgres.go index 7691b3130..fc908a0af 100644 --- a/rater/storage_postgres.go +++ b/rater/storage_postgres.go @@ -29,105 +29,6 @@ type PostgresStorage struct { Db *sql.DB } -var ( - postgres_schema = ` -CREATE TABLE ratingprofile IF NOT EXISTS ( - id SERIAL PRIMARY KEY, - fallbackkey VARCHAR(512), -); -CREATE TABLE ratingdestinations IF NOT EXISTS ( - id SERIAL PRIMARY KEY, - ratingprofile INTEGER REFERENCES ratingprofile(id) ON DELETE CASCADE, - destination INTEGER REFERENCES destination(id) ON DELETE CASCADE -); -CREATE TABLE destination IF NOT EXISTS ( - id SERIAL PRIMARY KEY, - ratingprofile INTEGER REFERENCES ratingprofile(id) ON DELETE CASCADE, - name VARCHAR(512), - prefixes TEXT -); -CREATE TABLE activationprofile IF NOT EXISTS( - id SERIAL PRIMARY KEY, - destination INTEGER REFERENCES destination(id) ON DELETE CASCADE, - activationtime TIMESTAMP -); -CREATE TABLE interval IF NOT EXISTS( - id SERIAL PRIMARY KEY, - activationprofile INTEGER REFERENCES activationprofile(id) ON DELETE CASCADE, - years TEXT, - months TEXT, - monthdays TEXT, - weekdays TEXT, - starttime TIMESTAMP, - endtime TIMESTAMP, - weight FLOAT8, - connectfee FLOAT8, - price FLOAT8, - pricedunits FLOAT8, - rateincrements FLOAT8 -); -CREATE TABLE minutebucket IF NOT EXISTS( - id SERIAL PRIMARY KEY, - destination INTEGER REFERENCES destination(id) ON DELETE CASCADE, - seconds FLOAT8, - weight FLOAT8, - price FLOAT8, - percent FLOAT8 -); -CREATE TABLE unitcounter IF NOT EXISTS( - id SERIAL PRIMARY KEY, - direction TEXT, - balance TEXT, - units FLOAT8 -); -CREATE TABLE unitcounterbucket IF NOT EXISTS( - id SERIAL PRIMARY KEY, - unitcounter INTEGER REFERENCES unitcounter(id) ON DELETE CASCADE, - minutebucket INTEGER REFERENCES minutebucket(id) ON DELETE CASCADE -); -CREATE TABLE actiontrigger IF NOT EXISTS( - id SERIAL PRIMARY KEY, - destination INTEGER REFERENCES destination(id) ON DELETE CASCADE, - actions INTEGER REFERENCES action(id) ON DELETE CASCADE, - balance TEXT, - direction TEXT, - thresholdvalue FLOAT8, - weight FLOAT8, - executed BOOL -); -CREATE TABLE balance IF NOT EXISTS( - id SERIAL PRIMARY KEY, - name TEXT; - value FLOAT8 -); -CREATE TABLE userbalance IF NOT EXISTS( - id SERIAL PRIMARY KEY, - unitcounter INTEGER REFERENCES unitcounter(id) ON DELETE CASCADE, - minutebucket INTEGER REFERENCES minutebucket(id) ON DELETE CASCADE - actiontriggers INTEGER REFERENCES actiontrigger(id) ON DELETE CASCADE, - balances INTEGER REFERENCES balance(id) ON DELETE CASCADE, - type TEXT -); -CREATE TABLE actiontiming IF NOT EXISTS( - id SERIAL PRIMARY KEY, - tag TEXT, - userbalances INTEGER REFERENCES userbalance(id) ON DELETE CASCADE, - timing INTEGER REFERENCES interval(id) ON DELETE CASCADE, - actions INTEGER REFERENCES action(id) ON DELETE CASCADE, - weight FLOAT8 -); -CREATE TABLE action IF NOT EXISTS( - id SERIAL PRIMARY KEY, - minutebucket INTEGER REFERENCES minutebucket(id) ON DELETE CASCADE, - actiontype TEXT, - balance TEXT, - direction TEXT, - units FLOAT8, - weight FLOAT8 -); -` -) - func NewPostgresStorage(host, port, name, user, password string) (DataStorage, error) { db, err := sql.Open("postgres", fmt.Sprintf("host=%s port=%s dbname=%s user=%s password=%s sslmode=disable", host, port, name, user, password)) if err != nil { @@ -221,17 +122,57 @@ func (psl *PostgresStorage) LogActionTiming(source string, at *ActionTiming, as } func (psl *PostgresStorage) LogError(uuid, source, errstr string) (err error) { return } -func (psl *PostgresStorage) GetCdr(string) (CDR, error) { - return nil, nil -} -func (psl *PostgresStorage) SetCdr(CDR) error { - return nil +func (psl *PostgresStorage) SetCdr(cdr CDR) (err error) { + startTime, err := cdr.GetStartTime() + if err != nil { + return err + } + endTime, err := cdr.GetEndTime() + if err != nil { + return err + } + _, err = psl.Db.Exec(fmt.Sprintf("INSERT INTO cdrs_primary VALUES ('%s', '%s','%s', '%s', '%s', '%s', '%s', '%s', %v, %v, '%s')", + cdr.GetCgrId(), + cdr.GetAccId(), + cdr.GetCdrHost(), + cdr.GetReqType(), + cdr.GetDirection(), + cdr.GetTenant(), + cdr.GetTOR(), + cdr.GetAccount(), + cdr.GetSubject(), + cdr.GetDestination(), + startTime, + endTime, //duration + )) + if err != nil { + Logger.Err(fmt.Sprintf("failed to execute cdr insert statement: %v", err)) + } + _, err = psl.Db.Exec(fmt.Sprintf("INSERT INTO cdrs_extra VALUES ('%s', '%s')", + cdr.GetCgrId(), + cdr.GetExtraParameters(), + )) + if err != nil { + Logger.Err(fmt.Sprintf("failed to execute cdr insert statement: %v", err)) + } + + return } -func (psl *PostgresStorage) SetMediatedCdr(CDR, *CallCost) error { - return nil -} +func (psl *PostgresStorage) SetRatedCdr(cdr CDR, callcost *CallCost) (err error) { + rate, err := cdr.GetRate() + if err != nil { + return err + } + _, err = psl.Db.Exec(fmt.Sprintf("INSERT INTO cdrs_extra VALUES ('%s', '%s', '%s', '%s')", + cdr.GetCgrId(), + rate, + "cgrcostid", + "cdrsrc", + )) + if err != nil { + Logger.Err(fmt.Sprintf("failed to execute cdr insert statement: %v", err)) + } -func (psl *PostgresStorage) GetMediatedCdr(string) (CDR, error) { - return nil, nil + return } diff --git a/rater/storage_redigo.go b/rater/storage_redigo.go index 604266cd4..0fb0b2bc6 100644 --- a/rater/storage_redigo.go +++ b/rater/storage_redigo.go @@ -215,17 +215,10 @@ func (rs *RedigoStorage) LogError(uuid, source, errstr string) (err error) { return } -func (rs *RedigoStorage) GetCdr(string) (CDR, error) { - return nil, nil -} func (rs *RedigoStorage) SetCdr(CDR) error { return nil } -func (rs *RedigoStorage) SetMediatedCdr(CDR, *CallCost) error { +func (rs *RedigoStorage) SetRatedCdr(CDR, *CallCost) error { return nil } - -func (rs *RedigoStorage) GetMediatedCdr(string) (CDR, error) { - return nil, nil -} diff --git a/rater/storage_redis.go b/rater/storage_redis.go index 17d43f9e3..623c4909a 100644 --- a/rater/storage_redis.go +++ b/rater/storage_redis.go @@ -241,17 +241,11 @@ func (rs *RedisStorage) LogError(uuid, source, errstr string) (err error) { } return } -func (rs *RedisStorage) GetCdr(string) (CDR, error) { - return nil, nil -} + func (rs *RedisStorage) SetCdr(CDR) error { return nil } -func (rs *RedisStorage) SetMediatedCdr(CDR, *CallCost) error { +func (rs *RedisStorage) SetRatedCdr(CDR, *CallCost) error { return nil } - -func (rs *RedisStorage) GetMediatedCdr(string) (CDR, error) { - return nil, nil -}