Skip to content

Commit

Permalink
feat(api): v2PageMetadata to log page information
Browse files Browse the repository at this point in the history
This new type is used to compute the total pages and the page number
when reading pages using the `client.NextPage()` function

Inside the Pageable interface, there are a few new functions that are
automatically implemented when attaching the `v2PageMetadata` type
into any Pageable struct, so attaching that struct is a requirement.

Signed-off-by: Salim Afiune Maya <[email protected]>
  • Loading branch information
afiune committed Feb 1, 2023
1 parent d3cb69a commit fd5338b
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 38 deletions.
3 changes: 3 additions & 0 deletions api/agent_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ func (svc *AgentInfoService) Search(response interface{}, filters SearchFilter)
type AgentInfoResponse struct {
Data []AgentInfo `json:"data"`
Paging V2Pagination `json:"paging"`

v2PageMetadata `json:"-"`
}

// Fulfill Pageable interface (look at api/v2.go)
Expand All @@ -43,6 +45,7 @@ func (r AgentInfoResponse) PageInfo() *V2Pagination {
}
func (r *AgentInfoResponse) ResetPaging() {
r.Paging = V2Pagination{}
r.Data = nil
}

type AgentInfo struct {
Expand Down
7 changes: 5 additions & 2 deletions api/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ func (a Alerts) SortBySeverity() {
type AlertsResponse struct {
Data Alerts `json:"data"`
Paging V2Pagination `json:"paging"`

v2PageMetadata `json:"-"`
}

// Fulfill Pageable interface (look at api/v2.go)
Expand All @@ -100,6 +102,7 @@ func (r AlertsResponse) PageInfo() *V2Pagination {
}
func (r *AlertsResponse) ResetPaging() {
r.Paging = V2Pagination{}
r.Data = nil
}

func (svc *AlertsService) List() (response AlertsResponse, err error) {
Expand Down Expand Up @@ -127,8 +130,8 @@ func (svc *AlertsService) ListAll() (response AlertsResponse, err error) {
break
}

response.Data = all
response.ResetPaging()
response.Data = all
return
}

Expand Down Expand Up @@ -172,7 +175,7 @@ func (svc *AlertsService) ListAllByTime(start, end time.Time) (
break
}

response.Data = all
response.ResetPaging()
response.Data = all
return
}
2 changes: 1 addition & 1 deletion api/alerts_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (svc *AlertsService) SearchAll(filter SearchFilter) (
break
}

response.Data = all
response.ResetPaging()
response.Data = all
return
}
1 change: 1 addition & 0 deletions api/compliance_evaluations_aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func (r ComplianceEvaluationAwsResponse) PageInfo() *V2Pagination {
}
func (r *ComplianceEvaluationAwsResponse) ResetPaging() {
r.Paging = V2Pagination{}
r.Data = nil
}

type ComplianceEvaluationAws struct {
Expand Down
20 changes: 15 additions & 5 deletions api/entities_containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,27 @@ func (svc *EntitiesService) ListAllContainers() (response ContainersEntityRespon
for {
all = append(all, response.Data...)

pageOk, err = svc.client.NextPage(&response)
newResponse := ContainersEntityResponse{
Paging: response.Paging,
}
pageOk, err = svc.client.NextPage(&newResponse)
if err == nil && pageOk {
response = newResponse
continue
}
break
}

response.Data = all
response.ResetPaging()
response.Data = all
return
}

// ListAllContainersWithFilters iterates over all pages to return all active container information at once based on a user defined filter
func (svc *EntitiesService) ListAllContainersWithFilters(filters SearchFilter) (response ContainersEntityResponse, err error) {
// ListAllContainersWithFilters iterates over all pages to return all active container
// information at once based on a user defined filter
func (svc *EntitiesService) ListAllContainersWithFilters(filters SearchFilter) (
response ContainersEntityResponse, err error,
) {
response, err = svc.ListContainersWithFilters(filters)
if err != nil {
return
Expand All @@ -93,14 +100,16 @@ func (svc *EntitiesService) ListAllContainersWithFilters(filters SearchFilter) (
break
}

response.Data = all
response.ResetPaging()
response.Data = all
return
}

type ContainersEntityResponse struct {
Data []ContainerEntity `json:"data"`
Paging V2Pagination `json:"paging"`

v2PageMetadata `json:"-"`
}

// Fulfill Pageable interface (look at api/v2.go)
Expand All @@ -109,6 +118,7 @@ func (r ContainersEntityResponse) PageInfo() *V2Pagination {
}
func (r *ContainersEntityResponse) ResetPaging() {
r.Paging = V2Pagination{}
r.Data = nil
}

// Total returns the total number of active containers
Expand Down
7 changes: 5 additions & 2 deletions api/entities_images.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ func (svc *EntitiesService) ListAllImages() (response ImagesEntityResponse, err
break
}

response.Data = all
response.ResetPaging()
response.Data = all
return
}

Expand All @@ -89,14 +89,16 @@ func (svc *EntitiesService) ListAllImagesWithFilters(filters SearchFilter) (resp
break
}

response.Data = all
response.ResetPaging()
response.Data = all
return
}

type ImagesEntityResponse struct {
Data []ImageEntity `json:"data"`
Paging V2Pagination `json:"paging"`

v2PageMetadata `json:"-"`
}

// Fulfill Pageable interface (look at api/v2.go)
Expand All @@ -105,6 +107,7 @@ func (r ImagesEntityResponse) PageInfo() *V2Pagination {
}
func (r *ImagesEntityResponse) ResetPaging() {
r.Paging = V2Pagination{}
r.Data = nil
}

type ImageEntity struct {
Expand Down
7 changes: 5 additions & 2 deletions api/entities_machine_details.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ func (svc *EntitiesService) ListAllMachineDetails() (response MachineDetailsEnti
break
}

response.Data = all
response.ResetPaging()
response.Data = all
return
}

Expand All @@ -91,14 +91,16 @@ func (svc *EntitiesService) ListAllMachineDetailsWithFilters(filters SearchFilte
break
}

response.Data = all
response.ResetPaging()
response.Data = all
return
}

type MachineDetailsEntityResponse struct {
Data []MachineDetailEntity `json:"data"`
Paging V2Pagination `json:"paging"`

v2PageMetadata `json:"-"`
}

// Fulfill Pageable interface (look at api/v2.go)
Expand All @@ -107,6 +109,7 @@ func (r MachineDetailsEntityResponse) PageInfo() *V2Pagination {
}
func (r *MachineDetailsEntityResponse) ResetPaging() {
r.Paging = V2Pagination{}
r.Data = nil
}

type MachineDetailEntity struct {
Expand Down
5 changes: 4 additions & 1 deletion api/entities_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,16 @@ func (svc *EntitiesService) ListAllUsers() (response UsersEntityResponse, err er
break
}

response.Data = all
response.ResetPaging()
response.Data = all
return
}

type UsersEntityResponse struct {
Data []UserEntity `json:"data"`
Paging V2Pagination `json:"paging"`

v2PageMetadata `json:"-"`
}

// Fulfill Pagination interface (look at api/v2.go)
Expand All @@ -72,6 +74,7 @@ func (r UsersEntityResponse) PageInfo() *V2Pagination {
}
func (r *UsersEntityResponse) ResetPaging() {
r.Paging = V2Pagination{}
r.Data = nil
}

type UserEntity struct {
Expand Down
1 change: 1 addition & 0 deletions api/inventory_aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func (r InventoryAwsResponse) PageInfo() *V2Pagination {
}
func (r *InventoryAwsResponse) ResetPaging() {
r.Paging = V2Pagination{}
r.Data = nil
}

type InventoryAws struct {
Expand Down
47 changes: 43 additions & 4 deletions api/v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,39 @@ type V2Pagination struct {
} `json:"urls"`
}

// v2PageMetadata is used to compute the total pages and the page number
// when reading pages using the client.NextPage() function
type v2PageMetadata struct {
totalPages int
pageNumber int
}

func (m v2PageMetadata) PageNumber() int {
return m.pageNumber
}
func (m v2PageMetadata) TotalPages() int {
return m.totalPages
}
func (m *v2PageMetadata) SetTotalPages(total int) {
m.totalPages = total
}
func (m *v2PageMetadata) PageRead() {
m.pageNumber++
}

// Pageable is the interface that structs should implement to become
// pageable and be able to use the client.NextPage() function
type Pageable interface {
PageInfo() *V2Pagination
ResetPaging()

// all these functions are automatically implemented when attaching
// the v2PageMetadata type into any Pageable struct, so attaching that
// struct is a requirement
PageRead()
SetTotalPages(int)
TotalPages() int
PageNumber() int
}

// NextPage
Expand Down Expand Up @@ -188,14 +216,24 @@ func (c *Client) NextPage(p Pageable) (bool, error) {
return false, nil
}

c.log.Info("pagination", zap.Int("rows", pagination.Rows),
zap.Int("total_rows", pagination.TotalRows),
zap.String("next_page", pagination.Urls.NextPage),
)
if pagination.Urls.NextPage == "" {
return false, nil
}

if p.PageNumber() == 0 {
// first page, initialize pagination metadata
p.SetTotalPages(pagination.TotalRows / pagination.Rows)
p.PageRead()
}

c.log.Info("pagination",
zap.Int("page_number", p.PageNumber()),
zap.Int("total_pages", p.TotalPages()),
zap.Int("rows", pagination.Rows),
zap.Int("total_rows", pagination.TotalRows),
zap.String("next_page", pagination.Urls.NextPage),
)

pageURL, err := url.Parse(pagination.Urls.NextPage)
if err != nil {
return false, errors.Wrap(err, "unable to part next page url")
Expand All @@ -209,5 +247,6 @@ func (c *Client) NextPage(p Pageable) (bool, error) {
p.ResetPaging()
c.log.Info("pagination reset")
err = c.RequestDecoder("GET", path, nil, p)
p.PageRead()
return true, err
}
Loading

0 comments on commit fd5338b

Please sign in to comment.