Fix sort order of request logs

This commit is contained in:
David Stotijn 2022-02-28 15:40:13 +01:00
parent ad26478043
commit af26987601
No known key found for this signature in database
GPG key ID: B23243A9C47CEE2D
2 changed files with 10 additions and 3 deletions

View file

@ -231,6 +231,7 @@ func findRequestLogIDsByProjectID(txn *badger.Txn, projectID ulid.ULID) ([]ulid.
reqLogIDs := make([]ulid.ULID, 0) reqLogIDs := make([]ulid.ULID, 0)
opts := badger.DefaultIteratorOptions opts := badger.DefaultIteratorOptions
opts.PrefetchValues = false opts.PrefetchValues = false
opts.Reverse = true
iterator := txn.NewIterator(opts) iterator := txn.NewIterator(opts)
defer iterator.Close() defer iterator.Close()
@ -238,7 +239,7 @@ func findRequestLogIDsByProjectID(txn *badger.Txn, projectID ulid.ULID) ([]ulid.
prefix := entryKey(reqLogPrefix, reqLogProjectIDIndex, projectID[:]) prefix := entryKey(reqLogPrefix, reqLogProjectIDIndex, projectID[:])
for iterator.Seek(prefix); iterator.ValidForPrefix(prefix); iterator.Next() { for iterator.Seek(append(prefix, 255)); iterator.ValidForPrefix(prefix); iterator.Next() {
projectIndexKey = iterator.Item().KeyCopy(projectIndexKey) projectIndexKey = iterator.Item().KeyCopy(projectIndexKey)
var id ulid.ULID var id ulid.ULID

View file

@ -46,7 +46,7 @@ func TestFindRequestLogs(t *testing.T) {
projectID := ulid.MustNew(ulid.Timestamp(time.Now()), ulidEntropy) projectID := ulid.MustNew(ulid.Timestamp(time.Now()), ulidEntropy)
exp := []reqlog.RequestLog{ fixtures := []reqlog.RequestLog{
{ {
ID: ulid.MustNew(ulid.Timestamp(time.Now()), ulidEntropy), ID: ulid.MustNew(ulid.Timestamp(time.Now()), ulidEntropy),
ProjectID: projectID, ProjectID: projectID,
@ -80,7 +80,7 @@ func TestFindRequestLogs(t *testing.T) {
} }
// Store fixtures. // Store fixtures.
for _, reqLog := range exp { for _, reqLog := range fixtures {
err = database.StoreRequestLog(context.Background(), reqLog) err = database.StoreRequestLog(context.Background(), reqLog)
if err != nil { if err != nil {
t.Fatalf("unexpected error creating request log fixture: %v", err) t.Fatalf("unexpected error creating request log fixture: %v", err)
@ -103,6 +103,12 @@ func TestFindRequestLogs(t *testing.T) {
t.Fatalf("unexpected error finding request logs: %v", err) t.Fatalf("unexpected error finding request logs: %v", err)
} }
// We expect the found request logs are *reversed*, e.g. newest first.
exp := make([]reqlog.RequestLog, len(fixtures))
for i, j := 0, len(fixtures)-1; i < j; i, j = i+1, j-1 {
exp[i], exp[j] = fixtures[j], fixtures[i]
}
if diff := cmp.Diff(exp, got); diff != "" { if diff := cmp.Diff(exp, got); diff != "" {
t.Fatalf("request logs not equal (-exp, +got):\n%v", diff) t.Fatalf("request logs not equal (-exp, +got):\n%v", diff)
} }