changed DataManager.GetTrendProfiles to GetTrendProfileIDs with new signature

This commit is contained in:
gezimbll
2024-10-03 15:58:22 +02:00
committed by Dan Christian Bogos
parent 28eb68241c
commit 06a9e1b661
5 changed files with 48 additions and 144 deletions

View File

@@ -41,38 +41,6 @@ func (apierSv1 *APIerSv1) GetTrendProfile(ctx *context.Context, arg *utils.Tenan
return
}
// GetTrendProfiles returns a list of Trendprofiles
func (apierSv1 *APIerSv1) GetTrendProfiles(ctx *context.Context, arg *engine.TrendProfilesAPI, reply *[]*engine.TrendProfile) (err error) {
tr, err := apierSv1.DataManager.GetTrendProfiles(arg.Tenant, arg.TpIDs)
if err != nil {
return utils.APIErrorHandler(err)
}
*reply = tr
return
}
// GetTrendProfileIDs returns list of trendProfile IDs registered for a tenant
func (apierSv1 *APIerSv1) GetTrendProfileIDs(ctx *context.Context, args *utils.PaginatorWithTenant, trPrfIDs *[]string) (err error) {
tnt := args.Tenant
if tnt == utils.EmptyString {
tnt = apierSv1.Config.GeneralCfg().DefaultTenant
}
prfx := utils.TrendsProfilePrefix + tnt + utils.ConcatenatedKeySep
keys, err := apierSv1.DataManager.DataDB().GetKeysForPrefix(prfx)
if err != nil {
return err
}
if len(keys) == 0 {
return utils.ErrNotFound
}
trIDs := make([]string, len(keys))
for i, key := range keys {
trIDs[i] = key[len(prfx):]
}
*trPrfIDs = args.PaginateStringSlice(trIDs)
return
}
// SetTrendProfile alters/creates a TrendProfile
func (apierSv1 *APIerSv1) SetTrendProfile(ctx *context.Context, arg *engine.TrendProfileWithAPIOpts, reply *string) error {
if missing := utils.MissingStructFields(arg.TrendProfile, []string{utils.ID}); len(missing) != 0 {

View File

@@ -38,7 +38,7 @@ var (
trendCfgPath string
trendCfg *config.CGRConfig
trendRPC *birpc.Client
trendProfile2 *engine.TrendProfileWithAPIOpts
trendProfile *engine.TrendProfileWithAPIOpts
trendConfigDIR string
sTestsTrend = []func(t *testing.T){
@@ -49,8 +49,6 @@ var (
testTrendSRPCConn,
testTrendSFromFolder,
testTrendSetTrendProfile,
testTrendSGetTrendProfileIDs,
testTrendSGetTrendProfiles,
testTrendSUpdateTrendProfile,
testTrendSRemTrendProfile,
testTrendSKillEngine,
@@ -126,114 +124,63 @@ func testTrendSetTrendProfile(t *testing.T) {
reply *engine.TrendProfileWithAPIOpts
result string
)
trendProfile2 = &engine.TrendProfileWithAPIOpts{
trendProfile = &engine.TrendProfileWithAPIOpts{
TrendProfile: &engine.TrendProfile{
Tenant: "cgrates.org",
ID: "Trend1",
ThresholdIDs: []string{"THD1"}},
Tenant: "cgrates.org",
ID: "Trend1",
ThresholdIDs: []string{"THD1"},
CorrelationType: utils.MetaAverage,
MinItems: 1,
QueueLength: 10,
StatID: "Stats1",
Schedule: "@every 4s",
},
}
if err := trendRPC.Call(context.Background(), utils.APIerSv1GetTrendProfile,
&utils.TenantID{Tenant: "cgrates.org", ID: "Trend1"}, &reply); err == nil ||
err.Error() != utils.ErrNotFound.Error() {
t.Fatal(err)
}
if err := trendRPC.Call(context.Background(), utils.APIerSv1SetTrendProfile, trendProfile2, &result); err != nil {
if err := trendRPC.Call(context.Background(), utils.APIerSv1SetTrendProfile, trendProfile, &result); err != nil {
t.Error(err)
} else if result != utils.OK {
t.Errorf("Expected: %v,Received: %v", utils.OK, result)
}
if err := trendRPC.Call(context.Background(), utils.APIerSv1GetTrendProfile, &utils.TenantID{Tenant: "cgrates.org", ID: "Trend1"}, &reply); err != nil {
t.Error(err)
} else if diff := cmp.Diff(trendProfile2, reply, cmpopts.SortSlices(func(a, b string) bool { return a < b })); diff != utils.EmptyString {
} else if diff := cmp.Diff(trendProfile, reply, cmpopts.SortSlices(func(a, b string) bool { return a < b })); diff != utils.EmptyString {
t.Errorf("Unnexpected profile (-expected +got):\n%s", diff)
}
}
func testTrendSGetTrendProfileIDs(t *testing.T) {
expected := []string{"Trend1", "TR_1"}
var result []string
if err := trendRPC.Call(context.Background(), utils.APIerSv1GetTrendProfileIDs, utils.PaginatorWithTenant{}, &result); err != nil {
t.Error(err)
} else if len(expected) != len(result) {
t.Errorf("Expecting : %+v, received: %+v", expected, result)
}
}
func testTrendSGetTrendProfiles(t *testing.T) {
trendPrf := &engine.TrendProfileWithAPIOpts{
TrendProfile: &engine.TrendProfile{
Tenant: "tenant1",
ID: "Trend_Last",
StatID: "Stat1",
Schedule: "*now",
}}
var result string
if err := trendRPC.Call(context.Background(), utils.APIerSv1SetTrendProfile, trendPrf, &result); err != nil {
t.Error(err)
} else if result != utils.OK {
t.Errorf("Expected: %v,Received: %v", utils.OK, result)
}
tpTr := &engine.TrendProfile{
Tenant: "cgrates.org",
ID: "TR_1",
StatID: "Stats1",
Schedule: "0 1 * * *",
Metrics: []string{"*acc"},
QueueLength: -1,
MinItems: 1,
CorrelationType: "*last",
Tolerance: 5,
Stored: false,
ThresholdIDs: []string{"TD1"},
}
expRes := []*engine.TrendProfile{
trendPrf.TrendProfile,
tpTr,
trendProfile2.TrendProfile,
}
var tResult []*engine.TrendProfile
//get all trend profiles from every tenant when no tenant is specified
if err := trendRPC.Call(context.Background(), utils.APIerSv1GetTrendProfiles, engine.TrendProfilesAPI{}, &tResult); err != nil {
t.Error(err)
} else if diff := cmp.Diff(expRes, tResult, cmpopts.SortSlices(func(a, b *engine.TrendProfile) bool { return a.ID < b.ID })); diff != utils.EmptyString {
t.Errorf("Unnexpected profiles (-expected +got):\n%s", diff)
}
expRes = []*engine.TrendProfile{
tpTr,
trendProfile2.TrendProfile,
}
//get all trend profiles of a tenant when ids length is 0
if err := trendRPC.Call(context.Background(), utils.APIerSv1GetTrendProfiles, engine.TrendProfilesAPI{Tenant: "cgrates.org"}, &tResult); err != nil {
t.Error(err)
} else if diff := cmp.Diff(expRes, tResult, cmpopts.SortSlices(func(a, b *engine.TrendProfile) bool { return a.ID < b.ID })); diff != utils.EmptyString {
t.Errorf("Unnexpected profiles (-expected +got):\n%s", diff)
}
expRes = []*engine.TrendProfile{
trendProfile2.TrendProfile,
}
//get trend profiles by their ids for a tenant
if err := trendRPC.Call(context.Background(), utils.APIerSv1GetTrendProfiles, engine.TrendProfilesAPI{Tenant: "cgrates.org", TpIDs: []string{"Trend1"}}, &tResult); err != nil {
t.Error(err)
} else if diff := cmp.Diff(expRes, tResult, cmpopts.SortSlices(func(a, b *engine.TrendProfile) bool { return a.ID < b.ID })); diff != utils.EmptyString {
t.Errorf("Unnexpected profiles (-expected +got):\n%s", diff)
}
}
func testTrendSUpdateTrendProfile(t *testing.T) {
trendProfile.MinItems = 4
trendProfile.QueueLength = 100
trendProfile.CorrelationType = utils.MetaLast
trendProfile.Schedule = "@every 1s"
trendProfile.StatID = "Stats2"
var (
reply *engine.TrendProfileWithAPIOpts
result string
)
if err := trendRPC.Call(context.Background(), utils.APIerSv1SetTrendProfile, trendProfile2, &result); err != nil {
expTrn := &engine.TrendProfileWithAPIOpts{
TrendProfile: &engine.TrendProfile{
Tenant: "cgrates.org",
ID: "Trend1",
ThresholdIDs: []string{"THD1"},
CorrelationType: utils.MetaLast,
MinItems: 4,
QueueLength: 100,
StatID: "Stats2",
Schedule: "@every 1s",
},
}
if err := trendRPC.Call(context.Background(), utils.APIerSv1SetTrendProfile, trendProfile, &result); err != nil {
t.Error(err)
}
if err := trendRPC.Call(context.Background(), utils.APIerSv1GetTrendProfile, &utils.TenantID{Tenant: "cgrates.org", ID: "Trend1"}, &reply); err != nil {
t.Error(err)
} else if diff := cmp.Diff(trendProfile2, reply, cmpopts.SortSlices(func(a, b string) bool { return a < b })); diff != utils.EmptyString {
} else if diff := cmp.Diff(expTrn, reply, cmpopts.SortSlices(func(a, b string) bool { return a < b })); diff != utils.EmptyString {
t.Errorf("Unnexpected profile (-expected +got):\n%s", diff)
}
}

View File

@@ -1428,14 +1428,10 @@ func (dm *DataManager) GetTrendProfile(tenant, id string, cacheRead, cacheWrite
return
}
func (dm *DataManager) GetTrendProfiles(tenant string, tpIDs []string) (tps []*TrendProfile, err error) {
func (dm *DataManager) GetTrendProfileIDs(tenants []string) (tps map[string][]string, err error) {
prfx := utils.TrendsProfilePrefix
if tenant != utils.EmptyString {
prfx += tenant + utils.ConcatenatedKeySep
}
var tp *TrendProfile
if tenant == utils.EmptyString || len(tpIDs) == 0 {
var keys []string
var keys []string
if len(tenants) == 0 {
keys, err = dm.dataDB.GetKeysForPrefix(prfx)
if err != nil {
return
@@ -1443,29 +1439,28 @@ func (dm *DataManager) GetTrendProfiles(tenant string, tpIDs []string) (tps []*T
if len(keys) == 0 {
return nil, utils.ErrNotFound
}
tps = make(map[string][]string)
for _, key := range keys {
indx := strings.Index(key, utils.ConcatenatedKeySep)
if len(prfx) == len(utils.TrendsProfilePrefix) {
tenant = key[len(utils.TrendsProfilePrefix):indx]
}
tenant := key[len(utils.TrendsProfilePrefix):indx]
id := key[indx+1:]
tp, err = dm.GetTrendProfile(tenant, id, true, false, utils.NonTransactional)
if err != nil {
return
}
tps = append(tps, tp)
tps[tenant] = append(tps[tenant], id)
}
return
}
for _, tpID := range tpIDs {
tp, err = dm.GetTrendProfile(tenant, tpID, true, false, utils.NonTransactional)
if err == utils.ErrNotFound {
continue
}
for _, tenant := range tenants {
tntPrfx := prfx + tenant + utils.ConcatenatedKeySep
keys, err = dm.dataDB.GetKeysForPrefix(tntPrfx)
if err != nil {
return
}
tps = append(tps, tp)
if len(keys) == 0 {
return nil, utils.ErrNotFound
}
tps = make(map[string][]string)
for _, key := range keys {
tps[tenant] = append(tps[tenant], key[len(tntPrfx):])
}
}
return
}

View File

@@ -76,11 +76,6 @@ type TrendProfileWithAPIOpts struct {
APIOpts map[string]any
}
type TrendProfilesAPI struct {
Tenant string
TpIDs []string
}
func (srp *TrendProfile) TenantID() string {
return utils.ConcatenatedKey(srp.Tenant, srp.ID)
}

View File

@@ -1701,7 +1701,6 @@ const (
APIerSv1RemoveTrendProfile = "APIerSv1.RemoveTrendProfile"
APIerSv1GetTrendProfile = "APIerSv1.GetTrendProfile"
APIerSv1GetTrendProfileIDs = "APIerSv1.GetTrendProfileIDs"
APIerSv1GetTrendProfiles = "APIerSv1.GetTrendProfiles"
TrendSv1Ping = "TrendSv1.Ping"
TrendSv1ScheduleQueries = "TrendSv1.ScheduleQueries"
TrendSv1GetTrend = "TrendSv1.GetTrend"