Update tp threshold to consider tenant

This commit is contained in:
TeoV
2019-01-09 04:07:52 -05:00
committed by Dan Christian Bogos
parent 08b76e84f5
commit d52e62daf3
11 changed files with 47 additions and 70 deletions

View File

@@ -23,29 +23,23 @@ import (
)
// Creates a new threshold within a tariff plan
func (self *ApierV1) SetTPThreshold(attr utils.TPThreshold, reply *string) error {
if missing := utils.MissingStructFields(&attr, []string{"TPid", "Tenant", "ID"}); len(missing) != 0 {
func (self *ApierV1) SetTPThreshold(attr *utils.TPThreshold, reply *string) error {
if missing := utils.MissingStructFields(attr, []string{"TPid", "Tenant", "ID"}); len(missing) != 0 {
return utils.NewErrMandatoryIeMissing(missing...)
}
if err := self.StorDb.SetTPThresholds([]*utils.TPThreshold{&attr}); err != nil {
if err := self.StorDb.SetTPThresholds([]*utils.TPThreshold{attr}); err != nil {
return utils.APIErrorHandler(err)
}
*reply = utils.OK
return nil
}
type AttrGetTPThreshold struct {
TPid string // Tariff plan id
Tenant string
ID string
}
// Queries specific Threshold on Tariff plan
func (self *ApierV1) GetTPThreshold(attr AttrGetTPThreshold, reply *utils.TPThreshold) error {
if missing := utils.MissingStructFields(&attr, []string{"TPid", "ID"}); len(missing) != 0 { //Params missing
func (self *ApierV1) GetTPThreshold(attr *utils.TPTntID, reply *utils.TPThreshold) error {
if missing := utils.MissingStructFields(attr, []string{"TPid", "Tenant", "ID"}); len(missing) != 0 { //Params missing
return utils.NewErrMandatoryIeMissing(missing...)
}
if rls, err := self.StorDb.GetTPThresholds(attr.TPid, attr.ID); err != nil {
if rls, err := self.StorDb.GetTPThresholds(attr.TPid, attr.Tenant, attr.ID); err != nil {
if err.Error() != utils.ErrNotFound.Error() {
err = utils.NewErrServerError(err)
}
@@ -63,8 +57,8 @@ type AttrGetTPThresholdIds struct {
}
// Queries Threshold identities on specific tariff plan.
func (self *ApierV1) GetTPThresholdIDs(attrs AttrGetTPThresholdIds, reply *[]string) error {
if missing := utils.MissingStructFields(&attrs, []string{"TPid"}); len(missing) != 0 { //Params missing
func (self *ApierV1) GetTPThresholdIDs(attrs *AttrGetTPThresholdIds, reply *[]string) error {
if missing := utils.MissingStructFields(attrs, []string{"TPid"}); len(missing) != 0 { //Params missing
return utils.NewErrMandatoryIeMissing(missing...)
}
if ids, err := self.StorDb.GetTpTableIds(attrs.TPid, utils.TBLTPThresholds, utils.TPDistinctIds{"id"}, nil, &attrs.Paginator); err != nil {
@@ -79,11 +73,12 @@ func (self *ApierV1) GetTPThresholdIDs(attrs AttrGetTPThresholdIds, reply *[]str
}
// Removes specific Threshold on Tariff plan
func (self *ApierV1) RemTPThreshold(attrs AttrGetTPThreshold, reply *string) error {
if missing := utils.MissingStructFields(&attrs, []string{"TPid", "Tenant", "ID"}); len(missing) != 0 { //Params missing
func (self *ApierV1) RemTPThreshold(attrs *utils.TPTntID, reply *string) error {
if missing := utils.MissingStructFields(attrs, []string{"TPid", "Tenant", "ID"}); len(missing) != 0 { //Params missing
return utils.NewErrMandatoryIeMissing(missing...)
}
if err := self.StorDb.RemTpData(utils.TBLTPThresholds, attrs.TPid, map[string]string{"tenant": attrs.Tenant, "id": attrs.ID}); err != nil {
if err := self.StorDb.RemTpData(utils.TBLTPThresholds, attrs.TPid,
map[string]string{"tenant": attrs.Tenant, "id": attrs.ID}); err != nil {
return utils.NewErrServerError(err)
} else {
*reply = utils.OK

View File

@@ -59,21 +59,21 @@ var sTestsTPThreshold = []func(t *testing.T){
}
//Test start here
func TestTPThreholdITMySql(t *testing.T) {
func TestTPThresholdITMySql(t *testing.T) {
tpThresholdConfigDIR = "tutmysql"
for _, stest := range sTestsTPThreshold {
t.Run(tpThresholdConfigDIR, stest)
}
}
func TestTPThreholdITMongo(t *testing.T) {
func TestTPThresholdITMongo(t *testing.T) {
tpThresholdConfigDIR = "tutmongo"
for _, stest := range sTestsTPThreshold {
t.Run(tpThresholdConfigDIR, stest)
}
}
func TestTPThreholdITPG(t *testing.T) {
func TestTPThresholdITPG(t *testing.T) {
tpThresholdConfigDIR = "tutpostgres"
for _, stest := range sTestsTPThreshold {
t.Run(tpThresholdConfigDIR, stest)
@@ -89,12 +89,8 @@ func testTPThreholdInitCfg(t *testing.T) {
}
tpThresholdCfg.DataFolderPath = tpThresholdDataDir // Share DataFolderPath through config towards StoreDb for Flush()
config.SetCgrConfig(tpThresholdCfg)
switch tpThresholdConfigDIR {
case "tutmongo": // Mongo needs more time to reset db
tpThresholdDelay = 2000
default:
tpThresholdDelay = 1000
}
tpThresholdDelay = 1000
}
// Wipe out the cdr database
@@ -123,7 +119,8 @@ func testTPThreholdRpcConn(t *testing.T) {
func testTPThreholdGetTPThreholdBeforeSet(t *testing.T) {
var reply *utils.TPThreshold
if err := tpThresholdRPC.Call("ApierV1.GetTPThreshold",
AttrGetTPThreshold{TPid: "TH1", ID: "Threshold"}, &reply); err == nil || err.Error() != utils.ErrNotFound.Error() {
&utils.TPTntID{TPid: "TH1", Tenant: "cgrates.org", ID: "Threshold"}, &reply); err == nil ||
err.Error() != utils.ErrNotFound.Error() {
t.Error(err)
}
}
@@ -132,7 +129,7 @@ func testTPThreholdSetTPThrehold(t *testing.T) {
tpThreshold = &utils.TPThreshold{
TPid: "TH1",
Tenant: "cgrates.org",
ID: "Threhold",
ID: "Threshold",
FilterIDs: []string{"FLTR_1", "FLTR_2"},
ActivationInterval: &utils.TPActivationInterval{
ActivationTime: "2014-07-29T15:00:00Z",
@@ -154,7 +151,8 @@ func testTPThreholdSetTPThrehold(t *testing.T) {
func testTPThreholdGetTPThreholdAfterSet(t *testing.T) {
var respond *utils.TPThreshold
if err := tpThresholdRPC.Call("ApierV1.GetTPThreshold", &AttrGetTPThreshold{TPid: tpThreshold.TPid, ID: tpThreshold.ID}, &respond); err != nil {
if err := tpThresholdRPC.Call("ApierV1.GetTPThreshold",
&utils.TPTntID{TPid: "TH1", Tenant: "cgrates.org", ID: "Threshold"}, &respond); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(tpThreshold, respond) {
t.Errorf("Expecting: %+v, received: %+v", tpThreshold, respond)
@@ -163,8 +161,9 @@ func testTPThreholdGetTPThreholdAfterSet(t *testing.T) {
func testTPThreholdGetTPThreholdIds(t *testing.T) {
var result []string
expectedTPID := []string{"Threhold"}
if err := tpThresholdRPC.Call("ApierV1.GetTPThresholdIDs", &AttrGetTPThresholdIds{TPid: tpThreshold.TPid}, &result); err != nil {
expectedTPID := []string{"Threshold"}
if err := tpThresholdRPC.Call("ApierV1.GetTPThresholdIDs",
&AttrGetTPThresholdIds{TPid: tpThreshold.TPid}, &result); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(result, expectedTPID) {
t.Errorf("Expecting: %+v, received: %+v", result, expectedTPID)
@@ -183,7 +182,8 @@ func testTPThreholdUpdateTPThrehold(t *testing.T) {
func testTPThreholdGetTPThreholdAfterUpdate(t *testing.T) {
var respond *utils.TPThreshold
if err := tpThresholdRPC.Call("ApierV1.GetTPThreshold", &AttrGetTPThreshold{TPid: tpThreshold.TPid, ID: tpThreshold.ID}, &respond); err != nil {
if err := tpThresholdRPC.Call("ApierV1.GetTPThreshold",
&utils.TPTntID{TPid: "TH1", Tenant: "cgrates.org", ID: "Threshold"}, &respond); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(tpThreshold, respond) {
t.Errorf("Expecting: %+v, received: %+v", tpThreshold, respond)
@@ -192,7 +192,8 @@ func testTPThreholdGetTPThreholdAfterUpdate(t *testing.T) {
func testTPThreholdRemTPThrehold(t *testing.T) {
var resp string
if err := tpThresholdRPC.Call("ApierV1.RemTPThreshold", &AttrGetTPThreshold{TPid: "TH1", ID: "Threhold"}, &resp); err != nil {
if err := tpThresholdRPC.Call("ApierV1.RemTPThreshold",
&utils.TPTntID{TPid: "TH1", Tenant: "cgrates.org", ID: "Threshold"}, &resp); err != nil {
t.Error(err)
} else if resp != utils.OK {
t.Error("Unexpected reply returned", resp)
@@ -201,7 +202,9 @@ func testTPThreholdRemTPThrehold(t *testing.T) {
func testTPThreholdGetTPThreholdAfterRemove(t *testing.T) {
var reply *utils.TPThreshold
if err := tpThresholdRPC.Call("ApierV1.GetTPThreshold", AttrGetTPThreshold{TPid: "TH1", ID: "Threshold"}, &reply); err == nil || err.Error() != utils.ErrNotFound.Error() {
if err := tpThresholdRPC.Call("ApierV1.GetTPThreshold",
&utils.TPTntID{TPid: "TH1", Tenant: "cgrates.org", ID: "Threshold"}, &reply); err == nil ||
err.Error() != utils.ErrNotFound.Error() {
t.Error(err)
}
}

View File

@@ -593,7 +593,7 @@ func (csvs *CSVStorage) GetTPStats(tpid, tenant, id string) ([]*utils.TPStats, e
return tpStats.AsTPStats(), nil
}
func (csvs *CSVStorage) GetTPThresholds(tpid, id string) ([]*utils.TPThreshold, error) {
func (csvs *CSVStorage) GetTPThresholds(tpid, tenant, id string) ([]*utils.TPThreshold, error) {
csvReader, fp, err := csvs.readerFunc(csvs.thresholdsFn, csvs.sep, getColumnCount(TpThreshold{}))
if err != nil {
//log.Print("Could not load threshold file: ", err)

View File

@@ -184,7 +184,7 @@ type LoadReader interface {
GetTPAccountActions(*utils.TPAccountActions) ([]*utils.TPAccountActions, error)
GetTPResources(string, string, string) ([]*utils.TPResource, error)
GetTPStats(string, string, string) ([]*utils.TPStats, error)
GetTPThresholds(string, string) ([]*utils.TPThreshold, error)
GetTPThresholds(string, string, string) ([]*utils.TPThreshold, error)
GetTPFilters(string, string) ([]*utils.TPFilterProfile, error)
GetTPSuppliers(string, string) ([]*utils.TPSupplierProfile, error)
GetTPAttributes(string, string, string) ([]*utils.TPAttributeProfile, error)

View File

@@ -79,7 +79,7 @@ func (ms *MapStorage) GetTPResources(tpid, tenant, id string) (resources []*util
func (ms *MapStorage) GetTPStats(tpid, tenant, id string) (stats []*utils.TPStats, err error) {
return nil, utils.ErrNotImplemented
}
func (ms *MapStorage) GetTPThresholds(tpid, id string) (ths []*utils.TPThreshold, err error) {
func (ms *MapStorage) GetTPThresholds(tpid, tenant, id string) (ths []*utils.TPThreshold, err error) {
return nil, utils.ErrNotImplemented
}
func (ms *MapStorage) GetTPFilters(tpid, id string) (fltrs []*utils.TPFilterProfile, err error) {

View File

@@ -1359,33 +1359,6 @@ func (ms *MongoStorage) GetCDRs(qryFltr *utils.CDRsFilter, remove bool) ([]*CDR,
return cdrs, 0, err
}
func (ms *MongoStorage) GetTPStat(tpid, id string) ([]*utils.TPStats, error) {
filter := bson.M{"tpid": tpid}
if id != "" {
filter["id"] = id
}
var results []*utils.TPStats
err := ms.client.UseSession(ms.ctx, 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 tp utils.TPStats
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) SetTPStats(tpSTs []*utils.TPStats) (err error) {
if len(tpSTs) == 0 {
return
@@ -1404,11 +1377,14 @@ func (ms *MongoStorage) SetTPStats(tpSTs []*utils.TPStats) (err error) {
})
}
func (ms *MongoStorage) GetTPThresholds(tpid, id string) ([]*utils.TPThreshold, error) {
func (ms *MongoStorage) GetTPThresholds(tpid, tenant, id string) ([]*utils.TPThreshold, error) {
filter := bson.M{"tpid": tpid}
if id != "" {
filter["id"] = id
}
if tenant != "" {
filter["tenant"] = tenant
}
var results []*utils.TPThreshold
err := ms.client.UseSession(ms.ctx, func(sctx mongo.SessionContext) (err error) {
cur, err := ms.getCol(utils.TBLTPThresholds).Find(sctx, filter)

View File

@@ -1512,12 +1512,15 @@ func (self *SQLStorage) GetTPStats(tpid, tenant, id string) ([]*utils.TPStats, e
return asts, nil
}
func (self *SQLStorage) GetTPThresholds(tpid, id string) ([]*utils.TPThreshold, error) {
func (self *SQLStorage) GetTPThresholds(tpid, tenant, id string) ([]*utils.TPThreshold, error) {
var ths TpThresholdS
q := self.db.Where("tpid = ?", tpid)
if len(id) != 0 {
q = q.Where("id = ?", id)
}
if len(tenant) != 0 {
q = q.Where("tenant = ?", tenant)
}
if err := q.Find(&ths).Error; err != nil {
return nil, err
}

View File

@@ -1338,7 +1338,7 @@ func (tpr *TpReader) LoadStats() error {
}
func (tpr *TpReader) LoadThresholdsFiltered(tag string) (err error) {
tps, err := tpr.lr.GetTPThresholds(tpr.tpid, tag)
tps, err := tpr.lr.GetTPThresholds(tpr.tpid, "", tag)
if err != nil {
return err
}

View File

@@ -240,7 +240,7 @@ func (self *TPExporter) Run() error {
}
}
storDataThresholds, err := self.storDb.GetTPThresholds(self.tpID, "")
storDataThresholds, err := self.storDb.GetTPThresholds(self.tpID, "", "")
if err != nil && err.Error() != utils.ErrNotFound.Error() {
return err
}

View File

@@ -352,7 +352,7 @@ func (self *TPCSVImporter) importThresholds(fn string) error {
if self.Verbose {
log.Printf("Processing file: <%s> ", fn)
}
sts, err := self.csvr.GetTPThresholds(self.TPid, "")
sts, err := self.csvr.GetTPThresholds(self.TPid, "", "")
if err != nil {
return err
}

View File

@@ -39,7 +39,7 @@ func (m *Migrator) migrateCurrentTPthresholds() (err error) {
}
for _, id := range ids {
thresholds, err := m.storDBIn.StorDB().GetTPThresholds(tpid, id)
thresholds, err := m.storDBIn.StorDB().GetTPThresholds(tpid, "", id)
if err != nil {
return err
}