diff --git a/apier/v1/tpresources.go b/apier/v1/tpresources.go index d58815743..d8c1eb742 100644 --- a/apier/v1/tpresources.go +++ b/apier/v1/tpresources.go @@ -24,7 +24,7 @@ import ( // Creates a new resource within a tariff plan func (self *ApierV1) SetTPResource(attr *utils.TPResource, reply *string) error { - if missing := utils.MissingStructFields(attr, []string{"TPid", "ID", "Limit"}); len(missing) != 0 { + if missing := utils.MissingStructFields(attr, []string{"TPid", "Tenant", "ID", "Limit"}); len(missing) != 0 { return utils.NewErrMandatoryIeMissing(missing...) } if err := self.StorDb.SetTPResources([]*utils.TPResource{attr}); err != nil { diff --git a/apier/v1/tpstats.go b/apier/v1/tpstats.go index 4d287b3ca..ac50f60e9 100644 --- a/apier/v1/tpstats.go +++ b/apier/v1/tpstats.go @@ -23,29 +23,23 @@ import ( ) // Creates a new stat within a tariff plan -func (self *ApierV1) SetTPStat(attr utils.TPStats, reply *string) error { - if missing := utils.MissingStructFields(&attr, []string{"TPid", "ID"}); len(missing) != 0 { +func (self *ApierV1) SetTPStat(attr *utils.TPStats, reply *string) error { + if missing := utils.MissingStructFields(attr, []string{"TPid", "Tenant", "ID"}); len(missing) != 0 { return utils.NewErrMandatoryIeMissing(missing...) } - if err := self.StorDb.SetTPStats([]*utils.TPStats{&attr}); err != nil { + if err := self.StorDb.SetTPStats([]*utils.TPStats{attr}); err != nil { return utils.APIErrorHandler(err) } *reply = utils.OK return nil } -type AttrGetTPStat struct { - TPid string // Tariff plan id - Tenant string - ID string -} - // Queries specific Stat on Tariff plan -func (self *ApierV1) GetTPStat(attr AttrGetTPStat, reply *utils.TPStats) error { - if missing := utils.MissingStructFields(&attr, []string{"TPid", "ID"}); len(missing) != 0 { //Params missing +func (self *ApierV1) GetTPStat(attr *utils.TPTntID, reply *utils.TPStats) error { + if missing := utils.MissingStructFields(attr, []string{"TPid", "Tenant", "ID"}); len(missing) != 0 { //Params missing return utils.NewErrMandatoryIeMissing(missing...) } - if rls, err := self.StorDb.GetTPStats(attr.TPid, attr.ID); err != nil { + if rls, err := self.StorDb.GetTPStats(attr.TPid, attr.Tenant, attr.ID); err != nil { if err.Error() != utils.ErrNotFound.Error() { err = utils.NewErrServerError(err) } @@ -63,11 +57,12 @@ type AttrGetTPStatIds struct { } // Queries Stat identities on specific tariff plan. -func (self *ApierV1) GetTPStatIDs(attrs AttrGetTPStatIds, reply *[]string) error { +func (self *ApierV1) GetTPStatIDs(attrs *AttrGetTPStatIds, 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.TBLTPStats, utils.TPDistinctIds{"id"}, nil, &attrs.Paginator); err != nil { + if ids, err := self.StorDb.GetTpTableIds(attrs.TPid, utils.TBLTPStats, + utils.TPDistinctIds{"id"}, nil, &attrs.Paginator); err != nil { if err.Error() != utils.ErrNotFound.Error() { err = utils.NewErrServerError(err) } @@ -79,11 +74,12 @@ func (self *ApierV1) GetTPStatIDs(attrs AttrGetTPStatIds, reply *[]string) error } // Removes specific Stat on Tariff plan -func (self *ApierV1) RemTPStat(attrs AttrGetTPStat, reply *string) error { - if missing := utils.MissingStructFields(&attrs, []string{"TPid", "ID"}); len(missing) != 0 { //Params missing +func (self *ApierV1) RemTPStat(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.TBLTPStats, attrs.TPid, map[string]string{"id": attrs.ID}); err != nil { + if err := self.StorDb.RemTpData(utils.TBLTPStats, attrs.TPid, + map[string]string{"tenant": attrs.Tenant, "id": attrs.ID}); err != nil { return utils.NewErrServerError(err) } else { *reply = utils.OK diff --git a/apier/v1/tpstats_it_test.go b/apier/v1/tpstats_it_test.go index a8962763d..408bdd6bc 100644 --- a/apier/v1/tpstats_it_test.go +++ b/apier/v1/tpstats_it_test.go @@ -88,12 +88,7 @@ func testTPStatsInitCfg(t *testing.T) { } tpStatCfg.DataFolderPath = tpStatDataDir // Share DataFolderPath through config towards StoreDb for Flush() config.SetCgrConfig(tpStatCfg) - switch tpStatConfigDIR { - case "tutmongo": // Mongo needs more time to reset db - tpStatDelay = 2000 - default: - tpStatDelay = 1000 - } + tpStatDelay = 1000 } // Wipe out the cdr database @@ -122,7 +117,7 @@ func testTPStatsRpcConn(t *testing.T) { func testTPStatsGetTPStatBeforeSet(t *testing.T) { var reply *utils.TPStats if err := tpStatRPC.Call("ApierV1.GetTPStat", - &AttrGetTPStat{TPid: "TPS1", ID: "Stat1"}, + &utils.TPTntID{TPid: "TPS1", Tenant: "cgrates.org", ID: "Stat1"}, &reply); err == nil || err.Error() != utils.ErrNotFound.Error() { t.Error(err) } @@ -162,7 +157,7 @@ func testTPStatsSetTPStat(t *testing.T) { func testTPStatsGetTPStatAfterSet(t *testing.T) { var respond *utils.TPStats if err := tpStatRPC.Call("ApierV1.GetTPStat", - &AttrGetTPStat{TPid: tpStat.TPid, ID: tpStat.ID}, &respond); err != nil { + &utils.TPTntID{TPid: "TPS1", Tenant: "cgrates.org", ID: "Stat1"}, &respond); err != nil { t.Error(err) } else if !reflect.DeepEqual(tpStat, respond) { t.Errorf("Expecting: %+v, received: %+v", tpStat, respond) @@ -192,7 +187,7 @@ func testTPStatsUpdateTPStat(t *testing.T) { func testTPStatsGetTPStatAfterUpdate(t *testing.T) { var expectedTPS *utils.TPStats if err := tpStatRPC.Call("ApierV1.GetTPStat", - &AttrGetTPStat{TPid: tpStat.TPid, ID: tpStat.ID}, &expectedTPS); err != nil { + &utils.TPTntID{TPid: "TPS1", Tenant: "cgrates.org", ID: "Stat1"}, &expectedTPS); err != nil { t.Error(err) } else if !reflect.DeepEqual(tpStat, expectedTPS) { t.Errorf("Expecting: %+v, received: %+v", tpStat, expectedTPS) @@ -202,7 +197,7 @@ func testTPStatsGetTPStatAfterUpdate(t *testing.T) { func testTPStatsRemTPStat(t *testing.T) { var resp string if err := tpStatRPC.Call("ApierV1.RemTPStat", - &AttrGetTPStat{TPid: tpStat.TPid, ID: tpStat.ID}, &resp); err != nil { + &utils.TPTntID{TPid: "TPS1", Tenant: "cgrates.org", ID: "Stat1"}, &resp); err != nil { t.Error(err) } else if resp != utils.OK { t.Error("Unexpected reply returned", resp) @@ -212,7 +207,7 @@ func testTPStatsRemTPStat(t *testing.T) { func testTPStatsGetTPStatAfterRemove(t *testing.T) { var respond *utils.TPStats if err := tpStatRPC.Call("ApierV1.GetTPStat", - &AttrGetTPStat{TPid: "TPS1", ID: "Stat1"}, + &utils.TPTntID{TPid: "TPS1", Tenant: "cgrates.org", ID: "Stat1"}, &respond); err == nil || err.Error() != utils.ErrNotFound.Error() { t.Error(err) } diff --git a/engine/storage_csv.go b/engine/storage_csv.go index 37b84a88c..00d7f780a 100644 --- a/engine/storage_csv.go +++ b/engine/storage_csv.go @@ -565,7 +565,7 @@ func (csvs *CSVStorage) GetTPResources(tpid, tenant, id string) ([]*utils.TPReso return tpResLimits.AsTPResources(), nil } -func (csvs *CSVStorage) GetTPStats(tpid, id string) ([]*utils.TPStats, error) { +func (csvs *CSVStorage) GetTPStats(tpid, tenant, id string) ([]*utils.TPStats, error) { csvReader, fp, err := csvs.readerFunc(csvs.statsFn, csvs.sep, getColumnCount(TpStats{})) if err != nil { //log.Print("Could not load stats file: ", err) diff --git a/engine/storage_interface.go b/engine/storage_interface.go index c01c2acf9..5859b992f 100644 --- a/engine/storage_interface.go +++ b/engine/storage_interface.go @@ -183,7 +183,7 @@ type LoadReader interface { GetTPActionTriggers(string, string) ([]*utils.TPActionTriggers, error) GetTPAccountActions(*utils.TPAccountActions) ([]*utils.TPAccountActions, error) GetTPResources(string, string, string) ([]*utils.TPResource, error) - GetTPStats(string, string) ([]*utils.TPStats, error) + GetTPStats(string, string, string) ([]*utils.TPStats, error) GetTPThresholds(string, string) ([]*utils.TPThreshold, error) GetTPFilters(string, string) ([]*utils.TPFilterProfile, error) GetTPSuppliers(string, string) ([]*utils.TPSupplierProfile, error) diff --git a/engine/storage_map_stordb.go b/engine/storage_map_stordb.go index 49178e0d5..8870d7dee 100755 --- a/engine/storage_map_stordb.go +++ b/engine/storage_map_stordb.go @@ -76,7 +76,7 @@ func (ms *MapStorage) GetTPAccountActions(filter *utils.TPAccountActions) (accou func (ms *MapStorage) GetTPResources(tpid, tenant, id string) (resources []*utils.TPResource, err error) { return nil, utils.ErrNotImplemented } -func (ms *MapStorage) GetTPStats(tpid, id string) (stats []*utils.TPStats, err error) { +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) { diff --git a/engine/storage_mongo_stordb.go b/engine/storage_mongo_stordb.go index c373037d5..cb9d5c8f1 100644 --- a/engine/storage_mongo_stordb.go +++ b/engine/storage_mongo_stordb.go @@ -479,13 +479,16 @@ func (ms *MongoStorage) GetTPResources(tpid, tenant, id string) ([]*utils.TPReso return results, err } -func (ms *MongoStorage) GetTPStats(tpid, id string) ([]*utils.TPStats, error) { +func (ms *MongoStorage) GetTPStats(tpid, tenant, id string) ([]*utils.TPStats, error) { filter := bson.M{ "tpid": tpid, } if id != "" { filter["id"] = id } + if tenant != "" { + filter["tenant"] = tenant + } 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) diff --git a/engine/storage_sql.go b/engine/storage_sql.go index 17f7d7a80..d60612029 100644 --- a/engine/storage_sql.go +++ b/engine/storage_sql.go @@ -1493,12 +1493,15 @@ func (self *SQLStorage) GetTPResources(tpid, tenant, id string) ([]*utils.TPReso return arls, nil } -func (self *SQLStorage) GetTPStats(tpid, id string) ([]*utils.TPStats, error) { +func (self *SQLStorage) GetTPStats(tpid, tenant, id string) ([]*utils.TPStats, error) { var sts TpStatsS 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(&sts).Error; err != nil { return nil, err } diff --git a/engine/tp_reader.go b/engine/tp_reader.go index e4e8d2043..c29388a12 100644 --- a/engine/tp_reader.go +++ b/engine/tp_reader.go @@ -1314,7 +1314,7 @@ func (tpr *TpReader) LoadResourceProfiles() error { } func (tpr *TpReader) LoadStatsFiltered(tag string) (err error) { - tps, err := tpr.lr.GetTPStats(tpr.tpid, tag) + tps, err := tpr.lr.GetTPStats(tpr.tpid, "", tag) if err != nil { return err } diff --git a/engine/tpexporter.go b/engine/tpexporter.go index 957f1cba3..b3897026f 100644 --- a/engine/tpexporter.go +++ b/engine/tpexporter.go @@ -229,7 +229,7 @@ func (self *TPExporter) Run() error { } } - storDataStats, err := self.storDb.GetTPStats(self.tpID, "") + storDataStats, err := self.storDb.GetTPStats(self.tpID, "", "") if err != nil && err.Error() != utils.ErrNotFound.Error() { return err } diff --git a/engine/tpimporter_csv.go b/engine/tpimporter_csv.go index 89f0e3076..1ee8252d3 100644 --- a/engine/tpimporter_csv.go +++ b/engine/tpimporter_csv.go @@ -341,7 +341,7 @@ func (self *TPCSVImporter) importStats(fn string) error { if self.Verbose { log.Printf("Processing file: <%s> ", fn) } - sts, err := self.csvr.GetTPStats(self.TPid, "") + sts, err := self.csvr.GetTPStats(self.TPid, "", "") if err != nil { return err } diff --git a/migrator/tp_stats.go b/migrator/tp_stats.go index 3e4de08c5..ee8e3dee6 100644 --- a/migrator/tp_stats.go +++ b/migrator/tp_stats.go @@ -39,7 +39,7 @@ func (m *Migrator) migrateCurrentTPstats() (err error) { } for _, id := range ids { - stats, err := m.storDBIn.StorDB().GetTPStats(tpid, id) + stats, err := m.storDBIn.StorDB().GetTPStats(tpid, "", id) if err != nil { return err }