Skip to content

Commit

Permalink
Merge branch 'master' into pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
caoyingjunz authored Dec 11, 2024
2 parents f9fb99e + 28ca96a commit bb2a1b0
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 29 deletions.
10 changes: 10 additions & 0 deletions api/server/middleware/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package middleware
import (
"context"
"net/http"
"strings"

"github.com/gin-contrib/requestid"
"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -84,6 +85,15 @@ func (w *auditWriter) asyncAudit(c *gin.Context) {

// getAuditStatus returns the status of operation.
func getAuditStatus(c *gin.Context) model.AuditOperationStatus {
// Directly use the native structure of kubernetes for the request of /pixiu/proxy and do separate processing
if strings.Contains(c.Request.RequestURI, "/pixiu/proxy") {
if responseOK(c.Writer.Status()) {
return model.AuditOpSuccess
} else {
return model.AuditOpFail
}
}

respCode := httputils.GetResponseCode(c)
if respCode == 0 {
return model.AuditOpUnknown
Expand Down
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
27 changes: 12 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,32 +54,29 @@ 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

func (a *audit) List(ctx context.Context, listOption types.ListOptions) (interface{}, error) {
// 获取对象总数量
total, err := a.factory.Audit().Count(ctx)
if err != nil {
klog.Errorf("failed to get tenant count: %v", err)
return nil, errors.ErrServerInternal
}
if total == 0 {
return &types.PageResponse{}, nil
klog.Errorf("failed to get audits count: %v", err)
return nil, err
}

objects, err := a.factory.Audit().List(ctx, req.BuildPageNation()...)
// 获取偏移列表
objects, err := a.factory.Audit().List(ctx, db.WithOffset(listOption.Page-1), db.WithLimit(int(listOption.Limit)), db.WithOrderByDesc())
if err != nil {
klog.Errorf("failed to get tenants: %v", err)
klog.Errorf("failed to get audit events: %v", err)
return nil, errors.ErrServerInternal
}

var ts []types.Audit
for _, object := range objects {
ts = append(ts, *a.model2Type(&object))
}

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

Expand Down
23 changes: 13 additions & 10 deletions pkg/db/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ type AuditInterface interface {
Get(ctx context.Context, id int64) (*model.Audit, error)
Create(ctx context.Context, object *model.Audit) (*model.Audit, error)
BatchDelete(ctx context.Context, opts ...Options) (int64, error)
Count(ctx context.Context) (int64, error)

Count(ctx context.Context, opts ...Options) (int64, error)
}

type audit struct {
Expand Down Expand Up @@ -64,17 +65,8 @@ func (a *audit) Get(ctx context.Context, aid int64) (*model.Audit, error) {
return audit, nil
}

func (a *audit) Count(ctx context.Context) (int64, error) {
var total int64
if err := a.db.WithContext(ctx).Model(&model.Audit{}).Count(&total).Error; err != nil {
return 0, err
}
return total, nil
}

func (a *audit) List(ctx context.Context, opts ...Options) ([]model.Audit, error) {
var audits []model.Audit

tx := a.db.WithContext(ctx)
for _, opt := range opts {
tx = opt(tx)
Expand All @@ -95,3 +87,14 @@ func (a *audit) BatchDelete(ctx context.Context, opts ...Options) (int64, error)
err := tx.Delete(&model.Audit{}).Error
return tx.RowsAffected, err
}

func (a *audit) Count(ctx context.Context, opts ...Options) (int64, error) {
tx := a.db.WithContext(ctx)
for _, opt := range opts {
tx = opt(tx)
}

var total int64
err := tx.Model(&model.Audit{}).Count(&total).Error
return total, err
}

0 comments on commit bb2a1b0

Please sign in to comment.