mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Update code for rates apis/drivers
This commit is contained in:
committed by
Dan Christian Bogos
parent
18866b903a
commit
ff5f69cd15
@@ -55,7 +55,7 @@ func (admS *AdminSv1) GetRateProfileRates(ctx *context.Context, args *utils.Args
|
||||
if args.Tenant == utils.EmptyString {
|
||||
args.Tenant = admS.cfg.GeneralCfg().DefaultTenant
|
||||
}
|
||||
rates, err := admS.dm.GetRateProfileRates(ctx, args)
|
||||
_, rates, err := admS.dm.GetRateProfileRates(ctx, args, false)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -103,7 +103,8 @@ func (admS *AdminSv1) GetRateProfileRateIDs(ctx *context.Context, args *utils.Ar
|
||||
args.Tenant = admS.cfg.GeneralCfg().DefaultTenant
|
||||
}
|
||||
var ids []string
|
||||
if ids, err = admS.dm.GetRateProfileRateIDs(ctx, args); err != nil {
|
||||
ids, _, err = admS.dm.GetRateProfileRates(ctx, args, true)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if len(ids) == 0 {
|
||||
@@ -168,7 +169,8 @@ func (admS *AdminSv1) GetRateProfileRateCount(ctx *context.Context, args *utils.
|
||||
}
|
||||
|
||||
var ids []string
|
||||
if ids, err = admS.dm.GetRateProfileRateIDs(ctx, args); err != nil {
|
||||
ids, _, err = admS.dm.GetRateProfileRates(ctx, args, true)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if len(ids) == 0 {
|
||||
|
||||
@@ -1314,6 +1314,7 @@ func testRateProfileRateIDsAndCount(t *testing.T) {
|
||||
} else if len(replyRts) != 5 { //RT_MONDAY, RT_THUESDAY, RT_WEDNESDAY, RT_THURSDAY AND RT_FRIDAY
|
||||
t.Errorf("Unexpected reply returned: %v", reply)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func testRateProfileUpdateRates(t *testing.T) {
|
||||
|
||||
@@ -27,8 +27,7 @@ type DataDBMock struct {
|
||||
RemoveRateProfileDrvF func(ctx *context.Context, str1 string, str2 string, rateIDs *[]string) error
|
||||
SetRateProfileDrvF func(*context.Context, *utils.RateProfile) error
|
||||
GetRateProfileDrvF func(*context.Context, string, string) (*utils.RateProfile, error)
|
||||
GetRateProfileRateidSDrvF func(*context.Context, string, string, string) ([]string, error)
|
||||
GetRateProfileRatesDrvF func(*context.Context, string, string, string) ([]*utils.Rate, error)
|
||||
GetRateProfileRatesDrvF func(*context.Context, string, string, string, bool) ([]string, []*utils.Rate, error)
|
||||
GetKeysForPrefixF func(*context.Context, string) ([]string, error)
|
||||
GetIndexesDrvF func(ctx *context.Context, idxItmType, tntCtx, idxKey, transactionID string) (indexes map[string]utils.StringSet, err error)
|
||||
SetIndexesDrvF func(ctx *context.Context, idxItmType, tntCtx string, indexes map[string]utils.StringSet, commit bool, transactionID string) (err error)
|
||||
@@ -377,18 +376,11 @@ func (dbM *DataDBMock) GetRateProfileDrv(ctx *context.Context, tnt string, id st
|
||||
return nil, utils.ErrNotImplemented
|
||||
}
|
||||
|
||||
func (dbM *DataDBMock) GetRateProfileRateIDsDrv(ctx *context.Context, tnt string, id string, prefixArgs string) ([]string, error) {
|
||||
if dbM.GetRateProfileRateidSDrvF != nil {
|
||||
return dbM.GetRateProfileRateIDsDrv(ctx, tnt, id, prefixArgs)
|
||||
}
|
||||
return nil, utils.ErrNotImplemented
|
||||
}
|
||||
|
||||
func (dbM *DataDBMock) GetRateProfileRatesDrv(ctx *context.Context, tnt string, id string, prefixArgs string) ([]*utils.Rate, error) {
|
||||
func (dbM *DataDBMock) GetRateProfileRatesDrv(ctx *context.Context, tnt string, id string, rtPrfx string, needIDs bool) ([]string, []*utils.Rate, error) {
|
||||
if dbM.GetRateProfileRatesDrvF != nil {
|
||||
return dbM.GetRateProfileRatesDrv(ctx, tnt, id, prefixArgs)
|
||||
return dbM.GetRateProfileRatesDrv(ctx, tnt, id, rtPrfx, needIDs)
|
||||
}
|
||||
return nil, utils.ErrNotImplemented
|
||||
return nil, nil, utils.ErrNotImplemented
|
||||
}
|
||||
|
||||
func (dbM *DataDBMock) SetRateProfileDrv(ctx *context.Context, rt *utils.RateProfile) error {
|
||||
|
||||
@@ -1943,18 +1943,11 @@ func (dm *DataManager) GetRateProfile(ctx *context.Context, tenant, id string, c
|
||||
return
|
||||
}
|
||||
|
||||
func (dm *DataManager) GetRateProfileRateIDs(ctx *context.Context, args *utils.ArgsSubItemIDs) (rateIDs []string, err error) {
|
||||
func (dm *DataManager) GetRateProfileRates(ctx *context.Context, args *utils.ArgsSubItemIDs, needIDs bool) (rateIDs []string, rates []*utils.Rate, err error) {
|
||||
if dm == nil {
|
||||
return nil, utils.ErrNoDatabaseConn
|
||||
return nil, nil, utils.ErrNoDatabaseConn
|
||||
}
|
||||
return dm.DataDB().GetRateProfileRateIDsDrv(ctx, args.Tenant, args.ProfileID, args.ItemsPrefix)
|
||||
}
|
||||
|
||||
func (dm *DataManager) GetRateProfileRates(ctx *context.Context, args *utils.ArgsSubItemIDs) (rateIDs []*utils.Rate, err error) {
|
||||
if dm == nil {
|
||||
return nil, utils.ErrNoDatabaseConn
|
||||
}
|
||||
return dm.DataDB().GetRateProfileRatesDrv(ctx, args.Tenant, args.ProfileID, args.ItemsPrefix)
|
||||
return dm.DataDB().GetRateProfileRatesDrv(ctx, args.Tenant, args.ProfileID, args.ItemsPrefix, needIDs)
|
||||
}
|
||||
|
||||
func (dm *DataManager) SetRateProfile(ctx *context.Context, rpp *utils.RateProfile, withIndex bool) (err error) {
|
||||
|
||||
@@ -86,8 +86,7 @@ type DataDB interface {
|
||||
SetDispatcherHostDrv(*context.Context, *DispatcherHost) error
|
||||
RemoveDispatcherHostDrv(*context.Context, string, string) error
|
||||
GetRateProfileDrv(*context.Context, string, string) (*utils.RateProfile, error)
|
||||
GetRateProfileRateIDsDrv(*context.Context, string, string, string) ([]string, error)
|
||||
GetRateProfileRatesDrv(*context.Context, string, string, string) ([]*utils.Rate, error)
|
||||
GetRateProfileRatesDrv(*context.Context, string, string, string, bool) ([]string, []*utils.Rate, error)
|
||||
SetRateProfileDrv(*context.Context, *utils.RateProfile) error
|
||||
RemoveRateProfileDrv(*context.Context, string, string, *[]string) error
|
||||
GetActionProfileDrv(*context.Context, string, string) (*ActionProfile, error)
|
||||
|
||||
@@ -476,14 +476,14 @@ func (iDB *InternalDB) GetRateProfileDrv(_ *context.Context, tenant, id string)
|
||||
return x.(*utils.RateProfile), nil
|
||||
}
|
||||
|
||||
func (iDB *InternalDB) GetRateProfileRateIDsDrv(ctx *context.Context, tenant, profileID, prefixArgs string) (rateIDs []string, err error) {
|
||||
func (iDB *InternalDB) GetRateProfileRatesDrv(ctx *context.Context, tenant, profileID, rtPrfx string, needIDs bool) (rateIDs []string, rates []*utils.Rate, err error) {
|
||||
x, ok := iDB.db.Get(utils.CacheRateProfiles, utils.ConcatenatedKey(tenant, profileID))
|
||||
if !ok || x == nil {
|
||||
return nil, utils.ErrNotFound
|
||||
return nil, nil, utils.ErrNotFound
|
||||
}
|
||||
for key := range x.(*utils.RateProfile).Rates {
|
||||
if strings.HasPrefix(key, prefixArgs) {
|
||||
rateIDs = append(rateIDs, key)
|
||||
for key, rt := range x.(*utils.RateProfile).Rates {
|
||||
if strings.HasPrefix(key, rtPrfx) {
|
||||
rates = append(rates, rt)
|
||||
}
|
||||
}
|
||||
return
|
||||
|
||||
@@ -1221,27 +1221,32 @@ func (ms *MongoStorage) GetRateProfileDrv(ctx *context.Context, tenant, id strin
|
||||
return utils.NewRateProfileFromMapDataDBMap(tenant, id, mapRP, ms.ms)
|
||||
}
|
||||
|
||||
func (ms *MongoStorage) GetRateProfileRateIDsDrv(ctx *context.Context, tenant, profileID, prefixArgs string) (rateIDs []string, err error) {
|
||||
prefix := utils.Rates + utils.ConcatenatedKeySep
|
||||
if prefixArgs != utils.EmptyString {
|
||||
prefix = utils.ConcatenatedKey(utils.Rates, prefixArgs)
|
||||
}
|
||||
mapRP := make(map[string]interface{})
|
||||
err = ms.query(ctx, func(sctx mongo.SessionContext) (err error) {
|
||||
cur := ms.getCol(ColRpp).FindOne(sctx, bson.M{"tenant": tenant, "id": profileID})
|
||||
if err := cur.Decode(mapRP); err != nil {
|
||||
if err == mongo.ErrNoDocuments {
|
||||
return utils.ErrNotFound
|
||||
func (ms *MongoStorage) GetRateProfileRatesDrv(ctx *context.Context, tenant, profileID, rtPrfx string, needIDs bool) (rateIDs []string, rates []*utils.Rate, err error) {
|
||||
/*
|
||||
prefix := utils.Rates + utils.ConcatenatedKeySep
|
||||
if rtPrfx != utils.EmptyString {
|
||||
prefix = utils.ConcatenatedKey(utils.Rates, rtPrfx)
|
||||
}
|
||||
mapRP := make(map[string]interface{})
|
||||
err = ms.query(ctx, func(sctx mongo.SessionContext) (err error) {
|
||||
cur := ms.getCol(ColRpp).FindOne(sctx, bson.M{"tenant": tenant, "id": profileID})
|
||||
if err := cur.Decode(mapRP); err != nil {
|
||||
if err == mongo.ErrNoDocuments {
|
||||
return utils.ErrNotFound
|
||||
}
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
for key := range mapRP {
|
||||
if strings.HasPrefix(key, prefix) {
|
||||
rateIDs = append(rateIDs, strings.TrimPrefix(key, utils.Rates+utils.ConcatenatedKeySep))
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
for key := range mapRP {
|
||||
if strings.HasPrefix(key, prefix) {
|
||||
rtToAppend := new(utils.RateProfile)
|
||||
err = ms.ms.Unmarshal([]byte())
|
||||
rates = append(rates, strings.TrimPrefix(key, utils.Rates+utils.ConcatenatedKeySep))
|
||||
}
|
||||
}
|
||||
*/
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -749,11 +749,11 @@ func (rs *RedisStorage) GetRateProfileDrv(ctx *context.Context, tenant, id strin
|
||||
}
|
||||
|
||||
// GetRateProfileRateIDsDrv will return back all the rate IDs from a profile
|
||||
func (rs *RedisStorage) GetRateProfileRateIDsDrv(ctx *context.Context, tnt, profileID, prefixArgs string) (rateIDs []string, err error) {
|
||||
func (rs *RedisStorage) GetRateProfileRatesDrv(ctx *context.Context, tnt, profileID, rtPrfx string, needIDs bool) (rateIDs []string, rates []*utils.Rate, err error) {
|
||||
key := utils.RateProfilePrefix + utils.ConcatenatedKey(tnt, profileID)
|
||||
prefix := utils.Rates + utils.ConcatenatedKeySep
|
||||
if prefixArgs != utils.EmptyString {
|
||||
prefix = utils.ConcatenatedKey(utils.Rates, prefixArgs)
|
||||
if rtPrfx != utils.EmptyString {
|
||||
prefix = utils.ConcatenatedKey(utils.Rates, rtPrfx)
|
||||
}
|
||||
var rateField string
|
||||
scan := radix.NewScanner(rs.client, radix.ScanOpts{
|
||||
@@ -761,44 +761,26 @@ func (rs *RedisStorage) GetRateProfileRateIDsDrv(ctx *context.Context, tnt, prof
|
||||
Key: key,
|
||||
Pattern: prefix + utils.Meta,
|
||||
})
|
||||
idx := 0
|
||||
for scan.Next(&rateField) {
|
||||
if strings.HasPrefix(rateField, prefix) {
|
||||
rateIDs = append(rateIDs, strings.TrimPrefix(rateField, utils.Rates+utils.ConcatenatedKeySep))
|
||||
}
|
||||
}
|
||||
if err = scan.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// GetRateProfileRatesDrv will return back all the rates from a profile
|
||||
func (rs *RedisStorage) GetRateProfileRatesDrv(ctx *context.Context, tnt, profileID, prefixArgs string) (rates []*utils.Rate, err error) {
|
||||
key := utils.RateProfilePrefix + utils.ConcatenatedKey(tnt, profileID)
|
||||
prefix := utils.Rates + utils.ConcatenatedKeySep
|
||||
if prefixArgs != utils.EmptyString {
|
||||
prefix = utils.ConcatenatedKey(utils.Rates, prefixArgs)
|
||||
}
|
||||
var rateField string
|
||||
scan := radix.NewScanner(rs.client, radix.ScanOpts{
|
||||
Command: redisHSCAN,
|
||||
Key: key,
|
||||
Pattern: prefix + utils.Meta,
|
||||
})
|
||||
for scan.Next(&rateField) {
|
||||
if strings.HasPrefix(rateField, prefix) {
|
||||
rtToAppend := new(utils.Rate)
|
||||
var rate string
|
||||
if ok := scan.Next(&rate); ok {
|
||||
if err = rs.ms.Unmarshal([]byte(rate), rtToAppend); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
idx++
|
||||
if idx%2 != 0 {
|
||||
if needIDs {
|
||||
rateIDs = append(rateIDs, strings.TrimPrefix(rateField, utils.Rates+utils.ConcatenatedKeySep))
|
||||
}
|
||||
rates = append(rates, rtToAppend)
|
||||
continue
|
||||
}
|
||||
if needIDs {
|
||||
continue // we don't deserialize values for needIDs
|
||||
}
|
||||
rtToAppend := new(utils.Rate)
|
||||
if err = rs.ms.Unmarshal([]byte(rateField), rtToAppend); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
rates = append(rates, rtToAppend)
|
||||
}
|
||||
if err = scan.Close(); err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user