mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-25 09:08:45 +05:00
Add Charger in migrator and add API for TPCharger
This commit is contained in:
committed by
Dan Christian Bogos
parent
38c087d626
commit
5ddbfbfc51
@@ -19,7 +19,7 @@ package engine
|
||||
|
||||
import (
|
||||
// "reflect"
|
||||
// "testing"
|
||||
"testing"
|
||||
// "time"
|
||||
|
||||
"github.com/cgrates/cgrates/config"
|
||||
@@ -38,7 +38,6 @@ func TestChargerPopulateChargerService(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Errorf("Error: %+v", err)
|
||||
}
|
||||
|
||||
chargerSrv, err = NewChargerService(dmCharger,
|
||||
&FilterS{dm: dmAtr, cfg: defaultCfg}, nil, defaultCfg)
|
||||
if err != nil {
|
||||
|
||||
@@ -304,7 +304,8 @@ cgrates.org,ALS1,con1,FLTR_1,2014-07-29T15:00:00Z,Field1,Initial1,Sub1,true,20
|
||||
cgrates.org,ALS1,con2;con3,,,Field2,Initial2,Sub2,false,
|
||||
`
|
||||
chargerProfiles = `
|
||||
|
||||
#Tenant,ID,FilterIDs,ActivationInterval,RunID,AttributeIDs,Weight
|
||||
cgrates.org,Charger1,*string:Account:1001,2014-07-29T15:00:00Z,*rated,ATTR_1001_SIMPLEAUTH,20
|
||||
`
|
||||
)
|
||||
|
||||
@@ -381,6 +382,9 @@ func init() {
|
||||
if err := csvr.LoadAttributeProfiles(); err != nil {
|
||||
log.Print("error in LoadAttributeProfiles:", err)
|
||||
}
|
||||
if err := csvr.LoadChargerProfiles(); err != nil {
|
||||
log.Print("error in LoadChargerProfiles:", err)
|
||||
}
|
||||
csvr.WriteToDatabase(false, false, false)
|
||||
Cache.Clear(nil)
|
||||
//dm.LoadDataDBCache(nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil)
|
||||
@@ -1768,6 +1772,29 @@ func TestLoadAttributeProfiles(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadChargerProfiles(t *testing.T) {
|
||||
eChargerProfiles := map[utils.TenantID]*utils.TPChargerProfile{
|
||||
utils.TenantID{Tenant: "cgrates.org", ID: "Charger1"}: &utils.TPChargerProfile{
|
||||
TPid: testTPID,
|
||||
Tenant: "cgrates.org",
|
||||
ID: "Charger1",
|
||||
FilterIDs: []string{"*string:Account:1001"},
|
||||
ActivationInterval: &utils.TPActivationInterval{
|
||||
ActivationTime: "2014-07-29T15:00:00Z",
|
||||
},
|
||||
RunID: "*rated",
|
||||
AttributeIDs: []string{"ATTR_1001_SIMPLEAUTH"},
|
||||
Weight: 20,
|
||||
},
|
||||
}
|
||||
cppKey := utils.TenantID{Tenant: "cgrates.org", ID: "Charger1"}
|
||||
if len(csvr.chargerProfiles) != len(eChargerProfiles) {
|
||||
t.Errorf("Failed to load chargerProfiles: %s", utils.ToIJSON(csvr.chargerProfiles))
|
||||
} else if !reflect.DeepEqual(eChargerProfiles[cppKey], csvr.chargerProfiles[cppKey]) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", eChargerProfiles[cppKey], csvr.chargerProfiles[cppKey])
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadResource(t *testing.T) {
|
||||
eResources := []*utils.TenantID{
|
||||
&utils.TenantID{
|
||||
|
||||
@@ -110,6 +110,7 @@ func TestLoaderITRemoveLoad(t *testing.T) {
|
||||
path.Join(*dataDir, "tariffplans", *tpCsvScenario, utils.FiltersCsv),
|
||||
path.Join(*dataDir, "tariffplans", *tpCsvScenario, utils.SuppliersCsv),
|
||||
path.Join(*dataDir, "tariffplans", *tpCsvScenario, utils.AttributesCsv),
|
||||
path.Join(*dataDir, "tariffplans", *tpCsvScenario, utils.ChargersCsv),
|
||||
), "", "")
|
||||
|
||||
if err = loader.LoadDestinations(); err != nil {
|
||||
@@ -172,6 +173,9 @@ func TestLoaderITRemoveLoad(t *testing.T) {
|
||||
if err = loader.LoadAttributeProfiles(); err != nil {
|
||||
t.Error("Failed loading Alias profiles: ", err.Error())
|
||||
}
|
||||
if err = loader.LoadChargerProfiles(); err != nil {
|
||||
t.Error("Failed loading Charger profiles: ", err.Error())
|
||||
}
|
||||
if err := loader.WriteToDatabase(true, false, false); err != nil {
|
||||
t.Error("Could not write data into dataDb: ", err.Error())
|
||||
}
|
||||
@@ -211,6 +215,7 @@ func TestLoaderITLoadFromCSV(t *testing.T) {
|
||||
path.Join(*dataDir, "tariffplans", *tpCsvScenario, utils.FiltersCsv),
|
||||
path.Join(*dataDir, "tariffplans", *tpCsvScenario, utils.SuppliersCsv),
|
||||
path.Join(*dataDir, "tariffplans", *tpCsvScenario, utils.AttributesCsv),
|
||||
path.Join(*dataDir, "tariffplans", *tpCsvScenario, utils.ChargersCsv),
|
||||
), "", "")
|
||||
|
||||
if err = loader.LoadDestinations(); err != nil {
|
||||
@@ -273,6 +278,9 @@ func TestLoaderITLoadFromCSV(t *testing.T) {
|
||||
if err = loader.LoadAttributeProfiles(); err != nil {
|
||||
t.Error("Failed loading Alias profiles: ", err.Error())
|
||||
}
|
||||
if err = loader.LoadChargerProfiles(); err != nil {
|
||||
t.Error("Failed loading Alias profiles: ", err.Error())
|
||||
}
|
||||
if err := loader.WriteToDatabase(true, false, false); err != nil {
|
||||
t.Error("Could not write data into dataDb: ", err.Error())
|
||||
}
|
||||
@@ -491,6 +499,20 @@ func TestLoaderITWriteToDatabase(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
for tenatid, cpp := range loader.chargerProfiles {
|
||||
rcv, err := loader.dm.GetChargerProfile(tenatid.Tenant, tenatid.ID, true, utils.NonTransactional)
|
||||
if err != nil {
|
||||
t.Errorf("Failed GetChargerProfile, tenant: %s, id: %s, error: %s ", cpp.Tenant, cpp.ID, err.Error())
|
||||
}
|
||||
cp, err := APItoChargerProfile(cpp, "UTC")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if !reflect.DeepEqual(cp, rcv) {
|
||||
t.Errorf("Expecting: %v, received: %v", cp, rcv)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Imports data from csv files in tpScenario to storDb
|
||||
@@ -514,7 +536,6 @@ func TestLoaderITImportToStorDb(t *testing.T) {
|
||||
|
||||
// Loads data from storDb into dataDb
|
||||
func TestLoaderITLoadFromStorDb(t *testing.T) {
|
||||
|
||||
loader := NewTpReader(dataDbStor.DataDB(), storDb, utils.TEST_SQL, "")
|
||||
if err := loader.LoadDestinations(); err != nil && err.Error() != utils.NotFoundCaps {
|
||||
t.Error("Failed loading destinations: ", err.Error())
|
||||
|
||||
@@ -160,7 +160,8 @@ func (ms *MongoStorage) EnsureIndexes() (err error) {
|
||||
Background: false, // Build index in background and return immediately
|
||||
Sparse: false, // Only index documents containing the Key fields
|
||||
}
|
||||
for _, col := range []string{colAct, colApl, colAAp, colAtr, colDcs, colRpl, colLcr, colDst, colRds, colAls, colUsr, colLht} {
|
||||
for _, col := range []string{colAct, colApl, colAAp, colAtr,
|
||||
colDcs, colRpl, colLcr, colDst, colRds, colAls, colUsr, colLht} {
|
||||
if err = db.C(col).EnsureIndex(idx); err != nil {
|
||||
|
||||
return
|
||||
@@ -173,7 +174,8 @@ func (ms *MongoStorage) EnsureIndexes() (err error) {
|
||||
Background: false,
|
||||
Sparse: false,
|
||||
}
|
||||
for _, col := range []string{colRsP, colRes, colSqs, colSqp, colTps, colThs, colSpp, colAttr, colFlt} {
|
||||
for _, col := range []string{colRsP, colRes, colSqs, colSqp,
|
||||
colTps, colThs, colSpp, colAttr, colFlt, colCpp} {
|
||||
if err = db.C(col).EnsureIndex(idx); err != nil {
|
||||
return
|
||||
}
|
||||
@@ -199,8 +201,10 @@ func (ms *MongoStorage) EnsureIndexes() (err error) {
|
||||
Background: false,
|
||||
Sparse: false,
|
||||
}
|
||||
for _, col := range []string{utils.TBLTPTimings, utils.TBLTPDestinations, utils.TBLTPDestinationRates, utils.TBLTPRatingPlans,
|
||||
utils.TBLTPSharedGroups, utils.TBLTPCdrStats, utils.TBLTPActions, utils.TBLTPActionPlans, utils.TBLTPActionTriggers,
|
||||
for _, col := range []string{utils.TBLTPTimings, utils.TBLTPDestinations,
|
||||
utils.TBLTPDestinationRates, utils.TBLTPRatingPlans,
|
||||
utils.TBLTPSharedGroups, utils.TBLTPCdrStats, utils.TBLTPActions,
|
||||
utils.TBLTPActionPlans, utils.TBLTPActionTriggers,
|
||||
utils.TBLTPStats, utils.TBLTPResources} {
|
||||
if err = db.C(col).EnsureIndex(idx); err != nil {
|
||||
return
|
||||
@@ -385,7 +389,8 @@ func (ms *MongoStorage) SelectDatabase(dbName string) (err error) {
|
||||
}
|
||||
|
||||
func (ms *MongoStorage) RebuildReverseForPrefix(prefix string) (err error) {
|
||||
if !utils.IsSliceMember([]string{utils.REVERSE_DESTINATION_PREFIX, utils.REVERSE_ALIASES_PREFIX, utils.AccountActionPlansPrefix}, prefix) {
|
||||
if !utils.IsSliceMember([]string{utils.REVERSE_DESTINATION_PREFIX,
|
||||
utils.REVERSE_ALIASES_PREFIX, utils.AccountActionPlansPrefix}, prefix) {
|
||||
return utils.ErrInvalidKey
|
||||
}
|
||||
colName, ok := ms.getColNameForPrefix(prefix)
|
||||
@@ -445,7 +450,8 @@ func (ms *MongoStorage) RebuildReverseForPrefix(prefix string) (err error) {
|
||||
}
|
||||
|
||||
func (ms *MongoStorage) RemoveReverseForPrefix(prefix string) (err error) {
|
||||
if !utils.IsSliceMember([]string{utils.REVERSE_DESTINATION_PREFIX, utils.REVERSE_ALIASES_PREFIX, utils.AccountActionPlansPrefix}, prefix) {
|
||||
if !utils.IsSliceMember([]string{utils.REVERSE_DESTINATION_PREFIX,
|
||||
utils.REVERSE_ALIASES_PREFIX, utils.AccountActionPlansPrefix}, prefix) {
|
||||
return utils.ErrInvalidKey
|
||||
}
|
||||
colName, ok := ms.getColNameForPrefix(prefix)
|
||||
|
||||
@@ -108,7 +108,7 @@ func (self *SQLStorage) IsDBEmpty() (resp bool, err error) {
|
||||
utils.TBLTPActionTriggers, utils.TBLTPAccountActions, utils.TBLTPDerivedChargers, utils.TBLTPUsers,
|
||||
utils.TBLTPAliases, utils.TBLTPResources, utils.TBLTPStats, utils.TBLTPThresholds,
|
||||
utils.TBLTPFilters, utils.SessionsCostsTBL, utils.CDRsTBL, utils.TBLTPActionPlans,
|
||||
utils.TBLVersions, utils.TBLTPSuppliers, utils.TBLTPAttributes,
|
||||
utils.TBLVersions, utils.TBLTPSuppliers, utils.TBLTPAttributes, utils.TBLTPChargers,
|
||||
}
|
||||
for _, tbl := range tbls {
|
||||
if self.db.HasTable(tbl) {
|
||||
@@ -127,7 +127,7 @@ func (self *SQLStorage) GetTpIds(colName string) ([]string, error) {
|
||||
qryStr := fmt.Sprintf(" (SELECT tpid FROM %s)", colName)
|
||||
if colName == "" {
|
||||
qryStr = fmt.Sprintf(
|
||||
"(SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s)",
|
||||
"(SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s)",
|
||||
utils.TBLTPTimings,
|
||||
utils.TBLTPDestinations,
|
||||
utils.TBLTPRates,
|
||||
@@ -149,7 +149,8 @@ func (self *SQLStorage) GetTpIds(colName string) ([]string, error) {
|
||||
utils.TBLTPFilters,
|
||||
utils.TBLTPActionPlans,
|
||||
utils.TBLTPSuppliers,
|
||||
utils.TBLTPAttributes)
|
||||
utils.TBLTPAttributes,
|
||||
utils.TBLTPChargers)
|
||||
}
|
||||
rows, err = self.Db.Query(qryStr)
|
||||
if err != nil {
|
||||
@@ -174,8 +175,8 @@ func (self *SQLStorage) GetTpIds(colName string) ([]string, error) {
|
||||
}
|
||||
|
||||
// ToDo: TEST
|
||||
func (self *SQLStorage) GetTpTableIds(tpid, table string, distinct utils.TPDistinctIds, filters map[string]string, pagination *utils.Paginator) ([]string, error) {
|
||||
|
||||
func (self *SQLStorage) GetTpTableIds(tpid, table string, distinct utils.TPDistinctIds,
|
||||
filters map[string]string, pagination *utils.Paginator) ([]string, error) {
|
||||
qry := fmt.Sprintf("SELECT DISTINCT %s FROM %s where tpid='%s'", distinct, table, tpid)
|
||||
for key, value := range filters {
|
||||
if key != "" && value != "" {
|
||||
@@ -236,10 +237,11 @@ func (self *SQLStorage) RemTpData(table, tpid string, args map[string]string) er
|
||||
|
||||
if len(table) == 0 { // Remove tpid out of all tables
|
||||
for _, tblName := range []string{utils.TBLTPTimings, utils.TBLTPDestinations, utils.TBLTPRates,
|
||||
utils.TBLTPDestinationRates, utils.TBLTPRatingPlans, utils.TBLTPRateProfiles, utils.TBLTPSharedGroups,
|
||||
utils.TBLTPCdrStats, utils.TBLTPLcrs, utils.TBLTPActions, utils.TBLTPActionPlans, utils.TBLTPActionTriggers,
|
||||
utils.TBLTPAccountActions, utils.TBLTPDerivedChargers, utils.TBLTPAliases, utils.TBLTPUsers,
|
||||
utils.TBLTPResources, utils.TBLTPStats, utils.TBLTPFilters, utils.TBLTPSuppliers, utils.TBLTPAttributes} {
|
||||
utils.TBLTPDestinationRates, utils.TBLTPRatingPlans, utils.TBLTPRateProfiles,
|
||||
utils.TBLTPSharedGroups, utils.TBLTPCdrStats, utils.TBLTPLcrs, utils.TBLTPActions,
|
||||
utils.TBLTPActionPlans, utils.TBLTPActionTriggers, utils.TBLTPAccountActions,
|
||||
utils.TBLTPDerivedChargers, utils.TBLTPAliases, utils.TBLTPUsers, utils.TBLTPResources,
|
||||
utils.TBLTPStats, utils.TBLTPFilters, utils.TBLTPSuppliers, utils.TBLTPAttributes, utils.TBLTPChargers} {
|
||||
if err := tx.Table(tblName).Where("tpid = ?", tpid).Delete(nil).Error; err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
|
||||
@@ -140,6 +140,7 @@ func CurrentDataDBVersions() Versions {
|
||||
utils.LCR: 1,
|
||||
utils.RatingPlan: 1,
|
||||
utils.RatingProfile: 1,
|
||||
utils.Chargers: 1,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -172,6 +173,7 @@ func CurrentStorDBVersions() Versions {
|
||||
utils.TpDestinations: 1,
|
||||
utils.TpRatingPlan: 1,
|
||||
utils.TpRatingProfile: 1,
|
||||
utils.TpChargers: 1,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user