mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
tables unified, normal tests passing
This commit is contained in:
@@ -19,12 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
package engine
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"path"
|
||||
"time"
|
||||
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
|
||||
"github.com/jinzhu/gorm"
|
||||
_ "github.com/lib/pq"
|
||||
@@ -50,97 +45,3 @@ func NewPostgresStorage(host, port, name, user, password string, maxConn, maxIdl
|
||||
|
||||
return &PostgresStorage{&SQLStorage{Db: db.DB(), db: db}}, nil
|
||||
}
|
||||
|
||||
func (self *PostgresStorage) Flush(scriptsPath string) (err error) {
|
||||
for _, scriptName := range []string{utils.CREATE_CDRS_TABLES_SQL, utils.CREATE_TARIFFPLAN_TABLES_SQL} {
|
||||
if err := self.CreateTablesFromScript(path.Join(scriptsPath, scriptName)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
for _, tbl := range []string{utils.TBL_CDRS_PRIMARY, utils.TBL_CDRS_EXTRA} {
|
||||
if _, err := self.Db.Query(fmt.Sprintf("SELECT 1 FROM %s", tbl)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *PostgresStorage) LogCallCost(cgrid, source, runid string, cc *CallCost) (err error) {
|
||||
if cc == nil {
|
||||
return nil
|
||||
}
|
||||
tss, err := json.Marshal(cc.Timespans)
|
||||
if err != nil {
|
||||
utils.Logger.Err(fmt.Sprintf("Error marshalling timespans to json: %v", err))
|
||||
return err
|
||||
}
|
||||
tx := self.db.Begin()
|
||||
cd := &TblCostDetail{
|
||||
Cgrid: cgrid,
|
||||
Runid: runid,
|
||||
Tor: cc.TOR,
|
||||
Direction: cc.Direction,
|
||||
Tenant: cc.Tenant,
|
||||
Category: cc.Category,
|
||||
Account: cc.Account,
|
||||
Subject: cc.Subject,
|
||||
Destination: cc.Destination,
|
||||
Cost: cc.Cost,
|
||||
Timespans: string(tss),
|
||||
CostSource: source,
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
|
||||
if tx.Save(cd).Error != nil { // Check further since error does not properly reflect duplicates here (sql: no rows in result set)
|
||||
tx.Rollback()
|
||||
tx = self.db.Begin()
|
||||
updated := tx.Model(TblCostDetail{}).Where(&TblCostDetail{Cgrid: cgrid, Runid: runid}).Updates(&TblCostDetail{Tor: cc.TOR, Direction: cc.Direction, Tenant: cc.Tenant, Category: cc.Category,
|
||||
Account: cc.Account, Subject: cc.Subject, Destination: cc.Destination, Cost: cc.Cost, Timespans: string(tss), CostSource: source, UpdatedAt: time.Now()})
|
||||
if updated.Error != nil {
|
||||
tx.Rollback()
|
||||
return updated.Error
|
||||
}
|
||||
}
|
||||
tx.Commit()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *PostgresStorage) SetRatedCdr(cdr *StoredCdr) (err error) {
|
||||
tx := self.db.Begin()
|
||||
saved := tx.Save(&TblRatedCdr{
|
||||
Cgrid: cdr.CgrId,
|
||||
Runid: cdr.MediationRunId,
|
||||
Reqtype: cdr.ReqType,
|
||||
Direction: cdr.Direction,
|
||||
Tenant: cdr.Tenant,
|
||||
Category: cdr.Category,
|
||||
Account: cdr.Account,
|
||||
Subject: cdr.Subject,
|
||||
Destination: cdr.Destination,
|
||||
SetupTime: cdr.SetupTime,
|
||||
AnswerTime: cdr.AnswerTime,
|
||||
Usage: cdr.Usage.Seconds(),
|
||||
Pdd: cdr.Pdd.Seconds(),
|
||||
Supplier: cdr.Supplier,
|
||||
DisconnectCause: cdr.DisconnectCause,
|
||||
Cost: cdr.Cost,
|
||||
ExtraInfo: cdr.ExtraInfo,
|
||||
CreatedAt: time.Now(),
|
||||
})
|
||||
if saved.Error != nil {
|
||||
tx.Rollback()
|
||||
tx = self.db.Begin()
|
||||
updated := tx.Model(TblRatedCdr{}).Where(&TblRatedCdr{Cgrid: cdr.CgrId, Runid: cdr.MediationRunId}).Updates(&TblRatedCdr{Reqtype: cdr.ReqType,
|
||||
Direction: cdr.Direction, Tenant: cdr.Tenant, Category: cdr.Category, Account: cdr.Account, Subject: cdr.Subject, Destination: cdr.Destination,
|
||||
SetupTime: cdr.SetupTime, AnswerTime: cdr.AnswerTime, Usage: cdr.Usage.Seconds(), Pdd: cdr.Pdd.Seconds(), Supplier: cdr.Supplier, DisconnectCause: cdr.DisconnectCause,
|
||||
Cost: cdr.Cost, ExtraInfo: cdr.ExtraInfo,
|
||||
UpdatedAt: time.Now()})
|
||||
if updated.Error != nil {
|
||||
tx.Rollback()
|
||||
return updated.Error
|
||||
}
|
||||
}
|
||||
tx.Commit()
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user