mirror of
https://github.com/netbirdio/netbird.git
synced 2026-10-25 05:49:06 +01:00
Move sqlite activity
This commit is contained in:
@@ -9,7 +9,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/miekg/dns"
|
"github.com/miekg/dns"
|
||||||
"github.com/netbirdio/netbird/management/server/activity"
|
"github.com/netbirdio/netbird/management/server/activity/sqlite"
|
||||||
httpapi "github.com/netbirdio/netbird/management/server/http"
|
httpapi "github.com/netbirdio/netbird/management/server/http"
|
||||||
"github.com/netbirdio/netbird/management/server/metrics"
|
"github.com/netbirdio/netbird/management/server/metrics"
|
||||||
"github.com/netbirdio/netbird/management/server/telemetry"
|
"github.com/netbirdio/netbird/management/server/telemetry"
|
||||||
@@ -143,7 +143,7 @@ var (
|
|||||||
if disableSingleAccMode {
|
if disableSingleAccMode {
|
||||||
mgmtSingleAccModeDomain = ""
|
mgmtSingleAccModeDomain = ""
|
||||||
}
|
}
|
||||||
eventStore, err := activity.NewSQLiteStore(config.Datadir)
|
eventStore, err := sqlite.NewSQLiteStore(config.Datadir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
package activity
|
package sqlite
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/netbirdio/netbird/management/server/activity"
|
||||||
|
|
||||||
// sqlite driver
|
// sqlite driver
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@@ -44,11 +46,11 @@ func NewSQLiteStore(dataDir string) (*SQLiteStore, error) {
|
|||||||
return &SQLiteStore{db: db}, nil
|
return &SQLiteStore{db: db}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func processResult(result *sql.Rows) ([]*Event, error) {
|
func processResult(result *sql.Rows) ([]*activity.Event, error) {
|
||||||
events := make([]*Event, 0)
|
events := make([]*activity.Event, 0)
|
||||||
for result.Next() {
|
for result.Next() {
|
||||||
var id int64
|
var id int64
|
||||||
var operation Activity
|
var operation activity.Activity
|
||||||
var timestamp time.Time
|
var timestamp time.Time
|
||||||
var initiator string
|
var initiator string
|
||||||
var target string
|
var target string
|
||||||
@@ -67,7 +69,7 @@ func processResult(result *sql.Rows) ([]*Event, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
events = append(events, &Event{
|
events = append(events, &activity.Event{
|
||||||
Timestamp: timestamp,
|
Timestamp: timestamp,
|
||||||
Activity: operation,
|
Activity: operation,
|
||||||
ID: uint64(id),
|
ID: uint64(id),
|
||||||
@@ -82,7 +84,7 @@ func processResult(result *sql.Rows) ([]*Event, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get returns "limit" number of events from index ordered descending or ascending by a timestamp
|
// 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) {
|
func (store *SQLiteStore) Get(accountID string, offset, limit int, descending bool) ([]*activity.Event, error) {
|
||||||
order := "DESC"
|
order := "DESC"
|
||||||
if !descending {
|
if !descending {
|
||||||
order = "ASC"
|
order = "ASC"
|
||||||
@@ -103,7 +105,7 @@ func (store *SQLiteStore) Get(accountID string, offset, limit int, descending bo
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Save an event in the SQLite events table
|
// Save an event in the SQLite events table
|
||||||
func (store *SQLiteStore) Save(event *Event) (*Event, error) {
|
func (store *SQLiteStore) Save(event *activity.Event) (*activity.Event, error) {
|
||||||
|
|
||||||
stmt, err := store.db.Prepare("INSERT INTO events(activity, timestamp, initiator_id, target_id, account_id, meta) VALUES(?, ?, ?, ?, ?, ?)")
|
stmt, err := store.db.Prepare("INSERT INTO events(activity, timestamp, initiator_id, target_id, account_id, meta) VALUES(?, ?, ?, ?, ?, ?)")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
+4
-3
@@ -1,7 +1,8 @@
|
|||||||
package activity
|
package sqlite
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/netbirdio/netbird/management/server/activity"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@@ -18,9 +19,9 @@ func TestNewSQLiteStore(t *testing.T) {
|
|||||||
accountID := "account_1"
|
accountID := "account_1"
|
||||||
|
|
||||||
for i := 0; i < 10; i++ {
|
for i := 0; i < 10; i++ {
|
||||||
_, err = store.Save(&Event{
|
_, err = store.Save(&activity.Event{
|
||||||
Timestamp: time.Now(),
|
Timestamp: time.Now(),
|
||||||
Activity: PeerAddedByUser,
|
Activity: activity.PeerAddedByUser,
|
||||||
InitiatorID: "user_" + fmt.Sprint(i),
|
InitiatorID: "user_" + fmt.Sprint(i),
|
||||||
TargetID: "peer_" + fmt.Sprint(i),
|
TargetID: "peer_" + fmt.Sprint(i),
|
||||||
AccountID: accountID,
|
AccountID: accountID,
|
||||||
Reference in New Issue
Block a user