mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Add paginator for GetActiveSessions and GetPassiveSessions
This commit is contained in:
committed by
Dan Christian Bogos
parent
4808861bc6
commit
032a572ca8
@@ -79,7 +79,7 @@ func (ssv1 *SessionSv1) ProcessEvent(args *sessions.V1ProcessEventArgs,
|
||||
|
||||
func (ssv1 *SessionSv1) GetActiveSessions(args *dispatchers.FilterSessionWithApiKey,
|
||||
rply *[]*sessions.ActiveSession) error {
|
||||
return ssv1.Ss.BiRPCv1GetActiveSessions(nil, args.Filters, rply)
|
||||
return ssv1.Ss.BiRPCv1GetActiveSessions(nil, &args.FilterWithPaginator, rply)
|
||||
}
|
||||
|
||||
func (ssv1 *SessionSv1) GetActiveSessionsCount(args *dispatchers.FilterSessionWithApiKey,
|
||||
@@ -94,7 +94,7 @@ func (ssv1 *SessionSv1) ForceDisconnect(args *dispatchers.FilterSessionWithApiKe
|
||||
|
||||
func (ssv1 *SessionSv1) GetPassiveSessions(args *dispatchers.FilterSessionWithApiKey,
|
||||
rply *[]*sessions.ActiveSession) error {
|
||||
return ssv1.Ss.BiRPCv1GetPassiveSessions(nil, args.Filters, rply)
|
||||
return ssv1.Ss.BiRPCv1GetPassiveSessions(nil, &args.FilterWithPaginator, rply)
|
||||
}
|
||||
|
||||
func (ssv1 *SessionSv1) GetPassiveSessionsCount(args *dispatchers.FilterSessionWithApiKey,
|
||||
|
||||
@@ -95,7 +95,7 @@ func (ssv1 *SessionSv1) BiRPCv1ProcessEvent(clnt *rpc2.Client, args *sessions.V1
|
||||
return ssv1.Ss.BiRPCv1ProcessEvent(clnt, args, rply)
|
||||
}
|
||||
|
||||
func (ssv1 *SessionSv1) BiRPCv1GetActiveSessions(clnt *rpc2.Client, args map[string]string,
|
||||
func (ssv1 *SessionSv1) BiRPCv1GetActiveSessions(clnt *rpc2.Client, args *sessions.FilterWithPaginator,
|
||||
rply *[]*sessions.ActiveSession) error {
|
||||
return ssv1.Ss.BiRPCv1GetActiveSessions(clnt, args, rply)
|
||||
}
|
||||
@@ -105,7 +105,7 @@ func (ssv1 *SessionSv1) BiRPCv1GetActiveSessionsCount(clnt *rpc2.Client, args ma
|
||||
return ssv1.Ss.BiRPCv1GetActiveSessionsCount(clnt, args, rply)
|
||||
}
|
||||
|
||||
func (ssv1 *SessionSv1) BiRPCv1GetPassiveSessions(clnt *rpc2.Client, args map[string]string,
|
||||
func (ssv1 *SessionSv1) BiRPCv1GetPassiveSessions(clnt *rpc2.Client, args *sessions.FilterWithPaginator,
|
||||
rply *[]*sessions.ActiveSession) error {
|
||||
return ssv1.Ss.BiRPCv1GetPassiveSessions(clnt, args, rply)
|
||||
}
|
||||
|
||||
@@ -197,7 +197,7 @@ func (dS *DispatcherService) SessionSv1GetActiveSessions(args *FilterSessionWith
|
||||
}
|
||||
}
|
||||
return dS.Dispatch(&utils.CGREvent{Tenant: args.TenantArg.Tenant}, utils.MetaSessionS, args.RouteID,
|
||||
utils.SessionSv1GetActiveSessions, args.Filters, reply)
|
||||
utils.SessionSv1GetActiveSessions, args.FilterWithPaginator, reply)
|
||||
}
|
||||
|
||||
func (dS *DispatcherService) SessionSv1GetActiveSessionsCount(args *FilterSessionWithApiKey,
|
||||
@@ -245,7 +245,7 @@ func (dS *DispatcherService) SessionSv1GetPassiveSessions(args *FilterSessionWit
|
||||
}
|
||||
}
|
||||
return dS.Dispatch(&utils.CGREvent{Tenant: args.TenantArg.Tenant}, utils.MetaSessionS, args.RouteID,
|
||||
utils.SessionSv1GetPassiveSessions, args.Filters, reply)
|
||||
utils.SessionSv1GetPassiveSessions, args.FilterWithPaginator, reply)
|
||||
}
|
||||
|
||||
func (dS *DispatcherService) SessionSv1GetPassiveSessionsCount(args *FilterSessionWithApiKey,
|
||||
|
||||
@@ -44,7 +44,7 @@ type DispatcherEvent struct {
|
||||
type FilterSessionWithApiKey struct {
|
||||
*utils.ArgDispatcher
|
||||
utils.TenantArg
|
||||
Filters map[string]string
|
||||
sessions.FilterWithPaginator
|
||||
}
|
||||
|
||||
type ArgsReplicateSessionsWithApiKey struct {
|
||||
|
||||
@@ -270,3 +270,8 @@ func (sr *SRun) debitReserve(dur time.Duration, lastUsage *time.Duration) (rDur
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
type FilterWithPaginator struct {
|
||||
Filters map[string]string
|
||||
*utils.Paginator
|
||||
}
|
||||
|
||||
@@ -1442,19 +1442,49 @@ func (sS *SessionS) CallBiRPC(clnt rpcclient.RpcClientConnection,
|
||||
|
||||
// BiRPCv1GetActiveSessions returns the list of active sessions based on filter
|
||||
func (sS *SessionS) BiRPCv1GetActiveSessions(clnt rpcclient.RpcClientConnection,
|
||||
fltr map[string]string, reply *[]*ActiveSession) (err error) {
|
||||
for fldName, fldVal := range fltr {
|
||||
args *FilterWithPaginator, reply *[]*ActiveSession) (err error) {
|
||||
if args == nil { //protection in case on nil
|
||||
args = &FilterWithPaginator{}
|
||||
}
|
||||
for fldName, fldVal := range args.Filters {
|
||||
if fldVal == "" {
|
||||
fltr[fldName] = utils.META_NONE
|
||||
args.Filters[fldName] = utils.META_NONE
|
||||
}
|
||||
}
|
||||
aSs, _, err := sS.asActiveSessions(fltr, false, false)
|
||||
aSs, _, err := sS.asActiveSessions(args.Filters, false, false)
|
||||
if err != nil {
|
||||
return utils.NewErrServerError(err)
|
||||
} else if len(aSs) == 0 {
|
||||
return utils.ErrNotFound
|
||||
}
|
||||
*reply = aSs
|
||||
if args.Paginator == nil { //small optimization
|
||||
*reply = aSs
|
||||
} else {
|
||||
var limit, offset int
|
||||
if args.Limit != nil && *args.Limit > 0 {
|
||||
limit = *args.Limit
|
||||
}
|
||||
if args.Offset != nil && *args.Offset > 0 {
|
||||
offset = *args.Offset
|
||||
}
|
||||
if limit == 0 && offset == 0 {
|
||||
*reply = aSs
|
||||
return
|
||||
}
|
||||
if offset > len(aSs) {
|
||||
return fmt.Errorf("Offset : %+v is greater than lenght of active sessions : %+v", offset, len(aSs))
|
||||
}
|
||||
if offset != 0 {
|
||||
limit = limit + offset
|
||||
}
|
||||
if limit == 0 {
|
||||
limit = len(aSs[offset:])
|
||||
} else if limit > len(aSs) {
|
||||
limit = len(aSs)
|
||||
}
|
||||
*reply = aSs[offset:limit]
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1476,19 +1506,49 @@ func (sS *SessionS) BiRPCv1GetActiveSessionsCount(clnt rpcclient.RpcClientConnec
|
||||
|
||||
// BiRPCv1GetPassiveSessions returns the passive sessions handled by SessionS
|
||||
func (sS *SessionS) BiRPCv1GetPassiveSessions(clnt rpcclient.RpcClientConnection,
|
||||
fltr map[string]string, reply *[]*ActiveSession) error {
|
||||
for fldName, fldVal := range fltr {
|
||||
args *FilterWithPaginator, reply *[]*ActiveSession) error {
|
||||
if args == nil { //protection in case on nil
|
||||
args = &FilterWithPaginator{}
|
||||
}
|
||||
for fldName, fldVal := range args.Filters {
|
||||
if fldVal == "" {
|
||||
fltr[fldName] = utils.META_NONE
|
||||
args.Filters[fldName] = utils.META_NONE
|
||||
}
|
||||
}
|
||||
pSs, _, err := sS.asActiveSessions(fltr, false, true)
|
||||
pSs, _, err := sS.asActiveSessions(args.Filters, false, true)
|
||||
if err != nil {
|
||||
return utils.NewErrServerError(err)
|
||||
} else if len(pSs) == 0 {
|
||||
return utils.ErrNotFound
|
||||
}
|
||||
*reply = pSs
|
||||
if args.Paginator == nil { //small optimization
|
||||
*reply = pSs
|
||||
} else {
|
||||
var limit, offset int
|
||||
if args.Limit != nil && *args.Limit > 0 {
|
||||
limit = *args.Limit
|
||||
}
|
||||
if args.Offset != nil && *args.Offset > 0 {
|
||||
offset = *args.Offset
|
||||
}
|
||||
if limit == 0 && offset == 0 {
|
||||
*reply = pSs
|
||||
return nil
|
||||
}
|
||||
if offset > len(pSs) {
|
||||
return fmt.Errorf("Offset : %+v is greater than lenght of passive sessions : %+v", offset, len(pSs))
|
||||
}
|
||||
if offset != 0 {
|
||||
limit = limit + offset
|
||||
}
|
||||
if limit == 0 {
|
||||
limit = len(pSs[offset:])
|
||||
} else if limit > len(pSs) {
|
||||
limit = len(pSs)
|
||||
}
|
||||
*reply = pSs[offset:limit]
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user