From cf7a14c8836ca06fdb398632cb2fc9003db4c557 Mon Sep 17 00:00:00 2001 From: dayuy <973860441@qq.com> Date: Tue, 2 Jan 2024 18:32:40 +0800 Subject: [PATCH] fix: correct the totalCount value returned by the listModels API --- apiserver/pkg/model/model.go | 29 +++++++---------------------- 1 file changed, 7 insertions(+), 22 deletions(-) diff --git a/apiserver/pkg/model/model.go b/apiserver/pkg/model/model.go index cdb6f09c7..02a1fb867 100644 --- a/apiserver/pkg/model/model.go +++ b/apiserver/pkg/model/model.go @@ -229,40 +229,25 @@ func ListModels(ctx context.Context, c dynamic.Interface, input generated.ListMo models.Items = append(systemModels.Items, models.Items...) } - totalCount := len(models.Items) - - result := make([]generated.PageNode, 0, pageSize) - pageStart := (page - 1) * pageSize - for index, u := range models.Items { - // skip if smaller than the start index - if index < pageStart { - continue - } - + result := make([]generated.PageNode, 0, len(models.Items)) + for _, u := range models.Items { m := obj2model(&u) // filter based on `keyword` - if keyword != "" { - if !strings.Contains(m.Name, keyword) && !strings.Contains(*m.DisplayName, keyword) { - continue - } + if keyword != "" && !strings.Contains(m.Name, keyword) && !strings.Contains(*m.DisplayName, keyword) { + continue } result = append(result, m) - - // break if page size matches - if len(result) == pageSize { - break - } } - + totalCount := len(result) + pageStart := (page - 1) * pageSize end := page * pageSize if end > totalCount { end = totalCount } - return &generated.PaginatedResult{ TotalCount: totalCount, HasNextPage: end < totalCount, - Nodes: result, + Nodes: result[pageStart:end], }, nil }