Move caching for ChargerProfile

This commit is contained in:
TeoV
2019-03-25 09:48:04 +02:00
committed by Dan Christian Bogos
parent 987bcc85e1
commit 8718b21f52
4 changed files with 55 additions and 56 deletions

View File

@@ -57,12 +57,25 @@ func (apierV1 *ApierV1) GetChargerProfileIDs(tenant string, chPrfIDs *[]string)
return nil
}
type ChargerWrapper struct {
*engine.ChargerProfile
Cache *string
}
//SetChargerProfile add/update a new Charger Profile
func (apierV1 *ApierV1) SetChargerProfile(cpp *engine.ChargerProfile, reply *string) error {
if missing := utils.MissingStructFields(cpp, []string{"Tenant", "ID"}); len(missing) != 0 {
func (apierV1 *ApierV1) SetChargerProfile(arg *ChargerWrapper, reply *string) error {
if missing := utils.MissingStructFields(arg.ChargerProfile, []string{"Tenant", "ID"}); len(missing) != 0 {
return utils.NewErrMandatoryIeMissing(missing...)
}
if err := apierV1.DataManager.SetChargerProfile(cpp, true); err != nil {
if err := apierV1.DataManager.SetChargerProfile(arg.ChargerProfile, true); err != nil {
return utils.APIErrorHandler(err)
}
//handle caching for ChargerProfile
argCache := engine.ArgsGetCacheItem{
CacheID: utils.CacheChargerProfiles,
ItemID: arg.TenantID(),
}
if err := apierV1.CallCache(GetCacheOpt(arg.Cache), argCache); err != nil {
return utils.APIErrorHandler(err)
}
*reply = utils.OK
@@ -70,16 +83,21 @@ func (apierV1 *ApierV1) SetChargerProfile(cpp *engine.ChargerProfile, reply *str
}
//RemoveChargerProfile remove a specific Charger Profile
func (apierV1 *ApierV1) RemoveChargerProfile(arg utils.TenantID, reply *string) error {
func (apierV1 *ApierV1) RemoveChargerProfile(arg utils.TenantIDWrapper, reply *string) error {
if missing := utils.MissingStructFields(&arg, []string{"Tenant", "ID"}); len(missing) != 0 { //Params missing
return utils.NewErrMandatoryIeMissing(missing...)
}
if err := apierV1.DataManager.RemoveChargerProfile(arg.Tenant,
arg.ID, utils.NonTransactional, true); err != nil {
if err.Error() != utils.ErrNotFound.Error() {
err = utils.NewErrServerError(err)
}
return err
return utils.APIErrorHandler(err)
}
//handle caching for ChargerProfile
argCache := engine.ArgsGetCacheItem{
CacheID: utils.CacheChargerProfiles,
ItemID: arg.TenantID(),
}
if err := apierV1.CallCache(GetCacheOpt(arg.Cache), argCache); err != nil {
return utils.APIErrorHandler(err)
}
*reply = utils.OK
return nil

View File

@@ -37,7 +37,7 @@ var (
chargerCfgPath string
chargerCfg *config.CGRConfig
chargerRPC *rpc.Client
chargerProfile *engine.ChargerProfile
chargerProfile *ChargerWrapper
chargerConfigDIR string //run tests for specific configuration
)
@@ -132,17 +132,20 @@ func testChargerSRPCConn(t *testing.T) {
}
func testChargerSLoadAddCharger(t *testing.T) {
chargerProfile := &engine.ChargerProfile{
Tenant: "cgrates.org",
ID: "Charger1",
FilterIDs: []string{"*string:~Account:1001"},
ActivationInterval: &utils.ActivationInterval{
ActivationTime: time.Date(2014, 7, 29, 15, 0, 0, 0, time.UTC),
chargerProfile := &ChargerWrapper{
ChargerProfile: &engine.ChargerProfile{
Tenant: "cgrates.org",
ID: "Charger1",
FilterIDs: []string{"*string:~Account:1001"},
ActivationInterval: &utils.ActivationInterval{
ActivationTime: time.Date(2014, 7, 29, 15, 0, 0, 0, time.UTC),
},
RunID: utils.MetaDefault,
AttributeIDs: []string{"ATTR_1001_SIMPLEAUTH"},
Weight: 20,
},
RunID: utils.MetaDefault,
AttributeIDs: []string{"ATTR_1001_SIMPLEAUTH"},
Weight: 20,
}
var result string
if err := chargerRPC.Call("ApierV1.SetChargerProfile", chargerProfile, &result); err != nil {
t.Error(err)
@@ -232,17 +235,19 @@ func testChargerSProcessEvent(t *testing.T) {
}
func testChargerSSetChargerProfile(t *testing.T) {
chargerProfile = &engine.ChargerProfile{
Tenant: "cgrates.org",
ID: "ApierTest",
FilterIDs: []string{"*string:~Account:1001", "*string:~Account:1002"},
ActivationInterval: &utils.ActivationInterval{
ActivationTime: time.Date(2014, 7, 14, 14, 35, 0, 0, time.UTC),
ExpiryTime: time.Date(2014, 7, 14, 14, 35, 0, 0, time.UTC),
chargerProfile = &ChargerWrapper{
ChargerProfile: &engine.ChargerProfile{
Tenant: "cgrates.org",
ID: "ApierTest",
FilterIDs: []string{"*string:~Account:1001", "*string:~Account:1002"},
ActivationInterval: &utils.ActivationInterval{
ActivationTime: time.Date(2014, 7, 14, 14, 35, 0, 0, time.UTC),
ExpiryTime: time.Date(2014, 7, 14, 14, 35, 0, 0, time.UTC),
},
RunID: utils.MetaDefault,
AttributeIDs: []string{"Attr1", "Attr2"},
Weight: 20,
},
RunID: utils.MetaDefault,
AttributeIDs: []string{"Attr1", "Attr2"},
Weight: 20,
}
var result string
if err := chargerRPC.Call("ApierV1.SetChargerProfile", chargerProfile, &result); err != nil {
@@ -254,8 +259,8 @@ func testChargerSSetChargerProfile(t *testing.T) {
if err := chargerRPC.Call("ApierV1.GetChargerProfile",
&utils.TenantID{Tenant: "cgrates.org", ID: "ApierTest"}, &reply); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(chargerProfile, reply) {
t.Errorf("Expecting : %+v, received: %+v", chargerProfile, reply)
} else if !reflect.DeepEqual(chargerProfile.ChargerProfile, reply) {
t.Errorf("Expecting : %+v, received: %+v", chargerProfile.ChargerProfile, reply)
}
}
@@ -281,8 +286,8 @@ func testChargerSUpdateChargerProfile(t *testing.T) {
if err := chargerRPC.Call("ApierV1.GetChargerProfile",
&utils.TenantID{Tenant: "cgrates.org", ID: "ApierTest"}, &reply); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(chargerProfile, reply) {
t.Errorf("Expecting : %+v, received: %+v", chargerProfile, reply)
} else if !reflect.DeepEqual(chargerProfile.ChargerProfile, reply) {
t.Errorf("Expecting : %+v, received: %+v", chargerProfile.ChargerProfile, reply)
}
}

View File

@@ -1194,9 +1194,6 @@ func (dm *DataManager) SetChargerProfile(cpp *ChargerProfile, withIndex bool) (e
if err = dm.DataDB().SetChargerProfileDrv(cpp); err != nil {
return err
}
if err = dm.CacheDataFromDB(utils.ChargerProfilePrefix, []string{cpp.TenantID()}, true); err != nil {
return
}
if withIndex {
if oldCpp != nil {
var needsRemove bool
@@ -1226,8 +1223,6 @@ func (dm *DataManager) RemoveChargerProfile(tenant, id string,
if err = dm.DataDB().RemoveChargerProfileDrv(tenant, id); err != nil {
return
}
Cache.Remove(utils.CacheChargerProfiles, utils.ConcatenatedKey(tenant, id),
cacheCommit(transactionID), transactionID)
if oldCpp == nil {
return utils.ErrNotFound
}

View File

@@ -2049,13 +2049,6 @@ func testOnStorITChargerProfile(t *testing.T) {
if err := onStor.SetChargerProfile(cpp, false); err != nil {
t.Error(err)
}
//get from cache
if rcv, err := onStor.GetChargerProfile("cgrates.org", "CPP_1",
true, false, utils.NonTransactional); err != nil {
t.Error(err)
} else if !(reflect.DeepEqual(cpp, rcv)) {
t.Errorf("Expecting: %v, received: %v", cpp, rcv)
}
//get from database
if rcv, err := onStor.GetChargerProfile("cgrates.org", "CPP_1",
false, false, utils.NonTransactional); err != nil {
@@ -2075,13 +2068,6 @@ func testOnStorITChargerProfile(t *testing.T) {
t.Error(err)
}
time.Sleep(sleepDelay)
//get from cache
if rcv, err := onStor.GetChargerProfile("cgrates.org", "CPP_1",
true, false, utils.NonTransactional); err != nil {
t.Error(err)
} else if !(reflect.DeepEqual(cpp, rcv)) {
t.Errorf("Expecting: %v, received: %v", cpp, rcv)
}
//get from database
if rcv, err := onStor.GetChargerProfile("cgrates.org", "CPP_1",
false, false, utils.NonTransactional); err != nil {
@@ -2093,11 +2079,6 @@ func testOnStorITChargerProfile(t *testing.T) {
utils.NonTransactional, false); err != nil {
t.Error(err)
}
//check cache if removed
if _, rcvErr := onStor.GetChargerProfile("cgrates.org", "CPP_1",
true, false, utils.NonTransactional); rcvErr != nil && rcvErr != utils.ErrNotFound {
t.Error(rcvErr)
}
//check database if removed
if _, rcvErr := onStor.GetChargerProfile("cgrates.org", "CPP_1",
false, false, utils.NonTransactional); rcvErr != nil && rcvErr != utils.ErrNotFound {