Upgrade MongoDB driver to 1.13

- 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 (deprecated since 1.12).
- 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:
ionutboangiu
2023-11-14 07:40:47 -05:00
committed by Dan Christian Bogos
parent e5ab21def5
commit 26cdb571b8
6 changed files with 600 additions and 1364 deletions

View File

@@ -19,8 +19,14 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package engine
import (
"errors"
"reflect"
"github.com/cgrates/birpc/context"
"github.com/cgrates/cgrates/utils"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/bsoncodec"
"go.mongodb.org/mongo-driver/bson/bsonrw"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
@@ -30,17 +36,30 @@ const (
)
func (ms *MongoStorage) GetSection(ctx *context.Context, section string, val any) error {
return ms.query(context.TODO(), func(sctx mongo.SessionContext) (err error) {
cur := ms.getCol(ColCfg).FindOne(sctx, bson.M{"section": section},
return ms.query(context.TODO(), func(sctx mongo.SessionContext) error {
sr := ms.getCol(ColCfg).FindOne(sctx, bson.M{"section": section},
options.FindOne().SetProjection(bson.M{"cfg": 1, "_id": 0 /*"section": 0, */}))
tmp := map[string]bson.Raw{}
if err = cur.Decode(&tmp); err != nil {
if err == mongo.ErrNoDocuments {
decodeErr := sr.Decode(&tmp)
if decodeErr != nil {
if errors.Is(decodeErr, mongo.ErrNoDocuments) {
return nil
}
return
return decodeErr
}
return bson.UnmarshalWithRegistry(mongoReg, tmp["cfg"], val)
reg := bson.NewRegistry()
decimalType := reflect.TypeOf(utils.Decimal{})
reg.RegisterTypeEncoder(decimalType, bsoncodec.ValueEncoderFunc(decimalEncoder))
reg.RegisterTypeDecoder(decimalType, bsoncodec.ValueDecoderFunc(decimalDecoder))
dec, err := bson.NewDecoder(bsonrw.NewBSONDocumentReader(tmp["cfg"]))
if err != nil {
return err
}
if err = dec.SetRegistry(reg); err != nil {
return err
}
return dec.Decode(val)
})
}

File diff suppressed because it is too large Load Diff

View File

@@ -20,7 +20,6 @@ package engine
import (
"fmt"
"regexp"
"strings"
"time"
@@ -31,765 +30,8 @@ import (
"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) {
getTpIDs := func(ctx mongo.SessionContext, col string, tpMap utils.StringSet) (utils.StringSet, error) {
if strings.HasPrefix(col, "tp_") {
result, err := ms.getCol(col).Distinct(ctx, "tpid", bson.D{})
if err != nil {
return tpMap, err
}
for _, tpid := range result {
tpMap.Add(tpid.(string))
}
}
return tpMap, nil
}
tpidMap := make(utils.StringSet)
if colName == "" {
if err := ms.query(context.TODO(), func(sctx mongo.SessionContext) error {
col, err := ms.DB().ListCollections(sctx, bson.D{}, options.ListCollections().SetNameOnly(true))
if err != nil {
return err
}
for col.Next(sctx) {
var elem struct{ Name string }
if err := col.Decode(&elem); err != nil {
return err
}
if tpidMap, err = getTpIDs(sctx, elem.Name, tpidMap); err != nil {
return err
}
}
return col.Close(sctx)
}); err != nil {
return nil, err
}
} else {
if err := ms.query(context.TODO(), func(sctx mongo.SessionContext) error {
tpidMap, err = getTpIDs(sctx, colName, tpidMap)
return err
}); err != nil {
return nil, err
}
}
tpids = tpidMap.AsSlice()
return tpids, nil
}
func (ms *MongoStorage) GetTpTableIds(tpid, table string, distinct []string,
filter map[string]string, pag *utils.PaginatorWithSearch) ([]string, error) {
findMap := bson.M{}
if tpid != "" {
findMap["tpid"] = tpid
}
for k, v := range filter {
if k != "" && v != "" {
findMap[k] = v
}
}
fop := options.Find()
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)+".*", "")})
}
// findMap["$and"] = []bson.M{{"$or": searchItems}} //before
findMap["$or"] = searchItems // after
}
if pag.Paginator != nil {
if pag.Limit != nil {
fop = fop.SetLimit(int64(*pag.Limit))
}
if pag.Offset != nil {
fop = fop.SetSkip(int64(*pag.Offset))
}
}
}
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"
}
selectors[distinct[i]] = 1
}
fop.SetProjection(selectors)
distinctIds := make(utils.StringSet)
if err := ms.query(context.TODO(), func(sctx mongo.SessionContext) (err error) {
cur, err := ms.getCol(table).Find(sctx, findMap, fop)
if err != nil {
return err
}
for cur.Next(sctx) {
var elem bson.D
err := cur.Decode(&elem)
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 {
id += distinctValue.(string)
}
if i < last {
id += utils.ConcatenatedKeySep
}
}
distinctIds.Add(id)
}
return cur.Close(sctx)
}); err != nil {
return nil, err
}
return distinctIds.AsSlice(), nil
}
func (ms *MongoStorage) GetTPResources(tpid, tenant, id string) ([]*utils.TPResourceProfile, error) {
filter := bson.M{"tpid": tpid}
if id != "" {
filter["id"] = id
}
if tenant != "" {
filter["tenant"] = tenant
}
var results []*utils.TPResourceProfile
err := ms.query(context.TODO(), func(sctx mongo.SessionContext) (err error) {
cur, err := ms.getCol(utils.TBLTPResources).Find(sctx, filter)
if err != nil {
return err
}
for cur.Next(sctx) {
var el utils.TPResourceProfile
err := cur.Decode(&el)
if err != nil {
return err
}
results = append(results, &el)
}
if len(results) == 0 {
return utils.ErrNotFound
}
return cur.Close(sctx)
})
return results, err
}
func (ms *MongoStorage) GetTPStats(tpid, tenant, id string) ([]*utils.TPStatProfile, error) {
filter := bson.M{
"tpid": tpid,
}
if id != "" {
filter["id"] = id
}
if tenant != "" {
filter["tenant"] = tenant
}
var results []*utils.TPStatProfile
err := ms.query(context.TODO(), func(sctx mongo.SessionContext) (err error) {
cur, err := ms.getCol(utils.TBLTPStats).Find(sctx, filter)
if err != nil {
return err
}
for cur.Next(sctx) {
var el utils.TPStatProfile
err := cur.Decode(&el)
if err != nil {
return err
}
results = append(results, &el)
}
if len(results) == 0 {
return utils.ErrNotFound
}
return cur.Close(sctx)
})
return results, err
}
func (ms *MongoStorage) RemTpData(table, tpid string, args map[string]string) error {
if len(table) == 0 { // Remove tpid out of all tables
return ms.query(context.TODO(), func(sctx mongo.SessionContext) error {
col, err := ms.DB().ListCollections(sctx, bson.D{}, options.ListCollections().SetNameOnly(true))
if err != nil {
return err
}
for col.Next(sctx) {
var elem struct{ Name string }
if err := col.Decode(&elem); err != nil {
return err
}
if strings.HasPrefix(elem.Name, "tp_") {
_, err = ms.getCol(elem.Name).DeleteMany(sctx, bson.M{"tpid": tpid})
if err != nil {
return err
}
}
}
return col.Close(sctx)
})
}
// Remove from a single table
if args == nil {
args = make(map[string]string)
}
if _, has := args["tag"]; has { // API uses tag to be compatible with SQL models, fix it here
args["id"] = args["tag"]
delete(args, "tag")
}
if tpid != "" {
args["tpid"] = tpid
}
return ms.query(context.TODO(), func(sctx mongo.SessionContext) (err error) {
dr, err := ms.getCol(table).DeleteOne(sctx, args)
if dr.DeletedCount == 0 {
return utils.ErrNotFound
}
return err
})
}
func (ms *MongoStorage) SetTPResources(tpRLs []*utils.TPResourceProfile) (err error) {
if len(tpRLs) == 0 {
return
}
return ms.query(context.TODO(), func(sctx mongo.SessionContext) (err error) {
for _, tp := range tpRLs {
_, err = ms.getCol(utils.TBLTPResources).UpdateOne(sctx, bson.M{"tpid": tp.TPid, "id": tp.ID},
bson.M{"$set": tp}, options.Update().SetUpsert(true))
if err != nil {
return err
}
}
return nil
})
}
func (ms *MongoStorage) SetTPRStats(tps []*utils.TPStatProfile) (err error) {
if len(tps) == 0 {
return
}
return ms.query(context.TODO(), func(sctx mongo.SessionContext) (err error) {
for _, tp := range tps {
_, err = ms.getCol(utils.TBLTPStats).UpdateOne(sctx, bson.M{"tpid": tp.TPid, "id": tp.ID},
bson.M{"$set": tp}, options.Update().SetUpsert(true))
if err != nil {
return err
}
}
return nil
})
}
func (ms *MongoStorage) SetTPStats(tpSTs []*utils.TPStatProfile) (err error) {
if len(tpSTs) == 0 {
return
}
return ms.query(context.TODO(), func(sctx mongo.SessionContext) (err error) {
for _, tp := range tpSTs {
_, err = ms.getCol(utils.TBLTPStats).UpdateOne(sctx, bson.M{"tpid": tp.TPid, "id": tp.ID},
bson.M{"$set": tp},
options.Update().SetUpsert(true),
)
if err != nil {
return err
}
}
return nil
})
}
func (ms *MongoStorage) GetTPThresholds(tpid, tenant, id string) ([]*utils.TPThresholdProfile, error) {
filter := bson.M{"tpid": tpid}
if id != "" {
filter["id"] = id
}
if tenant != "" {
filter["tenant"] = tenant
}
var results []*utils.TPThresholdProfile
err := ms.query(context.TODO(), func(sctx mongo.SessionContext) (err error) {
cur, err := ms.getCol(utils.TBLTPThresholds).Find(sctx, filter)
if err != nil {
return err
}
for cur.Next(sctx) {
var tp utils.TPThresholdProfile
err := cur.Decode(&tp)
if err != nil {
return err
}
results = append(results, &tp)
}
if len(results) == 0 {
return utils.ErrNotFound
}
return cur.Close(sctx)
})
return results, err
}
func (ms *MongoStorage) SetTPThresholds(tpTHs []*utils.TPThresholdProfile) (err error) {
if len(tpTHs) == 0 {
return
}
return ms.query(context.TODO(), func(sctx mongo.SessionContext) (err error) {
for _, tp := range tpTHs {
_, err = ms.getCol(utils.TBLTPThresholds).UpdateOne(sctx, bson.M{"tpid": tp.TPid, "id": tp.ID},
bson.M{"$set": tp},
options.Update().SetUpsert(true),
)
if err != nil {
return err
}
}
return nil
})
}
func (ms *MongoStorage) GetTPFilters(tpid, tenant, id string) ([]*utils.TPFilterProfile, error) {
filter := bson.M{"tpid": tpid}
if id != "" {
filter["id"] = id
}
if tenant != "" {
filter["tenant"] = tenant
}
results := []*utils.TPFilterProfile{}
err := ms.query(context.TODO(), func(sctx mongo.SessionContext) (err error) {
cur, err := ms.getCol(utils.TBLTPFilters).Find(sctx, filter)
if err != nil {
return err
}
for cur.Next(sctx) {
var tp utils.TPFilterProfile
err := cur.Decode(&tp)
if err != nil {
return err
}
results = append(results, &tp)
}
if len(results) == 0 {
return utils.ErrNotFound
}
return cur.Close(sctx)
})
return results, err
}
func (ms *MongoStorage) SetTPFilters(tpTHs []*utils.TPFilterProfile) (err error) {
if len(tpTHs) == 0 {
return
}
return ms.query(context.TODO(), func(sctx mongo.SessionContext) (err error) {
for _, tp := range tpTHs {
_, err = ms.getCol(utils.TBLTPFilters).UpdateOne(sctx, bson.M{"tpid": tp.TPid, "id": tp.ID},
bson.M{"$set": tp},
options.Update().SetUpsert(true),
)
if err != nil {
return err
}
}
return nil
})
}
func (ms *MongoStorage) GetTPRoutes(tpid, tenant, id string) ([]*utils.TPRouteProfile, error) {
filter := bson.M{"tpid": tpid}
if id != "" {
filter["id"] = id
}
if tenant != "" {
filter["tenant"] = tenant
}
var results []*utils.TPRouteProfile
err := ms.query(context.TODO(), func(sctx mongo.SessionContext) (err error) {
cur, err := ms.getCol(utils.TBLTPRoutes).Find(sctx, filter)
if err != nil {
return err
}
for cur.Next(sctx) {
var tp utils.TPRouteProfile
err := cur.Decode(&tp)
if err != nil {
return err
}
results = append(results, &tp)
}
if len(results) == 0 {
return utils.ErrNotFound
}
return cur.Close(sctx)
})
return results, err
}
func (ms *MongoStorage) SetTPRoutes(tpRoutes []*utils.TPRouteProfile) (err error) {
if len(tpRoutes) == 0 {
return
}
return ms.query(context.TODO(), func(sctx mongo.SessionContext) (err error) {
for _, tp := range tpRoutes {
_, err = ms.getCol(utils.TBLTPRoutes).UpdateOne(sctx, bson.M{"tpid": tp.TPid, "id": tp.ID},
bson.M{"$set": tp},
options.Update().SetUpsert(true),
)
if err != nil {
return err
}
}
return nil
})
}
func (ms *MongoStorage) GetTPAttributes(tpid, tenant, id string) ([]*utils.TPAttributeProfile, error) {
filter := bson.M{"tpid": tpid}
if id != "" {
filter["id"] = id
}
if tenant != "" {
filter["tenant"] = tenant
}
var results []*utils.TPAttributeProfile
err := ms.query(context.TODO(), func(sctx mongo.SessionContext) (err error) {
cur, err := ms.getCol(utils.TBLTPAttributes).Find(sctx, filter)
if err != nil {
return err
}
for cur.Next(sctx) {
var tp utils.TPAttributeProfile
err := cur.Decode(&tp)
if err != nil {
return err
}
results = append(results, &tp)
}
if len(results) == 0 {
return utils.ErrNotFound
}
return cur.Close(sctx)
})
return results, err
}
func (ms *MongoStorage) SetTPAttributes(tpRoutes []*utils.TPAttributeProfile) (err error) {
if len(tpRoutes) == 0 {
return
}
return ms.query(context.TODO(), func(sctx mongo.SessionContext) (err error) {
for _, tp := range tpRoutes {
_, err = ms.getCol(utils.TBLTPAttributes).UpdateOne(sctx, bson.M{"tpid": tp.TPid, "id": tp.ID},
bson.M{"$set": tp},
options.Update().SetUpsert(true),
)
if err != nil {
return err
}
}
return nil
})
}
func (ms *MongoStorage) GetTPChargers(tpid, tenant, id string) ([]*utils.TPChargerProfile, error) {
filter := bson.M{"tpid": tpid}
if id != "" {
filter["id"] = id
}
if tenant != "" {
filter["tenant"] = tenant
}
var results []*utils.TPChargerProfile
err := ms.query(context.TODO(), func(sctx mongo.SessionContext) (err error) {
cur, err := ms.getCol(utils.TBLTPChargers).Find(sctx, filter)
if err != nil {
return err
}
for cur.Next(sctx) {
var tp utils.TPChargerProfile
err := cur.Decode(&tp)
if err != nil {
return err
}
results = append(results, &tp)
}
if len(results) == 0 {
return utils.ErrNotFound
}
return cur.Close(sctx)
})
return results, err
}
func (ms *MongoStorage) SetTPChargers(tpCPP []*utils.TPChargerProfile) (err error) {
if len(tpCPP) == 0 {
return
}
return ms.query(context.TODO(), func(sctx mongo.SessionContext) (err error) {
for _, tp := range tpCPP {
_, err = ms.getCol(utils.TBLTPChargers).UpdateOne(sctx, bson.M{"tpid": tp.TPid, "id": tp.ID},
bson.M{"$set": tp},
options.Update().SetUpsert(true),
)
if err != nil {
return err
}
}
return nil
})
}
func (ms *MongoStorage) GetTPDispatcherProfiles(tpid, tenant, id string) ([]*utils.TPDispatcherProfile, error) {
filter := bson.M{"tpid": tpid}
if id != "" {
filter["id"] = id
}
if tenant != "" {
filter["tenant"] = tenant
}
var results []*utils.TPDispatcherProfile
err := ms.query(context.TODO(), func(sctx mongo.SessionContext) (err error) {
cur, err := ms.getCol(utils.TBLTPDispatchers).Find(sctx, filter)
if err != nil {
return err
}
for cur.Next(sctx) {
var tp utils.TPDispatcherProfile
err := cur.Decode(&tp)
if err != nil {
return err
}
results = append(results, &tp)
}
if len(results) == 0 {
return utils.ErrNotFound
}
return cur.Close(sctx)
})
return results, err
}
func (ms *MongoStorage) SetTPDispatcherProfiles(tpDPPs []*utils.TPDispatcherProfile) (err error) {
if len(tpDPPs) == 0 {
return
}
return ms.query(context.TODO(), func(sctx mongo.SessionContext) (err error) {
for _, tp := range tpDPPs {
_, err = ms.getCol(utils.TBLTPDispatchers).UpdateOne(sctx, bson.M{"tpid": tp.TPid, "id": tp.ID},
bson.M{"$set": tp},
options.Update().SetUpsert(true),
)
if err != nil {
return err
}
}
return nil
})
}
func (ms *MongoStorage) GetTPDispatcherHosts(tpid, tenant, id string) ([]*utils.TPDispatcherHost, error) {
filter := bson.M{"tpid": tpid}
if id != "" {
filter["id"] = id
}
if tenant != "" {
filter["tenant"] = tenant
}
var results []*utils.TPDispatcherHost
err := ms.query(context.TODO(), func(sctx mongo.SessionContext) (err error) {
cur, err := ms.getCol(utils.TBLTPDispatcherHosts).Find(sctx, filter)
if err != nil {
return err
}
for cur.Next(sctx) {
var tp utils.TPDispatcherHost
err := cur.Decode(&tp)
if err != nil {
return err
}
results = append(results, &tp)
}
if len(results) == 0 {
return utils.ErrNotFound
}
return cur.Close(sctx)
})
return results, err
}
func (ms *MongoStorage) SetTPDispatcherHosts(tpDPPs []*utils.TPDispatcherHost) (err error) {
if len(tpDPPs) == 0 {
return
}
return ms.query(context.TODO(), func(sctx mongo.SessionContext) (err error) {
for _, tp := range tpDPPs {
_, err = ms.getCol(utils.TBLTPDispatcherHosts).UpdateOne(sctx, bson.M{"tpid": tp.TPid, "id": tp.ID},
bson.M{"$set": tp},
options.Update().SetUpsert(true),
)
if err != nil {
return err
}
}
return nil
})
}
func (ms *MongoStorage) GetTPRateProfiles(tpid, tenant, id string) ([]*utils.TPRateProfile, error) {
filter := bson.M{"tpid": tpid}
if id != "" {
filter["id"] = id
}
if tenant != "" {
filter["tenant"] = tenant
}
var results []*utils.TPRateProfile
err := ms.query(context.TODO(), func(sctx mongo.SessionContext) (err error) {
cur, err := ms.getCol(utils.TBLTPRateProfiles).Find(sctx, filter)
if err != nil {
return err
}
for cur.Next(sctx) {
var tp utils.TPRateProfile
err := cur.Decode(&tp)
if err != nil {
return err
}
results = append(results, &tp)
}
if len(results) == 0 {
return utils.ErrNotFound
}
return cur.Close(sctx)
})
return results, err
}
func (ms *MongoStorage) SetTPRateProfiles(tpDPPs []*utils.TPRateProfile) (err error) {
if len(tpDPPs) == 0 {
return
}
return ms.query(context.TODO(), func(sctx mongo.SessionContext) (err error) {
for _, tp := range tpDPPs {
_, err = ms.getCol(utils.TBLTPRateProfiles).UpdateOne(sctx, bson.M{"tpid": tp.TPid, "id": tp.ID},
bson.M{"$set": tp},
options.Update().SetUpsert(true),
)
if err != nil {
return err
}
}
return nil
})
}
func (ms *MongoStorage) GetTPActionProfiles(tpid, tenant, id string) ([]*utils.TPActionProfile, error) {
filter := bson.M{"tpid": tpid}
if id != "" {
filter["id"] = id
}
if tenant != "" {
filter["tenant"] = tenant
}
var results []*utils.TPActionProfile
err := ms.query(context.TODO(), func(sctx mongo.SessionContext) (err error) {
cur, err := ms.getCol(utils.TBLTPActionProfiles).Find(sctx, filter)
if err != nil {
return err
}
for cur.Next(sctx) {
var tp utils.TPActionProfile
err := cur.Decode(&tp)
if err != nil {
return err
}
results = append(results, &tp)
}
if len(results) == 0 {
return utils.ErrNotFound
}
return cur.Close(sctx)
})
return results, err
}
func (ms *MongoStorage) GetTPAccounts(tpid, tenant, id string) ([]*utils.TPAccount, error) {
filter := bson.M{"tpid": tpid}
if id != "" {
filter["id"] = id
}
if tenant != "" {
filter["tenant"] = tenant
}
var results []*utils.TPAccount
err := ms.query(context.TODO(), func(sctx mongo.SessionContext) (err error) {
cur, err := ms.getCol(utils.TBLTPAccounts).Find(sctx, filter)
if err != nil {
return err
}
for cur.Next(sctx) {
var tp utils.TPAccount
err := cur.Decode(&tp)
if err != nil {
return err
}
results = append(results, &tp)
}
if len(results) == 0 {
return utils.ErrNotFound
}
return cur.Close(sctx)
})
return results, err
}
func (ms *MongoStorage) SetTPActionProfiles(tpAps []*utils.TPActionProfile) (err error) {
if len(tpAps) == 0 {
return
}
return ms.query(context.TODO(), func(sctx mongo.SessionContext) (err error) {
for _, tp := range tpAps {
_, err = ms.getCol(utils.TBLTPActionProfiles).UpdateOne(sctx, bson.M{"tpid": tp.TPid, "id": tp.ID},
bson.M{"$set": tp},
options.Update().SetUpsert(true),
)
if err != nil {
return err
}
}
return nil
})
}
func (ms *MongoStorage) SetTPAccounts(tpAps []*utils.TPAccount) (err error) {
if len(tpAps) == 0 {
return
}
return ms.query(context.TODO(), func(sctx mongo.SessionContext) (err error) {
for _, tp := range tpAps {
_, err = ms.getCol(utils.TBLTPAccounts).UpdateOne(sctx, bson.M{"tpid": tp.TPid, "id": tp.ID},
bson.M{"$set": tp},
options.Update().SetUpsert(true),
)
if err != nil {
return err
}
}
return nil
})
}
func (ms *MongoStorage) GetVersions(itm string) (vrs Versions, err error) {
fop := options.FindOne()
if itm != "" {
@@ -864,7 +106,7 @@ func (ms *MongoStorage) GetStorageType() string {
func (ms *MongoStorage) SetCDR(cdr *utils.CGREvent, allowUpdate bool) error {
if val, has := cdr.Event[utils.OrderID]; has && val == 0 {
cdr.Event[utils.OrderID] = ms.cnter.Next()
cdr.Event[utils.OrderID] = ms.counter.Next()
}
cdrTable := &CDR{
Tenant: cdr.Tenant,
@@ -998,14 +240,18 @@ func getQueryType(ruleType string, not bool, values []string) (msQuery string, v
if not {
msQuery = "$nin"
}
regex := make([]bsonx.Val, 0, len(values))
regex := make([]primitive.Regex, 0, len(values))
if ruleType == utils.MetaPrefix || ruleType == utils.MetaNotPrefix {
for _, val := range values {
regex = append(regex, bsonx.Regex("/^"+val+"/", utils.EmptyString))
regex = append(regex, primitive.Regex{
Pattern: "/^" + val + "/",
})
}
} else {
for _, val := range values {
regex = append(regex, bsonx.Regex("/"+val+"$/", utils.EmptyString))
regex = append(regex, primitive.Regex{
Pattern: "/" + val + "$/",
})
}
}
valChanged = regex

View File

@@ -20,6 +20,9 @@ package engine
import (
"fmt"
"net"
"net/url"
"path"
"strconv"
"strings"
@@ -79,3 +82,21 @@ 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
}

7
go.mod
View File

@@ -38,7 +38,7 @@ require (
github.com/peterh/liner v1.2.2
github.com/rabbitmq/amqp091-go v1.5.0
github.com/segmentio/kafka-go v0.4.32
go.mongodb.org/mongo-driver v1.11.0
go.mongodb.org/mongo-driver v1.13.0
golang.org/x/crypto v0.13.0
golang.org/x/exp v0.0.0-20230905200255-921286631fa9
golang.org/x/net v0.15.0
@@ -87,7 +87,6 @@ require (
github.com/nats-io/nats-server/v2 v2.10.1
github.com/nats-io/nkeys v0.4.5 // indirect
github.com/nats-io/nuid v1.0.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.12.2
github.com/rivo/uniseg v0.2.0 // indirect
github.com/steveyen/gtreap v0.1.0 // indirect
@@ -126,8 +125,8 @@ require (
github.com/prometheus/common v0.35.0 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.1 // indirect
github.com/xdg-go/stringprep v1.0.3 // indirect
github.com/xdg-go/scram v1.1.2 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect
golang.org/x/time v0.3.0 // indirect
)

27
go.sum
View File

@@ -413,11 +413,9 @@ github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxv
github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
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=
@@ -563,8 +561,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.6 h1:i+SbKraHhnrf9M5MYmvQhFnbLhAXSDWF8WWsuyRdocw=
@@ -575,10 +571,10 @@ 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 h1:VOMT+81stJgXW3CpHyqHN3AXDYIMsx56mEFrB37Mb/E=
github.com/xdg-go/scram v1.1.1/go.mod h1:RaEWvsqvNKKvBPvcKeFjrG2cJqOkHTiyTpzz23ni57g=
github.com/xdg-go/stringprep v1.0.3 h1:kdwGpVNwPFtjs98xCGkHjQtGKh86rDcRZN17QEMCOIs=
github.com/xdg-go/stringprep v1.0.3/go.mod h1:W3f5j4i+9rC0kuIEJL0ky1VpHXQU3ocBgklLGvcBnW8=
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.4 h1:XLI/Ng3O1Atzq0oBs3TWm+5ZVgkq2aqdlvP9JtoZ6c8=
github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gijq1dTyGkM=
github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c/go.mod h1:lB8K/P019DLNhemzwFU4jHLhdvlE6uDZjXFejJXr49I=
github.com/xdg/scram v1.0.5 h1:TuS0RFmt5Is5qm9Tm2SoD89OPqe4IRiFtyFY4iwWXsw=
github.com/xdg/scram v1.0.5/go.mod h1:lB8K/P019DLNhemzwFU4jHLhdvlE6uDZjXFejJXr49I=
@@ -594,12 +590,13 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q=
go.etcd.io/bbolt v1.3.5/go.mod h1:G5EMThwa9y8QZGBClrRx5EY+Yw9kAhnjy3bSjsnlVTQ=
go.etcd.io/bbolt v1.3.6 h1:/ecaJf0sk1l4l6V4awd65v2C3ILy7MSj+s/x1ADCIMU=
go.etcd.io/bbolt v1.3.6/go.mod h1:qXsaaIqmgQH0T+OPdb99Bf+PKfBBQVAdyD6TY9G8XM4=
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.13.0 h1:67DgFFjYOCMWdtTEmKFpV3ffWlFnh+CYZ8ZS/tXWUfY=
go.mongodb.org/mongo-driver v1.13.0/go.mod h1:/rGBTebI3XYboVmgz+Wv3Bcbl3aD0QF9zl6kDDw18rQ=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
@@ -678,6 +675,7 @@ golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
@@ -731,6 +729,7 @@ golang.org/x/net v0.0.0-20220412020605-290c469a71a5/go.mod h1:CfG3xpIq0wQ8r1q4Su
golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/net v0.0.0-20220607020251-c690dde0001d/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.0.0-20220617184016-355a448f1bc9/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.15.0 h1:ugBLEUaxABaB5AJqW9enI0ACdci2RUd4eP51NTBvuJ8=
golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
@@ -767,6 +766,7 @@ golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/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-20220601150217-0de741cfad7f/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.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
@@ -848,6 +848,7 @@ golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220610221304-9f5ed59c137d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220615213510-4f61da869c0c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
@@ -862,6 +863,8 @@ golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
@@ -927,6 +930,7 @@ golang.org/x/tools v0.1.3/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.4/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.6-0.20210726203631-07bc1bf47fb2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.13.0 h1:Iey4qkscZuv0VvIt8E0neZjtPVQFSc870HQ448QgEmQ=
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
golang.org/x/xerrors v0.0.0-20190410155217-1f06c39b4373/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
@@ -1139,9 +1143,8 @@ gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20220512140231-539c8e751b99 h1:dbuHpmKjkDzSOMKAWl10QNlgaZUd3V1q99xc81tt2Kc=
gopkg.in/yaml.v3 v3.0.0-20220512140231-539c8e751b99/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/mysql v1.3.4 h1:/KoBMgsUHC3bExsekDcmNYaBnfH2WNeFuXqqrqMc98Q=
gorm.io/driver/mysql v1.3.4/go.mod h1:s4Tq0KmD0yhPGHbZEwg1VPlH0vT/GBHJZorPzhcxBUE=
gorm.io/driver/postgres v1.3.7 h1:FKF6sIMDHDEvvMF/XJvbnCl0nu6KSKUaPXevJ4r+VYQ=