Skip to content

Commit

Permalink
fix: correct the totalCount value returned by the listModels API
Browse files Browse the repository at this point in the history
  • Loading branch information
dayuy committed Jan 2, 2024
1 parent a2fd073 commit 78ac1f2
Showing 1 changed file with 7 additions and 23 deletions.
30 changes: 7 additions & 23 deletions apiserver/pkg/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,40 +229,24 @@ 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
}
}
result = append(result, m)

// break if page size matches
if len(result) == pageSize {
break
if keyword == "" || strings.Contains(m.Name, keyword) || strings.Contains(*m.DisplayName, keyword) {
result = append(result, m)
}
}

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
}

Expand Down

0 comments on commit 78ac1f2

Please sign in to comment.