Skip to content

Commit

Permalink
fix: rest no instance handling (#3360)
Browse files Browse the repository at this point in the history
  • Loading branch information
rahulguptajss authored Dec 6, 2024
1 parent bb6faf6 commit 2eb15ab
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cmd/collectors/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ func (r *Rest) PollData() (map[string]*matrix.Matrix, error) {

processBatch := func(records []gjson.Result) error {
if len(records) == 0 {
return errs.New(errs.ErrNoInstance, "no "+r.Object+" instances on cluster")
return nil
}

// Process the current batch of records
Expand Down
23 changes: 17 additions & 6 deletions cmd/tools/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ func FetchAnalytics(client *Client, href string) ([]gjson.Result, gjson.Result,
func FetchAllStream(client *Client, href string, processBatch func([]gjson.Result) error, headers ...map[string]string) error {
var prevLink string
nextLink := href
recordsFound := false

for {
var records []gjson.Result
Expand All @@ -483,12 +484,16 @@ func FetchAllStream(client *Client, href string, processBatch func([]gjson.Resul

output := gjson.ParseBytes(response)
data := output.Get("records")
numRecords := output.Get("num_records")
next := output.Get("_links.next.href")

if data.Exists() {
// Process the current batch of records
if err := processBatch(data.Array()); err != nil {
return err
if numRecords.Int() > 0 {
recordsFound = true
// Process the current batch of records
if err := processBatch(data.Array()); err != nil {
return err
}
}

prevLink = nextLink
Expand All @@ -506,13 +511,19 @@ func FetchAllStream(client *Client, href string, processBatch func([]gjson.Resul
}
value := gjson.GetBytes(response, "records")
records = append(records, value.Array()...)
// Process the current batch of records
if err := processBatch(records); err != nil {
return err
if len(records) > 0 {
recordsFound = true
// Process the current batch of records
if err := processBatch(records); err != nil {
return err
}
}
break
}
}
if !recordsFound {
return errs.New(errs.ErrNoInstance, "no instances found")
}

return nil
}
Expand Down

0 comments on commit 2eb15ab

Please sign in to comment.