mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Update tp stats to consider tenant
This commit is contained in:
committed by
Dan Christian Bogos
parent
b5708c18a5
commit
08b76e84f5
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user