Skip to content

Commit 49bbae6

Browse files
authored
Merge pull request #56 from aliyun/feature/fix_list_projects
fix: add pagination support for listing projects
2 parents e64c2dd + a7a9cb8 commit 49bbae6

File tree

3 files changed

+46
-20
lines changed

3 files changed

+46
-20
lines changed

api/api_fs_project.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ FsProjectApiService List Projects
2020
2121
@return ListProjectsResponse
2222
*/
23-
func (a *FsProjectApiService) ListProjects() (ListProjectsResponse, error) {
23+
func (a *FsProjectApiService) ListProjects(pagesize, pagenumber int32) (ListProjectsResponse, error) {
2424
request := paifeaturestore.ListProjectsRequest{}
2525
request.SetName(a.client.cfg.ProjectName)
26+
request.SetPageSize(pagesize)
27+
request.SetPageNumber(pagenumber)
2628

2729
response, err := a.client.ListProjects(&a.client.instanceId, &request)
2830
var (
@@ -33,6 +35,7 @@ func (a *FsProjectApiService) ListProjects() (ListProjectsResponse, error) {
3335
return localVarReturnValue, err
3436
}
3537

38+
localVarReturnValue.TotalCount = int(*response.Body.TotalCount)
3639
var projects []*Project
3740
for _, projectItem := range response.Body.Projects {
3841
if id, err := strconv.Atoi(*projectItem.ProjectId); err == nil {

api/response_project.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package api
22

33
type ListProjectsResponse struct {
4-
Projects []*Project `json:"data,omitempty"`
4+
TotalCount int
5+
Projects []*Project
56
}

featurestore/feature_store_client.go

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -223,13 +223,27 @@ func (c *FeatureStoreClient) LoadProjectData() error {
223223
}
224224
projectData := make(map[string]*domain.Project, 0)
225225

226-
listProjectsResponse, err := c.client.FsProjectApi.ListProjects()
227-
if err != nil {
228-
c.logError(fmt.Errorf("list projects error, err=%v", err))
229-
return err
226+
var (
227+
pagesize = 100
228+
pagenumber = 1
229+
)
230+
projects := make([]*api.Project, 0)
231+
for {
232+
listProjectsResponse, err := c.client.FsProjectApi.ListProjects(int32(pagesize), int32(pagenumber))
233+
if err != nil {
234+
c.logError(fmt.Errorf("list projects error, err=%v", err))
235+
return err
236+
}
237+
projects = append(projects, listProjectsResponse.Projects...)
238+
239+
if len(listProjectsResponse.Projects) == 0 || pagesize*pagenumber > listProjectsResponse.TotalCount {
240+
break
241+
}
242+
243+
pagenumber++
230244
}
231245

232-
for _, p := range listProjectsResponse.Projects {
246+
for _, p := range projects {
233247
if p.ProjectName != c.client.GetConfig().ProjectName {
234248
continue
235249
}
@@ -269,10 +283,7 @@ func (c *FeatureStoreClient) LoadProjectData() error {
269283
project.SetApiClient(c.client)
270284
projectData[project.ProjectName] = project
271285

272-
var (
273-
pagesize = 100
274-
pagenumber = 1
275-
)
286+
pagenumber = 1
276287

277288
// get feature entities
278289
for {
@@ -385,13 +396,27 @@ func (c *FeatureStoreClient) lazyLoadProjectData() error {
385396
}
386397
projectData := make(map[string]*domain.Project, 0)
387398

388-
listProjectsResponse, err := c.client.FsProjectApi.ListProjects()
389-
if err != nil {
390-
c.logError(fmt.Errorf("list projects error, err=%v", err))
391-
return err
399+
var (
400+
pagesize = 100
401+
pagenumber = 1
402+
)
403+
projects := make([]*api.Project, 0)
404+
for {
405+
listProjectsResponse, err := c.client.FsProjectApi.ListProjects(int32(pagesize), int32(pagenumber))
406+
if err != nil {
407+
c.logError(fmt.Errorf("list projects error, err=%v", err))
408+
return err
409+
}
410+
projects = append(projects, listProjectsResponse.Projects...)
411+
412+
if len(listProjectsResponse.Projects) == 0 || pagesize*pagenumber > listProjectsResponse.TotalCount {
413+
break
414+
}
415+
416+
pagenumber++
392417
}
393418

394-
for _, p := range listProjectsResponse.Projects {
419+
for _, p := range projects {
395420
if p.ProjectName != c.client.GetConfig().ProjectName {
396421
continue
397422
}
@@ -431,10 +456,7 @@ func (c *FeatureStoreClient) lazyLoadProjectData() error {
431456
project.SetApiClient(c.client)
432457
projectData[project.ProjectName] = project
433458

434-
var (
435-
pagesize = 100
436-
pagenumber = 1
437-
)
459+
pagenumber = 1
438460

439461
// get feature entities
440462
for {

0 commit comments

Comments
 (0)