mirror of
https://github.com/netbirdio/netbird.git
synced 2026-04-17 15:56:39 +00:00
Fetch events with offset limit
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user