mirror of
https://github.com/cirruslabs/orchard.git
synced 2026-10-09 16:25:40 +02:00
* Add pagination support for listing VM events Introduced a paginated event listing API, added support for pagination parameters in the request, and included cursor-based navigation using headers. Relevant tests and Badger store implementations were updated to support the new logic. * Remove support for ordering VM events Dropped `ListOrder` type, `order` query parameter, and related logic for ordering VM events. Updated tests, API schema, and Badger store to reflect the removal. * Remove invalid VM events ordering test Deleted a test case for invalid VM events ordering since the `order` query parameter and related functionality have been removed. * Add support for ordering VM events Implemented `order` query parameter for specifying sort order (ascending/descending) of VM events. Updated API schema, Badger store, and added related tests. * Add support for limiting and ordering VM logs Introduced `--limit` and `--order` flags for controlling the number of log lines and their sort order (ascending/descending). Updated API client to handle new options. * Update internal/controller/store/badger/badger_events.go Co-authored-by: Nikolay Edigaryev <edigaryev@gmail.com> * fix: address PR review feedback - switch logs CLI to --tail with desc ordering - reuse ParseLogsOrder in controller with helpful errors - always use ListEventsPage and scope event cursors - move events pagination coverage to integration test 🤖 Generated with [Codex](https://chatgpt.com/codex) Co-Authored-By: Codex <codex@openai.com> * refactor: simplify prefix trimming and improve error formatting - Replaced manual prefix check with `bytes.TrimPrefix` in Badger store. - Enhanced error message formatting in VM logs controller. * fix: address PR review feedback - use suggested reverse seek in badger events pagination - add events pagination client helper and use it in integration test 🤖 Generated with [Codex](https://chatgpt.com/codex) Co-Authored-By: Codex <codex@openai.com> --------- Co-authored-by: Nikolay Edigaryev <edigaryev@gmail.com> Co-authored-by: Codex <codex@openai.com>
155 lines
3.3 KiB
Go
155 lines
3.3 KiB
Go
package badger
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"path"
|
|
"time"
|
|
|
|
storepkg "github.com/cirruslabs/orchard/internal/controller/store"
|
|
"github.com/cirruslabs/orchard/pkg/resource/v1"
|
|
"github.com/dgraph-io/badger/v3"
|
|
)
|
|
|
|
const SpaceEvents = "/events"
|
|
|
|
func scopePrefix(scope []string) []byte {
|
|
keyParts := []string{SpaceEvents}
|
|
keyParts = append(keyParts, scope...)
|
|
return []byte(path.Join(keyParts...))
|
|
}
|
|
|
|
func (txn *Transaction) AppendEvents(events []v1.Event, scope ...string) (err error) {
|
|
defer func() {
|
|
err = mapErr(err)
|
|
}()
|
|
|
|
injectionTime := time.Now().UnixNano()
|
|
|
|
for index, event := range events {
|
|
valueBytes, err := json.Marshal(event)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
eventUID := fmt.Sprintf("/%d-%d-%06d",
|
|
event.Timestamp,
|
|
injectionTime, // to preserve order in case two sequential batches have events with the same timestamp
|
|
index, // to preserve order in case a batch of events has some events with the same timestamp
|
|
)
|
|
|
|
eventKey := scopePrefix(scope)
|
|
eventKey = append(eventKey, []byte(eventUID)...)
|
|
|
|
err = txn.badgerTxn.Set(eventKey, valueBytes)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (txn *Transaction) ListEvents(scope ...string) (_ []v1.Event, err error) {
|
|
page, err := txn.ListEventsPage(storepkg.ListOptions{}, scope...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return page.Items, nil
|
|
}
|
|
|
|
func (txn *Transaction) ListEventsPage(options storepkg.ListOptions, scope ...string) (
|
|
result storepkg.Page[v1.Event],
|
|
err error,
|
|
) {
|
|
defer func() {
|
|
err = mapErr(err)
|
|
}()
|
|
|
|
// Declare an empty, non-nil slice to
|
|
// return [] when no events are found
|
|
result.Items = []v1.Event{}
|
|
|
|
prefix := scopePrefix(scope)
|
|
itOptions := badger.DefaultIteratorOptions
|
|
itOptions.Prefix = prefix
|
|
if options.Order == storepkg.ListOrderDesc {
|
|
itOptions.Reverse = true
|
|
}
|
|
|
|
it := txn.badgerTxn.NewIterator(itOptions)
|
|
defer it.Close()
|
|
|
|
cursor := options.Cursor
|
|
if len(cursor) > 0 {
|
|
if !bytes.HasPrefix(cursor, prefix) {
|
|
seekKey := make([]byte, 0, len(prefix)+len(cursor))
|
|
seekKey = append(seekKey, prefix...)
|
|
seekKey = append(seekKey, cursor...)
|
|
cursor = seekKey
|
|
}
|
|
it.Seek(cursor)
|
|
if it.ValidForPrefix(prefix) && bytes.Equal(it.Item().Key(), cursor) {
|
|
it.Next()
|
|
}
|
|
} else if options.Order == storepkg.ListOrderDesc {
|
|
it.Seek(append(prefix, 0xFF))
|
|
} else {
|
|
it.Rewind()
|
|
}
|
|
|
|
for it.ValidForPrefix(prefix) {
|
|
item := it.Item()
|
|
|
|
eventBytes, err := item.ValueCopy(nil)
|
|
if err != nil {
|
|
return result, err
|
|
}
|
|
|
|
var event v1.Event
|
|
|
|
if err := json.Unmarshal(eventBytes, &event); err != nil {
|
|
return result, err
|
|
}
|
|
|
|
result.Items = append(result.Items, event)
|
|
|
|
if options.Limit > 0 && len(result.Items) >= options.Limit {
|
|
lastKey := item.KeyCopy(nil)
|
|
it.Next()
|
|
if it.ValidForPrefix(prefix) {
|
|
lastKey = bytes.TrimPrefix(lastKey, prefix)
|
|
result.NextCursor = lastKey
|
|
}
|
|
break
|
|
}
|
|
|
|
it.Next()
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
func (txn *Transaction) DeleteEvents(scope ...string) (err error) {
|
|
defer func() {
|
|
err = mapErr(err)
|
|
}()
|
|
|
|
it := txn.badgerTxn.NewIterator(badger.IteratorOptions{
|
|
Prefix: scopePrefix(scope),
|
|
AllVersions: false,
|
|
PrefetchValues: false, // only need keys
|
|
})
|
|
defer it.Close()
|
|
|
|
for it.Rewind(); it.Valid(); it.Next() {
|
|
keyToDelete := it.Item().KeyCopy(nil)
|
|
err := txn.badgerTxn.Delete(keyToDelete)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|