Skip to content

Commit

Permalink
feat(pixiu): add pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
jihu committed Dec 12, 2024
1 parent 2a13d26 commit 014c7c5
Show file tree
Hide file tree
Showing 30 changed files with 403 additions and 261 deletions.
8 changes: 4 additions & 4 deletions api/server/router/audit/audit_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ func (a *auditRouter) listAudits(c *gin.Context) {
r := httputils.NewResponse()

var (
err error
req types.PageRequest
listOption types.ListOptions // 分页设置
err error
)
if err = httputils.ShouldBindAny(c, nil, nil, &req); err != nil {
if err = httputils.ShouldBindAny(c, nil, nil, &listOption); err != nil {
httputils.SetFailed(c, r, err)
return
}
if r.Result, err = a.c.Audit().List(c, &req); err != nil {
if r.Result, err = a.c.Audit().List(c, listOption); err != nil {
httputils.SetFailed(c, r, err)
return
}
Expand Down
20 changes: 11 additions & 9 deletions api/server/router/cluster/cluster_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,14 @@ func (cr *clusterRouter) listClusters(c *gin.Context) {
r := httputils.NewResponse()

var (
err error
req types.PageRequest
err error
listOptions types.ListOptions
)
if err = httputils.ShouldBindAny(c, nil, nil, &req); err != nil {
if err = httputils.ShouldBindAny(c, nil, nil, &listOptions); err != nil {
httputils.SetFailed(c, r, err)
return
}
if r.Result, err = cr.c.Cluster().List(c, &req); err != nil {
if r.Result, err = cr.c.Cluster().List(c, &listOptions); err != nil {
httputils.SetFailed(c, r, err)
return
}
Expand Down Expand Up @@ -276,17 +276,19 @@ func (cr *clusterRouter) getEventList(c *gin.Context) {
r := httputils.NewResponse()
var (
opts struct {
Cluster string `uri:"cluster" binding:"required"`
types.PageRequest `json:",inline"`
Cluster string `uri:"cluster" binding:"required"`
}
eventOpt struct {
types.EventOptions `json:",inline"`
types.ListOptions `json:",inline"`
}
eventOpt types.EventOptions
err error
err error
)
if err = httputils.ShouldBindAny(c, nil, &opts, &eventOpt); err != nil {
httputils.SetFailed(c, r, err)
return
}
if r.Result, err = cr.c.Cluster().GetEventList(c, opts.Cluster, eventOpt, opts.PageRequest); err != nil {
if r.Result, err = cr.c.Cluster().GetEventList(c, opts.Cluster, eventOpt.EventOptions, &eventOpt.ListOptions); err != nil {
httputils.SetFailed(c, r, err)
return
}
Expand Down
10 changes: 5 additions & 5 deletions api/server/router/cluster/helm_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ import (
func (cr *clusterRouter) ListReleases(c *gin.Context) {
r := httputils.NewResponse()
var (
err error
helmMeta types.PixiuObjectMeta
req types.PageRequest
err error
helmMeta types.PixiuObjectMeta
listOptions types.ListOptions
)
if err = httputils.ShouldBindAny(c, nil, &helmMeta, &req); err != nil {
if err = httputils.ShouldBindAny(c, nil, &helmMeta, &listOptions); err != nil {
httputils.SetFailed(c, r, err)
return
}
if r.Result, err = cr.c.Cluster().ListReleases(c, helmMeta.Cluster, helmMeta.Namespace, &req); err != nil {
if r.Result, err = cr.c.Cluster().ListReleases(c, helmMeta.Cluster, helmMeta.Namespace, &listOptions); err != nil {
httputils.SetFailed(c, r, err)
return
}
Expand Down
10 changes: 5 additions & 5 deletions api/server/router/plan/node_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,15 @@ func (t *planRouter) listPlanNodes(c *gin.Context) {
r := httputils.NewResponse()

var (
opt planMeta
req types.PageRequest
err error
opt planMeta
listOptions types.ListOptions
err error
)
if err = httputils.ShouldBindAny(c, nil, &opt, &req); err != nil {
if err = httputils.ShouldBindAny(c, nil, &opt, &listOptions); err != nil {
httputils.SetFailed(c, r, err)
return
}
if r.Result, err = t.c.Plan().ListNodes(c, opt.PlanId, &req); err != nil {
if r.Result, err = t.c.Plan().ListNodes(c, opt.PlanId, &listOptions); err != nil {
httputils.SetFailed(c, r, err)
return
}
Expand Down
8 changes: 4 additions & 4 deletions api/server/router/plan/plan_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,14 @@ func (t *planRouter) listPlans(c *gin.Context) {
r := httputils.NewResponse()

var (
err error
req types.PageRequest
err error
opts types.ListOptions
)
if err = httputils.ShouldBindAny(c, nil, nil, &req); err != nil {
if err = httputils.ShouldBindAny(c, nil, nil, &opts); err != nil {
httputils.SetFailed(c, r, err)
return
}
if r.Result, err = t.c.Plan().List(c, &req); err != nil {
if r.Result, err = t.c.Plan().List(c, &opts); err != nil {
httputils.SetFailed(c, r, err)
return
}
Expand Down
2 changes: 1 addition & 1 deletion api/server/router/plan/task_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (t *planRouter) listTasks(c *gin.Context) {

// 不是长连接请求则直接返回
if !watch.Watch {
if r.Result, err = t.c.Plan().ListTasks(c, opt.PlanId, &watch.PageRequest); err != nil {
if r.Result, err = t.c.Plan().ListTasks(c, opt.PlanId, &watch.ListOptions); err != nil {
httputils.SetFailed(c, r, err)
return
}
Expand Down
8 changes: 4 additions & 4 deletions api/server/router/tenant/tenant_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,14 @@ func (t *tenantRouter) listTenants(c *gin.Context) {
r := httputils.NewResponse()

var (
err error
req types.PageRequest
err error
listOptions types.ListOptions
)
if err = httputils.ShouldBindAny(c, nil, nil, &req); err != nil {
if err = httputils.ShouldBindAny(c, nil, nil, &listOptions); err != nil {
httputils.SetFailed(c, r, err)
return
}
if r.Result, err = t.c.Tenant().List(c, &req); err != nil {
if r.Result, err = t.c.Tenant().List(c, &listOptions); err != nil {
httputils.SetFailed(c, r, err)
return
}
Expand Down
2 changes: 1 addition & 1 deletion api/server/router/user/user_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func (u *userRouter) listUsers(c *gin.Context) {
if opts.Count {
r.Result, err = u.c.User().GetCount(c, opts)
} else {
r.Result, err = u.c.User().List(c, &opts.PageRequest)
r.Result, err = u.c.User().List(c, &opts)
}
if err != nil {
httputils.SetFailed(c, r, err)
Expand Down
2 changes: 1 addition & 1 deletion config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ default:

# 数据库地址信息
mysql:
host: peng
host: 124.222.40.110
user: root
password: Pixiu868686
port: 3306
Expand Down
8 changes: 4 additions & 4 deletions pkg/client/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type WrapObject struct {
type Task struct {
sync.RWMutex

Lister func(ctx context.Context, planId int64, opts ...db.Options) ([]model.Task, int64, error)
Lister func(ctx context.Context, planId int64, opts ...db.Options) ([]model.Task, error)
items map[int64]WrapObject
}

Expand All @@ -45,7 +45,7 @@ func NewTaskCache() *Task {
return t
}

func (t *Task) SetLister(Lister func(ctx context.Context, planId int64, opts ...db.Options) ([]model.Task, int64, error)) {
func (t *Task) SetLister(Lister func(ctx context.Context, planId int64, opts ...db.Options) ([]model.Task, error)) {
t.Lock()
defer t.Unlock()

Expand Down Expand Up @@ -127,7 +127,7 @@ func (t *Task) WaitForCacheSync(planId int64) error {
return nil
}

tasks, _, err := t.Lister(context.TODO(), planId)
tasks, err := t.Lister(context.TODO(), planId)
if err != nil {
return fmt.Errorf("failed to get plan(%d) tasks from database: %v", planId, err)
}
Expand Down Expand Up @@ -155,7 +155,7 @@ func (t *Task) syncTasks() {
continue
}

newTasks, _, err := t.Lister(context.TODO(), planId)
newTasks, err := t.Lister(context.TODO(), planId)
if err != nil {
klog.Errorf("[syncTasks] failed to list plan(%d) tasks: %v", planId, err)
delete(t.items, planId)
Expand Down
36 changes: 21 additions & 15 deletions pkg/controller/audit/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type AuditGetter interface {
}

type Interface interface {
List(ctx context.Context, req *types.PageRequest) (*types.PageResponse, error)
List(ctx context.Context, listOption types.ListOptions) (interface{}, error)
Get(ctx context.Context, aid int64) (*types.Audit, error)
}

Expand All @@ -54,29 +54,35 @@ func (a *audit) Get(ctx context.Context, aid int64) (*types.Audit, error) {
return a.model2Type(object), nil
}

func (a *audit) List(ctx context.Context, req *types.PageRequest) (*types.PageResponse, error) {
var (
ts []types.Audit
pageResp types.PageResponse
options = []db.Options{db.WithOrderByDesc()}
)
if req != nil {
options = append(options, db.WithPagination(req.Page, req.Limit))
func (a *audit) List(ctx context.Context, listOption types.ListOptions) (interface{}, error) {
var ts []types.Audit

// 获取对象总数量
total, err := a.factory.Audit().Count(ctx)
if err != nil {
klog.Errorf("failed to get audits count: %v", err)
return nil, err
}
if total == 0 {
return &types.PageResponse{}, nil
}

objects, total, err := a.factory.Audit().List(ctx, options...)
// 获取偏移列表
objects, err := a.factory.Audit().List(ctx, listOption.BuildPageNation()...)
if err != nil {
klog.Errorf("failed to get tenants: %v", err)
return nil, errors.ErrServerInternal
klog.Errorf("failed to get audit events: %v", err)
return nil, err
}

for _, object := range objects {
ts = append(ts, *a.model2Type(&object))
}
pageResp.Total = total
pageResp.Items = ts

return &pageResp, nil
return types.PageResponse{
PageRequest: listOption.PageRequest,
Total: int(total),
Items: ts,
}, nil
}

func (a *audit) model2Type(o *model.Audit) *types.Audit {
Expand Down
26 changes: 14 additions & 12 deletions pkg/controller/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ type (
Interface interface {
CreateRBACPolicy(ctx context.Context, req *types.RBACPolicyRequest) error
DeleteRBACPolicy(ctx context.Context, req *types.RBACPolicyRequest) error
ListRBACPolicies(ctx context.Context, req *types.ListRBACPolicyRequest) ([]types.RBACPolicy, error)
ListRBACPolicies(ctx context.Context, req *types.ListRBACPolicyRequest) (*types.PageResponse, error)

CreateGroupBinding(ctx context.Context, req *types.GroupBindingRequest) error
DeleteGroupBinding(ctx context.Context, req *types.GroupBindingRequest) error
ListGroupBindings(ctx context.Context, req *types.ListGroupBindingRequest) ([]types.RBACPolicy, error)
ListGroupBindings(ctx context.Context, req *types.ListGroupBindingRequest) (*types.PageResponse, error)
}
)

Expand Down Expand Up @@ -152,7 +152,7 @@ func (a *auth) DeleteRBACPolicy(ctx context.Context, req *types.RBACPolicyReques
return nil
}

func (a *auth) ListRBACPolicies(ctx context.Context, req *types.ListRBACPolicyRequest) ([]types.RBACPolicy, error) {
func (a *auth) ListRBACPolicies(ctx context.Context, req *types.ListRBACPolicyRequest) (*types.PageResponse, error) {
user, err := a.factory.User().Get(ctx, req.UserId)
if err != nil {
klog.Errorf("failed to get user(%d): %v", req.UserId, err)
Expand Down Expand Up @@ -183,16 +183,17 @@ func (a *auth) ListRBACPolicies(ctx context.Context, req *types.ListRBACPolicyRe
for i, policy := range policies {
rbacPolicies[i] = *model2Type(policy)
}
if req.PageRequest == nil {
return rbacPolicies, nil
}

rbacPolicies, err = a.authForPager(rbacPolicies, req.PageRequest)
if err != nil {
return nil, errors.ErrServerInternal
}

return rbacPolicies, nil
return &types.PageResponse{
Total: len(policies),
Items: rbacPolicies,
PageRequest: *req.PageRequest,
}, nil
}

func (a *auth) authForPager(rbacPolicies []types.RBACPolicy, req *types.PageRequest) ([]types.RBACPolicy, error) {
Expand Down Expand Up @@ -239,7 +240,7 @@ func (a *auth) DeleteGroupBinding(ctx context.Context, req *types.GroupBindingRe
return nil
}

func (a *auth) ListGroupBindings(ctx context.Context, req *types.ListGroupBindingRequest) ([]types.RBACPolicy, error) {
func (a *auth) ListGroupBindings(ctx context.Context, req *types.ListGroupBindingRequest) (*types.PageResponse, error) {
conds := make([]ctrlutil.BindingQueryCondition, 0)
if req.UserId != nil {
user, err := a.factory.User().Get(ctx, *req.UserId)
Expand Down Expand Up @@ -276,16 +277,17 @@ func (a *auth) ListGroupBindings(ctx context.Context, req *types.ListGroupBindin
for i, binding := range bindings {
bindingPolicies[i] = *model2Type(binding)
}
if req.PageRequest == nil {
return bindingPolicies, nil
}

bindingPolicies, err = a.authForPager(bindingPolicies, req.PageRequest)
if err != nil {
return nil, errors.ErrServerInternal
}

return bindingPolicies, nil
return &types.PageResponse{
Total: len(bindings),
Items: bindingPolicies,
PageRequest: *req.PageRequest,
}, nil
}

func model2Type(policy model.Policy) *types.RBACPolicy {
Expand Down
Loading

0 comments on commit 014c7c5

Please sign in to comment.