Skip to content

Commit

Permalink
handle partial events sync
Browse files Browse the repository at this point in the history
  • Loading branch information
jaroslaw-pieszka committed Jun 6, 2024
1 parent 2756dc2 commit 8824e3e
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions internal/subaccountsync/cis_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,22 @@ func (c *RateLimitedCisClient) buildEventRequest(page int, fromActionTime int64)

func (c *RateLimitedCisClient) FetchEventsWindow(fromActionTime int64) ([]Event, error) {
var events []Event
var currentPage int
var totalPages, currentPage int
for {
cisResponse, err := c.fetchEventsPage(currentPage, fromActionTime)
totalPages = cisResponse.TotalPages
if err != nil {
c.log.Error(fmt.Sprintf("while getting subaccount events for %d page: %v", currentPage, err))
return nil, err
c.log.Debug(fmt.Sprintf("Event window fetched partially - pages: %d out of %d, events: %d, from epoch: %d", currentPage-1, totalPages, len(events), fromActionTime))
return events, err
}
events = append(events, cisResponse.Events...)
currentPage++
if cisResponse.TotalPages == currentPage {
break
}
}
c.log.Debug(fmt.Sprintf("Fetched event window - pages: %d, events: %d, from epoch: %d", currentPage, len(events), fromActionTime))
c.log.Debug(fmt.Sprintf("Fetched event window - pages: %d out of %d, events: %d, from epoch: %d", currentPage, totalPages, len(events), fromActionTime))
return events, nil
}

Expand Down Expand Up @@ -78,16 +80,16 @@ func (c *RateLimitedCisClient) fetchEventsPage(page int, fromActionTime int64) (

func (c *RateLimitedCisClient) getEventsForSubaccounts(fromActionTime int64, logs slog.Logger, subaccountsMap subaccountsSetType) ([]Event, error) {
rawEvents, err := c.FetchEventsWindow(fromActionTime)
if err != nil {
logs.Error(fmt.Sprintf("while getting subaccount delete events: %s", err))
if err != nil && len(rawEvents) == 0 {
logs.Error(fmt.Sprintf("while getting events: %s", err))
return nil, err
}

// filter events to get only the ones in subaccounts map
filteredEventsFromCis := filterEvents(rawEvents, subaccountsMap)
logs.Info(fmt.Sprintf("Raw events: %d, filtered: %d", len(rawEvents), len(filteredEventsFromCis)))

return filteredEventsFromCis, nil
return filteredEventsFromCis, err
}

func filterEvents(rawEvents []Event, subaccounts subaccountsSetType) []Event {
Expand Down

0 comments on commit 8824e3e

Please sign in to comment.