mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-20 22:58:44 +05:00
first implementation of GetStoredCdrs
This commit is contained in:
@@ -32,28 +32,26 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
colDst = "destinations"
|
||||
colAct = "actions"
|
||||
colApl = "actionplans"
|
||||
colAtr = "actiontriggers"
|
||||
colRpl = "ratingplans"
|
||||
colRpf = "ratingprofiles"
|
||||
colAcc = "accounts"
|
||||
colShg = "sharedgroups"
|
||||
colLcr = "lcrrules"
|
||||
colDcs = "derivedchargers"
|
||||
colAls = "aliases"
|
||||
colStq = "statsqeues"
|
||||
colPbs = "pubsub"
|
||||
colUsr = "users"
|
||||
colCrs = "cdrstats"
|
||||
colLht = "loadhistory"
|
||||
colLogCC = "callcostlogs"
|
||||
colLogAtr = "actiontriggerslogs"
|
||||
colLogApl = "actionplanlogs"
|
||||
colLogErr = "errorlogs"
|
||||
colCdrs = "cdrs"
|
||||
colRatedCdrs = "ratedcdrs"
|
||||
colDst = "destinations"
|
||||
colAct = "actions"
|
||||
colApl = "actionplans"
|
||||
colAtr = "actiontriggers"
|
||||
colRpl = "ratingplans"
|
||||
colRpf = "ratingprofiles"
|
||||
colAcc = "accounts"
|
||||
colShg = "sharedgroups"
|
||||
colLcr = "lcrrules"
|
||||
colDcs = "derivedchargers"
|
||||
colAls = "aliases"
|
||||
colStq = "statsqeues"
|
||||
colPbs = "pubsub"
|
||||
colUsr = "users"
|
||||
colCrs = "cdrstats"
|
||||
colLht = "loadhistory"
|
||||
colLogAtr = "actiontriggerslogs"
|
||||
colLogApl = "actionplanlogs"
|
||||
colLogErr = "errorlogs"
|
||||
colCdrs = "cdrs"
|
||||
)
|
||||
|
||||
type MongoStorage struct {
|
||||
@@ -190,26 +188,13 @@ func NewMongoStorage(host, port, db, user, pass string) (*MongoStorage, error) {
|
||||
}
|
||||
}
|
||||
index = mgo.Index{
|
||||
Key: []string{"id", "source", "runid"},
|
||||
Key: []string{"cgrid", "cdrsource", "mediationrunid"},
|
||||
Unique: true,
|
||||
DropDups: false,
|
||||
Background: false,
|
||||
Sparse: false,
|
||||
}
|
||||
collections = []string{colLogCC}
|
||||
for _, col := range collections {
|
||||
if err = ndb.C(col).EnsureIndex(index); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
index = mgo.Index{
|
||||
Key: []string{"cgrid"},
|
||||
Unique: true,
|
||||
DropDups: false,
|
||||
Background: false,
|
||||
Sparse: false,
|
||||
}
|
||||
collections = []string{colCdrs, colRatedCdrs}
|
||||
collections = []string{colCdrs}
|
||||
for _, col := range collections {
|
||||
if err = ndb.C(col).EnsureIndex(index); err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -702,25 +702,20 @@ func (ms *MongoStorage) LogActionPlan(source string, at *ActionPlan, as Actions)
|
||||
}
|
||||
|
||||
func (ms *MongoStorage) LogCallCost(cgrid, source, runid string, cc *CallCost) error {
|
||||
s := &struct {
|
||||
Id string `bson:"id,omitempty"`
|
||||
Source string
|
||||
Runid string `bson:"runid,omitempty"`
|
||||
CallCost *CallCost
|
||||
}{cgrid, source, runid, cc}
|
||||
_, err := ms.db.C(colLogCC).Upsert(bson.M{"id": s.Id, "source": s.Source, "runid": s.Runid}, s)
|
||||
s := &StoredCdr{
|
||||
CgrId: cgrid,
|
||||
CdrSource: source,
|
||||
MediationRunId: runid,
|
||||
CostDetails: cc,
|
||||
}
|
||||
_, err := ms.db.C(colCdrs).Upsert(bson.M{"cgrid": cgrid, "cdrsource": source, "mediationrunid": runid}, s)
|
||||
return err
|
||||
}
|
||||
|
||||
func (ms *MongoStorage) GetCallCostLog(cgrid, source, runid string) (cc *CallCost, err error) {
|
||||
result := &struct {
|
||||
Id string `bson:"id,omitempty"`
|
||||
Source string
|
||||
Runid string `bson:"runid,omitempty"`
|
||||
CallCost *CallCost
|
||||
}{}
|
||||
err = ms.db.C(colLogCC).Find(bson.M{"id": cgrid, "source": source, "runid": runid}).One(result)
|
||||
cc = result.CallCost
|
||||
result := StoredCdr{}
|
||||
err = ms.db.C(colCdrs).Find(bson.M{"cgrid": cgrid, "cdrsource": source, "mediationrunid": runid}).One(&result)
|
||||
cc = result.CostDetails
|
||||
return
|
||||
}
|
||||
|
||||
@@ -730,7 +725,7 @@ func (ms *MongoStorage) SetCdr(cdr *StoredCdr) error {
|
||||
}
|
||||
|
||||
func (ms *MongoStorage) SetRatedCdr(storedCdr *StoredCdr) error {
|
||||
_, err := ms.db.C(colRatedCdrs).Upsert(bson.M{"cgrid": storedCdr.CgrId}, storedCdr)
|
||||
_, err := ms.db.C(colCdrs).Upsert(bson.M{"cgrid": storedCdr.CgrId}, storedCdr)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -739,15 +734,152 @@ func (ms *MongoStorage) RemStoredCdrs(cgrIds []string) error {
|
||||
if len(cgrIds) == 0 {
|
||||
return nil
|
||||
}
|
||||
return ms.db.C(colCdrs).Update(bson.M{"cgrid": bson.M{"$in": cgrIds}}, map[string]interface{}{"deleted_at": time.Now()})
|
||||
}
|
||||
|
||||
for _, col := range []string{colCdrs, colRatedCdrs, colLogCC} {
|
||||
if err := ms.db.C(col).Update(bson.M{"cgrid": bson.M{"$in": cgrIds}}, map[string]interface{}{"deleted_at": time.Now()}); err != nil {
|
||||
return err
|
||||
func (ms *MongoStorage) cleanEmptyFilters(filters bson.M) {
|
||||
for k, v := range filters {
|
||||
switch value := v.(type) {
|
||||
case *float64:
|
||||
if value == nil {
|
||||
delete(filters, k)
|
||||
}
|
||||
case *time.Time:
|
||||
if value == nil {
|
||||
delete(filters, k)
|
||||
}
|
||||
case []string:
|
||||
if len(value) == 0 {
|
||||
delete(filters, k)
|
||||
}
|
||||
case bson.M:
|
||||
ms.cleanEmptyFilters(value)
|
||||
if len(value) == 0 {
|
||||
delete(filters, k)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ms *MongoStorage) GetStoredCdrs(qryFltr *utils.CdrsFilter) ([]*StoredCdr, int64, error) {
|
||||
return nil, 0, utils.ErrNotImplemented
|
||||
filters := bson.M{
|
||||
"cgrid": bson.M{"$in": qryFltr.CgrIds, "$nin": qryFltr.NotCgrIds},
|
||||
"runid": bson.M{"$in": qryFltr.RunIds, "$nin": qryFltr.NotRunIds},
|
||||
"tor": bson.M{"$in": qryFltr.Tors, "$nin": qryFltr.NotTors},
|
||||
"cdrhost": bson.M{"$in": qryFltr.CdrHosts, "$nin": qryFltr.NotCdrHosts},
|
||||
"cdrsource": bson.M{"$in": qryFltr.CdrSources, "$nin": qryFltr.NotCdrSources},
|
||||
"reqtype": bson.M{"$in": qryFltr.ReqTypes, "$nin": qryFltr.NotReqTypes},
|
||||
"direction": bson.M{"$in": qryFltr.Directions, "$nin": qryFltr.NotDirections},
|
||||
"tenant": bson.M{"$in": qryFltr.Tenants, "$nin": qryFltr.NotTenants},
|
||||
"category": bson.M{"$in": qryFltr.Categories, "$nin": qryFltr.NotCategories},
|
||||
"account": bson.M{"$in": qryFltr.Accounts, "$nin": qryFltr.NotAccounts},
|
||||
"subject": bson.M{"$in": qryFltr.Subjects, "$nin": qryFltr.NotSubjects},
|
||||
"supplier": bson.M{"$in": qryFltr.Suppliers, "$nin": qryFltr.NotSuppliers},
|
||||
"disconnect_cause": bson.M{"$in": qryFltr.DisconnectCauses, "$nin": qryFltr.NotDisconnectCauses},
|
||||
"setuptime": bson.M{"$gte": qryFltr.SetupTimeStart, "$lt": qryFltr.SetupTimeEnd},
|
||||
"answertime": bson.M{"$gte": qryFltr.AnswerTimeStart, "$lt": qryFltr.AnswerTimeEnd},
|
||||
"created_at": bson.M{"$gte": qryFltr.CreatedAtStart, "$lt": qryFltr.CreatedAtEnd},
|
||||
"updated_at": bson.M{"$gte": qryFltr.UpdatedAtStart, "$lt": qryFltr.UpdatedAtEnd},
|
||||
"usage": bson.M{"$gte": qryFltr.MinUsage, "$lt": qryFltr.MaxUsage},
|
||||
"pdd": bson.M{"$gte": qryFltr.MinPdd, "$lt": qryFltr.MaxPdd},
|
||||
"costdetails.account": bson.M{"$in": qryFltr.RatedAccounts, "$nin": qryFltr.NotRatedAccounts},
|
||||
"costdetails.subject": bson.M{"$in": qryFltr.RatedSubjects, "$nin": qryFltr.NotRatedSubjects},
|
||||
}
|
||||
|
||||
ms.cleanEmptyFilters(filters)
|
||||
|
||||
if qryFltr.OrderIdStart != 0 {
|
||||
filters["id"] = bson.M{"$gte": qryFltr.OrderIdStart}
|
||||
}
|
||||
if qryFltr.OrderIdEnd != 0 {
|
||||
if m, ok := filters["id"]; ok {
|
||||
m.(bson.M)["id"] = bson.M{"$gte": qryFltr.OrderIdStart}
|
||||
} else {
|
||||
filters["id"] = bson.M{"$gte": qryFltr.OrderIdStart}
|
||||
}
|
||||
}
|
||||
|
||||
var regexes []bson.RegEx
|
||||
if len(qryFltr.DestPrefixes) != 0 {
|
||||
for _, prefix := range qryFltr.DestPrefixes {
|
||||
regexes = append(regexes, bson.RegEx{Pattern: prefix + ".*"})
|
||||
}
|
||||
}
|
||||
var notRegexes []bson.RegEx
|
||||
if len(qryFltr.NotDestPrefixes) != 0 {
|
||||
for _, prefix := range qryFltr.DestPrefixes {
|
||||
notRegexes = append(notRegexes, bson.RegEx{Pattern: prefix + ".*"})
|
||||
}
|
||||
}
|
||||
filters["destination"] = bson.M{"$in": regexes, "$nin": notRegexes}
|
||||
|
||||
regexes = make([]bson.RegEx, 0)
|
||||
if len(qryFltr.DestPrefixes) != 0 {
|
||||
for _, prefix := range qryFltr.DestPrefixes {
|
||||
regexes = append(regexes, bson.RegEx{Pattern: prefix + ".*"})
|
||||
}
|
||||
}
|
||||
notRegexes = make([]bson.RegEx, 0)
|
||||
if len(qryFltr.NotDestPrefixes) != 0 {
|
||||
for _, prefix := range qryFltr.DestPrefixes {
|
||||
notRegexes = append(notRegexes, bson.RegEx{Pattern: prefix + ".*"})
|
||||
}
|
||||
}
|
||||
filters["destination"] = bson.M{"$in": regexes, "$nin": notRegexes}
|
||||
|
||||
if len(qryFltr.ExtraFields) != 0 {
|
||||
var extrafields []bson.M
|
||||
for field, value := range qryFltr.ExtraFields {
|
||||
extrafields = append(extrafields, bson.M{"extrafields." + field: value})
|
||||
}
|
||||
filters["$or"] = extrafields
|
||||
}
|
||||
|
||||
if len(qryFltr.NotExtraFields) != 0 {
|
||||
var extrafields []bson.M
|
||||
for field, value := range qryFltr.ExtraFields {
|
||||
extrafields = append(extrafields, bson.M{"extrafields." + field: value})
|
||||
}
|
||||
filters["$not"] = bson.M{"$or": extrafields}
|
||||
}
|
||||
|
||||
if qryFltr.MinCost != nil {
|
||||
if qryFltr.MaxCost == nil {
|
||||
filters["cost"] = bson.M{"$gte": *qryFltr.MinCost}
|
||||
} else if *qryFltr.MinCost == 0.0 && *qryFltr.MaxCost == -1.0 { // Special case when we want to skip errors
|
||||
filters["cost"] = bson.M{"$or": []bson.M{bson.M{"$eq": nil}, bson.M{"$gte": 0.0}}}
|
||||
} else {
|
||||
filters["cost"] = bson.M{"$gte": *qryFltr.MinCost, "$lt": *qryFltr.MaxCost}
|
||||
}
|
||||
} else if qryFltr.MaxCost != nil {
|
||||
if *qryFltr.MaxCost == -1.0 { // Non-rated CDRs
|
||||
filters["cost"] = bson.M{"$eq": nil} // Need to include it otherwise all CDRs will be returned
|
||||
} else { // Above limited CDRs, since MinCost is empty, make sure we query also NULL cost
|
||||
filters["cost"] = bson.M{"$gte": *qryFltr.MinCost, "$lt": *qryFltr.MaxCost}
|
||||
}
|
||||
}
|
||||
q := ms.db.C(colCdrs).Find(filters)
|
||||
if qryFltr.Paginator.Limit != nil {
|
||||
q = q.Limit(*qryFltr.Paginator.Limit)
|
||||
}
|
||||
if qryFltr.Paginator.Offset != nil {
|
||||
q = q.Skip(*qryFltr.Paginator.Offset)
|
||||
}
|
||||
if qryFltr.Count {
|
||||
cnt, err := q.Count()
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
return nil, int64(cnt), nil
|
||||
}
|
||||
|
||||
// Execute query
|
||||
iter := q.Iter()
|
||||
var cdrs []*StoredCdr
|
||||
cdr := StoredCdr{}
|
||||
for iter.Next(&cdr) {
|
||||
clone := cdr
|
||||
cdrs = append(cdrs, &clone)
|
||||
}
|
||||
return cdrs, 0, nil
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ import (
|
||||
"io/ioutil"
|
||||
"strconv"
|
||||
"strings"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
@@ -644,22 +645,45 @@ func (self *SQLStorage) GetStoredCdrs(qryFltr *utils.CdrsFilter) ([]*StoredCdr,
|
||||
// Select string
|
||||
var selectStr string
|
||||
if qryFltr.FilterOnRated { // We use different tables to query account data in case of derived
|
||||
selectStr = fmt.Sprintf("%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.pdd,%s.supplier,%s.disconnect_cause,%s.extra_fields,%s.runid,%s.cost,%s.tor,%s.direction,%s.tenant,%s.category,%s.account,%s.subject,%s.destination,%s.cost,%s.timespans",
|
||||
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_RATED_CDRS, utils.TBL_RATED_CDRS, utils.TBL_RATED_CDRS,
|
||||
utils.TBL_CDRS_EXTRA, utils.TBL_RATED_CDRS, utils.TBL_RATED_CDRS, utils.TBL_COST_DETAILS, utils.TBL_COST_DETAILS, utils.TBL_COST_DETAILS, utils.TBL_COST_DETAILS, utils.TBL_COST_DETAILS,
|
||||
utils.TBL_COST_DETAILS, utils.TBL_COST_DETAILS, utils.TBL_COST_DETAILS, utils.TBL_COST_DETAILS)
|
||||
selectTmpl := template.Must(template.New("select").Parse("{{.Pr}}.cgrid,{{.Pr}}.id,{{.Pr}}.tor,{{.Pr}}.accid,{{.Pr}}.cdrhost,{{.Pr}}.cdrsource,{{.Rc}}.reqtype,{{.Rc}}.direction,{{.Rc}}.tenant,{{.Rc}}.category,{{.Rc}}.account,{{.Rc}}.subject,{{.Rc}}.destination,{{.Rc}}.setup_time,{{.Rc}}.answer_time,{{.Rc}}.usage,{{.Rc}}.pdd,{{.Rc}}.supplier,{{.Ex}}.disconnect_cause,{{.Rc}}.extra_fields,{{.Rc}}.runid,{{.Cd}}.cost,{{.Cd}}.tor,{{.Cd}}.direction,{{.Cd}}.tenant,{{.Cd}}.category,{{.Cd}}.account,{{.Cd}}.subject,{{.Cd}}.destination,{{.Cd}}.cost,{{.Cd}}.timespans"))
|
||||
var selectBuf bytes.Buffer
|
||||
selectTmpl.Execute(&selectBuf, &struct {
|
||||
Pr string
|
||||
Ex string
|
||||
Rc string
|
||||
Cd string
|
||||
}{utils.TBL_CDRS_PRIMARY,
|
||||
utils.TBL_CDRS_EXTRA,
|
||||
utils.TBL_RATED_CDRS,
|
||||
utils.TBL_COST_DETAILS})
|
||||
selectStr = selectBuf.String()
|
||||
} else {
|
||||
selectStr = fmt.Sprintf("%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.pdd,%s.supplier,%s.disconnect_cause,%s.extra_fields,%s.runid,%s.cost,%s.tor,%s.direction,%s.tenant,%s.category,%s.account,%s.subject,%s.destination,%s.cost,%s.timespans",
|
||||
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_PRIMARY, utils.TBL_CDRS_PRIMARY, utils.TBL_CDRS_PRIMARY,
|
||||
utils.TBL_CDRS_EXTRA, utils.TBL_RATED_CDRS, utils.TBL_RATED_CDRS, utils.TBL_COST_DETAILS, utils.TBL_COST_DETAILS, utils.TBL_COST_DETAILS, utils.TBL_COST_DETAILS, utils.TBL_COST_DETAILS,
|
||||
utils.TBL_COST_DETAILS, utils.TBL_COST_DETAILS, utils.TBL_COST_DETAILS, utils.TBL_COST_DETAILS)
|
||||
|
||||
selectTmpl := template.Must(template.New("select").Parse("{{.Pr}}.cgrid,{{.Pr}}.id,{{.Pr}}.tor,{{.Pr}}.accid,{{.Pr}}.cdrhost,{{.Pr}}.cdrsource,{{.Pr}}.reqtype,{{.Pr}}.direction,{{.Pr}}.tenant,{{.Pr}}.category,{{.Pr}}.account,{{.Pr}}.subject,{{.Pr}}.destination,{{.Pr}}.setup_time,{{.Pr}}.answer_time,{{.Pr}}.usage,{{.Pr}}.pdd,{{.Pr}}.supplier,{{.Pr}}.disconnect_cause,{{.Ex}}.extra_fields,{{.Rc}}.runid,{{.Rc}}.cost,{{.Cd}}.tor,{{.Cd}}.direction,{{.Cd}}.tenant,{{.Cd}}.category,{{.Cd}}.account,{{.Cd}}.subject,{{.Cd}}.destination,{{.Cd}}.cost,{{.Cd}}.timespans"))
|
||||
var selectBuf bytes.Buffer
|
||||
selectTmpl.Execute(&selectBuf, &struct {
|
||||
Pr string
|
||||
Ex string
|
||||
Rc string
|
||||
Cd string
|
||||
}{utils.TBL_CDRS_PRIMARY,
|
||||
utils.TBL_CDRS_EXTRA,
|
||||
utils.TBL_RATED_CDRS,
|
||||
utils.TBL_COST_DETAILS})
|
||||
selectStr = selectBuf.String()
|
||||
}
|
||||
// Join string
|
||||
joinStr := fmt.Sprintf("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_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)
|
||||
selectTmpl := template.Must(template.New("join").Parse("LEFT JOIN {{.Ex}} ON {{.Pr}}.cgrid={{.Ex}}.cgrid LEFT JOIN {{.Rc}} ON {{Pr.}}.cgrid={{.Rc}}.cgrid LEFT JOIN {{.Cd}} ON {{.Rc}}.cgrid={{.Cd}}.cgrid AND {{.Rc}}.runid={{.Cd}}.runid"))
|
||||
var joinBuf bytes.Buffer
|
||||
selectTmpl.Execute(&joinBuf, &struct {
|
||||
Pr string
|
||||
Ex string
|
||||
Rc string
|
||||
Cd string
|
||||
}{utils.TBL_CDRS_PRIMARY,
|
||||
utils.TBL_CDRS_EXTRA,
|
||||
utils.TBL_RATED_CDRS,
|
||||
utils.TBL_COST_DETAILS})
|
||||
joinStr := joinBuf.String()
|
||||
q := self.db.Table(utils.TBL_CDRS_PRIMARY).Select(selectStr).Joins(joinStr)
|
||||
if qryFltr.Unscoped {
|
||||
q = q.Unscoped()
|
||||
@@ -669,6 +693,10 @@ func (self *SQLStorage) GetStoredCdrs(qryFltr *utils.CdrsFilter) ([]*StoredCdr,
|
||||
q = q.Where(fmt.Sprintf("(%s.deleted_at IS NULL OR %s.deleted_at <= '0001-01-02')", tblName, tblName)) // Soft deletes
|
||||
}
|
||||
}
|
||||
tblName := utils.TBL_CDRS_PRIMARY
|
||||
if qryFltr.FilterOnRated {
|
||||
tblName = utils.TBL_RATED_CDRS
|
||||
}
|
||||
// Add filters, use in to replace the high number of ORs
|
||||
if len(qryFltr.CgrIds) != 0 {
|
||||
q = q.Where(utils.TBL_CDRS_PRIMARY+".cgrid in (?)", qryFltr.CgrIds)
|
||||
@@ -701,94 +729,42 @@ func (self *SQLStorage) GetStoredCdrs(qryFltr *utils.CdrsFilter) ([]*StoredCdr,
|
||||
q = q.Where(utils.TBL_CDRS_PRIMARY+".cdrsource not in (?)", qryFltr.NotCdrSources)
|
||||
}
|
||||
if len(qryFltr.ReqTypes) != 0 {
|
||||
tblName := utils.TBL_CDRS_PRIMARY
|
||||
if qryFltr.FilterOnRated {
|
||||
tblName = utils.TBL_RATED_CDRS
|
||||
}
|
||||
q = q.Where(tblName+".reqtype in (?)", qryFltr.ReqTypes)
|
||||
}
|
||||
if len(qryFltr.NotReqTypes) != 0 {
|
||||
tblName := utils.TBL_CDRS_PRIMARY
|
||||
if qryFltr.FilterOnRated {
|
||||
tblName = utils.TBL_RATED_CDRS
|
||||
}
|
||||
q = q.Where(tblName+".reqtype not in (?)", qryFltr.NotReqTypes)
|
||||
}
|
||||
if len(qryFltr.Directions) != 0 {
|
||||
tblName := utils.TBL_CDRS_PRIMARY
|
||||
if qryFltr.FilterOnRated {
|
||||
tblName = utils.TBL_RATED_CDRS
|
||||
}
|
||||
q = q.Where(tblName+".direction in (?)", qryFltr.Directions)
|
||||
}
|
||||
if len(qryFltr.NotDirections) != 0 {
|
||||
tblName := utils.TBL_CDRS_PRIMARY
|
||||
if qryFltr.FilterOnRated {
|
||||
tblName = utils.TBL_RATED_CDRS
|
||||
}
|
||||
q = q.Where(tblName+".direction not in (?)", qryFltr.NotDirections)
|
||||
}
|
||||
if len(qryFltr.Tenants) != 0 {
|
||||
tblName := utils.TBL_CDRS_PRIMARY
|
||||
if qryFltr.FilterOnRated {
|
||||
tblName = utils.TBL_RATED_CDRS
|
||||
}
|
||||
q = q.Where(tblName+".tenant in (?)", qryFltr.Tenants)
|
||||
}
|
||||
if len(qryFltr.NotTenants) != 0 {
|
||||
tblName := utils.TBL_CDRS_PRIMARY
|
||||
if qryFltr.FilterOnRated {
|
||||
tblName = utils.TBL_RATED_CDRS
|
||||
}
|
||||
q = q.Where(tblName+".tenant not in (?)", qryFltr.NotTenants)
|
||||
}
|
||||
if len(qryFltr.Categories) != 0 {
|
||||
tblName := utils.TBL_CDRS_PRIMARY
|
||||
if qryFltr.FilterOnRated {
|
||||
tblName = utils.TBL_RATED_CDRS
|
||||
}
|
||||
q = q.Where(tblName+".category in (?)", qryFltr.Categories)
|
||||
}
|
||||
if len(qryFltr.NotCategories) != 0 {
|
||||
tblName := utils.TBL_CDRS_PRIMARY
|
||||
if qryFltr.FilterOnRated {
|
||||
tblName = utils.TBL_RATED_CDRS
|
||||
}
|
||||
q = q.Where(tblName+".category not in (?)", qryFltr.NotCategories)
|
||||
}
|
||||
if len(qryFltr.Accounts) != 0 {
|
||||
tblName := utils.TBL_CDRS_PRIMARY
|
||||
if qryFltr.FilterOnRated {
|
||||
tblName = utils.TBL_RATED_CDRS
|
||||
}
|
||||
q = q.Where(tblName+".account in (?)", qryFltr.Accounts)
|
||||
}
|
||||
if len(qryFltr.NotAccounts) != 0 {
|
||||
tblName := utils.TBL_CDRS_PRIMARY
|
||||
if qryFltr.FilterOnRated {
|
||||
tblName = utils.TBL_RATED_CDRS
|
||||
}
|
||||
q = q.Where(tblName+".account not in (?)", qryFltr.NotAccounts)
|
||||
}
|
||||
if len(qryFltr.Subjects) != 0 {
|
||||
tblName := utils.TBL_CDRS_PRIMARY
|
||||
if qryFltr.FilterOnRated {
|
||||
tblName = utils.TBL_RATED_CDRS
|
||||
}
|
||||
q = q.Where(tblName+".subject in (?)", qryFltr.Subjects)
|
||||
}
|
||||
if len(qryFltr.NotSubjects) != 0 {
|
||||
tblName := utils.TBL_CDRS_PRIMARY
|
||||
if qryFltr.FilterOnRated {
|
||||
tblName = utils.TBL_RATED_CDRS
|
||||
}
|
||||
q = q.Where(tblName+".subject not in (?)", qryFltr.NotSubjects)
|
||||
}
|
||||
if len(qryFltr.DestPrefixes) != 0 { // A bit ugly but still more readable than scopes provided by gorm
|
||||
tblName := utils.TBL_CDRS_PRIMARY
|
||||
if qryFltr.FilterOnRated {
|
||||
tblName = utils.TBL_RATED_CDRS
|
||||
}
|
||||
qIds := bytes.NewBufferString("(")
|
||||
for idx, destPrefix := range qryFltr.DestPrefixes {
|
||||
if idx != 0 {
|
||||
@@ -800,10 +776,6 @@ func (self *SQLStorage) GetStoredCdrs(qryFltr *utils.CdrsFilter) ([]*StoredCdr,
|
||||
q = q.Where(qIds.String())
|
||||
}
|
||||
if len(qryFltr.NotDestPrefixes) != 0 { // A bit ugly but still more readable than scopes provided by gorm
|
||||
tblName := utils.TBL_CDRS_PRIMARY
|
||||
if qryFltr.FilterOnRated {
|
||||
tblName = utils.TBL_RATED_CDRS
|
||||
}
|
||||
qIds := bytes.NewBufferString("(")
|
||||
for idx, destPrefix := range qryFltr.NotDestPrefixes {
|
||||
if idx != 0 {
|
||||
@@ -815,31 +787,15 @@ func (self *SQLStorage) GetStoredCdrs(qryFltr *utils.CdrsFilter) ([]*StoredCdr,
|
||||
q = q.Where(qIds.String())
|
||||
}
|
||||
if len(qryFltr.Suppliers) != 0 {
|
||||
tblName := utils.TBL_CDRS_PRIMARY
|
||||
if qryFltr.FilterOnRated {
|
||||
tblName = utils.TBL_RATED_CDRS
|
||||
}
|
||||
q = q.Where(tblName+".supplier in (?)", qryFltr.Subjects)
|
||||
}
|
||||
if len(qryFltr.NotSuppliers) != 0 {
|
||||
tblName := utils.TBL_CDRS_PRIMARY
|
||||
if qryFltr.FilterOnRated {
|
||||
tblName = utils.TBL_RATED_CDRS
|
||||
}
|
||||
q = q.Where(tblName+".supplier not in (?)", qryFltr.NotSubjects)
|
||||
}
|
||||
if len(qryFltr.DisconnectCauses) != 0 {
|
||||
tblName := utils.TBL_CDRS_PRIMARY
|
||||
if qryFltr.FilterOnRated {
|
||||
tblName = utils.TBL_RATED_CDRS
|
||||
}
|
||||
q = q.Where(tblName+".disconnect_cause in (?)", qryFltr.DisconnectCauses)
|
||||
}
|
||||
if len(qryFltr.NotDisconnectCauses) != 0 {
|
||||
tblName := utils.TBL_CDRS_PRIMARY
|
||||
if qryFltr.FilterOnRated {
|
||||
tblName = utils.TBL_RATED_CDRS
|
||||
}
|
||||
q = q.Where(tblName+".disconnect_cause not in (?)", qryFltr.NotDisconnectCauses)
|
||||
}
|
||||
if len(qryFltr.RatedAccounts) != 0 {
|
||||
@@ -893,87 +849,39 @@ func (self *SQLStorage) GetStoredCdrs(qryFltr *utils.CdrsFilter) ([]*StoredCdr,
|
||||
q = q.Where(utils.TBL_CDRS_PRIMARY+".id < ?", qryFltr.OrderIdEnd)
|
||||
}
|
||||
if qryFltr.SetupTimeStart != nil {
|
||||
tblName := utils.TBL_CDRS_PRIMARY
|
||||
if qryFltr.FilterOnRated {
|
||||
tblName = utils.TBL_RATED_CDRS
|
||||
}
|
||||
q = q.Where(tblName+".setup_time >= ?", qryFltr.SetupTimeStart)
|
||||
}
|
||||
if qryFltr.SetupTimeEnd != nil {
|
||||
tblName := utils.TBL_CDRS_PRIMARY
|
||||
if qryFltr.FilterOnRated {
|
||||
tblName = utils.TBL_RATED_CDRS
|
||||
}
|
||||
q = q.Where(tblName+".setup_time < ?", qryFltr.SetupTimeEnd)
|
||||
}
|
||||
if qryFltr.AnswerTimeStart != nil && !qryFltr.AnswerTimeStart.IsZero() { // With IsZero we keep backwards compatible with ApierV1
|
||||
tblName := utils.TBL_CDRS_PRIMARY
|
||||
if qryFltr.FilterOnRated {
|
||||
tblName = utils.TBL_RATED_CDRS
|
||||
}
|
||||
q = q.Where(tblName+".answer_time >= ?", qryFltr.AnswerTimeStart)
|
||||
}
|
||||
if qryFltr.AnswerTimeEnd != nil && !qryFltr.AnswerTimeEnd.IsZero() {
|
||||
tblName := utils.TBL_CDRS_PRIMARY
|
||||
if qryFltr.FilterOnRated {
|
||||
tblName = utils.TBL_RATED_CDRS
|
||||
}
|
||||
q = q.Where(tblName+".answer_time < ?", qryFltr.AnswerTimeEnd)
|
||||
}
|
||||
if qryFltr.CreatedAtStart != nil && !qryFltr.CreatedAtStart.IsZero() { // With IsZero we keep backwards compatible with ApierV1
|
||||
tblName := utils.TBL_CDRS_PRIMARY
|
||||
if qryFltr.FilterOnRated {
|
||||
tblName = utils.TBL_RATED_CDRS
|
||||
}
|
||||
q = q.Where(tblName+".created_at >= ?", qryFltr.CreatedAtStart)
|
||||
}
|
||||
if qryFltr.CreatedAtEnd != nil && !qryFltr.CreatedAtEnd.IsZero() {
|
||||
tblName := utils.TBL_CDRS_PRIMARY
|
||||
if qryFltr.FilterOnRated {
|
||||
tblName = utils.TBL_RATED_CDRS
|
||||
}
|
||||
q = q.Where(tblName+".created_at < ?", qryFltr.CreatedAtEnd)
|
||||
}
|
||||
if qryFltr.UpdatedAtStart != nil && !qryFltr.UpdatedAtStart.IsZero() { // With IsZero we keep backwards compatible with ApierV1
|
||||
tblName := utils.TBL_CDRS_PRIMARY
|
||||
if qryFltr.FilterOnRated {
|
||||
tblName = utils.TBL_RATED_CDRS
|
||||
}
|
||||
q = q.Where(tblName+".updated_at >= ?", qryFltr.UpdatedAtStart)
|
||||
}
|
||||
if qryFltr.UpdatedAtEnd != nil && !qryFltr.UpdatedAtEnd.IsZero() {
|
||||
tblName := utils.TBL_CDRS_PRIMARY
|
||||
if qryFltr.FilterOnRated {
|
||||
tblName = utils.TBL_RATED_CDRS
|
||||
}
|
||||
q = q.Where(tblName+".updated_at < ?", qryFltr.UpdatedAtEnd)
|
||||
}
|
||||
if qryFltr.MinUsage != nil {
|
||||
tblName := utils.TBL_CDRS_PRIMARY
|
||||
if qryFltr.FilterOnRated {
|
||||
tblName = utils.TBL_RATED_CDRS
|
||||
}
|
||||
q = q.Where(tblName+".usage >= ?", qryFltr.MinUsage)
|
||||
}
|
||||
if qryFltr.MaxUsage != nil {
|
||||
tblName := utils.TBL_CDRS_PRIMARY
|
||||
if qryFltr.FilterOnRated {
|
||||
tblName = utils.TBL_RATED_CDRS
|
||||
}
|
||||
q = q.Where(tblName+".usage < ?", qryFltr.MaxUsage)
|
||||
}
|
||||
if qryFltr.MinPdd != nil {
|
||||
tblName := utils.TBL_CDRS_PRIMARY
|
||||
if qryFltr.FilterOnRated {
|
||||
tblName = utils.TBL_RATED_CDRS
|
||||
}
|
||||
q = q.Where(tblName+".pdd >= ?", qryFltr.MinPdd)
|
||||
}
|
||||
if qryFltr.MaxPdd != nil {
|
||||
tblName := utils.TBL_CDRS_PRIMARY
|
||||
if qryFltr.FilterOnRated {
|
||||
tblName = utils.TBL_RATED_CDRS
|
||||
}
|
||||
q = q.Where(tblName+".pdd < ?", qryFltr.MaxPdd)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user