Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event ordering when setting pageSize is wrong #46

Open
t-persson opened this issue May 3, 2023 · 1 comment
Open

Event ordering when setting pageSize is wrong #46

t-persson opened this issue May 3, 2023 · 1 comment
Labels
bug Something isn't working
Milestone

Comments

@t-persson
Copy link
Collaborator

Description

The ordering of events in the event repository is such that the latest event comes last and the oldest event comes first, this means that when we limit the size of the page with pageSize we will only get the oldest event instead of the latest.
It is possible to get the latest event if pageNo is set to totalNumberItems, but that just feels weird.

Motivation

I want to limit my query to the ER so that I only get the latest event.

Exemplification

Limiting the database & ER query in order to play nice with the services by setting pageSize=1 is not really possible.

Benefits

Easier to play nice with the service.

Possible Drawbacks

None

@t-persson t-persson added this to the v1.0.0 milestone May 3, 2023
@t-persson t-persson added the bug Something isn't working label May 3, 2023
@magnusbaeck
Copy link
Member

Is there anything more to this than just sorting allEvents before returning from GetEvents? We could maintain allEvents as a sorted slice at all time rather than just appending the events (line 191) but I don't know if that would actually be more efficient (and if it would matter given the relatively small number of elements).

func (m *Database) GetEvents(ctx context.Context, request requests.MultipleEventsRequest) ([]drivers.EiffelEvent, int64, error) {
filter, err := buildFilter(request.Conditions)
if err != nil {
m.logger.Errorf("Database: %v", err)
return nil, 0, err
}
collections, err := m.collections(ctx, filter)
if err != nil {
m.logger.Errorf("Database: %v", err)
return nil, 0, err
}
m.logger.Debugf("fetching events from %d collections", len(collections))
allEvents := make([]drivers.EiffelEvent, 0, request.PageSize)
var numberOfDocuments int64
for _, collection := range collections {
var events []drivers.EiffelEvent
limit := request.PageSize - int32(len(allEvents))
col := m.database.Collection(collection)
if limit > 0 {
cursor, err := col.Find(ctx, filter, options.Find().
SetProjection(bson.M{"_id": 0}).
SetSkip(int64((request.PageNo-1)*request.PageSize)).
SetLimit(int64(limit)),
)
if err != nil {
continue
}
if err = cursor.All(ctx, &events); err != nil {
m.logger.Info(err.Error())
continue
}
allEvents = append(allEvents, events...)
}
if len(events) > 0 && int32(len(events)) < limit && request.PageNo == 1 {
numberOfDocuments += int64(len(events))
} else {
count, _ := col.CountDocuments(ctx, filter, &options.CountOptions{})
numberOfDocuments += count
}
}
return allEvents, numberOfDocuments, nil
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants