Skip to content

Commit

Permalink
fix(proxy): 修复分页错误的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron3S committed Sep 15, 2021
1 parent aa41572 commit fcc52ed
Showing 1 changed file with 43 additions and 18 deletions.
61 changes: 43 additions & 18 deletions internal/api/v1/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,20 @@ func (h *Handler) KubernetesAPIProxy() iris.Handler {
ctx.Values().Set("message", err)
return
}
_, _ = ctx.JSON(resp)
klo := K8sListObj{
Kind: resp.Kind,
ApiVersion: resp.APIVersion,
Metadata: resp.ListMeta,
Items: resp.Items,
}

p, err := pagerAndSearch(ctx, klo, keywords)
if err != nil {
ctx.StatusCode(iris.StatusInternalServerError)
ctx.Values().Set("message", err)
return
}
_, _ = ctx.JSON(p)
return
}
if http.MethodGet == requestMethod && namespaced && namespace != "" && !hasNsFilter {
Expand All @@ -162,30 +175,19 @@ func (h *Handler) KubernetesAPIProxy() iris.Handler {
resp.StatusCode = http.StatusInternalServerError
}
if req.Method == http.MethodGet && search {
num, err1 := ctx.Values().GetInt("pageNum")
size, err2 := ctx.Values().GetInt("pageSize")
var listObj K8sListObj
if err := json.Unmarshal(rawResp, &listObj); err != nil {
ctx.StatusCode(iris.StatusInternalServerError)
ctx.Values().Set("message", err)
return
}
listObj.Sort()
if keywords != "" {
listObj.Items = fieldFilter(listObj.Items, withNamespaceAndNameMatcher(keywords))
}
total := len(listObj.Items)
if err1 == nil && err2 == nil {
tt, items, err := pageFilter(num, size, listObj.Items)
if err != nil {
ctx.StatusCode(iris.StatusInternalServerError)
ctx.Values().Set("message", err)
return
}
total = tt
listObj.Items = items
p, err := pagerAndSearch(ctx, listObj, keywords)
if err != nil {
ctx.StatusCode(iris.StatusInternalServerError)
ctx.Values().Set("message", err.Error())
return
}
_, _ = ctx.JSON(pkgV1.Page{Items: listObj.Items, Total: total})
_, _ = ctx.JSON(p)
return
}
ctx.StatusCode(resp.StatusCode)
Expand All @@ -196,6 +198,29 @@ func (h *Handler) KubernetesAPIProxy() iris.Handler {

var timeTemplate = "2006-01-02T15:04:05Z"

func pagerAndSearch(ctx *context.Context, listObj K8sListObj, keywords string) (*pkgV1.Page, error) {
num, err1 := ctx.Values().GetInt("pageNum")
size, err2 := ctx.Values().GetInt("pageSize")
var p pkgV1.Page
listObj.Sort()
if keywords != "" {
listObj.Items = fieldFilter(listObj.Items, withNamespaceAndNameMatcher(keywords))
}
total := len(listObj.Items)
if err1 == nil && err2 == nil {
tt, items, err := pageFilter(num, size, listObj.Items)
if err != nil {
return nil, err
}
total = tt
listObj.Items = items
p.Total = total
p.Items = listObj.Items
return &p, nil
}
return nil, nil
}

func getTime(obj interface{}) time.Time {
//判断是否存在lasttime
o := obj.(map[string]interface{})
Expand Down

0 comments on commit fcc52ed

Please sign in to comment.