mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 10:06:24 +05:00
Upgrade MongoDB driver to v1.12
- Set (but comment) serverAPI options (currently distinct api and create.size BSON field are deprecated + possible others that are untested) - Remove the custom time decoder used for mongo BSON datetime values. The custom decoder was only converting these values into UTC and was not any different from the default time.Time decoder in the MongoDB driver, which also handles BSON string, int64, and document values. - Implement 'buildURL' function to connect to mongo (can also be used for mysql and postgres) - Update function names, variable names, and comments for clarity - Replace 'bsonx.Regex' with the Regex primitive for v1.12 compatibility - Use simple concatenation instead of Sprintf - Declare 'decimalType' locally, replace global 'decimalType' - Simplify several functions without altering functionality - Converting directly from a D to an M is deprecated. We are now decoding directly in a M. - Used errors.As and errors.Is for proper error comparison and assertion - Revised sloppy reassignments and added missing error checks
This commit is contained in:
committed by
Dan Christian Bogos
parent
d1d43913c8
commit
38578524da
File diff suppressed because it is too large
Load Diff
@@ -20,6 +20,7 @@ package engine
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
@@ -28,9 +29,9 @@ import (
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"go.mongodb.org/mongo-driver/x/bsonx"
|
||||
)
|
||||
|
||||
func (ms *MongoStorage) GetTpIds(colName string) (tpids []string, err error) {
|
||||
@@ -79,7 +80,7 @@ func (ms *MongoStorage) GetTpIds(colName string) (tpids []string, err error) {
|
||||
return tpids, nil
|
||||
}
|
||||
|
||||
func (ms *MongoStorage) GetTpTableIds(tpid, table string, distinct utils.TPDistinctIds,
|
||||
func (ms *MongoStorage) GetTpTableIds(tpid, table string, distinctIDs utils.TPDistinctIds,
|
||||
filter map[string]string, pag *utils.PaginatorWithSearch) ([]string, error) {
|
||||
findMap := bson.M{}
|
||||
if tpid != "" {
|
||||
@@ -95,11 +96,16 @@ func (ms *MongoStorage) GetTpTableIds(tpid, table string, distinct utils.TPDisti
|
||||
if pag != nil {
|
||||
if pag.Search != "" {
|
||||
var searchItems []bson.M
|
||||
for _, d := range distinct {
|
||||
searchItems = append(searchItems, bson.M{d: bsonx.Regex(".*"+regexp.QuoteMeta(pag.Search)+".*", "")})
|
||||
for _, distinctID := range distinctIDs {
|
||||
searchItems = append(searchItems,
|
||||
bson.M{
|
||||
distinctID: primitive.Regex{
|
||||
Pattern: ".*" + regexp.QuoteMeta(pag.Search) + ".*",
|
||||
},
|
||||
},
|
||||
)
|
||||
}
|
||||
// findMap["$and"] = []bson.M{{"$or": searchItems}} //before
|
||||
findMap["$or"] = searchItems // after
|
||||
findMap["$or"] = searchItems
|
||||
}
|
||||
if pag.Paginator != nil {
|
||||
if pag.Limit != nil {
|
||||
@@ -112,32 +118,31 @@ func (ms *MongoStorage) GetTpTableIds(tpid, table string, distinct utils.TPDisti
|
||||
}
|
||||
|
||||
selectors := bson.M{"_id": 0}
|
||||
for i, d := range distinct {
|
||||
if d == "tag" { // convert the tag used in SQL into id used here
|
||||
distinct[i] = "id"
|
||||
for i, distinctID := range distinctIDs {
|
||||
if distinctID == "tag" { // convert the tag used in SQL into id used here
|
||||
distinctIDs[i] = "id"
|
||||
}
|
||||
selectors[distinct[i]] = 1
|
||||
selectors[distinctIDs[i]] = 1
|
||||
}
|
||||
fop.SetProjection(selectors)
|
||||
|
||||
distinctIds := make(utils.StringMap)
|
||||
if err := ms.query(func(sctx mongo.SessionContext) (err error) {
|
||||
cur, err := ms.getCol(table).Find(sctx, findMap, fop)
|
||||
if err != nil {
|
||||
return err
|
||||
err := ms.query(func(sctx mongo.SessionContext) (qryErr error) {
|
||||
cur, qryErr := ms.getCol(table).Find(sctx, findMap, fop)
|
||||
if qryErr != nil {
|
||||
return qryErr
|
||||
}
|
||||
for cur.Next(sctx) {
|
||||
var elem bson.D
|
||||
err := cur.Decode(&elem)
|
||||
var item bson.M
|
||||
err := cur.Decode(&item)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
item := elem.Map()
|
||||
|
||||
var id string
|
||||
last := len(distinct) - 1
|
||||
for i, d := range distinct {
|
||||
if distinctValue, ok := item[d]; ok {
|
||||
last := len(distinctIDs) - 1
|
||||
for i, distinctID := range distinctIDs {
|
||||
if distinctValue, ok := item[distinctID]; ok {
|
||||
id += distinctValue.(string)
|
||||
}
|
||||
if i < last {
|
||||
@@ -147,7 +152,8 @@ func (ms *MongoStorage) GetTpTableIds(tpid, table string, distinct utils.TPDisti
|
||||
distinctIds[id] = true
|
||||
}
|
||||
return cur.Close(sctx)
|
||||
}); err != nil {
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return distinctIds.Slice(), nil
|
||||
@@ -158,26 +164,26 @@ func (ms *MongoStorage) GetTPTimings(tpid, id string) ([]*utils.ApierTPTiming, e
|
||||
if id != "" {
|
||||
filter["id"] = id
|
||||
}
|
||||
var results []*utils.ApierTPTiming
|
||||
err := ms.query(func(sctx mongo.SessionContext) (err error) {
|
||||
cur, err := ms.getCol(utils.TBLTPTimings).Find(sctx, filter)
|
||||
if err != nil {
|
||||
return err
|
||||
var tpTimings []*utils.ApierTPTiming
|
||||
err := ms.query(func(sctx mongo.SessionContext) (qryErr error) {
|
||||
cur, qryErr := ms.getCol(utils.TBLTPTimings).Find(sctx, filter)
|
||||
if qryErr != nil {
|
||||
return qryErr
|
||||
}
|
||||
for cur.Next(sctx) {
|
||||
var el utils.ApierTPTiming
|
||||
err := cur.Decode(&el)
|
||||
if err != nil {
|
||||
return err
|
||||
qryErr = cur.Decode(&el)
|
||||
if qryErr != nil {
|
||||
return qryErr
|
||||
}
|
||||
results = append(results, &el)
|
||||
tpTimings = append(tpTimings, &el)
|
||||
}
|
||||
if len(results) == 0 {
|
||||
if len(tpTimings) == 0 {
|
||||
return utils.ErrNotFound
|
||||
}
|
||||
return cur.Close(sctx)
|
||||
})
|
||||
return results, err
|
||||
return tpTimings, err
|
||||
}
|
||||
|
||||
func (ms *MongoStorage) GetTPDestinations(tpid, id string) ([]*utils.TPDestination, error) {
|
||||
@@ -185,26 +191,26 @@ func (ms *MongoStorage) GetTPDestinations(tpid, id string) ([]*utils.TPDestinati
|
||||
if id != "" {
|
||||
filter["id"] = id
|
||||
}
|
||||
var results []*utils.TPDestination
|
||||
err := ms.query(func(sctx mongo.SessionContext) (err error) {
|
||||
cur, err := ms.getCol(utils.TBLTPDestinations).Find(sctx, filter)
|
||||
if err != nil {
|
||||
return err
|
||||
var tpDestinations []*utils.TPDestination
|
||||
err := ms.query(func(sctx mongo.SessionContext) (qryErr error) {
|
||||
cur, qryErr := ms.getCol(utils.TBLTPDestinations).Find(sctx, filter)
|
||||
if qryErr != nil {
|
||||
return qryErr
|
||||
}
|
||||
for cur.Next(sctx) {
|
||||
var el utils.TPDestination
|
||||
err := cur.Decode(&el)
|
||||
if err != nil {
|
||||
return err
|
||||
qryErr = cur.Decode(&el)
|
||||
if qryErr != nil {
|
||||
return qryErr
|
||||
}
|
||||
results = append(results, &el)
|
||||
tpDestinations = append(tpDestinations, &el)
|
||||
}
|
||||
if len(results) == 0 {
|
||||
if len(tpDestinations) == 0 {
|
||||
return utils.ErrNotFound
|
||||
}
|
||||
return cur.Close(sctx)
|
||||
})
|
||||
return results, err
|
||||
return tpDestinations, err
|
||||
}
|
||||
|
||||
func (ms *MongoStorage) GetTPRates(tpid, id string) ([]*utils.TPRateRALs, error) {
|
||||
@@ -212,11 +218,11 @@ func (ms *MongoStorage) GetTPRates(tpid, id string) ([]*utils.TPRateRALs, error)
|
||||
if id != "" {
|
||||
filter["id"] = id
|
||||
}
|
||||
var results []*utils.TPRateRALs
|
||||
err := ms.query(func(sctx mongo.SessionContext) (err error) {
|
||||
cur, err := ms.getCol(utils.TBLTPRates).Find(sctx, filter)
|
||||
if err != nil {
|
||||
return err
|
||||
var tpRates []*utils.TPRateRALs
|
||||
err := ms.query(func(sctx mongo.SessionContext) (qryErr error) {
|
||||
cur, qryErr := ms.getCol(utils.TBLTPRates).Find(sctx, filter)
|
||||
if qryErr != nil {
|
||||
return qryErr
|
||||
}
|
||||
for cur.Next(sctx) {
|
||||
var el utils.TPRateRALs
|
||||
@@ -227,14 +233,14 @@ func (ms *MongoStorage) GetTPRates(tpid, id string) ([]*utils.TPRateRALs, error)
|
||||
for _, rs := range el.RateSlots {
|
||||
rs.SetDurations()
|
||||
}
|
||||
results = append(results, &el)
|
||||
tpRates = append(tpRates, &el)
|
||||
}
|
||||
if len(results) == 0 {
|
||||
if len(tpRates) == 0 {
|
||||
return utils.ErrNotFound
|
||||
}
|
||||
return cur.Close(sctx)
|
||||
})
|
||||
return results, err
|
||||
return tpRates, err
|
||||
}
|
||||
|
||||
func (ms *MongoStorage) GetTPDestinationRates(tpid, id string, pag *utils.Paginator) ([]*utils.TPDestinationRate, error) {
|
||||
@@ -242,7 +248,7 @@ func (ms *MongoStorage) GetTPDestinationRates(tpid, id string, pag *utils.Pagina
|
||||
if id != "" {
|
||||
filter["id"] = id
|
||||
}
|
||||
var results []*utils.TPDestinationRate
|
||||
var tpDstRates []*utils.TPDestinationRate
|
||||
fop := options.Find()
|
||||
if pag != nil {
|
||||
if pag.Limit != nil {
|
||||
@@ -252,25 +258,25 @@ func (ms *MongoStorage) GetTPDestinationRates(tpid, id string, pag *utils.Pagina
|
||||
fop = fop.SetSkip(int64(*pag.Offset))
|
||||
}
|
||||
}
|
||||
err := ms.query(func(sctx mongo.SessionContext) (err error) {
|
||||
cur, err := ms.getCol(utils.TBLTPDestinationRates).Find(sctx, filter, fop)
|
||||
if err != nil {
|
||||
return err
|
||||
err := ms.query(func(sctx mongo.SessionContext) (qryErr error) {
|
||||
cur, qryErr := ms.getCol(utils.TBLTPDestinationRates).Find(sctx, filter, fop)
|
||||
if qryErr != nil {
|
||||
return qryErr
|
||||
}
|
||||
for cur.Next(sctx) {
|
||||
var el utils.TPDestinationRate
|
||||
err := cur.Decode(&el)
|
||||
if err != nil {
|
||||
return err
|
||||
qryErr = cur.Decode(&el)
|
||||
if qryErr != nil {
|
||||
return qryErr
|
||||
}
|
||||
results = append(results, &el)
|
||||
tpDstRates = append(tpDstRates, &el)
|
||||
}
|
||||
if len(results) == 0 {
|
||||
if len(tpDstRates) == 0 {
|
||||
return utils.ErrNotFound
|
||||
}
|
||||
return cur.Close(sctx)
|
||||
})
|
||||
return results, err
|
||||
return tpDstRates, err
|
||||
}
|
||||
|
||||
func (ms *MongoStorage) GetTPRatingPlans(tpid, id string, pag *utils.Paginator) ([]*utils.TPRatingPlan, error) {
|
||||
@@ -278,7 +284,7 @@ func (ms *MongoStorage) GetTPRatingPlans(tpid, id string, pag *utils.Paginator)
|
||||
if id != "" {
|
||||
filter["id"] = id
|
||||
}
|
||||
var results []*utils.TPRatingPlan
|
||||
var tpRatingPlans []*utils.TPRatingPlan
|
||||
fop := options.Find()
|
||||
if pag != nil {
|
||||
if pag.Limit != nil {
|
||||
@@ -288,25 +294,25 @@ func (ms *MongoStorage) GetTPRatingPlans(tpid, id string, pag *utils.Paginator)
|
||||
fop = fop.SetSkip(int64(*pag.Offset))
|
||||
}
|
||||
}
|
||||
err := ms.query(func(sctx mongo.SessionContext) (err error) {
|
||||
cur, err := ms.getCol(utils.TBLTPRatingPlans).Find(sctx, filter, fop)
|
||||
if err != nil {
|
||||
return err
|
||||
err := ms.query(func(sctx mongo.SessionContext) (qryErr error) {
|
||||
cur, qryErr := ms.getCol(utils.TBLTPRatingPlans).Find(sctx, filter, fop)
|
||||
if qryErr != nil {
|
||||
return qryErr
|
||||
}
|
||||
for cur.Next(sctx) {
|
||||
var el utils.TPRatingPlan
|
||||
err := cur.Decode(&el)
|
||||
if err != nil {
|
||||
return err
|
||||
qryErr = cur.Decode(&el)
|
||||
if qryErr != nil {
|
||||
return qryErr
|
||||
}
|
||||
results = append(results, &el)
|
||||
tpRatingPlans = append(tpRatingPlans, &el)
|
||||
}
|
||||
if len(results) == 0 {
|
||||
if len(tpRatingPlans) == 0 {
|
||||
return utils.ErrNotFound
|
||||
}
|
||||
return cur.Close(sctx)
|
||||
})
|
||||
return results, err
|
||||
return tpRatingPlans, err
|
||||
}
|
||||
|
||||
func (ms *MongoStorage) GetTPRatingProfiles(tp *utils.TPRatingProfile) ([]*utils.TPRatingProfile, error) {
|
||||
@@ -323,26 +329,26 @@ func (ms *MongoStorage) GetTPRatingProfiles(tp *utils.TPRatingProfile) ([]*utils
|
||||
if tp.LoadId != "" {
|
||||
filter["loadid"] = tp.LoadId
|
||||
}
|
||||
var results []*utils.TPRatingProfile
|
||||
err := ms.query(func(sctx mongo.SessionContext) (err error) {
|
||||
cur, err := ms.getCol(utils.TBLTPRatingProfiles).Find(sctx, filter)
|
||||
if err != nil {
|
||||
return err
|
||||
var tpRatingProfiles []*utils.TPRatingProfile
|
||||
err := ms.query(func(sctx mongo.SessionContext) (qryErr error) {
|
||||
cur, qryErr := ms.getCol(utils.TBLTPRatingProfiles).Find(sctx, filter)
|
||||
if qryErr != nil {
|
||||
return qryErr
|
||||
}
|
||||
for cur.Next(sctx) {
|
||||
var el utils.TPRatingProfile
|
||||
err := cur.Decode(&el)
|
||||
if err != nil {
|
||||
return err
|
||||
qryErr = cur.Decode(&el)
|
||||
if qryErr != nil {
|
||||
return qryErr
|
||||
}
|
||||
results = append(results, &el)
|
||||
tpRatingProfiles = append(tpRatingProfiles, &el)
|
||||
}
|
||||
if len(results) == 0 {
|
||||
if len(tpRatingProfiles) == 0 {
|
||||
return utils.ErrNotFound
|
||||
}
|
||||
return cur.Close(sctx)
|
||||
})
|
||||
return results, err
|
||||
return tpRatingProfiles, err
|
||||
}
|
||||
|
||||
func (ms *MongoStorage) GetTPSharedGroups(tpid, id string) ([]*utils.TPSharedGroups, error) {
|
||||
@@ -551,7 +557,7 @@ func (ms *MongoStorage) GetTPAccountActions(tp *utils.TPAccountActions) ([]*util
|
||||
}
|
||||
|
||||
func (ms *MongoStorage) RemTpData(table, tpid string, args map[string]string) error {
|
||||
if len(table) == 0 { // Remove tpid out of all tables
|
||||
if table == utils.EmptyString { // Remove tpid out of all tables
|
||||
return ms.query(func(sctx mongo.SessionContext) error {
|
||||
col, err := ms.DB().ListCollections(sctx, bson.D{}, options.ListCollections().SetNameOnly(true))
|
||||
if err != nil {
|
||||
@@ -890,7 +896,9 @@ func (ms *MongoStorage) GetSMCosts(cgrid, runid, originHost, originIDPrefix stri
|
||||
filter[OriginHostLow] = originHost
|
||||
}
|
||||
if originIDPrefix != "" {
|
||||
filter[OriginIDLow] = bsonx.Regex(fmt.Sprintf("^%s", originIDPrefix), "")
|
||||
filter[OriginIDLow] = primitive.Regex{
|
||||
Pattern: "^" + originIDPrefix,
|
||||
}
|
||||
}
|
||||
err = ms.query(func(sctx mongo.SessionContext) (err error) {
|
||||
cur, err := ms.getCol(utils.SessionCostsTBL).Find(sctx, filter)
|
||||
@@ -934,7 +942,7 @@ func (ms *MongoStorage) RemoveSMCosts(qryFltr *utils.SMCostFilter) error {
|
||||
|
||||
func (ms *MongoStorage) SetCDR(cdr *CDR, allowUpdate bool) error {
|
||||
if cdr.OrderID == 0 {
|
||||
cdr.OrderID = ms.cnter.Next()
|
||||
cdr.OrderID = ms.counter.Next()
|
||||
}
|
||||
return ms.query(func(sctx mongo.SessionContext) (err error) {
|
||||
if allowUpdate {
|
||||
@@ -984,20 +992,22 @@ func (ms *MongoStorage) cleanEmptyFilters(filters bson.M) {
|
||||
}
|
||||
|
||||
// _, err := col(ColCDRs).UpdateAll(bson.M{CGRIDLow: bson.M{"$in": cgrIds}}, bson.M{"$set": bson.M{"deleted_at": time.Now()}})
|
||||
func (ms *MongoStorage) GetCDRs(qryFltr *utils.CDRsFilter, remove bool) ([]*CDR, int64, error) {
|
||||
func (ms *MongoStorage) GetCDRs(qryFltr *utils.CDRsFilter, remove bool) (cdrs []*CDR, n int64, err error) {
|
||||
var minUsage, maxUsage *time.Duration
|
||||
if len(qryFltr.MinUsage) != 0 {
|
||||
if parsed, err := utils.ParseDurationWithNanosecs(qryFltr.MinUsage); err != nil {
|
||||
if qryFltr.MinUsage != utils.EmptyString {
|
||||
parsedDur, err := utils.ParseDurationWithNanosecs(qryFltr.MinUsage)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
} else {
|
||||
minUsage = &parsed
|
||||
minUsage = &parsedDur
|
||||
}
|
||||
}
|
||||
if len(qryFltr.MaxUsage) != 0 {
|
||||
if parsed, err := utils.ParseDurationWithNanosecs(qryFltr.MaxUsage); err != nil {
|
||||
if qryFltr.MaxUsage != utils.EmptyString {
|
||||
parsedDur, err := utils.ParseDurationWithNanosecs(qryFltr.MaxUsage)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
} else {
|
||||
maxUsage = &parsed
|
||||
maxUsage = &parsedDur
|
||||
}
|
||||
}
|
||||
filters := bson.M{
|
||||
@@ -1018,20 +1028,20 @@ func (ms *MongoStorage) GetCDRs(qryFltr *utils.CDRsFilter, remove bool) ([]*CDR,
|
||||
CreatedAtLow: bson.M{"$gte": qryFltr.CreatedAtStart, "$lt": qryFltr.CreatedAtEnd},
|
||||
UpdatedAtLow: bson.M{"$gte": qryFltr.UpdatedAtStart, "$lt": qryFltr.UpdatedAtEnd},
|
||||
UsageLow: bson.M{"$gte": minUsage, "$lt": maxUsage},
|
||||
//CostDetailsLow + "." + AccountLow: bson.M{"$in": qryFltr.RatedAccounts, "$nin": qryFltr.NotRatedAccounts},
|
||||
//CostDetailsLow + "." + SubjectLow: bson.M{"$in": qryFltr.RatedSubjects, "$nin": qryFltr.NotRatedSubjects},
|
||||
// CostDetailsLow + "." + AccountLow: bson.M{"$in": qryFltr.RatedAccounts, "$nin": qryFltr.NotRatedAccounts},
|
||||
// CostDetailsLow + "." + SubjectLow: bson.M{"$in": qryFltr.RatedSubjects, "$nin": qryFltr.NotRatedSubjects},
|
||||
}
|
||||
//file, _ := os.TempFile(os.TempDir(), "debug")
|
||||
//file.WriteString(fmt.Sprintf("FILTER: %v\n", utils.ToIJSON(qryFltr)))
|
||||
//file.WriteString(fmt.Sprintf("BEFORE: %v\n", utils.ToIJSON(filters)))
|
||||
// file, _ := os.TempFile(os.TempDir(), "debug")
|
||||
// file.WriteString(fmt.Sprintf("FILTER: %v\n", utils.ToIJSON(qryFltr)))
|
||||
// file.WriteString(fmt.Sprintf("BEFORE: %v\n", utils.ToIJSON(filters)))
|
||||
ms.cleanEmptyFilters(filters)
|
||||
if len(qryFltr.DestinationPrefixes) != 0 {
|
||||
var regexpRule string
|
||||
for _, prefix := range qryFltr.DestinationPrefixes {
|
||||
if len(prefix) == 0 {
|
||||
if prefix == utils.EmptyString {
|
||||
continue
|
||||
}
|
||||
if len(regexpRule) != 0 {
|
||||
if regexpRule != utils.EmptyString {
|
||||
regexpRule += "|"
|
||||
}
|
||||
regexpRule += "^(" + regexp.QuoteMeta(prefix) + ")"
|
||||
@@ -1039,17 +1049,29 @@ func (ms *MongoStorage) GetCDRs(qryFltr *utils.CDRsFilter, remove bool) ([]*CDR,
|
||||
if _, hasIt := filters["$and"]; !hasIt {
|
||||
filters["$and"] = make([]bson.M, 0)
|
||||
}
|
||||
filters["$and"] = append(filters["$and"].([]bson.M), bson.M{DestinationLow: bsonx.Regex(regexpRule, "")}) // $and gathers all rules not fitting top level query
|
||||
// The "$and" operator is used to include additional query conditions that cannot be
|
||||
// represented at the top level of the query.
|
||||
filters["$and"] = append(filters["$and"].([]bson.M), bson.M{
|
||||
DestinationLow: primitive.Regex{
|
||||
Pattern: regexpRule,
|
||||
},
|
||||
})
|
||||
}
|
||||
if len(qryFltr.NotDestinationPrefixes) != 0 {
|
||||
if _, hasIt := filters["$and"]; !hasIt {
|
||||
filters["$and"] = make([]bson.M, 0)
|
||||
}
|
||||
for _, prefix := range qryFltr.NotDestinationPrefixes {
|
||||
if len(prefix) == 0 {
|
||||
if prefix == utils.EmptyString {
|
||||
continue
|
||||
}
|
||||
filters["$and"] = append(filters["$and"].([]bson.M), bson.M{DestinationLow: bsonx.Regex("^(?!"+prefix+")", "")})
|
||||
filters["$and"] = append(filters["$and"].([]bson.M),
|
||||
bson.M{
|
||||
DestinationLow: primitive.Regex{
|
||||
Pattern: "^(?!" + prefix + ")",
|
||||
},
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1094,19 +1116,18 @@ func (ms *MongoStorage) GetCDRs(qryFltr *utils.CDRsFilter, remove bool) ([]*CDR,
|
||||
filters[CostLow] = bson.M{"$lt": *qryFltr.MaxCost}
|
||||
}
|
||||
}
|
||||
//file.WriteString(fmt.Sprintf("AFTER: %v\n", utils.ToIJSON(filters)))
|
||||
//file.Close()
|
||||
// file.WriteString(fmt.Sprintf("AFTER: %v\n", utils.ToIJSON(filters)))
|
||||
// file.Close()
|
||||
if remove {
|
||||
var chgd int64
|
||||
err := ms.query(func(sctx mongo.SessionContext) (err error) {
|
||||
dr, err := ms.getCol(ColCDRs).DeleteMany(sctx, filters)
|
||||
if err != nil {
|
||||
return err
|
||||
err := ms.query(func(sctx mongo.SessionContext) (qryErr error) {
|
||||
dr, qryErr := ms.getCol(ColCDRs).DeleteMany(sctx, filters)
|
||||
if qryErr != nil {
|
||||
return qryErr
|
||||
}
|
||||
chgd = dr.DeletedCount
|
||||
return err
|
||||
n = dr.DeletedCount
|
||||
return qryErr
|
||||
})
|
||||
return nil, chgd, err
|
||||
return nil, n, err
|
||||
}
|
||||
fop := options.Find()
|
||||
cop := options.Count()
|
||||
@@ -1139,26 +1160,26 @@ func (ms *MongoStorage) GetCDRs(qryFltr *utils.CDRsFilter, remove bool) ([]*CDR,
|
||||
case utils.Cost:
|
||||
orderVal += "cost"
|
||||
default:
|
||||
return nil, 0, fmt.Errorf("Invalid value : %s", separateVals[0])
|
||||
return nil, 0, fmt.Errorf("invalid value : %s", separateVals[0])
|
||||
}
|
||||
fop = fop.SetSort(bson.M{orderVal: ordVal})
|
||||
}
|
||||
if qryFltr.Count {
|
||||
var cnt int64
|
||||
if err := ms.query(func(sctx mongo.SessionContext) (err error) {
|
||||
cnt, err = ms.getCol(ColCDRs).CountDocuments(sctx, filters, cop)
|
||||
return err
|
||||
}); err != nil {
|
||||
err := ms.query(func(sctx mongo.SessionContext) (qryErr error) {
|
||||
cnt, qryErr = ms.getCol(ColCDRs).CountDocuments(sctx, filters, cop)
|
||||
return qryErr
|
||||
})
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
return nil, cnt, nil
|
||||
}
|
||||
// Execute query
|
||||
var cdrs []*CDR
|
||||
err := ms.query(func(sctx mongo.SessionContext) (err error) {
|
||||
cur, err := ms.getCol(ColCDRs).Find(sctx, filters, fop)
|
||||
if err != nil {
|
||||
return err
|
||||
err = ms.query(func(sctx mongo.SessionContext) (qryErr error) {
|
||||
cur, qryErr := ms.getCol(ColCDRs).Find(sctx, filters, fop)
|
||||
if qryErr != nil {
|
||||
return qryErr
|
||||
}
|
||||
for cur.Next(sctx) {
|
||||
cdr := CDR{}
|
||||
@@ -1532,37 +1553,40 @@ func (ms *MongoStorage) SetTPDispatcherHosts(tpDPPs []*utils.TPDispatcherHost) (
|
||||
})
|
||||
}
|
||||
|
||||
func (ms *MongoStorage) GetVersions(itm string) (vrs Versions, err error) {
|
||||
func (ms *MongoStorage) GetVersions(itm string) (Versions, error) {
|
||||
fop := options.FindOne()
|
||||
if itm != "" {
|
||||
fop.SetProjection(bson.M{itm: 1, "_id": 0})
|
||||
} else {
|
||||
fop.SetProjection(bson.M{"_id": 0})
|
||||
}
|
||||
if err = ms.query(func(sctx mongo.SessionContext) (err error) {
|
||||
cur := ms.getCol(ColVer).FindOne(sctx, bson.D{}, fop)
|
||||
if err := cur.Decode(&vrs); err != nil {
|
||||
if err == mongo.ErrNoDocuments {
|
||||
return utils.ErrNotFound
|
||||
}
|
||||
return err
|
||||
var vrs Versions
|
||||
err := ms.query(func(sctx mongo.SessionContext) (err error) {
|
||||
sr := ms.getCol(ColVer).FindOne(sctx, bson.D{}, fop)
|
||||
decodeErr := sr.Decode(&vrs)
|
||||
if errors.Is(decodeErr, mongo.ErrNoDocuments) {
|
||||
return utils.ErrNotFound
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
return decodeErr
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(vrs) == 0 {
|
||||
return nil, utils.ErrNotFound
|
||||
}
|
||||
return
|
||||
return vrs, nil
|
||||
}
|
||||
|
||||
func (ms *MongoStorage) SetVersions(vrs Versions, overwrite bool) (err error) {
|
||||
func (ms *MongoStorage) SetVersions(vrs Versions, overwrite bool) error {
|
||||
if overwrite {
|
||||
ms.RemoveVersions(nil)
|
||||
err := ms.RemoveVersions(nil)
|
||||
if err != nil && !errors.Is(err, utils.ErrNotFound) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return ms.query(func(sctx mongo.SessionContext) (err error) {
|
||||
_, err = ms.getCol(ColVer).UpdateOne(sctx, bson.D{}, bson.M{"$set": vrs},
|
||||
return ms.query(func(sctx mongo.SessionContext) error {
|
||||
_, err := ms.getCol(ColVer).UpdateOne(sctx, bson.D{}, bson.M{"$set": vrs},
|
||||
options.Update().SetUpsert(true),
|
||||
)
|
||||
return err
|
||||
@@ -1575,23 +1599,22 @@ func (ms *MongoStorage) SetVersions(vrs Versions, overwrite bool) (err error) {
|
||||
// _, err = col.Upsert(bson.M{}, bson.M{"$set": &vrs})
|
||||
}
|
||||
|
||||
func (ms *MongoStorage) RemoveVersions(vrs Versions) (err error) {
|
||||
func (ms *MongoStorage) RemoveVersions(vrs Versions) error {
|
||||
if len(vrs) == 0 {
|
||||
return ms.query(func(sctx mongo.SessionContext) (err error) {
|
||||
var dr *mongo.DeleteResult
|
||||
dr, err = ms.getCol(ColVer).DeleteOne(sctx, bson.D{})
|
||||
return ms.query(func(sctx mongo.SessionContext) error {
|
||||
dr, err := ms.getCol(ColVer).DeleteOne(sctx, bson.D{})
|
||||
if err != nil {
|
||||
return
|
||||
return err
|
||||
}
|
||||
if dr.DeletedCount == 0 {
|
||||
return utils.ErrNotFound
|
||||
}
|
||||
return
|
||||
return nil
|
||||
})
|
||||
}
|
||||
return ms.query(func(sctx mongo.SessionContext) (err error) {
|
||||
return ms.query(func(sctx mongo.SessionContext) error {
|
||||
for k := range vrs {
|
||||
if _, err = ms.getCol(ColVer).UpdateOne(sctx, bson.D{}, bson.M{"$unset": bson.M{k: 1}},
|
||||
if _, err := ms.getCol(ColVer).UpdateOne(sctx, bson.D{}, bson.M{"$unset": bson.M{k: 1}},
|
||||
options.Update().SetUpsert(true)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -20,6 +20,9 @@ package engine
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"net/url"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -81,6 +84,24 @@ func NewStorDBConn(dbType, host, port, name, user, pass, marshaler string,
|
||||
return
|
||||
}
|
||||
|
||||
func buildURL(scheme, host, port, db, user, pass string) (*url.URL, error) {
|
||||
u, err := url.Parse("//" + host)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if port != "0" {
|
||||
u.Host = net.JoinHostPort(u.Host, port)
|
||||
}
|
||||
if user != "" && pass != "" {
|
||||
u.User = url.UserPassword(user, pass)
|
||||
}
|
||||
if db != "" {
|
||||
u.Path = path.Join(u.Path, db)
|
||||
}
|
||||
u.Scheme = scheme
|
||||
return u, nil
|
||||
}
|
||||
|
||||
// SMCost stores one Cost coming from SM
|
||||
type SMCost struct {
|
||||
CGRID string
|
||||
|
||||
4
go.mod
4
go.mod
@@ -45,7 +45,7 @@ require (
|
||||
github.com/peterh/liner v1.2.2
|
||||
github.com/rabbitmq/amqp091-go v1.9.0
|
||||
github.com/segmentio/kafka-go v0.4.44
|
||||
go.mongodb.org/mongo-driver v1.11.0
|
||||
go.mongodb.org/mongo-driver v1.12.1
|
||||
golang.org/x/crypto v0.14.0
|
||||
golang.org/x/net v0.17.0
|
||||
golang.org/x/oauth2 v0.13.0
|
||||
@@ -94,7 +94,6 @@ require (
|
||||
github.com/jinzhu/now v1.1.5 // indirect
|
||||
github.com/jmespath/go-jmespath v0.4.0 // indirect
|
||||
github.com/klauspost/compress v1.17.1 // indirect
|
||||
github.com/kr/pretty v0.2.1 // indirect
|
||||
github.com/lib/pq v1.8.0 // indirect
|
||||
github.com/mattn/go-runewidth v0.0.15 // indirect
|
||||
github.com/minio/highwayhash v1.0.2 // indirect
|
||||
@@ -105,7 +104,6 @@ require (
|
||||
github.com/nats-io/nuid v1.0.1 // indirect
|
||||
github.com/philhofer/fwd v1.1.1 // indirect
|
||||
github.com/pierrec/lz4/v4 v4.1.18 // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/rivo/uniseg v0.4.4 // indirect
|
||||
github.com/steveyen/gtreap v0.1.0 // indirect
|
||||
github.com/syndtr/goleveldb v1.0.0 // indirect
|
||||
|
||||
13
go.sum
13
go.sum
@@ -253,11 +253,8 @@ github.com/kljensen/snowball v0.6.0/go.mod h1:27N7E8fVU5H68RlUmnWwZCfxgt4POBJfEN
|
||||
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
||||
github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
|
||||
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw=
|
||||
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
|
||||
github.com/lib/pq v1.1.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
|
||||
@@ -366,7 +363,6 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
|
||||
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||
@@ -377,8 +373,6 @@ github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpP
|
||||
github.com/tebeka/snowball v0.4.2/go.mod h1:4IfL14h1lvwZcp1sfXuuc7/7yCsvVffTWxWxCLfFpYg=
|
||||
github.com/tecbot/gorocksdb v0.0.0-20191217155057-f0fad39f321c h1:g+WoO5jjkqGAzHWCjJB1zZfXPIAaDpzXIEJ0eS6B5Ok=
|
||||
github.com/tecbot/gorocksdb v0.0.0-20191217155057-f0fad39f321c/go.mod h1:ahpPrc7HpcfEWDQRZEmnXMzHY03mLDYMCxeDzy46i+8=
|
||||
github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
|
||||
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
|
||||
github.com/tinylib/msgp v1.1.0/go.mod h1:+d+yLhGm8mzTaHzB+wgMYrodPfmZrzkirds8fDWklFE=
|
||||
github.com/tinylib/msgp v1.1.2/go.mod h1:+d+yLhGm8mzTaHzB+wgMYrodPfmZrzkirds8fDWklFE=
|
||||
github.com/tinylib/msgp v1.1.5 h1:2gXmtWueD2HefZHQe1QOy9HVzmFrLOVvsXwXBQ0ayy0=
|
||||
@@ -390,10 +384,8 @@ github.com/willf/bitset v1.1.11 h1:N7Z7E9UvjW+sGsEl7k/SJrvY2reP1A07MrGuCjIOjRE=
|
||||
github.com/willf/bitset v1.1.11/go.mod h1:83CECat5yLh5zVOf4P1ErAgKA5UDvKtgyUABdr3+MjI=
|
||||
github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=
|
||||
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
|
||||
github.com/xdg-go/scram v1.1.1/go.mod h1:RaEWvsqvNKKvBPvcKeFjrG2cJqOkHTiyTpzz23ni57g=
|
||||
github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY=
|
||||
github.com/xdg-go/scram v1.1.2/go.mod h1:RT/sEzTbU5y00aCK8UOx6R7YryM0iF1N2MOmC3kKLN4=
|
||||
github.com/xdg-go/stringprep v1.0.3/go.mod h1:W3f5j4i+9rC0kuIEJL0ky1VpHXQU3ocBgklLGvcBnW8=
|
||||
github.com/xdg-go/stringprep v1.0.4 h1:XLI/Ng3O1Atzq0oBs3TWm+5ZVgkq2aqdlvP9JtoZ6c8=
|
||||
github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gijq1dTyGkM=
|
||||
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
|
||||
@@ -404,8 +396,8 @@ github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5t
|
||||
github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q=
|
||||
go.etcd.io/bbolt v1.3.5 h1:XAzx9gjCb0Rxj7EoqcClPD1d5ZBxZJk0jbuoPHenBt0=
|
||||
go.etcd.io/bbolt v1.3.5/go.mod h1:G5EMThwa9y8QZGBClrRx5EY+Yw9kAhnjy3bSjsnlVTQ=
|
||||
go.mongodb.org/mongo-driver v1.11.0 h1:FZKhBSTydeuffHj9CBjXlR8vQLee1cQyTWYPA6/tqiE=
|
||||
go.mongodb.org/mongo-driver v1.11.0/go.mod h1:s7p5vEtfbeR1gYi6pnj3c3/urpbLv2T5Sfd6Rp2HBB8=
|
||||
go.mongodb.org/mongo-driver v1.12.1 h1:nLkghSU8fQNaK7oUmDhQFsnrtcoNy7Z6LVFKsEecqgE=
|
||||
go.mongodb.org/mongo-driver v1.12.1/go.mod h1:/rGBTebI3XYboVmgz+Wv3Bcbl3aD0QF9zl6kDDw18rQ=
|
||||
go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0=
|
||||
go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo=
|
||||
go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
|
||||
@@ -476,7 +468,6 @@ golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJ
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.4.0 h1:zxkM55ReGkDlKSM+Fu41A+zmbZuaPVbGMzvvdUPznYQ=
|
||||
|
||||
Reference in New Issue
Block a user