mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-19 22:28:45 +05:00
Refactoring resProfiles,sqProfiles,thProfiles with map[utils.TenantID]
This commit is contained in:
committed by
Dan Christian Bogos
parent
b6763759cc
commit
007a37fa82
@@ -1401,116 +1401,111 @@ func TestLoadReverseAliases(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestLoadResourceProfiles(t *testing.T) {
|
||||
eResProfiles := map[string]map[string]*utils.TPResource{
|
||||
"cgrates.org": map[string]*utils.TPResource{
|
||||
"ResGroup21": &utils.TPResource{
|
||||
TPid: testTPID,
|
||||
Tenant: "cgrates.org",
|
||||
ID: "ResGroup21",
|
||||
Filters: []*utils.TPRequestFilter{
|
||||
&utils.TPRequestFilter{Type: MetaString, FieldName: "HdrAccount", Values: []string{"1001", "1002"}},
|
||||
&utils.TPRequestFilter{Type: MetaStringPrefix, FieldName: "HdrDestination", Values: []string{"10", "20"}},
|
||||
&utils.TPRequestFilter{Type: MetaRSRFields, Values: []string{"HdrSubject(~^1.*1$)", "HdrDestination(1002)"}},
|
||||
},
|
||||
ActivationInterval: &utils.TPActivationInterval{
|
||||
ActivationTime: "2014-07-29T15:00:00Z",
|
||||
},
|
||||
UsageTTL: "1s",
|
||||
AllocationMessage: "call",
|
||||
Weight: 10,
|
||||
Limit: "2",
|
||||
eResProfiles := map[utils.TenantID]*utils.TPResource{
|
||||
utils.TenantID{Tenant: "cgrates.org", ID: "ResGroup21"}: &utils.TPResource{
|
||||
TPid: testTPID,
|
||||
Tenant: "cgrates.org",
|
||||
ID: "ResGroup21",
|
||||
Filters: []*utils.TPRequestFilter{
|
||||
&utils.TPRequestFilter{Type: MetaString, FieldName: "HdrAccount", Values: []string{"1001", "1002"}},
|
||||
&utils.TPRequestFilter{Type: MetaStringPrefix, FieldName: "HdrDestination", Values: []string{"10", "20"}},
|
||||
&utils.TPRequestFilter{Type: MetaRSRFields, Values: []string{"HdrSubject(~^1.*1$)", "HdrDestination(1002)"}},
|
||||
},
|
||||
"ResGroup22": &utils.TPResource{
|
||||
TPid: testTPID,
|
||||
Tenant: "cgrates.org",
|
||||
ID: "ResGroup22",
|
||||
Filters: []*utils.TPRequestFilter{
|
||||
&utils.TPRequestFilter{Type: MetaDestinations, FieldName: "HdrDestination", Values: []string{"DST_FS"}},
|
||||
},
|
||||
ActivationInterval: &utils.TPActivationInterval{
|
||||
ActivationTime: "2014-07-29T15:00:00Z",
|
||||
},
|
||||
UsageTTL: "3600s",
|
||||
AllocationMessage: "premium_call",
|
||||
Blocker: true,
|
||||
Stored: true,
|
||||
Weight: 10,
|
||||
Limit: "2",
|
||||
ActivationInterval: &utils.TPActivationInterval{
|
||||
ActivationTime: "2014-07-29T15:00:00Z",
|
||||
},
|
||||
UsageTTL: "1s",
|
||||
AllocationMessage: "call",
|
||||
Weight: 10,
|
||||
Limit: "2",
|
||||
},
|
||||
utils.TenantID{Tenant: "cgrates.org", ID: "ResGroup22"}: &utils.TPResource{
|
||||
TPid: testTPID,
|
||||
Tenant: "cgrates.org",
|
||||
ID: "ResGroup22",
|
||||
Filters: []*utils.TPRequestFilter{
|
||||
&utils.TPRequestFilter{Type: MetaDestinations, FieldName: "HdrDestination", Values: []string{"DST_FS"}},
|
||||
},
|
||||
ActivationInterval: &utils.TPActivationInterval{
|
||||
ActivationTime: "2014-07-29T15:00:00Z",
|
||||
},
|
||||
UsageTTL: "3600s",
|
||||
AllocationMessage: "premium_call",
|
||||
Blocker: true,
|
||||
Stored: true,
|
||||
Weight: 10,
|
||||
Limit: "2",
|
||||
},
|
||||
}
|
||||
if len(csvr.resProfiles["cgrates.org"]) != len(eResProfiles["cgrates.org"]) {
|
||||
t.Errorf("Failed to load resourceProfiles: %s", utils.ToIJSON(csvr.resProfiles))
|
||||
} else if !reflect.DeepEqual(eResProfiles["cgrates.org"]["ResGroup22"], csvr.resProfiles["cgrates.org"]["ResGroup22"]) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", eResProfiles["cgrates.org"]["ResGroup22"], csvr.resProfiles["cgrates.org"]["ResGroup22"])
|
||||
resKey := utils.TenantID{Tenant: "cgrates.org", ID: "ResGroup21"}
|
||||
if len(csvr.resProfiles) != len(eResProfiles) {
|
||||
t.Errorf("Failed to load ResourceProfiles: %s", utils.ToIJSON(csvr.resProfiles))
|
||||
} else if !reflect.DeepEqual(eResProfiles[resKey], csvr.resProfiles[resKey]) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", eResProfiles[resKey], csvr.resProfiles[resKey])
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestLoadStatProfiles(t *testing.T) {
|
||||
eStats := map[string]map[string]*utils.TPStats{
|
||||
"cgrates.org": map[string]*utils.TPStats{
|
||||
"Stats1": &utils.TPStats{
|
||||
Tenant: "cgrates.org",
|
||||
TPid: testTPID,
|
||||
ID: "Stats1",
|
||||
Filters: []*utils.TPRequestFilter{
|
||||
&utils.TPRequestFilter{Type: MetaString, FieldName: "Account", Values: []string{"1001", "1002"}},
|
||||
},
|
||||
ActivationInterval: &utils.TPActivationInterval{
|
||||
ActivationTime: "2014-07-29T15:00:00Z",
|
||||
},
|
||||
QueueLength: 100,
|
||||
TTL: "1s",
|
||||
Metrics: []string{"*asr", "*acc", "*tcc", "*acd", "*tcd", "*pdd"},
|
||||
Thresholds: []string{"THRESH1", "THRESH2"},
|
||||
Blocker: true,
|
||||
Stored: true,
|
||||
Weight: 20,
|
||||
MinItems: 2,
|
||||
eStats := map[utils.TenantID]*utils.TPStats{
|
||||
utils.TenantID{Tenant: "cgrates.org", ID: "Stats1"}: &utils.TPStats{
|
||||
Tenant: "cgrates.org",
|
||||
TPid: testTPID,
|
||||
ID: "Stats1",
|
||||
Filters: []*utils.TPRequestFilter{
|
||||
&utils.TPRequestFilter{Type: MetaString, FieldName: "Account", Values: []string{"1001", "1002"}},
|
||||
},
|
||||
ActivationInterval: &utils.TPActivationInterval{
|
||||
ActivationTime: "2014-07-29T15:00:00Z",
|
||||
},
|
||||
QueueLength: 100,
|
||||
TTL: "1s",
|
||||
Metrics: []string{"*asr", "*acc", "*tcc", "*acd", "*tcd", "*pdd"},
|
||||
Thresholds: []string{"THRESH1", "THRESH2"},
|
||||
Blocker: true,
|
||||
Stored: true,
|
||||
Weight: 20,
|
||||
MinItems: 2,
|
||||
},
|
||||
}
|
||||
|
||||
if len(csvr.sqProfiles["cgrates.org"]) != len(eStats["cgrates.org"]) {
|
||||
t.Error("Failed to load stats: ", len(csvr.sqProfiles))
|
||||
} else if !reflect.DeepEqual(eStats["cgrates.org"]["Stats1"], csvr.sqProfiles["cgrates.org"]["Stats1"]) {
|
||||
t.Errorf("Expecting: %s, received: %s",
|
||||
utils.ToJSON(eStats["cgrates.org"]["Stats1"]), utils.ToJSON(csvr.sqProfiles["cgrates.org"]["Stats1"]))
|
||||
stKey := utils.TenantID{Tenant: "cgrates.org", ID: "Stats1"}
|
||||
if len(csvr.sqProfiles) != len(eStats) {
|
||||
t.Errorf("Failed to load StatQueueProfiles: %s", utils.ToIJSON(csvr.sqProfiles))
|
||||
} else if !reflect.DeepEqual(eStats[stKey], csvr.sqProfiles[stKey]) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", eStats[stKey], csvr.sqProfiles[stKey])
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadThresholdProfiles(t *testing.T) {
|
||||
eThresholds := map[string]map[string]*utils.TPThreshold{
|
||||
"cgrates.org": map[string]*utils.TPThreshold{
|
||||
"Threshold1": &utils.TPThreshold{
|
||||
TPid: testTPID,
|
||||
Tenant: "cgrates.org",
|
||||
ID: "Threshold1",
|
||||
FilterIDs: []string{"FilterID1", "FilterID2"},
|
||||
ActivationInterval: &utils.TPActivationInterval{
|
||||
ActivationTime: "2014-07-29T15:00:00Z",
|
||||
},
|
||||
Recurrent: true,
|
||||
MinHits: 10,
|
||||
MinSleep: "1s",
|
||||
Blocker: true,
|
||||
Weight: 10,
|
||||
ActionIDs: []string{"THRESH1", "THRESH2"},
|
||||
Async: true,
|
||||
eThresholds := map[utils.TenantID]*utils.TPThreshold{
|
||||
utils.TenantID{Tenant: "cgrates.org", ID: "Threshold1"}: &utils.TPThreshold{
|
||||
TPid: testTPID,
|
||||
Tenant: "cgrates.org",
|
||||
ID: "Threshold1",
|
||||
FilterIDs: []string{"FilterID1", "FilterID2"},
|
||||
ActivationInterval: &utils.TPActivationInterval{
|
||||
ActivationTime: "2014-07-29T15:00:00Z",
|
||||
},
|
||||
Recurrent: true,
|
||||
MinHits: 10,
|
||||
MinSleep: "1s",
|
||||
Blocker: true,
|
||||
Weight: 10,
|
||||
ActionIDs: []string{"THRESH1", "THRESH2"},
|
||||
Async: true,
|
||||
},
|
||||
}
|
||||
if len(csvr.thProfiles["cgrates.org"]) != len(eThresholds["cgrates.org"]) {
|
||||
t.Error("Failed to load thresholds: ", len(csvr.thProfiles))
|
||||
} else if !reflect.DeepEqual(eThresholds["cgrates.org"]["Threshold1"], csvr.thProfiles["cgrates.org"]["Threshold1"]) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", eThresholds["cgrates.org"]["Threshold1"], csvr.thProfiles["cgrates.org"]["Threshold1"])
|
||||
thkey := utils.TenantID{Tenant: "cgrates.org", ID: "Threshold1"}
|
||||
if len(csvr.thProfiles) != len(eThresholds) {
|
||||
t.Errorf("Failed to load ThresholdProfiles: %s", utils.ToIJSON(csvr.thProfiles))
|
||||
} else if !reflect.DeepEqual(eThresholds[thkey], csvr.thProfiles[thkey]) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", eThresholds[thkey], csvr.thProfiles[thkey])
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadFilters(t *testing.T) {
|
||||
eFilters := map[utils.TenantID]*utils.TPFilter{
|
||||
utils.TenantID{"cgrates.org", "FLTR_1"}: &utils.TPFilter{
|
||||
utils.TenantID{Tenant: "cgrates.org", ID: "FLTR_1"}: &utils.TPFilter{
|
||||
TPid: testTPID,
|
||||
Tenant: "cgrates.org",
|
||||
ID: "FLTR_1",
|
||||
@@ -1535,7 +1530,7 @@ func TestLoadFilters(t *testing.T) {
|
||||
ActivationTime: "2014-07-29T15:00:00Z",
|
||||
},
|
||||
},
|
||||
utils.TenantID{"cgrates.org", "FLTR_ACNT_dan"}: &utils.TPFilter{
|
||||
utils.TenantID{Tenant: "cgrates.org", ID: "FLTR_ACNT_dan"}: &utils.TPFilter{
|
||||
TPid: testTPID,
|
||||
Tenant: "cgrates.org",
|
||||
ID: "FLTR_ACNT_dan",
|
||||
@@ -1550,7 +1545,7 @@ func TestLoadFilters(t *testing.T) {
|
||||
ActivationTime: "2014-07-29T15:00:00Z",
|
||||
},
|
||||
},
|
||||
utils.TenantID{"cgrates.org", "FLTR_DST_DE"}: &utils.TPFilter{
|
||||
utils.TenantID{Tenant: "cgrates.org", ID: "FLTR_DST_DE"}: &utils.TPFilter{
|
||||
TPid: testTPID,
|
||||
Tenant: "cgrates.org",
|
||||
ID: "FLTR_DST_DE",
|
||||
@@ -1565,7 +1560,7 @@ func TestLoadFilters(t *testing.T) {
|
||||
ActivationTime: "2014-07-29T15:00:00Z",
|
||||
},
|
||||
},
|
||||
utils.TenantID{"cgrates.org", "FLTR_DST_NL"}: &utils.TPFilter{
|
||||
utils.TenantID{Tenant: "cgrates.org", ID: "FLTR_DST_NL"}: &utils.TPFilter{
|
||||
TPid: testTPID,
|
||||
Tenant: "cgrates.org",
|
||||
ID: "FLTR_DST_NL",
|
||||
@@ -1581,9 +1576,9 @@ func TestLoadFilters(t *testing.T) {
|
||||
},
|
||||
},
|
||||
}
|
||||
fltrKey := utils.TenantID{"cgrates.org", "FLTR_1"}
|
||||
fltrKey := utils.TenantID{Tenant: "cgrates.org", ID: "FLTR_1"}
|
||||
if len(csvr.filters) != len(eFilters) {
|
||||
t.Errorf("Failed to load FilterProfiles: %s", utils.ToIJSON(csvr.filters))
|
||||
t.Errorf("Failed to load Filters: %s", utils.ToIJSON(csvr.filters))
|
||||
} else if !reflect.DeepEqual(eFilters[fltrKey], csvr.filters[fltrKey]) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", eFilters[fltrKey], csvr.filters[fltrKey])
|
||||
}
|
||||
|
||||
@@ -313,50 +313,44 @@ func TestLoaderITWriteToDatabase(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
for _, mapIDs := range loader.resProfiles {
|
||||
for _, rl := range mapIDs {
|
||||
rcv, err := loader.dm.GetResourceProfile(rl.Tenant, rl.ID, true, utils.NonTransactional)
|
||||
if err != nil {
|
||||
t.Error("Failed GetResourceProfile: ", err.Error())
|
||||
}
|
||||
rlT, err := APItoResource(rl, "UTC")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if !reflect.DeepEqual(rlT, rcv) {
|
||||
t.Errorf("Expecting: %v, received: %v", rlT, rcv)
|
||||
}
|
||||
for tenantid, rl := range loader.resProfiles {
|
||||
rcv, err := loader.dm.GetResourceProfile(tenantid.Tenant, tenantid.ID, true, utils.NonTransactional)
|
||||
if err != nil {
|
||||
t.Error("Failed GetResourceProfile: ", err.Error())
|
||||
}
|
||||
rlT, err := APItoResource(rl, "UTC")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if !reflect.DeepEqual(rlT, rcv) {
|
||||
t.Errorf("Expecting: %v, received: %v", rlT, rcv)
|
||||
}
|
||||
}
|
||||
for _, mpIDs := range loader.sqProfiles {
|
||||
for _, st := range mpIDs {
|
||||
rcv, err := loader.dm.GetStatQueueProfile(st.Tenant, st.ID, true, utils.NonTransactional)
|
||||
if err != nil {
|
||||
t.Errorf("Failed GetStatsQueue, tenant: %s, id: %s, error: %s ", st.Tenant, st.ID, err.Error())
|
||||
}
|
||||
sts, err := APItoStats(st, "UTC")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if !reflect.DeepEqual(sts, rcv) {
|
||||
t.Errorf("Expecting: %v, received: %v", sts, rcv)
|
||||
}
|
||||
for tenantid, st := range loader.sqProfiles {
|
||||
rcv, err := loader.dm.GetStatQueueProfile(tenantid.Tenant, tenantid.ID, true, utils.NonTransactional)
|
||||
if err != nil {
|
||||
t.Errorf("Failed GetStatsQueue, tenant: %s, id: %s, error: %s ", tenantid.Tenant, tenantid.ID, err.Error())
|
||||
}
|
||||
sts, err := APItoStats(st, "UTC")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if !reflect.DeepEqual(sts, rcv) {
|
||||
t.Errorf("Expecting: %v, received: %v", sts, rcv)
|
||||
}
|
||||
}
|
||||
|
||||
for _, mpIDs := range loader.thProfiles {
|
||||
for _, th := range mpIDs {
|
||||
rcv, err := loader.dm.GetThresholdProfile(th.Tenant, th.ID, true, utils.NonTransactional)
|
||||
if err != nil {
|
||||
t.Errorf("Failed GetThresholdProfile, tenant: %s, id: %s, error: %s ", th.Tenant, th.ID, err.Error())
|
||||
}
|
||||
sts, err := APItoThresholdProfile(th, "UTC")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if !reflect.DeepEqual(sts, rcv) {
|
||||
t.Errorf("Expecting: %v, received: %v", sts, rcv)
|
||||
}
|
||||
for tenatid, th := range loader.thProfiles {
|
||||
rcv, err := loader.dm.GetThresholdProfile(tenatid.Tenant, tenatid.ID, true, utils.NonTransactional)
|
||||
if err != nil {
|
||||
t.Errorf("Failed GetThresholdProfile, tenant: %s, id: %s, error: %s ", th.Tenant, th.ID, err.Error())
|
||||
}
|
||||
sts, err := APItoThresholdProfile(th, "UTC")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if !reflect.DeepEqual(sts, rcv) {
|
||||
t.Errorf("Expecting: %v, received: %v", sts, rcv)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,9 +53,9 @@ type TpReader struct {
|
||||
cdrStats map[string]*CdrStats
|
||||
users map[string]*UserProfile
|
||||
aliases map[string]*Alias
|
||||
resProfiles map[string]map[string]*utils.TPResource
|
||||
sqProfiles map[string]map[string]*utils.TPStats
|
||||
thProfiles map[string]map[string]*utils.TPThreshold
|
||||
resProfiles map[utils.TenantID]*utils.TPResource
|
||||
sqProfiles map[utils.TenantID]*utils.TPStats
|
||||
thProfiles map[utils.TenantID]*utils.TPThreshold
|
||||
filters map[utils.TenantID]*utils.TPFilter
|
||||
resources []*utils.TenantID // IDs of resources which need creation based on resourceProfiles
|
||||
statQueues []*utils.TenantID // IDs of statQueues which need creation based on statQueueProfiles
|
||||
@@ -131,9 +131,9 @@ func (tpr *TpReader) Init() {
|
||||
tpr.users = make(map[string]*UserProfile)
|
||||
tpr.aliases = make(map[string]*Alias)
|
||||
tpr.derivedChargers = make(map[string]*utils.DerivedChargers)
|
||||
tpr.resProfiles = make(map[string]map[string]*utils.TPResource)
|
||||
tpr.sqProfiles = make(map[string]map[string]*utils.TPStats)
|
||||
tpr.thProfiles = make(map[string]map[string]*utils.TPThreshold)
|
||||
tpr.resProfiles = make(map[utils.TenantID]*utils.TPResource)
|
||||
tpr.sqProfiles = make(map[utils.TenantID]*utils.TPStats)
|
||||
tpr.thProfiles = make(map[utils.TenantID]*utils.TPThreshold)
|
||||
tpr.filters = make(map[utils.TenantID]*utils.TPFilter)
|
||||
tpr.revDests = make(map[string][]string)
|
||||
tpr.revAliases = make(map[string][]string)
|
||||
@@ -1603,22 +1603,16 @@ func (tpr *TpReader) LoadResourceProfilesFiltered(tag string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
mapRsPfls := make(map[string]map[string]*utils.TPResource)
|
||||
mapRsPfls := make(map[utils.TenantID]*utils.TPResource)
|
||||
for _, rl := range rls {
|
||||
if _, has := mapRsPfls[rl.Tenant]; !has {
|
||||
mapRsPfls[rl.Tenant] = make(map[string]*utils.TPResource)
|
||||
}
|
||||
mapRsPfls[rl.Tenant][rl.ID] = rl
|
||||
mapRsPfls[utils.TenantID{Tenant: rl.Tenant, ID: rl.ID}] = rl
|
||||
}
|
||||
tpr.resProfiles = mapRsPfls
|
||||
for tenant, mpID := range mapRsPfls {
|
||||
for id := range mpID {
|
||||
rTid := &utils.TenantID{tenant, id}
|
||||
if has, err := tpr.dm.DataDB().HasData(utils.ResourcesPrefix, rTid.TenantID()); err != nil {
|
||||
return err
|
||||
} else if !has {
|
||||
tpr.resources = append(tpr.resources, rTid)
|
||||
}
|
||||
for tenantid, _ := range mapRsPfls {
|
||||
if has, err := tpr.dm.DataDB().HasData(utils.ResourcesPrefix, tenantid.TenantID()); err != nil {
|
||||
return err
|
||||
} else if !has {
|
||||
tpr.resources = append(tpr.resources, &utils.TenantID{Tenant: tenantid.Tenant, ID: tenantid.ID})
|
||||
}
|
||||
}
|
||||
return nil
|
||||
@@ -1633,22 +1627,16 @@ func (tpr *TpReader) LoadStatsFiltered(tag string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
mapSTs := make(map[string]map[string]*utils.TPStats)
|
||||
mapSTs := make(map[utils.TenantID]*utils.TPStats)
|
||||
for _, st := range tps {
|
||||
if _, has := mapSTs[st.Tenant]; !has {
|
||||
mapSTs[st.Tenant] = make(map[string]*utils.TPStats)
|
||||
}
|
||||
mapSTs[st.Tenant][st.ID] = st
|
||||
mapSTs[utils.TenantID{Tenant: st.Tenant, ID: st.ID}] = st
|
||||
}
|
||||
tpr.sqProfiles = mapSTs
|
||||
for tenant, mpID := range mapSTs {
|
||||
for sqID := range mpID {
|
||||
sqTntID := &utils.TenantID{tenant, sqID}
|
||||
if has, err := tpr.dm.DataDB().HasData(utils.StatQueuePrefix, sqTntID.TenantID()); err != nil {
|
||||
return err
|
||||
} else if !has {
|
||||
tpr.statQueues = append(tpr.statQueues, sqTntID)
|
||||
}
|
||||
for tenantid, _ := range mapSTs {
|
||||
if has, err := tpr.dm.DataDB().HasData(utils.StatQueuePrefix, tenantid.TenantID()); err != nil {
|
||||
return err
|
||||
} else if !has {
|
||||
tpr.statQueues = append(tpr.statQueues, &utils.TenantID{Tenant: tenantid.Tenant, ID: tenantid.ID})
|
||||
}
|
||||
}
|
||||
return nil
|
||||
@@ -1663,43 +1651,38 @@ func (tpr *TpReader) LoadThresholdsFiltered(tag string) (err error) {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
mapTHs := make(map[string]map[string]*utils.TPThreshold)
|
||||
mapTHs := make(map[utils.TenantID]*utils.TPThreshold)
|
||||
for _, th := range tps {
|
||||
if _, has := mapTHs[th.Tenant]; !has {
|
||||
mapTHs[th.Tenant] = make(map[string]*utils.TPThreshold)
|
||||
}
|
||||
mapTHs[th.Tenant][th.ID] = th
|
||||
mapTHs[utils.TenantID{th.Tenant, th.ID}] = th
|
||||
}
|
||||
tpr.thProfiles = mapTHs
|
||||
for tenant, mpID := range mapTHs {
|
||||
thdIndxrKey := utils.ThresholdStringIndex + tenant
|
||||
for thID, t := range mpID {
|
||||
thTntID := &utils.TenantID{Tenant: tenant, ID: thID}
|
||||
if has, err := tpr.dm.DataDB().HasData(utils.ThresholdPrefix, thTntID.TenantID()); err != nil {
|
||||
return err
|
||||
} else if !has {
|
||||
tpr.thresholds = append(tpr.thresholds, thTntID)
|
||||
for tenant, th := range mapTHs {
|
||||
thdIndxrKey := utils.ThresholdStringIndex + tenant.TenantID()
|
||||
if has, err := tpr.dm.DataDB().HasData(utils.ThresholdPrefix, tenant.TenantID()); err != nil {
|
||||
return err
|
||||
} else if !has {
|
||||
tpr.thresholds = append(tpr.thresholds, &utils.TenantID{Tenant: tenant.Tenant, ID: tenant.ID})
|
||||
}
|
||||
// index thresholds for filters
|
||||
if _, has := tpr.thdsIndexers[tenant.TenantID()]; !has {
|
||||
if tpr.thdsIndexers[tenant.TenantID()], err = NewReqFilterIndexer(tpr.dm, thdIndxrKey); err != nil {
|
||||
return
|
||||
}
|
||||
// index thresholds for filters
|
||||
if _, has := tpr.thdsIndexers[tenant]; !has {
|
||||
if tpr.thdsIndexers[tenant], err = NewReqFilterIndexer(tpr.dm, thdIndxrKey); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
for _, fltrID := range t.FilterIDs {
|
||||
tpFltr, has := tpr.filters[utils.TenantID{tenant, fltrID}]
|
||||
if !has {
|
||||
var fltr *Filter
|
||||
if fltr, err = tpr.dm.GetFilter(tenant, fltrID, false, utils.NonTransactional); err != nil {
|
||||
if err == utils.ErrNotFound {
|
||||
err = fmt.Errorf("broken reference to filter: %s for threshold: %s", fltrID, thID)
|
||||
}
|
||||
return
|
||||
} else {
|
||||
tpFltr = FilterToTPFilter(fltr)
|
||||
}
|
||||
for _, fltrID := range th.FilterIDs {
|
||||
tpFltr, has := tpr.filters[utils.TenantID{Tenant: tenant.Tenant, ID: fltrID}]
|
||||
if !has {
|
||||
var fltr *Filter
|
||||
if fltr, err = tpr.dm.GetFilter(tenant.Tenant, fltrID, false, utils.NonTransactional); err != nil {
|
||||
if err == utils.ErrNotFound {
|
||||
err = fmt.Errorf("broken reference to filter: %s for threshold: %s", fltrID, th)
|
||||
}
|
||||
return
|
||||
} else {
|
||||
tpFltr = FilterToTPFilter(fltr)
|
||||
}
|
||||
tpr.thdsIndexers[tenant].IndexTPFilter(tpFltr, thID)
|
||||
} else {
|
||||
tpr.thdsIndexers[tenant.TenantID()].IndexTPFilter(tpFltr, th.ID)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2038,18 +2021,16 @@ func (tpr *TpReader) WriteToDatabase(flush, verbose, disable_reverse bool) (err
|
||||
if verbose {
|
||||
log.Print("ResourceProfiles:")
|
||||
}
|
||||
for _, mpID := range tpr.resProfiles {
|
||||
for _, tpRsp := range mpID {
|
||||
rsp, err := APItoResource(tpRsp, tpr.timezone)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err = tpr.dm.SetResourceProfile(rsp); err != nil {
|
||||
return err
|
||||
}
|
||||
if verbose {
|
||||
log.Print("\t", rsp.TenantID())
|
||||
}
|
||||
for _, tpRsp := range tpr.resProfiles {
|
||||
rsp, err := APItoResource(tpRsp, tpr.timezone)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err = tpr.dm.SetResourceProfile(rsp); err != nil {
|
||||
return err
|
||||
}
|
||||
if verbose {
|
||||
log.Print("\t", rsp.TenantID())
|
||||
}
|
||||
}
|
||||
if verbose {
|
||||
@@ -2066,18 +2047,16 @@ func (tpr *TpReader) WriteToDatabase(flush, verbose, disable_reverse bool) (err
|
||||
if verbose {
|
||||
log.Print("StatQueueProfiles:")
|
||||
}
|
||||
for _, mpID := range tpr.sqProfiles {
|
||||
for _, tpST := range mpID {
|
||||
st, err := APItoStats(tpST, tpr.timezone)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err = tpr.dm.SetStatQueueProfile(st); err != nil {
|
||||
return err
|
||||
}
|
||||
if verbose {
|
||||
log.Print("\t", st.TenantID())
|
||||
}
|
||||
for _, tpST := range tpr.sqProfiles {
|
||||
st, err := APItoStats(tpST, tpr.timezone)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err = tpr.dm.SetStatQueueProfile(st); err != nil {
|
||||
return err
|
||||
}
|
||||
if verbose {
|
||||
log.Print("\t", st.TenantID())
|
||||
}
|
||||
}
|
||||
if verbose {
|
||||
@@ -2086,8 +2065,8 @@ func (tpr *TpReader) WriteToDatabase(flush, verbose, disable_reverse bool) (err
|
||||
for _, sqTntID := range tpr.statQueues {
|
||||
sq := &StatQueue{Tenant: sqTntID.Tenant, ID: sqTntID.ID,
|
||||
SQMetrics: make(map[string]StatMetric)}
|
||||
for _, metricID := range tpr.sqProfiles[sqTntID.Tenant][sqTntID.ID].Metrics {
|
||||
if metric, err := NewStatMetric(metricID, tpr.sqProfiles[sqTntID.Tenant][sqTntID.ID].MinItems); err != nil {
|
||||
for _, metricID := range tpr.sqProfiles[utils.TenantID{Tenant: sqTntID.Tenant, ID: sqTntID.ID}].Metrics {
|
||||
if metric, err := NewStatMetric(metricID, tpr.sqProfiles[utils.TenantID{Tenant: sqTntID.Tenant, ID: sqTntID.ID}].MinItems); err != nil {
|
||||
return err
|
||||
} else {
|
||||
sq.SQMetrics[metricID] = metric
|
||||
@@ -2103,18 +2082,16 @@ func (tpr *TpReader) WriteToDatabase(flush, verbose, disable_reverse bool) (err
|
||||
if verbose {
|
||||
log.Print("ThresholdProfiles:")
|
||||
}
|
||||
for _, mpID := range tpr.thProfiles {
|
||||
for _, tpTH := range mpID {
|
||||
th, err := APItoThresholdProfile(tpTH, tpr.timezone)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err = tpr.dm.SetThresholdProfile(th); err != nil {
|
||||
return err
|
||||
}
|
||||
if verbose {
|
||||
log.Print("\t", th.TenantID())
|
||||
}
|
||||
for _, tpTH := range tpr.thProfiles {
|
||||
th, err := APItoThresholdProfile(tpTH, tpr.timezone)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err = tpr.dm.SetThresholdProfile(th); err != nil {
|
||||
return err
|
||||
}
|
||||
if verbose {
|
||||
log.Print("\t", th.TenantID())
|
||||
}
|
||||
}
|
||||
if verbose {
|
||||
@@ -2168,20 +2145,18 @@ func (tpr *TpReader) WriteToDatabase(flush, verbose, disable_reverse bool) (err
|
||||
if verbose {
|
||||
log.Print("Indexing resource profiles")
|
||||
}
|
||||
for tenant, mpID := range tpr.resProfiles {
|
||||
rlIdxr, err := NewReqFilterIndexer(tpr.dm, utils.ResourceProfilesStringIndex+tenant)
|
||||
for tenantid, tpRL := range tpr.resProfiles {
|
||||
rlIdxr, err := NewReqFilterIndexer(tpr.dm, utils.ResourceProfilesStringIndex+tenantid.TenantID())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, tpRL := range mpID {
|
||||
if rl, err := APItoResource(tpRL, tpr.timezone); err != nil {
|
||||
return err
|
||||
} else {
|
||||
rlIdxr.IndexFilters(rl.ID, rl.Filters)
|
||||
}
|
||||
if rl, err := APItoResource(tpRL, tpr.timezone); err != nil {
|
||||
return err
|
||||
} else {
|
||||
rlIdxr.IndexFilters(rl.ID, rl.Filters)
|
||||
}
|
||||
if verbose {
|
||||
log.Printf("Indexed ResourceProfile tenant: %s, keys: %+v", tenant, rlIdxr.ChangedKeys().Slice())
|
||||
log.Printf("Indexed ResourceProfile tenant: %s, keys: %+v", tenantid.TenantID(), rlIdxr.ChangedKeys().Slice())
|
||||
}
|
||||
if err := rlIdxr.StoreIndexes(); err != nil {
|
||||
return err
|
||||
@@ -2192,20 +2167,18 @@ func (tpr *TpReader) WriteToDatabase(flush, verbose, disable_reverse bool) (err
|
||||
if verbose {
|
||||
log.Print("Indexing stats")
|
||||
}
|
||||
for tenant, mpID := range tpr.sqProfiles {
|
||||
stIdxr, err := NewReqFilterIndexer(tpr.dm, utils.StatQueuesStringIndex+tenant)
|
||||
for tenantid, st := range tpr.sqProfiles {
|
||||
stIdxr, err := NewReqFilterIndexer(tpr.dm, utils.StatQueuesStringIndex+tenantid.TenantID())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, tpST := range mpID {
|
||||
if st, err := APItoStats(tpST, tpr.timezone); err != nil {
|
||||
return err
|
||||
} else {
|
||||
stIdxr.IndexFilters(st.ID, st.Filters)
|
||||
}
|
||||
if st, err := APItoStats(st, tpr.timezone); err != nil {
|
||||
return err
|
||||
} else {
|
||||
stIdxr.IndexFilters(st.ID, st.Filters)
|
||||
}
|
||||
if verbose {
|
||||
log.Printf("Indexed Stats tenant: %s, keys %+v", tenant, stIdxr.ChangedKeys().Slice())
|
||||
log.Printf("Indexed Stats tenant: %s, keys %+v", tenantid.TenantID(), stIdxr.ChangedKeys().Slice())
|
||||
}
|
||||
if err := stIdxr.StoreIndexes(); err != nil {
|
||||
return err
|
||||
@@ -2402,11 +2375,11 @@ func (tpr *TpReader) GetLoadedIds(categ string) ([]string, error) {
|
||||
}
|
||||
return keys, nil
|
||||
case utils.ResourceProfilesPrefix:
|
||||
keys := make([]string, 0)
|
||||
for tenant, mpID := range tpr.resProfiles {
|
||||
for id := range mpID {
|
||||
keys = append(keys, utils.ConcatenatedKey(tenant, id))
|
||||
}
|
||||
keys := make([]string, len(tpr.resProfiles))
|
||||
i := 0
|
||||
for k, _ := range tpr.resProfiles {
|
||||
keys[i] = k.TenantID()
|
||||
i++
|
||||
}
|
||||
return keys, nil
|
||||
case utils.ACTION_TRIGGER_PREFIX:
|
||||
@@ -2428,16 +2401,16 @@ func (tpr *TpReader) GetLoadedIds(categ string) ([]string, error) {
|
||||
case utils.StatQueueProfilePrefix:
|
||||
keys := make([]string, len(tpr.sqProfiles))
|
||||
i := 0
|
||||
for k := range tpr.sqProfiles {
|
||||
keys[i] = k
|
||||
for k, _ := range tpr.sqProfiles {
|
||||
keys[i] = k.TenantID()
|
||||
i++
|
||||
}
|
||||
return keys, nil
|
||||
case utils.ThresholdProfilePrefix:
|
||||
keys := make([]string, len(tpr.thProfiles))
|
||||
i := 0
|
||||
for k := range tpr.thProfiles {
|
||||
keys[i] = k
|
||||
for k, _ := range tpr.thProfiles {
|
||||
keys[i] = k.TenantID()
|
||||
i++
|
||||
}
|
||||
return keys, nil
|
||||
|
||||
Reference in New Issue
Block a user