This commit is contained in:
DanB
2014-10-09 14:12:07 +02:00
26 changed files with 148 additions and 143 deletions

View File

@@ -221,7 +221,7 @@ func TestApierTPTiming(t *testing.T) {
// Test getIds
var rplyTmIds []string
expectedTmIds := []string{"ALWAYS", "ASAP"}
if err := rater.Call("ApierV1.GetTPTimingIds", AttrGetTPTimingIds{tmAlways.TPid, 0, 0, ""}, &rplyTmIds); err != nil {
if err := rater.Call("ApierV1.GetTPTimingIds", AttrGetTPTimingIds{tmAlways.TPid, utils.Paginator{0, 0, ""}}, &rplyTmIds); err != nil {
t.Error("Calling ApierV1.GetTPTimingIds, got error: ", err.Error())
} else if !reflect.DeepEqual(expectedTmIds, rplyTmIds) {
t.Errorf("Calling ApierV1.GetTPTimingIds expected: %v, received: %v", expectedTmIds, rplyTmIds)
@@ -329,7 +329,7 @@ func TestApierTPRate(t *testing.T) {
// Test getIds
var rplyRtIds []string
expectedRtIds := []string{"RT_FS_USERS"}
if err := rater.Call("ApierV1.GetTPRateIds", AttrGetTPRateIds{rt.TPid, 0, 0, ""}, &rplyRtIds); err != nil {
if err := rater.Call("ApierV1.GetTPRateIds", AttrGetTPRateIds{rt.TPid, utils.Paginator{0, 0, ""}}, &rplyRtIds); err != nil {
t.Error("Calling ApierV1.GetTPRateIds, got error: ", err.Error())
} else if !reflect.DeepEqual(expectedRtIds, rplyRtIds) {
t.Errorf("Calling ApierV1.GetTPDestinationIds expected: %v, received: %v", expectedRtIds, rplyRtIds)
@@ -372,13 +372,13 @@ func TestApierTPDestinationRate(t *testing.T) {
}
// Test get
var rplyDr2 *utils.TPDestinationRate
if err := rater.Call("ApierV1.GetTPDestinationRate", AttrGetTPDestinationRate{dr2.TPid, dr2.DestinationRateId}, &rplyDr2); err != nil {
if err := rater.Call("ApierV1.GetTPDestinationRate", AttrGetTPDestinationRate{dr2.TPid, dr2.DestinationRateId, utils.Paginator{}}, &rplyDr2); err != nil {
t.Error("Calling ApierV1.GetTPDestinationRate, got error: ", err.Error())
} else if !reflect.DeepEqual(dr2, rplyDr2) {
t.Errorf("Calling ApierV1.GetTPDestinationRate expected: %v, received: %v", dr2, rplyDr2)
}
// Test remove
if err := rater.Call("ApierV1.RemTPDestinationRate", AttrGetTPDestinationRate{dr2.TPid, dr2.DestinationRateId}, &reply); err != nil {
if err := rater.Call("ApierV1.RemTPDestinationRate", AttrGetTPDestinationRate{dr2.TPid, dr2.DestinationRateId, utils.Paginator{}}, &reply); err != nil {
t.Error("Calling ApierV1.RemTPRate, got error: ", err.Error())
} else if reply != "OK" {
t.Error("Calling ApierV1.RemTPRate received: ", reply)
@@ -386,7 +386,7 @@ func TestApierTPDestinationRate(t *testing.T) {
// Test getIds
var rplyDrIds []string
expectedDrIds := []string{"DR_FREESWITCH_USERS"}
if err := rater.Call("ApierV1.GetTPDestinationRateIds", AttrTPDestinationRateIds{dr.TPid, 0, 0, ""}, &rplyDrIds); err != nil {
if err := rater.Call("ApierV1.GetTPDestinationRateIds", AttrTPDestinationRateIds{dr.TPid, utils.Paginator{0, 0, ""}}, &rplyDrIds); err != nil {
t.Error("Calling ApierV1.GetTPDestinationRateIds, got error: ", err.Error())
} else if !reflect.DeepEqual(expectedDrIds, rplyDrIds) {
t.Errorf("Calling ApierV1.GetTPDestinationRateIds expected: %v, received: %v", expectedDrIds, rplyDrIds)
@@ -440,7 +440,7 @@ func TestApierTPRatingPlan(t *testing.T) {
// Test getIds
var rplyRpIds []string
expectedRpIds := []string{"RETAIL1"}
if err := rater.Call("ApierV1.GetTPRatingPlanIds", AttrGetTPRatingPlanIds{rp.TPid, 0, 0, ""}, &rplyRpIds); err != nil {
if err := rater.Call("ApierV1.GetTPRatingPlanIds", AttrGetTPRatingPlanIds{rp.TPid, utils.Paginator{0, 0, ""}}, &rplyRpIds); err != nil {
t.Error("Calling ApierV1.GetTPRatingPlanIds, got error: ", err.Error())
} else if !reflect.DeepEqual(expectedRpIds, rplyRpIds) {
t.Errorf("Calling ApierV1.GetTPRatingPlanIds expected: %v, received: %v", expectedRpIds, rplyRpIds)
@@ -1396,38 +1396,38 @@ func TestCdrServer(t *testing.T) {
/*
func TestExportCdrsToFile(t *testing.T) {
if !*testLocal {
return
}
var reply *utils.ExportedFileCdrs
req := utils.AttrExpFileCdrs{}
//if err := rater.Call("ApierV1.ExportCdrsToFile", req, &reply); err == nil || !strings.HasPrefix(err.Error(), utils.ERR_MANDATORY_IE_MISSING) {
// t.Error("Failed to detect missing parameter")
//}
dryRun := utils.CDRE_DRYRUN
req.CdrFormat = &dryRun
tm1, _ := utils.ParseTimeDetectLayout("2013-11-07T08:42:22Z")
tm2, _ := utils.ParseTimeDetectLayout("2013-11-07T08:42:23Z")
expectReply := &utils.ExportedFileCdrs{ExportedFilePath: utils.CDRE_DRYRUN, TotalRecords: 2, ExportedCgrIds: []string{utils.Sha1("dsafdsaf", tm1.String()),
utils.Sha1("adsafdsaf", tm2.String())}}
if err := rater.Call("ApierV1.ExportCdrsToFile", req, &reply); err != nil {
t.Error(err.Error())
} else if !reflect.DeepEqual(reply, expectReply) {
t.Errorf("Unexpected reply: %v", reply)
}
Need to implement temporary file writing in order to test removal from db, not possible on DRYRUN
req.RemoveFromDb = true
if err := rater.Call("ApierV1.ExportCdrsToFile", req, &reply); err != nil {
t.Error(err.Error())
} else if !reflect.DeepEqual(reply, expectReply) {
t.Errorf("Unexpected reply: %v", reply)
}
expectReply.NumberOfRecords = 0 // We should have deleted previously
if err := rater.Call("ApierV1.ExportCdrsToFile", req, &reply); err != nil {
t.Error(err.Error())
} else if !reflect.DeepEqual(reply, expectReply) {
t.Errorf("Unexpected reply: %v", reply)
}
if !*testLocal {
return
}
var reply *utils.ExportedFileCdrs
req := utils.AttrExpFileCdrs{}
//if err := rater.Call("ApierV1.ExportCdrsToFile", req, &reply); err == nil || !strings.HasPrefix(err.Error(), utils.ERR_MANDATORY_IE_MISSING) {
// t.Error("Failed to detect missing parameter")
//}
dryRun := utils.CDRE_DRYRUN
req.CdrFormat = &dryRun
tm1, _ := utils.ParseTimeDetectLayout("2013-11-07T08:42:22Z")
tm2, _ := utils.ParseTimeDetectLayout("2013-11-07T08:42:23Z")
expectReply := &utils.ExportedFileCdrs{ExportedFilePath: utils.CDRE_DRYRUN, TotalRecords: 2, ExportedCgrIds: []string{utils.Sha1("dsafdsaf", tm1.String()),
utils.Sha1("adsafdsaf", tm2.String())}}
if err := rater.Call("ApierV1.ExportCdrsToFile", req, &reply); err != nil {
t.Error(err.Error())
} else if !reflect.DeepEqual(reply, expectReply) {
t.Errorf("Unexpected reply: %v", reply)
}
Need to implement temporary file writing in order to test removal from db, not possible on DRYRUN
req.RemoveFromDb = true
if err := rater.Call("ApierV1.ExportCdrsToFile", req, &reply); err != nil {
t.Error(err.Error())
} else if !reflect.DeepEqual(reply, expectReply) {
t.Errorf("Unexpected reply: %v", reply)
}
expectReply.NumberOfRecords = 0 // We should have deleted previously
if err := rater.Call("ApierV1.ExportCdrsToFile", req, &reply); err != nil {
t.Error(err.Error())
} else if !reflect.DeepEqual(reply, expectReply) {
t.Errorf("Unexpected reply: %v", reply)
}
}
*/

View File

@@ -62,7 +62,7 @@ func (apier *ApierV1) GetCdrs(attrs utils.AttrGetCdrs, reply *[]*utils.CgrCdrOut
}
if cdrs, err := apier.CdrDb.GetStoredCdrs(attrs.CgrIds, attrs.MediationRunIds, attrs.TORs, attrs.CdrHosts, attrs.CdrSources, attrs.ReqTypes, attrs.Directions,
attrs.Tenants, attrs.Categories, attrs.Accounts, attrs.Subjects, attrs.DestinationPrefixes, attrs.RatedAccounts, attrs.RatedSubjects,
attrs.OrderIdStart, attrs.OrderIdEnd, tStart, tEnd, attrs.SkipErrors, attrs.SkipRated, false, &utils.TPPagination{Page: attrs.Page, ItemsPerPage: attrs.ItemsPerPage, SearchTerm: attrs.SearchTerm}); err != nil {
attrs.OrderIdStart, attrs.OrderIdEnd, tStart, tEnd, attrs.SkipErrors, attrs.SkipRated, false, &attrs.Paginator); err != nil {
return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
} else {
for _, cdr := range cdrs {

View File

@@ -100,9 +100,7 @@ import (
type AttrsGetScheduledActions struct {
Direction, Tenant, Account string
TimeStart, TimeEnd time.Time // Filter based on next runTime
Page int
ItemsPerPage int
SearchTerm string
utils.Paginator
}
type ScheduledActions struct {
@@ -117,17 +115,16 @@ func (self *ApierV1) GetScheduledActions(attrs AttrsGetScheduledActions, reply *
return errors.New("SCHEDULER_NOT_ENABLED")
}
scheduledActions := self.Sched.GetQueue()
paginator := &utils.TPPagination{Page: attrs.Page, ItemsPerPage: attrs.ItemsPerPage, SearchTerm: attrs.SearchTerm}
min, max := paginator.GetLimits()
min, max := attrs.GetLimits()
if max > len(scheduledActions) {
max = len(scheduledActions)
}
scheduledActions = scheduledActions[min : min+max]
for _, qActions := range scheduledActions {
sas := &ScheduledActions{ActionsId: qActions.ActionsId, ActionPlanId: qActions.Id, ActionPlanUuid: qActions.Uuid}
if paginator.SearchTerm != "" &&
!(strings.Contains(sas.ActionPlanId, paginator.SearchTerm) ||
strings.Contains(sas.ActionsId, paginator.SearchTerm)) {
if attrs.SearchTerm != "" &&
!(strings.Contains(sas.ActionPlanId, attrs.SearchTerm) ||
strings.Contains(sas.ActionsId, attrs.SearchTerm)) {
continue
}
sas.NextRunTime = qActions.GetNextStartTime(time.Now())

View File

@@ -105,10 +105,8 @@ func (self *ApierV1) GetTPAccountActions(attrs AttrGetTPAccountActions, reply *u
}
type AttrGetTPAccountActionIds struct {
TPid string // Tariff plan id
Page int
ItemsPerPage int
SearchTerm string
TPid string // Tariff plan id
utils.Paginator
}
// Queries AccountActions identities on specific tariff plan.
@@ -116,7 +114,7 @@ func (self *ApierV1) GetTPAccountActionLoadIds(attrs AttrGetTPAccountActionIds,
if missing := utils.MissingStructFields(&attrs, []string{"TPid"}); len(missing) != 0 { //Params missing
return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing)
}
if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_ACCOUNT_ACTIONS, utils.TPDistinctIds{"loadid"}, nil, &utils.TPPagination{Page: attrs.Page, ItemsPerPage: attrs.ItemsPerPage, SearchTerm: attrs.SearchTerm}); err != nil {
if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_ACCOUNT_ACTIONS, utils.TPDistinctIds{"loadid"}, nil, &attrs.Paginator); err != nil {
return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
} else if ids == nil {
return errors.New(utils.ERR_NOT_FOUND)
@@ -131,7 +129,7 @@ func (self *ApierV1) GetTPAccountActionIds(attrs AttrGetTPAccountActionIds, repl
if missing := utils.MissingStructFields(&attrs, []string{"TPid"}); len(missing) != 0 { //Params missing
return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing)
}
if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_ACCOUNT_ACTIONS, utils.TPDistinctIds{"loadid", "direction", "tenant", "account"}, nil, &utils.TPPagination{Page: attrs.Page, ItemsPerPage: attrs.ItemsPerPage, SearchTerm: attrs.SearchTerm}); err != nil {
if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_ACCOUNT_ACTIONS, utils.TPDistinctIds{"loadid", "direction", "tenant", "account"}, nil, &attrs.Paginator); err != nil {
return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
} else if ids == nil {
return errors.New(utils.ERR_NOT_FOUND)

View File

@@ -67,10 +67,8 @@ func (self *ApierV1) GetTPActions(attrs AttrGetTPActions, reply *utils.TPActions
}
type AttrGetTPActionIds struct {
TPid string // Tariff plan id
Page int
ItemsPerPage int
SearchTerm string
TPid string // Tariff plan id
utils.Paginator
}
// Queries Actions identities on specific tariff plan.
@@ -78,7 +76,7 @@ func (self *ApierV1) GetTPActionIds(attrs AttrGetTPActionIds, reply *[]string) e
if missing := utils.MissingStructFields(&attrs, []string{"TPid"}); len(missing) != 0 { //Params missing
return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing)
}
if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_ACTIONS, utils.TPDistinctIds{"id"}, nil, &utils.TPPagination{Page: attrs.Page, ItemsPerPage: attrs.ItemsPerPage, SearchTerm: attrs.SearchTerm}); err != nil {
if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_ACTIONS, utils.TPDistinctIds{"id"}, nil, &attrs.Paginator); err != nil {
return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
} else if ids == nil {
return errors.New(utils.ERR_NOT_FOUND)

View File

@@ -65,10 +65,8 @@ func (self *ApierV1) GetTPActionPlan(attrs AttrGetTPActionPlan, reply *utils.TPA
}
type AttrGetTPActionPlanIds struct {
TPid string // Tariff plan id
Page int
ItemsPerPage int
SearchTerm string
TPid string // Tariff plan id
utils.Paginator
}
// Queries ActionPlan identities on specific tariff plan.
@@ -76,7 +74,7 @@ func (self *ApierV1) GetTPActionPlanIds(attrs AttrGetTPActionPlanIds, reply *[]s
if missing := utils.MissingStructFields(&attrs, []string{"TPid"}); len(missing) != 0 { //Params missing
return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing)
}
if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_ACTION_PLANS, utils.TPDistinctIds{"id"}, nil, &utils.TPPagination{Page: attrs.Page, ItemsPerPage: attrs.ItemsPerPage, SearchTerm: attrs.SearchTerm}); err != nil {
if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_ACTION_PLANS, utils.TPDistinctIds{"id"}, nil, &attrs.Paginator); err != nil {
return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
} else if ids == nil {
return errors.New(utils.ERR_NOT_FOUND)

View File

@@ -63,10 +63,8 @@ func (self *ApierV1) GetTPActionTriggers(attrs AttrGetTPActionTriggers, reply *u
}
type AttrGetTPActionTriggerIds struct {
TPid string // Tariff plan id
Page int
ItemsPerPage int
SearchTerm string
TPid string // Tariff plan id
utils.Paginator
}
// Queries ActionTriggers identities on specific tariff plan.
@@ -74,7 +72,7 @@ func (self *ApierV1) GetTPActionTriggerIds(attrs AttrGetTPActionTriggerIds, repl
if missing := utils.MissingStructFields(&attrs, []string{"TPid"}); len(missing) != 0 { //Params missing
return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing)
}
if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_ACTION_TRIGGERS, utils.TPDistinctIds{"id"}, nil, &utils.TPPagination{Page: attrs.Page, ItemsPerPage: attrs.ItemsPerPage, SearchTerm: attrs.SearchTerm}); err != nil {
if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_ACTION_TRIGGERS, utils.TPDistinctIds{"id"}, nil, &attrs.Paginator); err != nil {
return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
} else if ids == nil {
return errors.New(utils.ERR_NOT_FOUND)

View File

@@ -67,10 +67,8 @@ func (self *ApierV1) GetTPCdrStats(attrs AttrGetTPCdrStats, reply *utils.TPCdrSt
}
type AttrGetTPCdrStatIds struct {
TPid string // Tariff plan id
Page int
ItemsPerPage int
SearchTerm string
TPid string // Tariff plan id
utils.Paginator
}
// Queries CdrStats identities on specific tariff plan.
@@ -78,7 +76,7 @@ func (self *ApierV1) GetTPCdrStatsIds(attrs AttrGetTPCdrStatIds, reply *[]string
if missing := utils.MissingStructFields(&attrs, []string{"TPid"}); len(missing) != 0 { //Params missing
return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing)
}
if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_CDR_STATS, utils.TPDistinctIds{"id"}, nil, &utils.TPPagination{Page: attrs.Page, ItemsPerPage: attrs.ItemsPerPage, SearchTerm: attrs.SearchTerm}); err != nil {
if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_CDR_STATS, utils.TPDistinctIds{"id"}, nil, &attrs.Paginator); err != nil {
return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
} else if ids == nil {
return errors.New(utils.ERR_NOT_FOUND)

View File

@@ -74,10 +74,8 @@ func (self *ApierV1) GetTPDerivedChargers(attrs AttrGetTPDerivedChargers, reply
}
type AttrGetTPDerivedChargeIds struct {
TPid string // Tariff plan id
Page int
ItemsPerPage int
SearchTerm string
TPid string // Tariff plan id
utils.Paginator
}
// Queries DerivedCharges identities on specific tariff plan.
@@ -85,7 +83,7 @@ func (self *ApierV1) GetTPDerivedChargerIds(attrs AttrGetTPDerivedChargeIds, rep
if missing := utils.MissingStructFields(&attrs, []string{"TPid"}); len(missing) != 0 { //Params missing
return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing)
}
if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_DERIVED_CHARGERS, utils.TPDistinctIds{"loadid", "direction", "tenant", "category", "account", "subject"}, nil, &utils.TPPagination{Page: attrs.Page, ItemsPerPage: attrs.ItemsPerPage, SearchTerm: attrs.SearchTerm}); err != nil {
if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_DERIVED_CHARGERS, utils.TPDistinctIds{"loadid", "direction", "tenant", "category", "account", "subject"}, nil, &attrs.Paginator); err != nil {
return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
} else if ids == nil {
return errors.New(utils.ERR_NOT_FOUND)

View File

@@ -42,6 +42,7 @@ func (self *ApierV1) SetTPDestinationRate(attrs utils.TPDestinationRate, reply *
type AttrGetTPDestinationRate struct {
TPid string // Tariff plan id
DestinationRateId string // Rate id
utils.Paginator
}
// Queries specific DestinationRate profile on tariff plan
@@ -49,7 +50,7 @@ func (self *ApierV1) GetTPDestinationRate(attrs AttrGetTPDestinationRate, reply
if missing := utils.MissingStructFields(&attrs, []string{"TPid", "DestinationRateId"}); len(missing) != 0 { //Params missing
return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing)
}
if drs, err := self.StorDb.GetTpDestinationRates(attrs.TPid, attrs.DestinationRateId); err != nil {
if drs, err := self.StorDb.GetTpDestinationRates(attrs.TPid, attrs.DestinationRateId, &attrs.Paginator); err != nil {
return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
} else if len(drs) == 0 {
return errors.New(utils.ERR_NOT_FOUND)
@@ -60,10 +61,8 @@ func (self *ApierV1) GetTPDestinationRate(attrs AttrGetTPDestinationRate, reply
}
type AttrTPDestinationRateIds struct {
TPid string // Tariff plan id
Page int
ItemsPerPage int
SearchTerm string
TPid string // Tariff plan id
utils.Paginator
}
// Queries DestinationRate identities on specific tariff plan.
@@ -71,7 +70,7 @@ func (self *ApierV1) GetTPDestinationRateIds(attrs AttrGetTPRateIds, reply *[]st
if missing := utils.MissingStructFields(&attrs, []string{"TPid"}); len(missing) != 0 { //Params missing
return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing)
}
if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_DESTINATION_RATES, utils.TPDistinctIds{"id"}, nil, &utils.TPPagination{Page: attrs.Page, ItemsPerPage: attrs.ItemsPerPage, SearchTerm: attrs.SearchTerm}); err != nil {
if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_DESTINATION_RATES, utils.TPDistinctIds{"id"}, nil, &attrs.Paginator); err != nil {
return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
} else if ids == nil {
return errors.New(utils.ERR_NOT_FOUND)

View File

@@ -62,10 +62,8 @@ func (self *ApierV1) GetTPDestination(attrs AttrGetTPDestination, reply *utils.T
}
type AttrGetTPDestinationIds struct {
TPid string // Tariff plan id
Page int
ItemsPerPage int
SearchTerm string
TPid string // Tariff plan id
utils.Paginator
}
// Queries destination identities on specific tariff plan.
@@ -73,7 +71,7 @@ func (self *ApierV1) GetTPDestinationIds(attrs AttrGetTPDestinationIds, reply *[
if missing := utils.MissingStructFields(&attrs, []string{"TPid"}); len(missing) != 0 { //Params missing
return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing)
}
if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_DESTINATIONS, utils.TPDistinctIds{"id"}, nil, &utils.TPPagination{Page: attrs.Page, ItemsPerPage: attrs.ItemsPerPage, SearchTerm: attrs.SearchTerm}); err != nil {
if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_DESTINATIONS, utils.TPDistinctIds{"id"}, nil, &attrs.Paginator); err != nil {
return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
} else if ids == nil {
return errors.New(utils.ERR_NOT_FOUND)

View File

@@ -60,10 +60,8 @@ func (self *ApierV1) GetTPRate(attrs AttrGetTPRate, reply *utils.TPRate) error {
}
type AttrGetTPRateIds struct {
TPid string // Tariff plan id
Page int
ItemsPerPage int
SearchTerm string
TPid string // Tariff plan id
utils.Paginator
}
// Queries rate identities on specific tariff plan.
@@ -71,7 +69,7 @@ func (self *ApierV1) GetTPRateIds(attrs AttrGetTPRateIds, reply *[]string) error
if missing := utils.MissingStructFields(&attrs, []string{"TPid"}); len(missing) != 0 { //Params missing
return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing)
}
if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_RATES, utils.TPDistinctIds{"id"}, nil, &utils.TPPagination{Page: attrs.Page, ItemsPerPage: attrs.ItemsPerPage, SearchTerm: attrs.SearchTerm}); err != nil {
if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_RATES, utils.TPDistinctIds{"id"}, nil, &attrs.Paginator); err != nil {
return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
} else if ids == nil {
return errors.New(utils.ERR_NOT_FOUND)

View File

@@ -42,6 +42,7 @@ func (self *ApierV1) SetTPRatingPlan(attrs utils.TPRatingPlan, reply *string) er
type AttrGetTPRatingPlan struct {
TPid string // Tariff plan id
RatingPlanId string // Rate id
utils.Paginator
}
// Queries specific RatingPlan profile on tariff plan
@@ -49,7 +50,7 @@ func (self *ApierV1) GetTPRatingPlan(attrs AttrGetTPRatingPlan, reply *utils.TPR
if missing := utils.MissingStructFields(&attrs, []string{"TPid", "RatingPlanId"}); len(missing) != 0 { //Params missing
return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing)
}
if rps, err := self.StorDb.GetTpRatingPlans(attrs.TPid, attrs.RatingPlanId); err != nil {
if rps, err := self.StorDb.GetTpRatingPlans(attrs.TPid, attrs.RatingPlanId, &attrs.Paginator); err != nil {
return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
} else if len(rps) == 0 {
return errors.New(utils.ERR_NOT_FOUND)
@@ -60,10 +61,8 @@ func (self *ApierV1) GetTPRatingPlan(attrs AttrGetTPRatingPlan, reply *utils.TPR
}
type AttrGetTPRatingPlanIds struct {
TPid string // Tariff plan id
Page int
ItemsPerPage int
SearchTerm string
TPid string // Tariff plan id
utils.Paginator
}
// Queries RatingPlan identities on specific tariff plan.
@@ -71,7 +70,7 @@ func (self *ApierV1) GetTPRatingPlanIds(attrs AttrGetTPRatingPlanIds, reply *[]s
if missing := utils.MissingStructFields(&attrs, []string{"TPid"}); len(missing) != 0 { //Params missing
return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing)
}
if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_RATING_PLANS, utils.TPDistinctIds{"id"}, nil, &utils.TPPagination{Page: attrs.Page, ItemsPerPage: attrs.ItemsPerPage, SearchTerm: attrs.SearchTerm}); err != nil {
if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_RATING_PLANS, utils.TPDistinctIds{"id"}, nil, &attrs.Paginator); err != nil {
return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
} else if ids == nil {
return errors.New(utils.ERR_NOT_FOUND)

View File

@@ -82,7 +82,7 @@ func (self *ApierV1) GetTPRatingProfileLoadIds(attrs utils.AttrTPRatingProfileId
"tor": attrs.Category,
"direction": attrs.Direction,
"subject": attrs.Subject,
}, new(utils.TPPagination)); err != nil {
}, new(utils.Paginator)); err != nil {
return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
} else if ids == nil {
return errors.New(utils.ERR_NOT_FOUND)
@@ -125,10 +125,8 @@ func (self *ApierV1) GetTPRatingProfile(attrs AttrGetTPRatingProfile, reply *uti
}
type AttrGetTPRatingProfileIds struct {
TPid string // Tariff plan id
Page int
ItemsPerPage int
SearchTerm string
TPid string // Tariff plan id
utils.Paginator
}
// Queries RatingProfiles identities on specific tariff plan.
@@ -136,7 +134,7 @@ func (self *ApierV1) GetTPRatingProfileIds(attrs AttrGetTPRatingProfileIds, repl
if missing := utils.MissingStructFields(&attrs, []string{"TPid"}); len(missing) != 0 { //Params missing
return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing)
}
if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_RATE_PROFILES, utils.TPDistinctIds{"loadid", "direction", "tenant", "category", "subject"}, nil, &utils.TPPagination{Page: attrs.Page, ItemsPerPage: attrs.ItemsPerPage, SearchTerm: attrs.SearchTerm}); err != nil {
if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_RATE_PROFILES, utils.TPDistinctIds{"loadid", "direction", "tenant", "category", "subject"}, nil, &attrs.Paginator); err != nil {
return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
} else if ids == nil {
return errors.New(utils.ERR_NOT_FOUND)

View File

@@ -67,10 +67,8 @@ func (self *ApierV1) GetTPSharedGroups(attrs AttrGetTPSharedGroups, reply *utils
}
type AttrGetTPSharedGroupIds struct {
TPid string // Tariff plan id
Page int
ItemsPerPage int
SearchTerm string
TPid string // Tariff plan id
utils.Paginator
}
// Queries SharedGroups identities on specific tariff plan.
@@ -78,7 +76,7 @@ func (self *ApierV1) GetTPSharedGroupIds(attrs AttrGetTPSharedGroupIds, reply *[
if missing := utils.MissingStructFields(&attrs, []string{"TPid"}); len(missing) != 0 { //Params missing
return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing)
}
if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_SHARED_GROUPS, utils.TPDistinctIds{"id"}, nil, &utils.TPPagination{Page: attrs.Page, ItemsPerPage: attrs.ItemsPerPage, SearchTerm: attrs.SearchTerm}); err != nil {
if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_SHARED_GROUPS, utils.TPDistinctIds{"id"}, nil, &attrs.Paginator); err != nil {
return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
} else if ids == nil {
return errors.New(utils.ERR_NOT_FOUND)

View File

@@ -62,10 +62,8 @@ func (self *ApierV1) GetTPTiming(attrs AttrGetTPTiming, reply *utils.ApierTPTimi
}
type AttrGetTPTimingIds struct {
TPid string // Tariff plan id
Page int
ItemsPerPage int
SearchTerm string
TPid string // Tariff plan id
utils.Paginator
}
// Queries timing identities on specific tariff plan.
@@ -73,7 +71,7 @@ func (self *ApierV1) GetTPTimingIds(attrs AttrGetTPTimingIds, reply *[]string) e
if missing := utils.MissingStructFields(&attrs, []string{"TPid"}); len(missing) != 0 { //Params missing
return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing)
}
if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_TIMINGS, utils.TPDistinctIds{"id"}, nil, &utils.TPPagination{Page: attrs.Page, ItemsPerPage: attrs.ItemsPerPage, SearchTerm: attrs.SearchTerm}); err != nil {
if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_TIMINGS, utils.TPDistinctIds{"id"}, nil, &attrs.Paginator); err != nil {
return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
} else if ids == nil {
return errors.New(utils.ERR_NOT_FOUND)

View File

@@ -479,6 +479,17 @@ func (ub *Account) CleanExpiredBalances() {
}
}
func (ub *Account) allBalancesExpired() bool {
for _, bm := range ub.BalanceMap {
for i := 0; i < len(bm); i++ {
if !bm[i].IsExpired() {
return false
}
}
}
return true
}
// returns the shared groups that this user balance belnongs to
func (ub *Account) GetSharedGroups() (groups []string) {
for _, balanceChain := range ub.BalanceMap {

View File

@@ -268,9 +268,9 @@ func TestActionTimingHourYears(t *testing.T) {
func TestActionTimingHourMonthdaysYear(t *testing.T) {
y, m, d := now.Date()
testTime := time.Date(y, 1, d, 10, 1, 0, 0, time.Local)
testTime := time.Date(y, m, d, 10, 1, 0, 0, time.Local)
tomorrow := time.Date(y, m, d, 10, 1, 0, 0, time.Local).AddDate(0, 0, 1)
nextYear := time.Date(y, 1, 1, 10, 1, 0, 0, time.Local).AddDate(1, 0, 0)
nextYear := time.Date(y, 1, d, 10, 1, 0, 0, time.Local).AddDate(1, 0, 0)
expected := testTime
if referenceDate.After(testTime) {
if referenceDate.After(tomorrow) {
@@ -287,6 +287,7 @@ func TestActionTimingHourMonthdaysYear(t *testing.T) {
},
}}
t.Log(at.Timing.Timing.CronString())
t.Log(time.Now(), referenceDate, referenceDate.After(testTime), referenceDate.After(testTime))
st := at.GetNextStartTime(referenceDate)
if !st.Equal(expected) {
t.Errorf("Expected %v was %v", expected, st)
@@ -1049,6 +1050,8 @@ func TestTopupActionLoaded(t *testing.T) {
initialValue := initialUb.BalanceMap[CREDIT+OUTBOUND].GetTotalValue()
afterValue := afterUb.BalanceMap[CREDIT+OUTBOUND].GetTotalValue()
if initialValue != 100 || afterValue != 125 {
t.Logf("Initial: %+v", initialUb)
t.Logf("After: %+v", afterUb)
t.Error("Bad topup before and after: ", initialValue, afterValue)
}
}

View File

@@ -312,7 +312,7 @@ func (dbr *DbReader) LoadRates() (err error) {
}
func (dbr *DbReader) LoadDestinationRates() (err error) {
dbr.destinationRates, err = dbr.storDb.GetTpDestinationRates(dbr.tpid, "")
dbr.destinationRates, err = dbr.storDb.GetTpDestinationRates(dbr.tpid, "", nil)
if err != nil {
return err
}
@@ -340,7 +340,7 @@ func (dbr *DbReader) LoadDestinationRates() (err error) {
}
func (dbr *DbReader) LoadRatingPlans() error {
mpRpls, err := dbr.storDb.GetTpRatingPlans(dbr.tpid, "")
mpRpls, err := dbr.storDb.GetTpRatingPlans(dbr.tpid, "", nil)
if err != nil {
return err
}
@@ -411,7 +411,7 @@ func (dbr *DbReader) LoadRatingProfiles() error {
// Returns true, nil in case of load success, false, nil in case of RatingPlan not found in storDb
func (dbr *DbReader) LoadRatingPlanByTag(tag string) (bool, error) {
mpRpls, err := dbr.storDb.GetTpRatingPlans(dbr.tpid, tag)
mpRpls, err := dbr.storDb.GetTpRatingPlans(dbr.tpid, tag, nil)
if err != nil {
return false, err
} else if len(mpRpls) == 0 {
@@ -427,7 +427,7 @@ func (dbr *DbReader) LoadRatingPlanByTag(tag string) (bool, error) {
return false, fmt.Errorf("No Timings profile with id %s: %v", rp.TimingId, err)
}
rp.SetTiming(tm[rp.TimingId])
drm, err := dbr.storDb.GetTpDestinationRates(dbr.tpid, rp.DestinationRatesId)
drm, err := dbr.storDb.GetTpDestinationRates(dbr.tpid, rp.DestinationRatesId, nil)
if err != nil || len(drm) == 0 {
return false, fmt.Errorf("No DestinationRates profile with id %s: %v", rp.DestinationRatesId, err)
}

View File

@@ -242,7 +242,7 @@ func TestLoadIndividualProfiles(t *testing.T) {
}
loader := NewDbReader(storDb, ratingDbApier, accountDbApier, TEST_SQL)
// Load ratingPlans. This will also set destination keys
if ratingPlans, err := storDb.GetTpRatingPlans(TEST_SQL, ""); err != nil {
if ratingPlans, err := storDb.GetTpRatingPlans(TEST_SQL, "", nil); err != nil {
t.Fatal("Could not retrieve rating plans")
} else {
for tag := range ratingPlans {

View File

@@ -116,7 +116,7 @@ type CdrStorage interface {
SetCdr(*utils.StoredCdr) error
SetRatedCdr(*utils.StoredCdr, string) error
GetStoredCdrs([]string, []string, []string, []string, []string, []string, []string, []string, []string, []string, []string, []string, []string, []string,
int64, int64, time.Time, time.Time, bool, bool, bool, *utils.TPPagination) ([]*utils.StoredCdr, error)
int64, int64, time.Time, time.Time, bool, bool, bool, *utils.Paginator) ([]*utils.StoredCdr, error)
RemStoredCdrs([]string) error
}
@@ -135,7 +135,7 @@ type LoadStorage interface {
// Apier functions
RemTPData(string, string, ...string) error
GetTPIds() ([]string, error)
GetTPTableIds(string, string, utils.TPDistinctIds, map[string]string, *utils.TPPagination) ([]string, error)
GetTPTableIds(string, string, utils.TPDistinctIds, map[string]string, *utils.Paginator) ([]string, error)
SetTPTiming(string, *utils.TPTiming) error
GetTpTimings(string, string) (map[string]*utils.TPTiming, error)
@@ -147,10 +147,10 @@ type LoadStorage interface {
GetTpRates(string, string) (map[string]*utils.TPRate, error)
SetTPDestinationRates(string, map[string][]*utils.DestinationRate) error
GetTpDestinationRates(string, string) (map[string]*utils.TPDestinationRate, error)
GetTpDestinationRates(string, string, *utils.Paginator) (map[string]*utils.TPDestinationRate, error)
SetTPRatingPlans(string, map[string][]*utils.TPRatingPlanBinding) error
GetTpRatingPlans(string, string) (map[string][]*utils.TPRatingPlanBinding, error)
GetTpRatingPlans(string, string, *utils.Paginator) (map[string][]*utils.TPRatingPlanBinding, error)
SetTPRatingProfiles(string, map[string]*utils.TPRatingProfile) error
GetTpRatingProfiles(*utils.TPRatingProfile) (map[string]*utils.TPRatingProfile, error)

View File

@@ -491,9 +491,14 @@ func (ms *MapStorage) GetAccount(key string) (ub *Account, err error) {
func (ms *MapStorage) SetAccount(ub *Account) (err error) {
// never override existing account with an empty one
// UPDATE: if all balances expired and were clean it makes
// sense to write empty balance map
if len(ub.BalanceMap) == 0 {
if ac, err := ms.GetAccount(ub.Id); err == nil {
if ac, err := ms.GetAccount(ub.Id); err == nil && !ac.allBalancesExpired() {
ac.ActionTriggers = ub.ActionTriggers
ac.UnitCounters = ub.UnitCounters
ac.AllowNegative = ub.AllowNegative
ac.Disabled = ub.Disabled
ub = ac
}
}

View File

@@ -23,6 +23,7 @@ import (
"compress/zlib"
"errors"
"fmt"
"log"
"github.com/cgrates/cgrates/cache2go"
"github.com/cgrates/cgrates/utils"
@@ -625,12 +626,18 @@ func (rs *RedisStorage) GetAccount(key string) (ub *Account, err error) {
func (rs *RedisStorage) SetAccount(ub *Account) (err error) {
// never override existing account with an empty one
// UPDATE: if all balances expired and were clean it makes
// sense to write empty balance map
if len(ub.BalanceMap) == 0 {
if ac, err := rs.GetAccount(ub.Id); err == nil {
if ac, err := rs.GetAccount(ub.Id); err == nil && !ac.allBalancesExpired() {
ac.ActionTriggers = ub.ActionTriggers
ac.UnitCounters = ub.UnitCounters
ac.AllowNegative = ub.AllowNegative
ac.Disabled = ub.Disabled
ub = ac
}
}
log.Print("Acc: ", ub)
result, err := rs.ms.Marshal(ub)
err = rs.db.Set(ACCOUNT_PREFIX+ub.Id, result)
return

View File

@@ -113,7 +113,7 @@ func (self *SQLStorage) GetTPIds() ([]string, error) {
return ids, nil
}
func (self *SQLStorage) GetTPTableIds(tpid, table string, distinct utils.TPDistinctIds, filters map[string]string, pagination *utils.TPPagination) ([]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 {
@@ -750,7 +750,7 @@ func (self *SQLStorage) SetRatedCdr(storedCdr *utils.StoredCdr, extraInfo string
// ignoreErr - do not consider cdrs with rating errors
// ignoreRated - do not consider cdrs which were already rated, including here the ones with errors
func (self *SQLStorage) GetStoredCdrs(cgrIds, runIds, tors, cdrHosts, cdrSources, reqTypes, directions, tenants, categories, accounts, subjects, destPrefixes, ratedAccounts, ratedSubjects []string,
orderIdStart, orderIdEnd int64, timeStart, timeEnd time.Time, ignoreErr, ignoreRated, ignoreDerived bool, pagination *utils.TPPagination) ([]*utils.StoredCdr, error) {
orderIdStart, orderIdEnd int64, timeStart, timeEnd time.Time, ignoreErr, ignoreRated, ignoreDerived bool, pagination *utils.Paginator) ([]*utils.StoredCdr, error) {
var cdrs []*utils.StoredCdr
var q *bytes.Buffer // Need to query differently since in case of primary, unmediated CDRs some values will be missing
if ignoreDerived {
@@ -1195,13 +1195,18 @@ func (self *SQLStorage) GetTpRates(tpid, tag string) (map[string]*utils.TPRate,
return rts, nil
}
func (self *SQLStorage) GetTpDestinationRates(tpid, tag string) (map[string]*utils.TPDestinationRate, error) {
func (self *SQLStorage) GetTpDestinationRates(tpid, tag string, pagination *utils.Paginator) (map[string]*utils.TPDestinationRate, error) {
rts := make(map[string]*utils.TPDestinationRate)
var tpDestinationRates []TpDestinationRate
q := self.db.Where("tpid = ?", tpid)
if len(tag) != 0 {
q = q.Where("id = ?", tag)
}
if pagination != nil {
limLow, limHigh := pagination.GetLimits()
q = q.Offset(limLow)
q = q.Limit(limHigh)
}
if err := q.Find(&tpDestinationRates).Error; err != nil {
return nil, err
}
@@ -1249,7 +1254,7 @@ func (self *SQLStorage) GetTpTimings(tpid, tag string) (map[string]*utils.TPTimi
return tms, nil
}
func (self *SQLStorage) GetTpRatingPlans(tpid, tag string) (map[string][]*utils.TPRatingPlanBinding, error) {
func (self *SQLStorage) GetTpRatingPlans(tpid, tag string, pagination *utils.Paginator) (map[string][]*utils.TPRatingPlanBinding, error) {
rpbns := make(map[string][]*utils.TPRatingPlanBinding)
var tpRatingPlans []TpRatingPlan
@@ -1261,6 +1266,12 @@ func (self *SQLStorage) GetTpRatingPlans(tpid, tag string) (map[string][]*utils.
return nil, err
}
if pagination != nil {
limLow, limHigh := pagination.GetLimits()
q = q.Offset(limLow)
q = q.Limit(limHigh)
}
for _, tpRp := range tpRatingPlans {
rpb := &utils.TPRatingPlanBinding{
DestinationRatesId: tpRp.DestratesId,

View File

@@ -19,9 +19,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package general_tests
import (
"github.com/cgrates/cgrates/engine"
"reflect"
"testing"
"github.com/cgrates/cgrates/engine"
)
var ratingDbAcntActs engine.RatingStorage
@@ -69,9 +70,6 @@ ENABLE_ACNT,*enable_account,,,,,,,,,,,10`
}
}
/*
//ToDo
// This test fails due to Disabled which is ignored on SetAccount with partial content. SetAccount should only be called with account when no previous one set in dataDb.
func TestAcntActsDisableAcnt(t *testing.T) {
acnt1Tag := "*out:cgrates.org:1"
at := &engine.ActionTiming{
@@ -88,7 +86,6 @@ func TestAcntActsDisableAcnt(t *testing.T) {
t.Errorf("Expecting: %+v, received: %+v", expectAcnt, acnt)
}
}
*/
func TestAcntActsEnableAcnt(t *testing.T) {
acnt1Tag := "*out:cgrates.org:1"

View File

@@ -34,13 +34,13 @@ func (tpdi TPDistinctIds) String() string {
}
// To paginate stuff from stordb (e.g. ids)
type TPPagination struct {
type Paginator struct {
Page int
ItemsPerPage int
SearchTerm string
}
func (pag *TPPagination) GetLimits() (low, high int) {
func (pag *Paginator) GetLimits() (low, high int) {
if pag.ItemsPerPage == 0 {
return 0, math.MaxInt32
}
@@ -582,9 +582,7 @@ type AttrGetCdrs struct {
TimeEnd string // If provided, it will represent the end of the CDRs interval (<)
SkipErrors bool // Do not export errored CDRs
SkipRated bool // Do not export rated CDRs
Page int
ItemsPerPage int
SearchTerm string
Paginator
}
type AttrRemCdrs struct {