mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Common GetStoredCdrs for MySQL and Postgres, partially using Gorm
This commit is contained in:
@@ -19,18 +19,14 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
package engine
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/cgrates/cgrates/config"
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
"path"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
_ "github.com/bmizerany/pq"
|
||||
"github.com/go-sql-driver/mysql"
|
||||
"github.com/jinzhu/gorm"
|
||||
)
|
||||
|
||||
@@ -159,371 +155,3 @@ func (self *PostgresStorage) SetRatedCdr(cdr *utils.StoredCdr, extraInfo string)
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
func (self *PostgresStorage) GetStoredCdrs(cgrIds, runIds, tors, cdrHosts, cdrSources, reqTypes, directions, tenants, categories, accounts, subjects, destPrefixes, ratedAccounts, ratedSubjects []string,
|
||||
orderIdStart, orderIdEnd int64, timeStart, timeEnd time.Time, ignoreErr, ignoreRated, ignoreDerived bool, pagination *utils.Paginator) ([]*utils.StoredCdr, error) {
|
||||
var cdrs []*utils.StoredCdr
|
||||
var q *bytes.Buffer // Need to query differently since in case of primary, unmediated CDRs some values will be missing
|
||||
if ignoreDerived {
|
||||
q = bytes.NewBufferString(fmt.Sprintf("SELECT %s.cgrid,%s.id,%s.tor,%s.accid,%s.cdrhost,%s.cdrsource,%s.reqtype,%s.direction,%s.tenant,%s.category,%s.account,%s.subject,%s.destination,%s.setup_time,%s.answer_time,%s.usage,%s.extra_fields,%s.runid,%s.account,%s.subject,%s.cost FROM %s LEFT JOIN %s ON %s.cgrid=%s.cgrid LEFT JOIN %s ON %s.cgrid=%s.cgrid LEFT JOIN %s ON %s.cgrid=%s.cgrid AND %s.runid=%s.runid",
|
||||
utils.TBL_CDRS_PRIMARY,
|
||||
utils.TBL_CDRS_PRIMARY,
|
||||
utils.TBL_CDRS_PRIMARY,
|
||||
utils.TBL_CDRS_PRIMARY,
|
||||
utils.TBL_CDRS_PRIMARY,
|
||||
utils.TBL_CDRS_PRIMARY,
|
||||
utils.TBL_CDRS_PRIMARY,
|
||||
utils.TBL_CDRS_PRIMARY,
|
||||
utils.TBL_CDRS_PRIMARY,
|
||||
utils.TBL_CDRS_PRIMARY,
|
||||
utils.TBL_CDRS_PRIMARY,
|
||||
utils.TBL_CDRS_PRIMARY,
|
||||
utils.TBL_CDRS_PRIMARY,
|
||||
utils.TBL_CDRS_PRIMARY,
|
||||
utils.TBL_CDRS_PRIMARY,
|
||||
utils.TBL_CDRS_PRIMARY,
|
||||
utils.TBL_CDRS_EXTRA,
|
||||
utils.TBL_RATED_CDRS,
|
||||
utils.TBL_COST_DETAILS,
|
||||
utils.TBL_COST_DETAILS,
|
||||
utils.TBL_RATED_CDRS,
|
||||
utils.TBL_CDRS_PRIMARY,
|
||||
utils.TBL_CDRS_EXTRA,
|
||||
utils.TBL_CDRS_PRIMARY,
|
||||
utils.TBL_CDRS_EXTRA,
|
||||
utils.TBL_RATED_CDRS,
|
||||
utils.TBL_CDRS_PRIMARY,
|
||||
utils.TBL_RATED_CDRS,
|
||||
utils.TBL_COST_DETAILS,
|
||||
utils.TBL_RATED_CDRS,
|
||||
utils.TBL_COST_DETAILS,
|
||||
utils.TBL_RATED_CDRS,
|
||||
utils.TBL_COST_DETAILS))
|
||||
} else {
|
||||
q = bytes.NewBufferString(fmt.Sprintf("SELECT %s.cgrid,%s.id,%s.tor,%s.accid,%s.cdrhost,%s.cdrsource,%s.reqtype,%s.direction,%s.tenant,%s.category,%s.account,%s.subject,%s.destination,%s.setup_time,%s.answer_time,%s.usage,%s.extra_fields,%s.runid,%s.account,%s.subject,%s.cost FROM %s LEFT JOIN %s ON %s.cgrid=%s.cgrid LEFT JOIN %s ON %s.cgrid=%s.cgrid LEFT JOIN %s ON %s.cgrid=%s.cgrid AND %s.runid=%s.runid",
|
||||
utils.TBL_CDRS_PRIMARY,
|
||||
utils.TBL_CDRS_PRIMARY,
|
||||
utils.TBL_CDRS_PRIMARY,
|
||||
utils.TBL_CDRS_PRIMARY,
|
||||
utils.TBL_CDRS_PRIMARY,
|
||||
utils.TBL_CDRS_PRIMARY,
|
||||
utils.TBL_RATED_CDRS,
|
||||
utils.TBL_RATED_CDRS,
|
||||
utils.TBL_RATED_CDRS,
|
||||
utils.TBL_RATED_CDRS,
|
||||
utils.TBL_RATED_CDRS,
|
||||
utils.TBL_RATED_CDRS,
|
||||
utils.TBL_RATED_CDRS,
|
||||
utils.TBL_RATED_CDRS,
|
||||
utils.TBL_RATED_CDRS,
|
||||
utils.TBL_RATED_CDRS,
|
||||
utils.TBL_CDRS_EXTRA,
|
||||
utils.TBL_RATED_CDRS,
|
||||
utils.TBL_COST_DETAILS,
|
||||
utils.TBL_COST_DETAILS,
|
||||
utils.TBL_RATED_CDRS,
|
||||
utils.TBL_CDRS_PRIMARY,
|
||||
utils.TBL_CDRS_EXTRA,
|
||||
utils.TBL_CDRS_PRIMARY,
|
||||
utils.TBL_CDRS_EXTRA,
|
||||
utils.TBL_RATED_CDRS,
|
||||
utils.TBL_CDRS_PRIMARY,
|
||||
utils.TBL_RATED_CDRS,
|
||||
utils.TBL_COST_DETAILS,
|
||||
utils.TBL_RATED_CDRS,
|
||||
utils.TBL_COST_DETAILS,
|
||||
utils.TBL_RATED_CDRS,
|
||||
utils.TBL_COST_DETAILS))
|
||||
}
|
||||
q.WriteString(" WHERE")
|
||||
for idx, tblName := range []string{utils.TBL_CDRS_PRIMARY, utils.TBL_CDRS_EXTRA, utils.TBL_COST_DETAILS, utils.TBL_RATED_CDRS} {
|
||||
if idx != 0 {
|
||||
q.WriteString(" AND")
|
||||
}
|
||||
q.WriteString(fmt.Sprintf(" (%s.deleted_at IS NULL OR %s.deleted_at <= '0001-01-02')", tblName, tblName))
|
||||
}
|
||||
fltr := new(bytes.Buffer)
|
||||
if len(cgrIds) != 0 {
|
||||
qIds := bytes.NewBufferString(" (")
|
||||
for idxId, cgrId := range cgrIds {
|
||||
if idxId != 0 {
|
||||
qIds.WriteString(" OR")
|
||||
}
|
||||
qIds.WriteString(fmt.Sprintf(" %s.cgrid='%s'", utils.TBL_CDRS_PRIMARY, cgrId))
|
||||
}
|
||||
qIds.WriteString(" )")
|
||||
if fltr.Len() != 0 {
|
||||
fltr.WriteString(" AND")
|
||||
}
|
||||
fltr.Write(qIds.Bytes())
|
||||
}
|
||||
if len(runIds) != 0 {
|
||||
qIds := bytes.NewBufferString(" (")
|
||||
for idx, runId := range runIds {
|
||||
if idx != 0 {
|
||||
qIds.WriteString(" OR")
|
||||
}
|
||||
qIds.WriteString(fmt.Sprintf(" %s.runid='%s'", utils.TBL_RATED_CDRS, runId))
|
||||
}
|
||||
qIds.WriteString(" )")
|
||||
if fltr.Len() != 0 {
|
||||
fltr.WriteString(" AND")
|
||||
}
|
||||
fltr.Write(qIds.Bytes())
|
||||
}
|
||||
if len(tors) != 0 {
|
||||
qIds := bytes.NewBufferString(" (")
|
||||
for idx, host := range tors {
|
||||
if idx != 0 {
|
||||
qIds.WriteString(" OR")
|
||||
}
|
||||
qIds.WriteString(fmt.Sprintf(" %s.tor='%s'", utils.TBL_CDRS_PRIMARY, host))
|
||||
}
|
||||
qIds.WriteString(" )")
|
||||
if fltr.Len() != 0 {
|
||||
fltr.WriteString(" AND")
|
||||
}
|
||||
fltr.Write(qIds.Bytes())
|
||||
}
|
||||
if len(cdrHosts) != 0 {
|
||||
qIds := bytes.NewBufferString(" (")
|
||||
for idx, host := range cdrHosts {
|
||||
if idx != 0 {
|
||||
qIds.WriteString(" OR")
|
||||
}
|
||||
qIds.WriteString(fmt.Sprintf(" %s.cdrhost='%s'", utils.TBL_CDRS_PRIMARY, host))
|
||||
}
|
||||
qIds.WriteString(" )")
|
||||
if fltr.Len() != 0 {
|
||||
fltr.WriteString(" AND")
|
||||
}
|
||||
fltr.Write(qIds.Bytes())
|
||||
}
|
||||
if len(cdrSources) != 0 {
|
||||
qIds := bytes.NewBufferString(" (")
|
||||
for idx, src := range cdrSources {
|
||||
if idx != 0 {
|
||||
qIds.WriteString(" OR")
|
||||
}
|
||||
qIds.WriteString(fmt.Sprintf(" %s.cdrsource='%s'", utils.TBL_CDRS_PRIMARY, src))
|
||||
}
|
||||
qIds.WriteString(" )")
|
||||
if fltr.Len() != 0 {
|
||||
fltr.WriteString(" AND")
|
||||
}
|
||||
fltr.Write(qIds.Bytes())
|
||||
}
|
||||
if len(reqTypes) != 0 {
|
||||
qIds := bytes.NewBufferString(" (")
|
||||
for idx, reqType := range reqTypes {
|
||||
if idx != 0 {
|
||||
qIds.WriteString(" OR")
|
||||
}
|
||||
qIds.WriteString(fmt.Sprintf(" %s.reqtype='%s'", utils.TBL_CDRS_PRIMARY, reqType))
|
||||
}
|
||||
qIds.WriteString(" )")
|
||||
if fltr.Len() != 0 {
|
||||
fltr.WriteString(" AND")
|
||||
}
|
||||
fltr.Write(qIds.Bytes())
|
||||
}
|
||||
if len(directions) != 0 {
|
||||
qIds := bytes.NewBufferString(" (")
|
||||
for idx, direction := range directions {
|
||||
if idx != 0 {
|
||||
qIds.WriteString(" OR")
|
||||
}
|
||||
qIds.WriteString(fmt.Sprintf(" %s.direction='%s'", utils.TBL_CDRS_PRIMARY, direction))
|
||||
}
|
||||
qIds.WriteString(" )")
|
||||
if fltr.Len() != 0 {
|
||||
fltr.WriteString(" AND")
|
||||
}
|
||||
fltr.Write(qIds.Bytes())
|
||||
}
|
||||
if len(tenants) != 0 {
|
||||
qIds := bytes.NewBufferString(" (")
|
||||
for idx, tenant := range tenants {
|
||||
if idx != 0 {
|
||||
qIds.WriteString(" OR")
|
||||
}
|
||||
qIds.WriteString(fmt.Sprintf(" %s.tenant='%s'", utils.TBL_CDRS_PRIMARY, tenant))
|
||||
}
|
||||
qIds.WriteString(" )")
|
||||
if fltr.Len() != 0 {
|
||||
fltr.WriteString(" AND")
|
||||
}
|
||||
fltr.Write(qIds.Bytes())
|
||||
}
|
||||
if len(categories) != 0 {
|
||||
qIds := bytes.NewBufferString(" (")
|
||||
for idx, category := range categories {
|
||||
if idx != 0 {
|
||||
qIds.WriteString(" OR")
|
||||
}
|
||||
qIds.WriteString(fmt.Sprintf(" %s.category='%s'", utils.TBL_CDRS_PRIMARY, category))
|
||||
}
|
||||
qIds.WriteString(" )")
|
||||
if fltr.Len() != 0 {
|
||||
fltr.WriteString(" AND")
|
||||
}
|
||||
fltr.Write(qIds.Bytes())
|
||||
}
|
||||
if len(accounts) != 0 {
|
||||
qIds := bytes.NewBufferString(" (")
|
||||
for idx, account := range accounts {
|
||||
if idx != 0 {
|
||||
qIds.WriteString(" OR")
|
||||
}
|
||||
qIds.WriteString(fmt.Sprintf(" %s.account='%s'", utils.TBL_CDRS_PRIMARY, account))
|
||||
}
|
||||
qIds.WriteString(" )")
|
||||
if fltr.Len() != 0 {
|
||||
fltr.WriteString(" AND")
|
||||
}
|
||||
fltr.Write(qIds.Bytes())
|
||||
}
|
||||
if len(subjects) != 0 {
|
||||
qIds := bytes.NewBufferString(" (")
|
||||
for idx, subject := range subjects {
|
||||
if idx != 0 {
|
||||
qIds.WriteString(" OR")
|
||||
}
|
||||
qIds.WriteString(fmt.Sprintf(" %s.subject='%s'", utils.TBL_CDRS_PRIMARY, subject))
|
||||
}
|
||||
qIds.WriteString(" )")
|
||||
if fltr.Len() != 0 {
|
||||
fltr.WriteString(" AND")
|
||||
}
|
||||
fltr.Write(qIds.Bytes())
|
||||
}
|
||||
if len(destPrefixes) != 0 {
|
||||
qIds := bytes.NewBufferString(" (")
|
||||
for idx, destPrefix := range destPrefixes {
|
||||
if idx != 0 {
|
||||
qIds.WriteString(" OR")
|
||||
}
|
||||
qIds.WriteString(fmt.Sprintf(" %s.destination LIKE '%s%%'", utils.TBL_CDRS_PRIMARY, destPrefix))
|
||||
}
|
||||
qIds.WriteString(" )")
|
||||
if fltr.Len() != 0 {
|
||||
fltr.WriteString(" AND")
|
||||
}
|
||||
fltr.Write(qIds.Bytes())
|
||||
}
|
||||
if len(ratedAccounts) != 0 {
|
||||
qIds := bytes.NewBufferString(" (")
|
||||
for idx, ratedAccount := range ratedAccounts {
|
||||
if idx != 0 {
|
||||
qIds.WriteString(" OR")
|
||||
}
|
||||
qIds.WriteString(fmt.Sprintf(" %s.account='%s'", utils.TBL_COST_DETAILS, ratedAccount))
|
||||
}
|
||||
qIds.WriteString(" )")
|
||||
if fltr.Len() != 0 {
|
||||
fltr.WriteString(" AND")
|
||||
}
|
||||
fltr.Write(qIds.Bytes())
|
||||
}
|
||||
if len(ratedSubjects) != 0 {
|
||||
qIds := bytes.NewBufferString(" (")
|
||||
for idx, ratedSubject := range ratedSubjects {
|
||||
if idx != 0 {
|
||||
qIds.WriteString(" OR")
|
||||
}
|
||||
qIds.WriteString(fmt.Sprintf(" %s.subject='%s'", utils.TBL_COST_DETAILS, ratedSubject))
|
||||
}
|
||||
qIds.WriteString(" )")
|
||||
if fltr.Len() != 0 {
|
||||
fltr.WriteString(" AND")
|
||||
}
|
||||
fltr.Write(qIds.Bytes())
|
||||
}
|
||||
if orderIdStart != 0 {
|
||||
if fltr.Len() != 0 {
|
||||
fltr.WriteString(" AND")
|
||||
}
|
||||
fltr.WriteString(fmt.Sprintf(" %s.id>=%d", utils.TBL_CDRS_PRIMARY, orderIdStart))
|
||||
}
|
||||
if orderIdEnd != 0 {
|
||||
if fltr.Len() != 0 {
|
||||
fltr.WriteString(" AND")
|
||||
}
|
||||
fltr.WriteString(fmt.Sprintf(" %s.id<%d", utils.TBL_CDRS_PRIMARY, orderIdEnd))
|
||||
}
|
||||
if !timeStart.IsZero() {
|
||||
if fltr.Len() != 0 {
|
||||
fltr.WriteString(" AND")
|
||||
}
|
||||
fltr.WriteString(fmt.Sprintf(" %s.answer_time>='%s'", utils.TBL_CDRS_PRIMARY, timeStart.Format(time.RFC3339Nano)))
|
||||
}
|
||||
if !timeEnd.IsZero() {
|
||||
if fltr.Len() != 0 {
|
||||
fltr.WriteString(" AND")
|
||||
}
|
||||
fltr.WriteString(fmt.Sprintf(" %s.answer_time<'%s'", utils.TBL_CDRS_PRIMARY, timeEnd.Format(time.RFC3339Nano)))
|
||||
}
|
||||
if ignoreRated {
|
||||
if fltr.Len() != 0 {
|
||||
fltr.WriteString(" AND")
|
||||
}
|
||||
if ignoreErr {
|
||||
fltr.WriteString(fmt.Sprintf(" %s.cost IS NULL", utils.TBL_RATED_CDRS))
|
||||
} else {
|
||||
fltr.WriteString(fmt.Sprintf(" (%s.cost=-1 OR %s.cost IS NULL)", utils.TBL_RATED_CDRS, utils.TBL_RATED_CDRS))
|
||||
}
|
||||
} else if ignoreErr {
|
||||
if fltr.Len() != 0 {
|
||||
fltr.WriteString(" AND")
|
||||
}
|
||||
fltr.WriteString(fmt.Sprintf(" (%s.cost<>-1 OR %s.cost IS NULL)", utils.TBL_RATED_CDRS, utils.TBL_RATED_CDRS))
|
||||
}
|
||||
if ignoreDerived {
|
||||
if fltr.Len() != 0 {
|
||||
fltr.WriteString(" AND")
|
||||
}
|
||||
fltr.WriteString(fmt.Sprintf(" (%s.runid='%s' OR %s.cost IS NULL)", utils.TBL_RATED_CDRS, utils.DEFAULT_RUNID, utils.TBL_RATED_CDRS))
|
||||
}
|
||||
if fltr.Len() != 0 {
|
||||
q.WriteString(fmt.Sprintf(" AND %s", fltr.String()))
|
||||
}
|
||||
if pagination != nil {
|
||||
limLow, limHigh := pagination.GetLimits()
|
||||
q.WriteString(fmt.Sprintf(" LIMIT %d,%d", limLow, limHigh))
|
||||
}
|
||||
rows, err := self.Db.Query(q.String())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
var cgrid, tor, accid, cdrhost, cdrsrc, reqtype, direction, tenant, category, account, subject, destination, runid, ratedAccount, ratedSubject sql.NullString
|
||||
var extraFields []byte
|
||||
var setupTime, answerTime mysql.NullTime
|
||||
var orderid int64
|
||||
var usage, cost sql.NullFloat64
|
||||
var extraFieldsMp map[string]string
|
||||
if err := rows.Scan(&cgrid, &orderid, &tor, &accid, &cdrhost, &cdrsrc, &reqtype, &direction, &tenant, &category, &account, &subject, &destination, &setupTime, &answerTime, &usage,
|
||||
&extraFields, &runid, &ratedAccount, &ratedSubject, &cost); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(extraFields) != 0 {
|
||||
if err := json.Unmarshal(extraFields, &extraFieldsMp); err != nil {
|
||||
return nil, fmt.Errorf("JSON unmarshal error for cgrid: %s, runid: %v, error: %s", cgrid.String, runid.String, err.Error())
|
||||
}
|
||||
}
|
||||
usageDur, _ := time.ParseDuration(strconv.FormatFloat(usage.Float64, 'f', -1, 64) + "s")
|
||||
storCdr := &utils.StoredCdr{
|
||||
CgrId: cgrid.String, OrderId: orderid, TOR: tor.String, AccId: accid.String, CdrHost: cdrhost.String, CdrSource: cdrsrc.String, ReqType: reqtype.String,
|
||||
Direction: direction.String, Tenant: tenant.String,
|
||||
Category: category.String, Account: account.String, Subject: subject.String, Destination: destination.String,
|
||||
SetupTime: setupTime.Time, AnswerTime: answerTime.Time, Usage: usageDur,
|
||||
ExtraFields: extraFieldsMp, MediationRunId: runid.String, RatedAccount: ratedAccount.String, RatedSubject: ratedSubject.String, Cost: cost.Float64,
|
||||
}
|
||||
if !cost.Valid { //There was no cost provided, will fakely insert 0 if we do not handle it and reflect on re-rating
|
||||
storCdr.Cost = -1
|
||||
}
|
||||
cdrs = append(cdrs, storCdr)
|
||||
}
|
||||
return cdrs, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user