Fetch events with offset limit

This commit is contained in:
braginini
2022-12-07 10:30:59 +01:00
parent ef97e0840c
commit 0f4d132b93
2 changed files with 13 additions and 7 deletions

View File

@@ -9,6 +9,7 @@ const (
ManagementEvent Type = "management"
)
// Type of the Event
type Type string
// Store provides an interface to store or stream events.
@@ -17,8 +18,8 @@ type Store interface {
Save(event Event) (*Event, error)
// GetSince returns a list of events from the store for a given account since the specified time
GetSince(accountID string, from time.Time) ([]Event, error)
// GetLast returns a top N of events from the store for a given account (ordered by timestamp desc)
GetLast(accountID string, limit int) ([]Event, error)
// Get returns "limit" number of events from the "offset" index ordered descending or ascending by a timestamp
Get(accountID string, offset, limit int, descending bool) ([]Event, error)
// Close the sink flushing events if necessary
Close() error
}

View File

@@ -2,6 +2,7 @@ package event
import (
"database/sql"
"fmt"
_ "github.com/mattn/go-sqlite3"
"path/filepath"
"time"
@@ -68,15 +69,19 @@ func processResult(result *sql.Rows) ([]Event, error) {
return events, nil
}
// GetLast returns a top N of events from the store for a given account (ordered by timestamp desc)
func (store *SQLiteStore) GetLast(accountID string, limit int) ([]Event, error) {
stmt, err := store.db.Prepare("SELECT id, operation, timestamp, modifier, target, account, type" +
" FROM events WHERE account = ? ORDER BY timestamp DESC limit ?;")
// Get returns "limit" number of events from index ordered descending or ascending by a timestamp
func (store *SQLiteStore) Get(accountID string, offset, limit int, descending bool) ([]Event, error) {
order := "DESC"
if !descending {
order = "ASC"
}
stmt, err := store.db.Prepare(fmt.Sprintf("SELECT id, operation, timestamp, modifier, target, account, type"+
" FROM events WHERE account = ? ORDER BY timestamp %s LIMIT ? OFFSET ?;", order))
if err != nil {
return nil, err
}
result, err := stmt.Query(accountID, limit)
result, err := stmt.Query(accountID, limit, offset)
if err != nil {
return nil, err
}